1 23 24 package com.sun.enterprise.web.connector.coyote; 25 26 import java.util.logging.Logger ; 27 import java.util.logging.Level ; 28 import java.io.BufferedReader ; 29 import java.io.IOException ; 30 import java.io.UnsupportedEncodingException ; 31 import javax.servlet.ServletRequest ; 32 import javax.servlet.http.Cookie ; 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 54 public class PwcCoyoteRequest extends CoyoteRequest { 55 56 private static Logger logger = LogDomains.getLogger(LogDomains.PWC_LOGGER); 57 58 private boolean requestEncodingSet = false; 59 60 private byte[] formData = null; 62 private int formDataLen = 0; 63 65 public void setContext(Context ctx) { 66 if (ctx == null) { 67 return; 70 } 71 72 super.setContext(ctx); 73 CoyoteResponse response = (CoyoteResponse) getResponse(); 74 if (response != null) { 76 String [] 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 getReader() throws IOException { 86 setRequestEncoding(); 87 return super.getReader(); 88 } 89 90 91 99 public String getCharacterEncoding() { 100 String enc = super.getCharacterEncoding(); 101 if (enc != null) { 102 return enc; 103 } 104 105 setRequestEncoding(); 106 return super.getCharacterEncoding(); 107 } 108 109 110 116 public void configureSessionCookie(Cookie 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 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 [] { 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 public void recycle() { 152 super.recycle(); 153 formDataLen = 0; 154 } 155 157 158 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 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 uee) { 190 logger.log(Level.WARNING, "pe_coyote.request.encoding", uee); 191 } 192 } 193 } 194 195 196 206 private String getFormHintFieldEncoding(PwcWebModule wm) { 207 208 String encoding = null; 209 210 String formHintField = wm.getFormHintField(); 211 if (formHintField == null){ 212 return null; 213 } 214 215 if ("POST".equalsIgnoreCase(getMethod())) { 216 encoding = getPostDataEncoding(formHintField); 218 } else { 219 String query = getQueryString(); 220 if (query != null) { 221 encoding = parseFormHintField(query, formHintField); 222 } 223 } 224 225 return encoding; 226 } 227 228 229 private String getPostDataEncoding(String formHintField) { 230 231 if (!getMethod().equalsIgnoreCase("POST")) { 232 return null; 233 } 234 235 String 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 ("Post too large"); 256 } 257 258 String 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 formDataLen = actualLen; 273 String formDataString = new String (formData).substring(0, len); 275 encoding = parseFormHintField(formDataString, formHintField); 276 } 277 } catch (Throwable t) { 278 ; } 280 281 return encoding; 282 } 283 284 285 294 private String parseFormHintField(String paramsString, 295 String formHintField) { 296 297 String 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 322 protected byte[] getPostBody() throws IOException { 323 324 if (formDataLen > 0) { 325 return formData; 327 } else { 328 return super.getPostBody(); 329 } 330 } 331 } 333 | Popular Tags |