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

StaticHandler

public class StaticHandler extends Object implements com.sun.enterprise.web.connector.grizzly.Handler
This Handler is invoked after the request line has been parsed.
author
Jeanfrancois Arcand

Fields Summary
private SocketChannel
socketChannel
The SocketChannel used to send a static resources.
protected com.sun.enterprise.web.connector.grizzly.FileCache
fileCache
The FileCache mechanism used to cache static resources.
Constructors Summary
public StaticHandler()

    
Methods Summary
public voidattachChannel(java.nio.channels.SocketChannel socketChannel)
Attach a SocketChannel to this object.

        this.socketChannel = socketChannel;
        if ( fileCache == null && socketChannel != null){
            fileCache = FileCacheFactory.getFactory(
                    socketChannel.socket().getLocalPort()).getFileCache();
        }
    
protected intfindBytes(org.apache.tomcat.util.buf.ByteChunk bc, byte[] b)
Specialized utility method: find a sequence of lower case bytes inside a ByteChunk.


        byte first = b[0];
        byte[] buff = bc.getBuffer();
        int start = bc.getStart();
        int end = bc.getEnd();

        // Look for first char 
        int srcEnd = b.length;

        for (int i = start; i <= (end - srcEnd); i++) {
            if (Ascii.toLower(buff[i]) != first) continue;
            // found first char, now look for a match
            int myPos = i+1;
            for (int srcPos = 1; srcPos < srcEnd; ) {
                    if (Ascii.toLower(buff[myPos++]) != b[srcPos++])
                break;
                    if (srcPos == srcEnd) return i - start; // found it
            }
        }
        return -1;
    
public inthandle(org.apache.coyote.Request req, int handlerCode)
Intercept the request and decide if we cache the static resource. If the static resource is already cached, return it.

        if (fileCache == null) return Handler.CONTINUE;
        
        if (handlerCode == Handler.RESPONSE_PROCEEDED && fileCache.isEnabled()){
            String docroot = SelectorThread.getWebAppRootPath();
            MessageBytes mb = req.requestURI();
            ByteChunk requestURI = mb.getByteChunk();       
            String uri = req.requestURI().toString();                
            fileCache.add(FileCache.DEFAULT_SERVLET_NAME,docroot,uri,
                          req.getResponse().getMimeHeaders(),false);        
        } else if (handlerCode == Handler.REQUEST_LINE_PARSED) {
            ByteChunk requestURI = req.requestURI().getByteChunk(); 
            if (fileCache.sendCache(requestURI.getBytes(), requestURI.getStart(),
                                requestURI.getLength(), socketChannel,
                                keepAlive(req))){
                return Handler.BREAK;   
            }
        }     
        return Handler.CONTINUE;
    
private booleankeepAlive(org.apache.coyote.Request request)
Get the keep-alive header.

        MimeHeaders headers = request.getMimeHeaders();

        // Check connection header
        MessageBytes connectionValueMB = headers.getValue("connection");
        if (connectionValueMB != null) {
            ByteChunk connectionValueBC = connectionValueMB.getByteChunk();
            if (findBytes(connectionValueBC, Constants.CLOSE_BYTES) != -1) {
                return false;
            } else if (findBytes(connectionValueBC, 
                                 Constants.KEEPALIVE_BYTES) != -1) {
                return true;
            }
        }
        return true;