1 16 package org.apache.cocoon.environment.portlet; 17 18 import org.apache.cocoon.environment.Cookie; 19 import org.apache.cocoon.environment.Request; 20 import org.apache.cocoon.environment.Session; 21 import org.apache.cocoon.portlet.multipart.MultipartActionRequest; 22 23 import org.apache.avalon.framework.CascadingRuntimeException; 24 25 import javax.portlet.PortalContext; 26 import javax.portlet.PortletMode; 27 import javax.portlet.PortletPreferences; 28 import javax.portlet.WindowState; 29 30 import java.util.Collections ; 31 import java.util.Enumeration ; 32 import java.util.HashMap ; 33 import java.util.Locale ; 34 import java.util.Map ; 35 import java.util.NoSuchElementException ; 36 import java.util.Vector ; 37 38 45 public abstract class PortletRequest implements Request { 46 47 48 private String servletPath; 49 50 51 private String pathInfo; 52 53 54 private final javax.portlet.PortletRequest request; 55 56 57 private final PortletEnvironment environment; 58 59 60 private String form_encoding; 61 62 63 private String container_encoding; 64 65 66 private PortletSession session; 67 68 private Cookie[] wrappedCookies; 69 private Map wrappedCookieMap; 70 protected String portletRequestURI; 71 72 73 76 protected PortletRequest(String servletPath, 77 String pathInfo, 78 javax.portlet.PortletRequest request, 79 PortletEnvironment environment) { 80 super(); 81 this.servletPath = servletPath; 82 this.pathInfo = pathInfo; 83 this.request = request; 84 this.environment = environment; 85 } 86 87 90 public Object get(String name) { 91 if (request instanceof MultipartActionRequest) { 93 return ((MultipartActionRequest) request).get(name); 94 } else { 95 String [] values = request.getParameterValues(name); 96 if (values == null) { 97 return null; 98 } 99 if (values.length == 1) { 100 return values[0]; 101 } 102 if (values.length > 1) { 103 Vector vect = new Vector (values.length); 104 for (int i = 0; i < values.length; i++) { 105 vect.add(values[i]); 106 } 107 return vect; 108 } 109 return null; 110 } 111 } 112 113 114 115 public String getAuthType() { 116 return this.request.getAuthType(); 117 } 118 119 private synchronized void wrapCookies() { 120 this.wrappedCookieMap = new HashMap (); 121 PortletPreferences cookies = this.request.getPreferences(); 122 if (cookies != null) { 123 this.wrappedCookies = new Cookie[cookies.getMap().size()]; 124 int i = 0; 125 for (Enumeration e = cookies.getNames(); e.hasMoreElements(); i++) { 126 String name = (String ) e.nextElement(); 127 PortletCookie cookie = new PortletCookie(name, cookies.getValue(name, null)); 128 this.wrappedCookies[i] = cookie; 129 this.wrappedCookieMap.put(cookie.getName(), cookie); 130 } 131 } 132 this.wrappedCookieMap = Collections.unmodifiableMap(this.wrappedCookieMap); 133 } 134 135 public Cookie[] getCookies() { 136 if (this.wrappedCookieMap == null) { 137 wrapCookies(); 138 } 139 return this.wrappedCookies; 140 } 141 142 public Map getCookieMap() { 143 if (this.wrappedCookieMap == null) { 144 wrapCookies(); 145 } 146 return this.wrappedCookieMap; 147 } 148 149 public long getDateHeader(String name) { 150 return Long.parseLong(this.request.getProperty(name)); 151 } 152 153 public String getHeader(String name) { 154 if (PortletEnvironment.HEADER_PORTLET_MODE.equals(name)) { 155 return this.request.getPortletMode().toString(); 156 } else if (PortletEnvironment.HEADER_WINDOW_STATE.equals(name)) { 157 return this.request.getWindowState().toString(); 158 } else { 159 return this.request.getProperty(name); 160 } 161 } 162 163 public Enumeration getHeaders(String name) { 164 return this.request.getProperties(name); 165 } 166 167 public Enumeration getHeaderNames() { 168 final Enumeration names = this.request.getPropertyNames(); 169 return new Enumeration () { 171 int position; 172 173 public boolean hasMoreElements() { 174 return names.hasMoreElements() || position < 2; 175 } 176 177 public Object nextElement() throws NoSuchElementException { 178 if (names.hasMoreElements()) { 179 return names.nextElement(); 180 } 181 182 if (position == 0) { 183 position++; 184 return PortletEnvironment.HEADER_PORTLET_MODE; 185 } else if (position == 1) { 186 position++; 187 return PortletEnvironment.HEADER_WINDOW_STATE; 188 } else { 189 throw new NoSuchElementException (); 190 } 191 } 192 }; 193 } 194 195 198 public abstract String getMethod(); 199 200 public String getPathInfo() { 201 return this.pathInfo; 202 } 203 204 public String getPathTranslated() { 205 return null; 207 } 208 209 public String getContextPath() { 210 return this.request.getContextPath(); 211 } 212 213 public String getQueryString() { 214 return ""; 216 } 217 218 public String getRemoteUser() { 219 return this.request.getRemoteUser(); 220 } 221 222 public boolean isUserInRole(String role) { 223 return this.request.isUserInRole(role); 224 } 225 226 public java.security.Principal getUserPrincipal() { 227 return this.request.getUserPrincipal(); 228 } 229 230 public String getRequestedSessionId() { 231 return this.request.getRequestedSessionId(); 232 } 233 234 public String getRequestURI() { 235 if (this.portletRequestURI == null) { 236 final StringBuffer buffer = new StringBuffer (); 237 buffer.append(this.request.getContextPath()); 238 239 if (getServletPath() != null) { 240 if (buffer.charAt(buffer.length()-1) != '/') { 241 buffer.append('/'); 242 } 243 buffer.append(getServletPath()); 244 } 245 246 if (getPathInfo() != null) { 247 if (buffer.charAt(buffer.length()-1) != '/') { 248 buffer.append('/'); 249 } 250 buffer.append(getPathInfo()); 251 } 252 253 this.portletRequestURI = buffer.toString(); 254 } 255 return this.portletRequestURI; 256 } 257 258 261 public String getSitemapURI() { 262 return this.environment.getURI(); 263 } 264 265 public String getSitemapURIPrefix() { 266 return this.environment.getURIPrefix(); 267 } 268 269 public String getServletPath() { 270 return this.servletPath; 271 } 272 273 public Session getSession(boolean create) { 274 javax.portlet.PortletSession serverSession = this.request.getPortletSession(create); 275 if (null != serverSession) { 276 if (null != this.session) { 277 if (this.session.session != serverSession) { 278 this.session.session = serverSession; 280 } 281 } else { 282 this.session = new PortletSession(serverSession, 284 this.environment.getDefaultSessionScope()); 285 } 286 } else { 287 this.session = null; 289 } 290 return this.session; 291 } 292 293 public Session getSession() { 294 return this.getSession(true); 295 } 296 297 public boolean isRequestedSessionIdValid() { 298 return this.request.isRequestedSessionIdValid(); 299 } 300 301 305 public boolean isRequestedSessionIdFromCookie() { 306 return false; 307 } 308 309 313 public boolean isRequestedSessionIdFromURL() { 314 return true; 315 } 316 317 318 319 public Object getAttribute(String name) { 320 return this.request.getAttribute(name); 321 } 322 323 public Enumeration getAttributeNames() { 324 return this.request.getAttributeNames(); 325 } 326 327 public String getCharacterEncoding() { 328 return this.form_encoding; 329 } 330 331 public void setCharacterEncoding(String form_encoding) throws java.io.UnsupportedEncodingException { 332 this.form_encoding = form_encoding; 333 } 334 335 338 public void setContainerEncoding(String container_encoding) { 339 this.container_encoding = container_encoding; 340 } 341 342 public int getContentLength() { 343 return -1; 346 } 347 348 public String getContentType() { 349 return null; 352 } 353 354 public String getParameter(String name) { 355 String value = this.request.getParameter(name); 356 if (this.form_encoding == null || value == null) { 357 return value; 358 } 359 return decode(value); 360 } 361 362 private String decode(String str) { 363 if (str == null) { 364 return null; 365 } 366 367 try { 368 if (this.container_encoding == null) { 369 this.container_encoding = "ISO-8859-1"; 370 } 371 byte[] bytes = str.getBytes(this.container_encoding); 372 return new String (bytes, form_encoding); 373 } catch (java.io.UnsupportedEncodingException uee) { 374 throw new CascadingRuntimeException("Unsupported Encoding Exception", uee); 375 } 376 } 377 378 public Enumeration getParameterNames() { 379 return this.request.getParameterNames(); 380 } 381 382 public String [] getParameterValues(String name) { 383 String [] values = this.request.getParameterValues(name); 384 if (values == null) { 385 return null; 386 } else if (this.form_encoding == null) { 387 return values; 388 } 389 String [] decoded_values = new String [values.length]; 390 for (int i = 0; i < values.length; ++i) { 391 decoded_values[i] = decode(values[i]); 392 } 393 return decoded_values; 394 } 395 396 public String getProtocol() { 397 return "JSR168"; 398 } 399 400 public String getScheme() { 401 return this.request.getScheme(); 402 } 403 404 public String getServerName() { 405 return this.request.getServerName(); 406 } 407 408 public int getServerPort() { 409 return this.request.getServerPort(); 410 } 411 412 public String getRemoteAddr() { 413 return null; 416 } 417 418 public String getRemoteHost() { 419 return null; 422 } 423 424 public void setAttribute(String name, Object o) { 425 this.request.setAttribute(name, o); 426 } 427 428 public void removeAttribute(String name) { 429 this.request.removeAttribute(name); 430 } 431 432 public Locale getLocale() { 433 return this.request.getLocale(); 434 } 435 436 public Enumeration getLocales() { 437 return this.request.getLocales(); 438 } 439 440 public boolean isSecure() { 441 return this.request.isSecure(); 442 } 443 444 445 446 447 451 public javax.portlet.PortletRequest getPortletRequest() { 452 return request; 453 } 454 455 public Map getParameterMap() { 456 return this.request.getParameterMap(); 457 } 458 459 public PortalContext getPortalContext() { 460 return this.request.getPortalContext(); 461 } 462 463 public PortletMode getPortletMode() { 464 return this.request.getPortletMode(); 465 } 466 467 public javax.portlet.PortletSession getPortletSession() { 468 return this.request.getPortletSession(); 469 } 470 471 public javax.portlet.PortletSession getPortletSession(boolean create) { 472 return this.request.getPortletSession(create); 473 } 474 475 public PortletPreferences getPreferences() { 476 return this.request.getPreferences(); 477 } 478 479 public Enumeration getProperties(String name) { 480 return this.request.getProperties(name); 481 } 482 483 public String getProperty(String name) { 484 return this.request.getProperty(name); 485 } 486 487 public Enumeration getPropertyNames() { 488 return this.request.getPropertyNames(); 489 } 490 491 public String getResponseContentType() { 492 return this.request.getResponseContentType(); 493 } 494 495 public Enumeration getResponseContentTypes() { 496 return this.request.getResponseContentTypes(); 497 } 498 499 public WindowState getWindowState() { 500 return this.request.getWindowState(); 501 } 502 503 public boolean isPortletModeAllowed(PortletMode mode) { 504 return this.request.isPortletModeAllowed(mode); 505 } 506 507 public boolean isWindowStateAllowed(WindowState state) { 508 return this.request.isWindowStateAllowed(state); 509 } 510 } 511 | Popular Tags |