KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > grizzly > handlers > ContentLengthHandler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

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 JavaDoc;
31 import java.nio.channels.SocketChannel JavaDoc;
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 /**
42  * This <code>Handler</code> is invoked after the request line has been parsed.
43  *
44  * @author Jeanfrancois Arcand
45  */

46 public class ContentLengthHandler implements Handler<Request> {
47     
48     
49     /**
50      * The <code>SocketChannel</code> used to send a static resources.
51      */

52     private SocketChannel JavaDoc socketChannel;
53  
54     
55     /**
56      * The FileCache mechanism used to cache static resources.
57      */

58     protected FileCache fileCache;
59     
60     
61     /**
62      * The <code>Algorithm</code> associated with this handler
63      */

64     private ContentLengthAlgorithm algorithm;
65     
66     
67     // ---------------------------------------------------------------------//
68

69     
70     public ContentLengthHandler(ContentLengthAlgorithm algorithm){
71         this.algorithm = algorithm;
72         fileCache =
73              FileCacheFactory.getFactory(algorithm.getPort()).getFileCache();
74     }
75     
76     
77     /**
78      * Attach a <code>SocketChannel</code> to this object.
79      */

80     public void attachChannel(SocketChannel JavaDoc socketChannel){
81         this.socketChannel = socketChannel;
82     }
83     
84     
85     /**
86      * Add a request URI to the <code>FileCache</code> or use the cache to
87      * send the static resources.
88      */

89     public int handle(Request request, int handlerCode) throws IOException JavaDoc{
90         if ( socketChannel == null || !FileCacheFactory.isEnabled())
91             return Handler.CONTINUE;
92         
93         // If not initialized, dont' continue
94
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 JavaDoc 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 JavaDoc docroot;
115                     if ( cr.getContextPath().equals("") ){
116                         docroot = cr.getContext().getDocBase();
117                     } else {
118                         docroot = SelectorThread.getWebAppRootPath();
119                     }
120                     String JavaDoc 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