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

ContentLengthHandler

public class ContentLengthHandler 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.
private com.sun.enterprise.web.connector.grizzly.algorithms.ContentLengthAlgorithm
algorithm
The Algorithm associated with this handler
Constructors Summary
public ContentLengthHandler(com.sun.enterprise.web.connector.grizzly.algorithms.ContentLengthAlgorithm algorithm)

        this.algorithm = algorithm;
    
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();
        }        
    
public inthandle(org.apache.coyote.Request request, int handlerCode)
Add a request URI to the FileCache or use the cache to send the static resources.

        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);
            
            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_BUFFERED ) {
            if ( algorithm.startReq != -1 ){
                if ( fileCache.sendCache(algorithm.ascbuf,
                                         algorithm.startReq, 
                                         algorithm.lengthReq, 
                                         socketChannel, true ) ){
                    return Handler.BREAK; 
                }
            }            
        }     
        return Handler.CONTINUE;