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

Generated on Sat Dec 24 17:28:33 2005 for Mango by  doxygen 1.4.0