KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > connector > coyote > PwcCoyoteRequest


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.coyote;
25
26 import java.util.logging.Logger JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.io.BufferedReader JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.UnsupportedEncodingException JavaDoc;
31 import javax.servlet.ServletRequest JavaDoc;
32 import javax.servlet.http.Cookie JavaDoc;
33 import org.apache.catalina.Context;
34 import org.apache.catalina.Globals;
35 import org.apache.coyote.tomcat5.CoyoteRequest;
36 import org.apache.coyote.tomcat5.CoyoteResponse;
37 import org.apache.coyote.tomcat5.CoyoteConnector;
38 import org.apache.coyote.tomcat5.CoyoteInputStream;
39 import org.apache.coyote.tomcat5.CoyoteReader;
40 import org.apache.coyote.tomcat5.InputBuffer;
41 import org.apache.tomcat.util.http.Parameters;
42 import com.sun.enterprise.web.PwcWebModule;
43 import com.sun.enterprise.web.session.SessionCookieConfig;
44 import com.sun.enterprise.web.logging.pwc.LogDomains;
45
46 /**
47  * Customized version of the Tomcat 5 CoyoteRequest
48  * This is required for supporting Web Programmatic Login and setting the
49  * request encoding (charset).
50  *
51  * @author Jeanfrancois Arcand
52  * @author Jan Luehe
53  */

54 public class PwcCoyoteRequest extends CoyoteRequest {
55
56     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.PWC_LOGGER);
57
58     private boolean requestEncodingSet = false;
59
60     // START SJSAS 6346738
61
private byte[] formData = null;
62     private int formDataLen = 0;
63     // END SJSAS 6346738
64

65     public void setContext(Context ctx) {
66         if (ctx == null) {
67             // Invalid request. Response will be handled by
68
// the StandardEngineValve
69
return;
70         }
71         
72         super.setContext(ctx);
73         CoyoteResponse response = (CoyoteResponse) getResponse();
74         // Assert response!=null
75
if (response != null) {
76             String JavaDoc[] cacheControls = ((PwcWebModule) ctx).getCacheControls();
77             for (int i=0; cacheControls!=null && i<cacheControls.length; i++) {
78                 response.addHeader("Cache-Control", cacheControls[i]);
79             }
80         }
81
82         requestEncodingSet = false;
83     }
84
85     public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
86         setRequestEncoding();
87         return super.getReader();
88     }
89
90
91     /**
92      * Return the character encoding for this Request.
93      *
94      * If there is no request charset specified in the request, determines and
95      * sets the request charset using the locale-charset-info,
96      * locale-charset-map, and parameter-encoding elements provided in the
97      * sun-web.xml.
98      */

99     public String JavaDoc getCharacterEncoding() {
100         String JavaDoc enc = super.getCharacterEncoding();
101         if (enc != null) {
102             return enc;
103         }
104     
105         setRequestEncoding();
106         return super.getCharacterEncoding();
107     }
108
109
110     /*
111      * Configures the given JSESSIONID cookie with the cookie-properties from
112      * sun-web.xml.
113      *
114      * @param cookie The JSESSIONID cookie to be configured
115      */

116     public void configureSessionCookie(Cookie JavaDoc cookie) {
117
118         super.configureSessionCookie(cookie);
119
120         PwcWebModule wm = (PwcWebModule) getContext();
121         SessionCookieConfig cookieConfig = wm.getSessionCookieConfig();
122
123         if (cookieConfig != null) {
124
125             String JavaDoc name = cookieConfig.getName();
126             if (name != null && !name.equals(Globals.SESSION_COOKIE_NAME)) {
127                 logger.log(Level.WARNING,
128                            "pe_coyote.request.illegal_cookie_name",
129                            new String JavaDoc[] { name, Globals.SESSION_COOKIE_NAME });
130             }
131      
132             if (cookieConfig.getPath() != null) {
133                 cookie.setPath(cookieConfig.getPath());
134             }
135
136             cookie.setMaxAge(cookieConfig.getMaxAge());
137
138             if (cookieConfig.getDomain() != null) {
139                 cookie.setDomain(cookieConfig.getDomain());
140             }
141
142             if (cookieConfig.getComment() != null) {
143                 cookie.setVersion(1);
144                 cookie.setComment(cookieConfig.getComment());
145             }
146         }
147     }
148     
149
150     // START SJSAS 6346738
151
public void recycle() {
152         super.recycle();
153         formDataLen = 0;
154     }
155     // END SJSAS 6346738
156

157
158     /**
159      * If there is no request charset specified in the request, determines and
160      * sets the request charset using the locale-charset-info,
161      * locale-charset-map, and parameter-encoding elements provided in the
162      * sun-web.xml.
163      */

164     private void setRequestEncoding() {
165  
166         if (requestEncodingSet) {
167             return;
168         }
169
170         requestEncodingSet = true;
171
172         if (super.getCharacterEncoding() != null) {
173             return;
174         }
175
176         PwcWebModule wm = (PwcWebModule) getContext();
177
178         String JavaDoc encoding = getFormHintFieldEncoding(wm);
179         if (encoding == null) {
180             encoding = wm.getDefaultCharset();
181             if (encoding == null && wm.hasLocaleToCharsetMapping()) {
182                 encoding = wm.mapLocalesToCharset(getLocales());
183             }
184         }
185
186         if (encoding != null) {
187             try {
188                 setCharacterEncoding(encoding);
189             } catch (UnsupportedEncodingException JavaDoc uee) {
190                 logger.log(Level.WARNING, "pe_coyote.request.encoding", uee);
191             }
192         }
193     }
194
195
196     /*
197      * Returns the value of the query parameter whose name
198      * corresponds to the value of the form-hint-field attribute of the
199      * <parameter-encoding> element in sun-web.xml.
200      *
201      * @return The value of the query parameter whose name corresponds to the
202      * value of the form-hint-field attribute in sun-web.xml, or null if the
203      * request does not have any query string, or the query string does not
204      * contain a query parameter with that name
205      */

206     private String JavaDoc getFormHintFieldEncoding(PwcWebModule wm) {
207
208         String JavaDoc encoding = null;
209
210         String JavaDoc formHintField = wm.getFormHintField();
211         if (formHintField == null){
212             return null;
213         }
214
215         if ("POST".equalsIgnoreCase(getMethod())) {
216             // POST
217
encoding = getPostDataEncoding(formHintField);
218         } else {
219             String JavaDoc query = getQueryString();
220             if (query != null) {
221                 encoding = parseFormHintField(query, formHintField);
222             }
223         }
224
225         return encoding;
226     }
227         
228     
229     private String JavaDoc getPostDataEncoding(String JavaDoc formHintField) {
230
231         if (!getMethod().equalsIgnoreCase("POST")) {
232             return null;
233         }
234
235         String JavaDoc contentType = getContentType();
236         if (contentType == null)
237             contentType = "";
238         int semicolon = contentType.indexOf(';');
239         if (semicolon >= 0) {
240             contentType = contentType.substring(0, semicolon).trim();
241         } else {
242             contentType = contentType.trim();
243         }
244         if (!("application/x-www-form-urlencoded".equals(contentType))) {
245             return null;
246         }
247
248         int len = getContentLength();
249         if (len <= 0) {
250             return null;
251         }
252         int maxPostSize = ((CoyoteConnector) connector).getMaxPostSize();
253         if ((maxPostSize > 0) && (len > maxPostSize)) {
254             logger.log(Level.WARNING, "peCoyoteRequest.postTooLarge");
255             throw new IllegalStateException JavaDoc("Post too large");
256         }
257
258         String JavaDoc encoding = null;
259
260         try {
261             formData = null;
262             if (len < CACHED_POST_LEN) {
263                 if (postData == null)
264                     postData = new byte[CACHED_POST_LEN];
265                 formData = postData;
266             } else {
267                 formData = new byte[len];
268             }
269             int actualLen = readPostBody(formData, len);
270             if (actualLen == len) {
271                 // START SJSAS 6346738
272
formDataLen = actualLen;
273                 // END SJSAS 6346738
274
String JavaDoc formDataString = new String JavaDoc(formData).substring(0, len);
275                 encoding = parseFormHintField(formDataString, formHintField);
276             }
277         } catch (Throwable JavaDoc t) {
278             ; // Ignore
279
}
280
281         return encoding;
282     }
283
284
285     /*
286      * Parses the value of the specified form-hint-field from the given
287      * parameter string.
288      *
289      * @param paramsString Parameter string
290      * @param formHintField From-hint-field
291      *
292      * @return Value of form-hint-field, or null if not found
293      */

294     private String JavaDoc parseFormHintField(String JavaDoc paramsString,
295                                       String JavaDoc formHintField) {
296
297         String JavaDoc encoding = null;
298
299         formHintField += "=";
300         int index = paramsString.indexOf(formHintField);
301         if (index != -1) {
302             int endIndex = paramsString.indexOf('&', index);
303             if (endIndex != -1) {
304                 encoding = paramsString.substring(
305                     index + formHintField.length(), endIndex);
306             } else {
307                 encoding = paramsString.substring(
308                     index + formHintField.length());
309             }
310         }
311
312         return encoding;
313     }
314
315
316     // START SJSAS 6346738
317
/**
318      * Gets the POST body of this request.
319      *
320      * @return The POST body of this request
321      */

322     protected byte[] getPostBody() throws IOException JavaDoc {
323
324         if (formDataLen > 0) {
325             // POST body already read
326
return formData;
327         } else {
328             return super.getPostBody();
329         }
330     }
331     // END SJSAS 6346738
332
}
333
Popular Tags