FileDocCategorySizeDatePackage
NoParsingHandler.javaAPI DocGlassfish v2 API7787Fri May 04 22:37:10 BST 2007com.sun.enterprise.web.connector.grizzly.handlers

NoParsingHandler

public class NoParsingHandler 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 NoParsingHandler()

    
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 request, int handlerCode)
Intercept the request and decide if we cache the static resource. If the static resource is already cached, return it.

        if ( socketChannel == null || !FileCacheFactory.isEnabled())
            return Handler.CONTINUE;
                
        // If not initialized, dont' continue
        if ( fileCache == null && handlerCode != Handler.RESPONSE_PROCEEDED){
             return Handler.CONTINUE;  
        }
        
        if ( handlerCode == Handler.RESPONSE_PROCEEDED ){
            CoyoteRequest cr = 
                (CoyoteRequest)request.getNote(CoyoteAdapter.ADAPTER_NOTES);
            // We can cache it only if no security constraint and no
            // filter have been defined.
            if ( cr != null && cr.getWrapper() != null){

                String mappedServlet = cr.getWrapper().getName();
                
                if ( !mappedServlet.equals(FileCache.DEFAULT_SERVLET_NAME) ) 
                    return Handler.CONTINUE;
                
                if ( cr.getContext().findConstraints().length == 0 
                    && cr.getContext().findFilterDefs().length == 0 ){

                    if (!fileCache.isEnabled()) return Handler.CONTINUE;  
                    
                    String docroot;
                    if ( cr.getContextPath().equals("") ){
                        docroot = cr.getContext().getDocBase();
                    } else {
                        docroot = SelectorThread.getWebAppRootPath();
                    }
                    String requestURI = cr.getRequestURI();
                    Response response = cr.getCoyoteRequest().getResponse();  
                    MimeHeaders headers = response.getMimeHeaders();
                    boolean xPoweredBy = (
                            (CoyoteConnector)cr.getConnector()).isXpoweredBy();

                    fileCache.add(mappedServlet,docroot,requestURI,headers, 
                            xPoweredBy);
                }
            }       
        } else if ( handlerCode == Handler.REQUEST_LINE_PARSED ) {
            ByteChunk bc = request.requestURI().getByteChunk();
            
            if ( fileCache.sendCache(bc.getBytes(),bc.getStart(), 
                    bc.getLength(),socketChannel,keepAlive(request)) ){
                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;