1 16 package org.apache.cocoon.environment.http; 17 18 import java.io.BufferedReader ; 19 import java.io.IOException ; 20 import java.lang.ref.WeakReference ; 21 import java.util.Collections ; 22 import java.util.Enumeration ; 23 import java.util.HashMap ; 24 import java.util.Locale ; 25 import java.util.Map ; 26 import java.util.Vector ; 27 import java.util.WeakHashMap ; 28 29 import javax.servlet.RequestDispatcher ; 30 import javax.servlet.ServletInputStream ; 31 import javax.servlet.http.HttpServletRequest ; 32 33 import org.apache.avalon.framework.CascadingRuntimeException; 34 import org.apache.cocoon.environment.Cookie; 35 import org.apache.cocoon.environment.Request; 36 import org.apache.cocoon.environment.Session; 37 import org.apache.cocoon.environment.http.HttpSession; 38 import org.apache.cocoon.servlet.multipart.MultipartHttpServletRequest; 39 40 47 public final class HttpRequest implements Request { 48 49 50 private final HttpServletRequest req; 51 52 53 private final HttpEnvironment env; 54 55 56 private String form_encoding; 57 58 59 private String container_encoding; 60 61 62 private static final Map sessions = new WeakHashMap (); 63 64 67 protected HttpRequest(HttpServletRequest req, HttpEnvironment env) { 68 super(); 69 this.req = req; 70 this.env = env; 71 } 72 73 76 public Object get(String name) { 77 if (req instanceof MultipartHttpServletRequest) { 79 return ((MultipartHttpServletRequest) req).get(name); 80 } else { 81 String [] values = req.getParameterValues(name); 82 if (values == null) { 83 return null; 84 } 85 if (values.length == 1) { 86 return values[0]; 87 } 88 if (values.length > 1) { 89 Vector vect = new Vector (values.length); 90 for (int i = 0; i < values.length; i++) { 91 vect.add(values[i]); 92 } 93 return vect; 94 } 95 } 96 return null; 97 } 98 99 100 101 public String getAuthType() { 102 return this.req.getAuthType(); 103 } 104 105 private Cookie[] wrappedCookies = null; 106 private Map wrappedCookieMap = null; 107 108 public Cookie[] getCookies() { 109 if (this.wrappedCookieMap == null) { 110 wrapCookies(); 111 } 112 return this.wrappedCookies; 113 } 114 115 public Map getCookieMap() { 116 if (this.wrappedCookieMap == null) { 117 wrapCookies(); 118 } 119 return this.wrappedCookieMap; 120 } 121 122 private synchronized void wrapCookies() { 123 this.wrappedCookieMap = new HashMap (); 124 javax.servlet.http.Cookie [] cookies = this.req.getCookies(); 125 if (cookies != null) { 126 this.wrappedCookies = new Cookie[cookies.length]; 127 for(int i=0; i<cookies.length;i++) { 128 HttpCookie cookie = new HttpCookie(cookies[i]); 129 this.wrappedCookies[i] = cookie; 130 this.wrappedCookieMap.put(cookie.getName(),cookie); 131 } 132 } 133 this.wrappedCookieMap = Collections.unmodifiableMap(this.wrappedCookieMap); 134 } 135 136 public long getDateHeader(String name) { 137 return this.req.getDateHeader(name); 138 } 139 140 public String getHeader(String name) { 141 return this.req.getHeader(name); 142 } 143 144 public Enumeration getHeaders(String name) { 145 return this.req.getHeaders(name); 146 } 147 148 public Enumeration getHeaderNames() { 149 return this.req.getHeaderNames(); 150 } 151 152 public int getIntHeader(String name) { 153 return this.req.getIntHeader(name); 154 } 155 156 public String getMethod() { 157 return this.req.getMethod(); 158 } 159 160 public String getPathInfo() { 161 return this.req.getPathInfo(); 162 } 163 164 public String getPathTranslated() { 165 return this.req.getPathTranslated(); 166 } 167 168 public String getContextPath() { 169 return this.req.getContextPath(); 170 } 171 172 public String getQueryString() { 173 return this.req.getQueryString(); 174 } 175 176 public String getRemoteUser() { 177 return this.req.getRemoteUser(); 178 } 179 180 public boolean isUserInRole(String role) { 181 return this.req.isUserInRole(role); 182 } 183 184 public java.security.Principal getUserPrincipal() { 185 return this.req.getUserPrincipal(); 186 } 187 188 public String getRequestedSessionId() { 189 return this.req.getRequestedSessionId(); 190 } 191 192 protected String reqURI; 193 194 public String getRequestURI() { 195 if (this.reqURI == null) { 196 this.reqURI = this.req.getRequestURI(); 197 if ( this.reqURI.equals("/") ) { 198 String s = this.req.getServletPath(); 199 final StringBuffer buffer = new StringBuffer (); 200 if ( null != s ) buffer.append(s); 201 s = this.req.getPathInfo(); 202 if ( null != s ) buffer.append(s); 203 this.reqURI = buffer.toString(); 204 } 205 } 206 return this.reqURI; 207 } 208 209 public String getSitemapURI() { 210 return this.env.getURI(); 211 } 212 213 public String getSitemapURIPrefix() { 214 return this.env.getURIPrefix(); 215 } 216 217 public String getServletPath() { 218 return this.req.getServletPath(); 219 } 220 221 229 public Session getSession(boolean create) { 230 javax.servlet.http.HttpSession serverSession = this.req.getSession(create); 231 HttpSession session; 232 if (serverSession != null) { 233 synchronized (sessions) { 234 WeakReference ref = (WeakReference )sessions.get(serverSession); 236 if (ref == null || (session = (HttpSession)ref.get()) == null) { 237 session = new HttpSession(serverSession); 239 sessions.put(serverSession, new WeakReference (session)); 240 } 241 } 242 } else { 243 session = null; 245 } 246 return session; 247 } 248 249 public Session getSession() { 250 return this.getSession(true); 251 } 252 253 public boolean isRequestedSessionIdValid() { 254 return this.req.isRequestedSessionIdValid(); 255 } 256 257 public boolean isRequestedSessionIdFromCookie() { 258 return this.req.isRequestedSessionIdFromCookie(); 259 } 260 261 public boolean isRequestedSessionIdFromURL() { 262 return this.req.isRequestedSessionIdFromURL(); 263 } 264 265 269 public boolean isRequestedSessionIdFromUrl() { 270 return this.req.isRequestedSessionIdFromURL(); 271 } 272 273 274 275 public Object getAttribute(String name) { 276 return this.req.getAttribute(name); 277 } 278 279 public Enumeration getAttributeNames() { 280 return this.req.getAttributeNames(); 281 } 282 283 public String getCharacterEncoding() { 284 if (this.form_encoding == null) { 285 return this.req.getCharacterEncoding(); 286 } else { 287 return this.form_encoding; 288 } 289 } 290 291 public void setCharacterEncoding(String form_encoding) 292 throws java.io.UnsupportedEncodingException { 293 this.form_encoding = form_encoding; 294 } 295 296 299 public void setContainerEncoding(String container_encoding) { 300 this.container_encoding = container_encoding; 301 } 302 303 public int getContentLength() { 304 return this.req.getContentLength(); 305 } 306 307 public String getContentType() { 308 return this.req.getContentType(); 309 } 310 311 public ServletInputStream getInputStream() throws IOException { 312 return this.req.getInputStream(); 313 } 314 315 public String getParameter(String name) { 316 String value = this.req.getParameter(name); 317 if (this.form_encoding == null || value == null) { 318 return value; 319 } 320 return decode(value); 321 } 322 323 private String decode(String str) { 324 if (str == null) return null; 325 try { 326 if (this.container_encoding == null) 327 this.container_encoding = "ISO-8859-1"; 328 byte[] bytes = str.getBytes(this.container_encoding); 329 return new String (bytes, form_encoding); 330 } catch (java.io.UnsupportedEncodingException uee) { 331 throw new CascadingRuntimeException("Unsupported Encoding Exception", uee); 332 } 333 } 334 335 public Enumeration getParameterNames() { 336 return this.req.getParameterNames(); 337 } 338 339 public String [] getParameterValues(String name) { 340 String [] values = this.req.getParameterValues(name); 341 if (values == null) return null; 342 if (this.form_encoding == null) { 343 return values; 344 } 345 String [] decoded_values = new String [values.length]; 346 for (int i = 0; i < values.length; ++i) { 347 decoded_values[i] = decode(values[i]); 348 } 349 return decoded_values; 350 } 351 352 public String getProtocol() { 353 return this.req.getProtocol(); 354 } 355 356 public String getScheme() { 357 return this.req.getScheme(); 358 } 359 360 public String getServerName() { 361 return this.req.getServerName(); 362 } 363 364 public int getServerPort() { 365 return this.req.getServerPort(); 366 } 367 368 public BufferedReader getReader() throws IOException { 369 return this.req.getReader(); 370 } 371 372 public String getRemoteAddr() { 373 return this.req.getRemoteAddr(); 374 } 375 376 public String getRemoteHost() { 377 return this.req.getRemoteHost(); 378 } 379 380 public void setAttribute(String name, Object o) { 381 this.req.setAttribute(name, o); 382 } 383 384 public void removeAttribute(String name) { 385 this.req.removeAttribute(name); 386 } 387 388 public Locale getLocale() { 389 return this.req.getLocale(); 390 } 391 392 public Enumeration getLocales() { 393 return this.req.getLocales(); 394 } 395 396 public boolean isSecure() { 397 return this.req.isSecure(); 398 } 399 400 public RequestDispatcher getRequestDispatcher(String path) { 401 return this.req.getRequestDispatcher(path); 402 } 403 404 408 public String getRealPath(String path) { 409 return this.req.getRealPath(path); 410 } 411 } 412 | Popular Tags |