Annotation of java/classes/org/w3c/jigsaw/servlet/JigsawServletContext.java, revision 1.1

1.1     ! bmahe       1: // JigsawServletContext.java
        !             2: // $Id$
        !             3: // (c) COPYRIGHT MIT and INRIA, 1998.
        !             4: // Please first read the full copyright statement in file COPYRIGHT.html
        !             5: 
        !             6: package org.w3c.jigsaw.servlet;
        !             7: 
        !             8: import java.io.*;
        !             9: import java.net.*;
        !            10: import java.util.*;
        !            11: 
        !            12: import javax.servlet.*;
        !            13: 
        !            14: import org.w3c.tools.resources.store.*;
        !            15: import org.w3c.tools.resources.*;
        !            16: import org.w3c.jigsaw.frames.*;
        !            17: 
        !            18: /**
        !            19:  * @version $Revision$
        !            20:  * @author  Benoît Mahé (bmahe@w3.org)
        !            21:  */
        !            22: public class JigsawServletContext implements ServletContext {
        !            23:     
        !            24:     private ResourceReference reference = null;
        !            25: 
        !            26:     /**
        !            27:      * A useful utility routine that tries to guess the content-type
        !            28:      * of an object based upon its extension.
        !            29:      */
        !            30:     protected static String guessContentTypeFromName(String fname) {
        !            31:        String ext = "";
        !            32:        int i = fname.lastIndexOf('#');
        !            33: 
        !            34:        if (i != -1)
        !            35:            fname = fname.substring(0, i - 1);
        !            36:        i = fname.lastIndexOf('.');
        !            37:        i = Math.max(i, fname.lastIndexOf('/'));
        !            38:        i = Math.max(i, fname.lastIndexOf('?'));
        !            39: 
        !            40:        if (i != -1 && fname.charAt(i) == '.') {
        !            41:            ext = fname.substring(i).toLowerCase();
        !            42:        }
        !            43:        return (String) extension_map.get(ext);
        !            44:     }
        !            45: 
        !            46:     static Hashtable extension_map = new Hashtable();
        !            47: 
        !            48:     static {
        !            49:        setSuffix("", "content/unknown");
        !            50:        setSuffix(".uu", "application/octet-stream");
        !            51:        setSuffix(".saveme", "application/octet-stream");
        !            52:        setSuffix(".dump", "application/octet-stream");
        !            53:        setSuffix(".hqx", "application/octet-stream");
        !            54:        setSuffix(".arc", "application/octet-stream");
        !            55:        setSuffix(".o", "application/octet-stream");
        !            56:        setSuffix(".a", "application/octet-stream");
        !            57:        setSuffix(".bin", "application/octet-stream");
        !            58:        setSuffix(".exe", "application/octet-stream");
        !            59:        /* Temporary only. */
        !            60:        setSuffix(".z", "application/octet-stream");
        !            61:        setSuffix(".gz", "application/octet-stream");
        !            62: 
        !            63:        setSuffix(".oda", "application/oda");
        !            64:        setSuffix(".pdf", "application/pdf");
        !            65:        setSuffix(".eps", "application/postscript");
        !            66:        setSuffix(".ai", "application/postscript");
        !            67:        setSuffix(".ps", "application/postscript");
        !            68:        setSuffix(".rtf", "application/rtf");
        !            69:        setSuffix(".dvi", "application/x-dvi");
        !            70:        setSuffix(".hdf", "application/x-hdf");
        !            71:        setSuffix(".latex", "application/x-latex");
        !            72:        setSuffix(".cdf", "application/x-netcdf");
        !            73:        setSuffix(".nc", "application/x-netcdf");
        !            74:        setSuffix(".tex", "application/x-tex");
        !            75:        setSuffix(".texinfo", "application/x-texinfo");
        !            76:        setSuffix(".texi", "application/x-texinfo");
        !            77:        setSuffix(".t", "application/x-troff");
        !            78:        setSuffix(".tr", "application/x-troff");
        !            79:        setSuffix(".roff", "application/x-troff");
        !            80:        setSuffix(".man", "application/x-troff-man");
        !            81:        setSuffix(".me", "application/x-troff-me");
        !            82:        setSuffix(".ms", "application/x-troff-ms");
        !            83:        setSuffix(".src", "application/x-wais-source");
        !            84:        setSuffix(".wsrc", "application/x-wais-source");
        !            85:        setSuffix(".zip", "application/zip");
        !            86:        setSuffix(".bcpio", "application/x-bcpio");
        !            87:        setSuffix(".cpio", "application/x-cpio");
        !            88:        setSuffix(".gtar", "application/x-gtar");
        !            89:        setSuffix(".shar", "application/x-shar");
        !            90:        setSuffix(".sh", "application/x-shar");
        !            91:        setSuffix(".sv4cpio", "application/x-sv4cpio");
        !            92:        setSuffix(".sv4crc", "application/x-sv4crc");
        !            93:        setSuffix(".tar", "application/x-tar");
        !            94:        setSuffix(".ustar", "application/x-ustar");
        !            95:        setSuffix(".snd", "audio/basic");
        !            96:        setSuffix(".au", "audio/basic");
        !            97:        setSuffix(".aifc", "audio/x-aiff");
        !            98:        setSuffix(".aif", "audio/x-aiff");
        !            99:        setSuffix(".aiff", "audio/x-aiff");
        !           100:        setSuffix(".wav", "audio/x-wav");
        !           101:        setSuffix(".gif", "image/gif");
        !           102:        setSuffix(".ief", "image/ief");
        !           103:        setSuffix(".jfif", "image/jpeg");
        !           104:        setSuffix(".jfif-tbnl", "image/jpeg");
        !           105:        setSuffix(".jpe", "image/jpeg");
        !           106:        setSuffix(".jpg", "image/jpeg");
        !           107:        setSuffix(".jpeg", "image/jpeg");
        !           108:        setSuffix(".tif", "image/tiff");
        !           109:        setSuffix(".tiff", "image/tiff");
        !           110:        setSuffix(".ras", "image/x-cmu-rast");
        !           111:        setSuffix(".pnm", "image/x-portable-anymap");
        !           112:        setSuffix(".pbm", "image/x-portable-bitmap");
        !           113:        setSuffix(".pgm", "image/x-portable-graymap");
        !           114:        setSuffix(".ppm", "image/x-portable-pixmap");
        !           115:        setSuffix(".rgb", "image/x-rgb");
        !           116:        setSuffix(".xbm", "image/x-xbitmap");
        !           117:        setSuffix(".xpm", "image/x-xpixmap");
        !           118:        setSuffix(".xwd", "image/x-xwindowdump");
        !           119:        setSuffix(".htm", "text/html");
        !           120:        setSuffix(".html", "text/html");
        !           121:        setSuffix(".text", "text/plain");
        !           122:        setSuffix(".c", "text/plain");
        !           123:        setSuffix(".cc", "text/plain");
        !           124:        setSuffix(".c++", "text/plain");
        !           125:        setSuffix(".h", "text/plain");
        !           126:        setSuffix(".pl", "text/plain");
        !           127:        setSuffix(".txt", "text/plain");
        !           128:        setSuffix(".java", "text/plain");
        !           129:        setSuffix(".rtx", "application/rtf");
        !           130:        setSuffix(".tsv", "text/tab-separated-values");
        !           131:        setSuffix(".etx", "text/x-setext");
        !           132:        setSuffix(".mpg", "video/mpeg");
        !           133:        setSuffix(".mpe", "video/mpeg");
        !           134:        setSuffix(".mpeg", "video/mpeg");
        !           135:        setSuffix(".mov", "video/quicktime");
        !           136:        setSuffix(".qt", "video/quicktime");
        !           137:        setSuffix(".avi", "application/x-troff-msvideo");
        !           138:        setSuffix(".movie", "video/x-sgi-movie");
        !           139:        setSuffix(".mv", "video/x-sgi-movie");
        !           140:        setSuffix(".mime", "message/rfc822");
        !           141:     }
        !           142: 
        !           143:     static private void setSuffix(String ext, String ct) {
        !           144:        extension_map.put(ext, ct);
        !           145:     }
        !           146: 
        !           147:     /**
        !           148:      * ServletContext implementation - Get the MIME type for given file.
        !           149:      */
        !           150: 
        !           151:     public String getMimeType(String filename) {
        !           152:        return guessContentTypeFromName(filename);
        !           153:     }
        !           154: 
        !           155:     public File getDirectory() {
        !           156:        try {
        !           157:            Resource res = reference.lock();
        !           158:            return ((ServletDirectoryFrame)res).getDirectory();
        !           159:        } catch(InvalidResourceException ex) {
        !           160:            ex.printStackTrace();
        !           161:            return null;
        !           162:        } finally {
        !           163:            reference.unlock();
        !           164:        }
        !           165:     }
        !           166: 
        !           167:     /**
        !           168:      * ServletContext implementation - Lookup a given servlet.
        !           169:      */
        !           170: 
        !           171:     public Servlet getServlet(String name) {
        !           172:        try {
        !           173:            Resource res = reference.lock();
        !           174:            return ((ServletDirectoryFrame)res).getServlet(name);
        !           175:        } catch(InvalidResourceException ex) {
        !           176:            ex.printStackTrace();
        !           177:            return null;
        !           178:        } finally {
        !           179:            reference.unlock();
        !           180:        }
        !           181:     }
        !           182: 
        !           183:     /**
        !           184:      * ServletContext implementation - Enumerate all servlets within context.
        !           185:      */
        !           186: 
        !           187:     public Enumeration getServlets() {
        !           188:        try {
        !           189:            Resource res = reference.lock();
        !           190:            return ((ServletDirectoryFrame)res).getServlets();
        !           191:        } catch(InvalidResourceException ex) {
        !           192:            ex.printStackTrace();
        !           193:            return null;
        !           194:        } finally {
        !           195:            reference.unlock();
        !           196:        }
        !           197:     }
        !           198: 
        !           199:     public Enumeration getServletNames() {
        !           200:        return getServlets();
        !           201:     }
        !           202: 
        !           203:     /**
        !           204:      * ServletContext implementation - Log a message.
        !           205:      */
        !           206:     
        !           207:     public void log(String msg) {
        !           208:        System.out.println(msg);
        !           209:     }
        !           210:     
        !           211:     public void log(Exception ex, String msg) {
        !           212:        log(msg+" : "+ex.getMessage());
        !           213:     }
        !           214: 
        !           215:     /**
        !           216:      * ServletContext implementation - Translate a piece of path.
        !           217:      */
        !           218: 
        !           219:     public String getRealPath(String path) {
        !           220:        return path;
        !           221:     }
        !           222: 
        !           223:     /**
        !           224:      * ServletContext implementation - Get server informations.
        !           225:      */
        !           226: 
        !           227:     public String getServerInfo() {
        !           228:        try {
        !           229:            Resource res = reference.lock();
        !           230:            return ((ServletDirectoryFrame)res).getServerInfo();
        !           231:        } catch(InvalidResourceException ex) {
        !           232:            ex.printStackTrace();
        !           233:            return null;
        !           234:        } finally {
        !           235:            reference.unlock();
        !           236:        }       
        !           237:     }
        !           238: 
        !           239:     /**
        !           240:      * ServletContext implementation - Get an attribute value.
        !           241:      * We map this into the ServletWrapper attributes, without
        !           242:      * support for name clashes though.
        !           243:      * @param name The attribute name.
        !           244:      */
        !           245:     
        !           246:     public Object getAttribute(String name) {
        !           247:        try {
        !           248:            Resource res = reference.lock();
        !           249:            return ((ServletDirectoryFrame)res).getAttribute(name);
        !           250:        } catch(InvalidResourceException ex) {
        !           251:            ex.printStackTrace();
        !           252:            return null;
        !           253:        } finally {
        !           254:            reference.unlock();
        !           255:        }
        !           256:     }
        !           257: 
        !           258:     /**
        !           259:      * Create a new ServletContext.
        !           260:      * @param ref a ResourceReference pointing on a ServletDirectoryFrame.
        !           261:      */
        !           262:     protected JigsawServletContext(ResourceReference ref ) {
        !           263:        this.reference = ref;
        !           264:     }
        !           265: 
        !           266: }

Webmaster