Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

ServletContext.d

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003         @file ServletContext.d
00004         
00005         Copyright (c) 2004 Kris Bell
00006         
00007         This software is provided 'as-is', without any express or implied
00008         warranty. In no event will the authors be held liable for damages
00009         of any kind arising from the use of this software.
00010         
00011         Permission is hereby granted to anyone to use this software for any 
00012         purpose, including commercial applications, and to alter it and/or 
00013         redistribute it freely, subject to the following restrictions:
00014         
00015         1. The origin of this software must not be misrepresented; you must 
00016            not claim that you wrote the original software. If you use this 
00017            software in a product, an acknowledgment within documentation of 
00018            said product would be appreciated but is not required.
00019 
00020         2. Altered source versions must be plainly marked as such, and must 
00021            not be misrepresented as being the original software.
00022 
00023         3. This notice may not be removed or altered from any distribution
00024            of the source.
00025 
00026         4. Derivative works are permitted, but they must carry this notice
00027            in full and credit the original source.
00028 
00029 
00030                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00031 
00032         
00033         @version        Initial version, April 2004      
00034         @author         Kris
00035 
00036 
00037 *******************************************************************************/
00038 
00039 module mango.servlet.ServletContext;
00040 
00041 private import  mango.log.Logger;
00042 
00043 private import  mango.utils.Text;
00044 
00045 private import  mango.io.FileStyle,
00046                 mango.io.FileConduit;
00047               
00048 private import  mango.servlet.Servlet;
00049 
00050 private import  mango.http.utils.Dictionary;
00051 
00052 /******************************************************************************
00053 
00054         Provided equivalent functionality of the Java class by the same 
00055         name.
00056 
00057 ******************************************************************************/
00058 
00059 class ServletContext
00060 {
00061         private char[]                  name,
00062                                         basePath;
00063         private MutableDictionary       attributes,
00064                                         configuration;
00065 
00066         private static Logger           logger;
00067 
00068         private static ServletException irp;
00069 
00070         private static MutableDictionary mimeMap;
00071 
00072         private static const char[]     ServerIdentity = "Mango.Servlet/Beta 9";
00073         
00074 
00075         // content to build contentType map for file extensions
00076         private struct MimeMap
00077         {
00078                 char[]  ext,
00079                         mime;
00080         }
00081 
00082         // a set of file extension and their mime types 
00083         private static const MimeMap list[] = 
00084         [
00085         {"a",           "application/octet-stream"}, 
00086         {"ai",          "application/postscript"}, 
00087         {"aif",         "audio/x-aiff"}, 
00088         {"aifc",        "audio/x-aiff"}, 
00089         {"aiff",        "audio/x-aiff"}, 
00090         {"arc",         "application/octet-stream"}, 
00091         {"au",          "audio/basic"}, 
00092         {"avi",         "application/x-troff-msvideo"}, 
00093         {"bcpio",       "application/x-bcpio"}, 
00094         {"bin",         "application/octet-stream"}, 
00095         {"bmp",         "image/bmp"}, 
00096         {"c",           "text/plain"}, 
00097         {"c++",         "text/plain"}, 
00098         {"cc",          "text/plain"}, 
00099         {"cdf",         "application/x-netcdf"}, 
00100         {"cpio",        "application/x-cpio"}, 
00101         {"d",           "text/plain"}, 
00102         {"djv",         "image/x-djvu"}, 
00103         {"djvu",        "image/x-djvu"}, 
00104         {"dump",        "application/octet-stream"}, 
00105         {"dvi",         "application/x-dvi"}, 
00106         {"eps",         "application/postscript"}, 
00107         {"etx",         "text/x-setext"}, 
00108         {"exe",         "application/octet-stream"}, 
00109         {"gif",         "image/gif"}, 
00110         {"gtar",        "application/x-gtar"}, 
00111         {"gz",          "application/octet-stream"}, 
00112         {"h",           "text/plain"}, 
00113         {"hdf",         "application/x-hdf"}, 
00114         {"hqx",         "application/octet-stream"}, 
00115         {"htm",         "text/html"}, 
00116         {"html",        "text/html"}, 
00117         {"iw4",         "image/x-iw44"}, 
00118         {"iw44",        "image/x-iw44"}, 
00119         {"ief",         "image/ief"}, 
00120         {"java",        "text/plain"}, 
00121         {"jfif",        "image/jpeg"}, 
00122         {"jfif-tbnl",   "image/jpeg"}, 
00123         {"jpe",         "image/jpeg"}, 
00124         {"jpeg",        "image/jpeg"}, 
00125         {"jpg",         "image/jpeg"}, 
00126         {"latex",       "application/x-latex"}, 
00127         {"man",         "application/x-troff-man"}, 
00128         {"me",          "application/x-troff-me"}, 
00129         {"mime",        "message/rfc822"}, 
00130         {"mov",         "video/quicktime"}, 
00131         {"movie",       "video/x-sgi-movie"}, 
00132         {"mpe",         "video/mpeg"}, 
00133         {"mpeg",        "video/mpeg"}, 
00134         {"mpg",         "video/mpeg"}, 
00135         {"ms",          "application/x-troff-ms"}, 
00136         {"mv",          "video/x-sgi-movie"}, 
00137         {"nc",          "application/x-netcdf"}, 
00138         {"o",           "application/octet-stream"}, 
00139         {"oda",         "application/oda"}, 
00140         {"pbm",         "image/x-portable-bitmap"}, 
00141         {"pdf",         "application/pdf"}, 
00142         {"pgm",         "image/x-portable-graymap"}, 
00143         {"pl",          "text/plain"}, 
00144         {"png",         "image/png"}, 
00145         {"pnm",         "image/x-portable-anymap"}, 
00146         {"ppm",         "image/x-portable-pixmap"}, 
00147         {"ps",          "application/postscript"}, 
00148         {"qt",          "video/quicktime"}, 
00149         {"ras",         "image/x-cmu-rast"}, 
00150         {"rgb",         "image/x-rgb"}, 
00151         {"roff",        "application/x-troff"}, 
00152         {"rtf",         "application/rtf"}, 
00153         {"rtx",         "application/rtf"}, 
00154         {"saveme",      "application/octet-stream"}, 
00155         {"sh",          "application/x-shar"}, 
00156         {"shar",        "application/x-shar"}, 
00157         {"snd",         "audio/basic"}, 
00158         {"src",         "application/x-wais-source"}, 
00159         {"sv4cpio",     "application/x-sv4cpio"}, 
00160         {"sv4crc",      "application/x-sv4crc"}, 
00161         {"t",           "application/x-troff"}, 
00162         {"tar",         "application/x-tar"}, 
00163         {"tex",         "application/x-tex"}, 
00164         {"texi",        "application/x-texinfo"}, 
00165         {"texinfo",     "application/x-texinfo"}, 
00166         {"text",        "text/plain"}, 
00167         {"tif",         "image/tiff"}, 
00168         {"tiff",        "image/tiff"}, 
00169         {"tr",          "application/x-troff"}, 
00170         {"tsv",         "text/tab-separated-values"}, 
00171         {"txt",         "text/plain"}, 
00172         {"ustar",       "application/x-ustar"}, 
00173         {"uu",          "application/octet-stream"}, 
00174         {"wav",         "audio/x-wav"}, 
00175         {"wsrc",        "application/x-wais-source"}, 
00176         {"xbm",         "image/x-xbitmap"}, 
00177         {"xpm",         "image/x-xpixmap"}, 
00178         {"xwd",         "image/x-xwindowdump"}, 
00179         {"z",           "application/octet-stream"}, 
00180         {"zip",         "application/zip"}, 
00181         ];
00182 
00183         /***********************************************************************
00184         
00185                 Initialize the mime-map and the servlet logging instance
00186 
00187         ***********************************************************************/
00188 
00189         static this ()
00190         {
00191                 irp = new ServletException ("Invalid resource path");
00192                 
00193                 mimeMap = new MutableDictionary;
00194 
00195                 // load up the content-types
00196                 foreach (MimeMap mm; list)
00197                          mimeMap.put (mm.ext, mm.mime);
00198                 mimeMap.optimize();
00199                                
00200                 // setup 
00201                 logger = Logger.getLogger ("mango.servlet.Context");
00202         }
00203 
00204         /***********************************************************************
00205         
00206                 Construct a context with the given name, and a base-path
00207                 of the current working directory.
00208 
00209         ***********************************************************************/
00210 
00211         this (char[] name)
00212         {
00213                 this (name, ".");
00214         }
00215 
00216         /***********************************************************************
00217         
00218                 Construct a context with the given name, and the specified 
00219                 base-path. The latter is where files and properties will be
00220                 located from.                .
00221 
00222         ***********************************************************************/
00223 
00224         this (char[] name, char[] basePath)
00225         in {
00226            assert (name !== null);
00227            assert (basePath !== null);
00228            }
00229         body
00230         {
00231                 this.name = name;
00232                 this.basePath = basePath;
00233 
00234                 if (name.length > 0)
00235                     if (name[0] != '/' || name[name.length-1] == '/')
00236                         throw new ServletException ("Invalid context specification");
00237 
00238                 if (basePath.length > 0)
00239                     if (basePath[basePath.length-1] == '/')
00240                         throw new ServletException ("Invalid path specification");
00241 
00242                 attributes = new MutableDictionary;
00243                 configuration = new MutableDictionary;
00244         }
00245     
00246         /***********************************************************************
00247         
00248                 Return the name of this context.
00249 
00250         ***********************************************************************/
00251 
00252         char[] getName ()
00253         {
00254                 return name;
00255         }
00256     
00257         /***********************************************************************
00258         
00259                 Return the attributes of this context
00260 
00261         ***********************************************************************/
00262 
00263         synchronized Dictionary getAttributes ()
00264         {
00265                 return attributes;
00266         }
00267     
00268         /***********************************************************************
00269         
00270                 Return the current configuration of this context
00271 
00272         ***********************************************************************/
00273 
00274         synchronized Dictionary getConfiguration ()
00275         {
00276                 return configuration;
00277         }
00278     
00279         /***********************************************************************
00280         
00281                 Swizzle the attributes of this context. This is not to
00282                 be exposed publicly.
00283 
00284         ***********************************************************************/
00285 
00286         protected synchronized ServletContext setAttributes (MutableDictionary other)
00287         {
00288                 attributes = other;
00289                 return this;
00290         }
00291     
00292         /***********************************************************************
00293         
00294                 Swizzle the configuration of this context. This is not to
00295                 be exposed publicly.
00296 
00297         ***********************************************************************/
00298 
00299         protected synchronized ServletContext setConfiguration (MutableDictionary other)
00300         {
00301                 configuration = other;
00302                 return this;
00303         }
00304     
00305         /***********************************************************************
00306         
00307                 Return the major version number.
00308 
00309         ***********************************************************************/
00310 
00311         int getMajorVersion ()
00312         {
00313                 return 1;
00314         }
00315     
00316         /***********************************************************************
00317         
00318                 Return the minor number.
00319 
00320         ***********************************************************************/
00321 
00322         int getMinorVersion ()
00323         {
00324                 return 0;
00325         }
00326 
00327         /***********************************************************************
00328         
00329                 Return the mime type for a given file extension. Returns
00330                 null if the extension is not known.
00331 
00332         ***********************************************************************/
00333 
00334         char[] getMimeType (char[] ext)
00335         {
00336                 return mimeMap.get (ext);
00337         }
00338 
00339         /***********************************************************************
00340         
00341                 Return a FileConduit for the given path. The file is located
00342                 via the base-path. 
00343 
00344                 Throws an IOException if the path is invalid, or there's a
00345                 problem of some kind with the file.
00346 
00347         ***********************************************************************/
00348 
00349         FileConduit getResourceAsFile (char[] path)
00350         {
00351                 checkPath (path);
00352                 return new FileConduit (basePath~path, FileStyle.ReadExisting);
00353         }
00354 
00355         /***********************************************************************
00356         
00357                 Send an informational message to the logger subsystem
00358 
00359         ***********************************************************************/
00360 
00361         ServletContext log (char[] msg)
00362         {
00363                 logger.info (msg);
00364                 return this;
00365         }
00366 
00367         /***********************************************************************
00368         
00369                 Send a error message to the logger subsystem
00370 
00371         ***********************************************************************/
00372 
00373         ServletContext log (char[] msg, Object error)
00374         {
00375                 logger.error (msg ~ error.toString());
00376                 return this;
00377         }
00378 
00379         /***********************************************************************
00380         
00381                 Return the identity of this server
00382 
00383         ***********************************************************************/
00384 
00385         char[] getServerInfo ()
00386         {
00387                 return ServerIdentity;
00388         }
00389 
00390         /***********************************************************************
00391         
00392                 Check the given path to see if it tries to subvert the
00393                 base-path notion. Throws an IOException if anything dodgy
00394                 is noted.
00395 
00396         ***********************************************************************/
00397 
00398         ServletContext checkPath (char[] path)
00399         {
00400                 if (path.length == 0)
00401                     throw irp;
00402 
00403                 char c = path [path.length-1];
00404                 if (Text.indexOf (path, "..") >= 0     || 
00405                     c == '/'                            ||
00406                     c == '\\'                           ||
00407                     c == '.')
00408                     throw irp;
00409 
00410                 return this;
00411         }
00412 }

Generated on Tue Jan 25 21:18:23 2005 for Mango by doxygen 1.3.6