FileDocCategorySizeDatePackage
StaticResourcesAdapter.javaAPI DocGlassfish v2 API6772Fri May 04 22:37:10 BST 2007com.sun.enterprise.web.connector.grizzly.standalone

StaticResourcesAdapter

public class StaticResourcesAdapter extends Object implements org.apache.coyote.Adapter
Simple HTTP based Web Server. Part of this class is from Tomcat sandbox code from Costin Manolache.
author
Jeanfrancois Arcand

Fields Summary
static Properties
contentTypes
private String
rootFolder
private File
rootFolderF
private ConcurrentHashMap
cache
Constructors Summary
public StaticResourcesAdapter()

       
    
      
        rootFolder = SelectorThread.getWebAppRootPath();
        rootFolderF = new File(rootFolder);
        try {
            rootFolder = rootFolderF.getCanonicalPath();
            SelectorThread.logger().log(Level.INFO, "Servicing page from: " 
                + rootFolder);
        
        } catch (IOException e) {
        }
    
Methods Summary
public voidafterService(org.apache.coyote.Request req, org.apache.coyote.Response res)

        // Recycle the wrapper request and response
        req.recycle();
        res.recycle();     
    
public voidfireAdapterEvent(java.lang.String string, java.lang.Object object)

    
public java.lang.StringgetContentType(java.lang.String ext)

        return contentTypes.getProperty( ext, "text/plain" );
    
public java.lang.StringgetRootFolder()

        return rootFolder;
    
static voidinitContentTypes()

    
     
        initContentTypes();
    
        contentTypes.put("html", "text/html");
        contentTypes.put("txt", "text/plain");
    
public voidservice(org.apache.coyote.Request req, org.apache.coyote.Response res)

        MessageBytes mb = req.requestURI();
        ByteChunk requestURI = mb.getByteChunk();
        String uri = req.requestURI().toString();
        if (uri.indexOf("..") >= 0) {
            res.setStatus(404);
            return;
        }        
        // local file
        File resource = cache.get(uri);
        if (resource == null){
            resource = new File(rootFolderF, uri);
            cache.put(uri,resource);
        }

        if (!resource.getCanonicalPath().startsWith(rootFolder)) {
            res.setStatus(404);
            return;
        }

        if (resource.isDirectory()) {
            resource = new File(resource, "index.html");
            cache.put(uri,resource);            
        }

        if (!resource.exists()) {
            SelectorThread.logger().log(Level.INFO,"File not found  " + resource);
            res.setStatus(404);
            return;
        }        
        res.setStatus(200);

        int dot=uri.lastIndexOf(".");
        if( dot > 0 ) {
            String ext=uri.substring(dot+1);
            String ct=getContentType(ext);
            if( ct!=null) {
                res.setContentType(ct);
            }
        }

        res.setContentLength((int)resource.length());        
        res.sendHeaders();

        /* Workaround Linux NIO bug
         * 6427312: (fc) FileChannel.transferTo() throws IOException "system call interrupted"
         * 5103988: (fc) FileChannel.transferTo should return -1 for EAGAIN instead throws IOException
         * 6253145: (fc) FileChannel.transferTo on Linux fails when going beyond 2GB boundary
         * 6470086: (fc) FileChannel.transferTo(2147483647, 1, channel) cause "Value too large" exception 
         */
        FileInputStream fis = new FileInputStream(resource);
        byte b[] = new byte[8192];
        ByteChunk chunk = new ByteChunk();
        int rd = 0;
        while ((rd = fis.read(b)) > 0) {
            chunk.setBytes(b, 0, rd);
            res.doWrite(chunk);
        }

        try{
            req.action( ActionCode.ACTION_POST_REQUEST , null);
        }catch (Throwable t) {
            t.printStackTrace();
        }
        
        res.finish();