1 23 24 package com.sun.enterprise.web.connector.grizzly.handlers; 25 import com.sun.enterprise.web.connector.grizzly.FileCacheFactory; 26 import com.sun.enterprise.web.connector.grizzly.Handler; 27 import com.sun.enterprise.web.connector.grizzly.FileCache; 28 import com.sun.enterprise.web.connector.grizzly.SelectorThread; 29 import com.sun.enterprise.web.connector.grizzly.algorithms.ContentLengthAlgorithm; 30 import java.io.IOException ; 31 import java.nio.channels.SocketChannel ; 32 33 import org.apache.coyote.Request; 34 import org.apache.coyote.Response; 35 import org.apache.coyote.tomcat5.CoyoteConnector; 36 import org.apache.coyote.tomcat5.CoyoteRequest; 37 import org.apache.coyote.tomcat5.CoyoteAdapter; 38 import org.apache.tomcat.util.http.MimeHeaders; 39 40 41 46 public class ContentLengthHandler implements Handler<Request> { 47 48 49 52 private SocketChannel socketChannel; 53 54 55 58 protected FileCache fileCache; 59 60 61 64 private ContentLengthAlgorithm algorithm; 65 66 67 69 70 public ContentLengthHandler(ContentLengthAlgorithm algorithm){ 71 this.algorithm = algorithm; 72 fileCache = 73 FileCacheFactory.getFactory(algorithm.getPort()).getFileCache(); 74 } 75 76 77 80 public void attachChannel(SocketChannel socketChannel){ 81 this.socketChannel = socketChannel; 82 } 83 84 85 89 public int handle(Request request, int handlerCode) throws IOException { 90 if ( socketChannel == null || !FileCacheFactory.isEnabled()) 91 return Handler.CONTINUE; 92 93 if ( fileCache == null && handlerCode != Handler.RESPONSE_PROCEEDED){ 95 return Handler.CONTINUE; 96 } 97 98 if ( handlerCode == Handler.RESPONSE_PROCEEDED ){ 99 CoyoteRequest cr = 100 (CoyoteRequest)request.getNote(CoyoteAdapter.ADAPTER_NOTES); 101 102 if ( cr != null && cr.getWrapper() != null){ 103 104 String mappedServlet = cr.getWrapper().getName(); 105 106 if ( !mappedServlet.equals(FileCache.DEFAULT_SERVLET_NAME) ) 107 return Handler.CONTINUE; 108 109 if ( cr.getContext().findConstraints().length == 0 110 && cr.getContext().findFilterDefs().length == 0 ){ 111 112 if (!fileCache.isEnabled()) return Handler.CONTINUE; 113 114 String docroot; 115 if ( cr.getContextPath().equals("") ){ 116 docroot = cr.getContext().getDocBase(); 117 } else { 118 docroot = SelectorThread.getWebAppRootPath(); 119 } 120 String requestURI = cr.getRequestURI(); 121 Response response = cr.getCoyoteRequest().getResponse(); 122 MimeHeaders headers = response.getMimeHeaders(); 123 boolean xPoweredBy = ( 124 (CoyoteConnector)cr.getConnector()).isXpoweredBy(); 125 126 fileCache.add(mappedServlet,docroot,requestURI,headers, 127 xPoweredBy); 128 } 129 } 130 } else if ( handlerCode == Handler.REQUEST_BUFFERED ) { 131 if ( algorithm.startReq != -1 ){ 132 if ( fileCache.sendCache(algorithm.ascbuf, 133 algorithm.startReq, 134 algorithm.lengthReq, 135 socketChannel, true ) ){ 136 return Handler.BREAK; 137 } 138 } 139 } 140 return Handler.CONTINUE; 141 } 142 } 143 | Popular Tags |