KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constants;
26 import com.sun.enterprise.web.connector.grizzly.FileCacheFactory;
27 import com.sun.enterprise.web.connector.grizzly.Handler;
28 import com.sun.enterprise.web.connector.grizzly.FileCache;
29 import com.sun.enterprise.web.connector.grizzly.SelectorThread;
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.buf.Ascii;
39 import org.apache.tomcat.util.buf.ByteChunk;
40 import org.apache.tomcat.util.buf.MessageBytes;
41 import org.apache.tomcat.util.http.MimeHeaders;
42 /**
43  * This <code>Handler</code> is invoked after the request line has been parsed.
44  *
45  * @author Jeanfrancois Arcand
46  */

47 public class NoParsingHandler implements Handler<Request> {
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     // ----------------------------------------------------- Constructor ----//
62

63     
64     public NoParsingHandler(int port){
65         fileCache =
66              FileCacheFactory.getFactory(port).getFileCache();
67     }
68     
69       
70     /**
71      * Attach a <code>SocketChannel</code> to this object.
72      */

73     public void attachChannel(SocketChannel JavaDoc socketChannel){
74         this.socketChannel = socketChannel;
75     }
76     
77     
78     /**
79      * Intercept the request and decide if we cache the static resource. If the
80      * static resource is already cached, return it.
81      */

82     public int handle(Request request, int handlerCode) throws IOException JavaDoc{
83         if ( socketChannel == null || !FileCacheFactory.isEnabled())
84             return Handler.CONTINUE;
85                 
86         // If not initialized, dont' continue
87
if ( fileCache == null && handlerCode != Handler.RESPONSE_PROCEEDED){
88              return Handler.CONTINUE;
89         }
90         
91         if ( handlerCode == Handler.RESPONSE_PROCEEDED ){
92             CoyoteRequest cr =
93                 (CoyoteRequest)request.getNote(CoyoteAdapter.ADAPTER_NOTES);
94             // We can cache it only if no security constraint and no
95
// filter have been defined.
96
if ( cr != null && cr.getWrapper() != null){
97
98                 String JavaDoc mappedServlet = cr.getWrapper().getName();
99                 
100                 if ( !mappedServlet.equals(FileCache.DEFAULT_SERVLET_NAME) )
101                     return Handler.CONTINUE;
102                 
103                 if ( cr.getContext().findConstraints().length == 0
104                     && cr.getContext().findFilterDefs().length == 0 ){
105
106                     if (!fileCache.isEnabled()) return Handler.CONTINUE;
107                     
108                     String JavaDoc docroot;
109                     if ( cr.getContextPath().equals("") ){
110                         docroot = cr.getContext().getDocBase();
111                     } else {
112                         docroot = SelectorThread.getWebAppRootPath();
113                     }
114                     String JavaDoc requestURI = cr.getRequestURI();
115                     Response response = cr.getCoyoteRequest().getResponse();
116                     MimeHeaders headers = response.getMimeHeaders();
117                     boolean xPoweredBy = (
118                             (CoyoteConnector)cr.getConnector()).isXpoweredBy();
119
120                     fileCache.add(mappedServlet,docroot,requestURI,headers,
121                             xPoweredBy);
122                 }
123             }
124         } else if ( handlerCode == Handler.REQUEST_LINE_PARSED ) {
125             ByteChunk bc = request.requestURI().getByteChunk();
126             
127             if ( fileCache.sendCache(bc.getBytes(),bc.getStart(),
128                     bc.getLength(),socketChannel,keepAlive(request)) ){
129                 return Handler.BREAK;
130             }
131         }
132         return Handler.CONTINUE;
133     }
134        
135     
136     /**
137      * Get the keep-alive header.
138      */

139     private boolean keepAlive(Request request){
140         MimeHeaders headers = request.getMimeHeaders();
141
142         // Check connection header
143
MessageBytes connectionValueMB = headers.getValue("connection");
144         if (connectionValueMB != null) {
145             ByteChunk connectionValueBC = connectionValueMB.getByteChunk();
146             if (findBytes(connectionValueBC, Constants.CLOSE_BYTES) != -1) {
147                 return false;
148             } else if (findBytes(connectionValueBC,
149                                  Constants.KEEPALIVE_BYTES) != -1) {
150                 return true;
151             }
152         }
153         return true;
154     }
155     
156     
157     /**
158      * Specialized utility method: find a sequence of lower case bytes inside
159      * a ByteChunk.
160      */

161     protected int findBytes(ByteChunk bc, byte[] b) {
162
163         byte first = b[0];
164         byte[] buff = bc.getBuffer();
165         int start = bc.getStart();
166         int end = bc.getEnd();
167
168         // Look for first char
169
int srcEnd = b.length;
170
171         for (int i = start; i <= (end - srcEnd); i++) {
172             if (Ascii.toLower(buff[i]) != first) continue;
173             // found first char, now look for a match
174
int myPos = i+1;
175             for (int srcPos = 1; srcPos < srcEnd; ) {
176                     if (Ascii.toLower(buff[myPos++]) != b[srcPos++])
177                 break;
178                     if (srcPos == srcEnd) return i - start; // found it
179
}
180         }
181         return -1;
182     }
183 }
184
Popular Tags