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

Generated on Fri Nov 11 18:44:22 2005 for Mango by  doxygen 1.4.0