1 5 6 13 package org.exoplatform.services.portletcontainer.impl.portletAPIImp; 14 15 16 import javax.portlet.PortletRequest ; 17 import javax.portlet.PortletContext ; 18 import javax.portlet.PortalContext ; 19 import javax.portlet.PortletPreferences ; 20 import javax.portlet.PortletMode ; 21 import javax.portlet.WindowState ; 22 import javax.portlet.PortletSession ; 23 import javax.portlet.PortletConfig ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletRequestWrapper ; 26 import javax.servlet.http.HttpSession ; 27 import java.util.*; 28 29 import org.apache.commons.logging.Log; 30 import org.exoplatform.container.PortalContainer; 31 import org.exoplatform.services.log.LogUtil; 32 import org.exoplatform.services.portletcontainer.ExoPortletRequest; 33 import org.exoplatform.services.portletcontainer.helper.PortletWindowInternal; 34 import org.exoplatform.services.portletcontainer.impl.PortletApplicationProxy; 35 import org.exoplatform.services.portletcontainer.impl.PortletContainerConf; 36 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.helpers.SharedSessionWrapper; 37 import org.exoplatform.services.portletcontainer.impl.portletAPIImp.pool.EmptyRequest; 38 import org.exoplatform.services.portletcontainer.pci.*; 39 import org.exoplatform.services.portletcontainer.pci.model.*; 40 41 42 49 public class PortletRequestImp extends HttpServletRequestWrapper 50 implements PortletRequest, ExoPortletRequest, Map { 51 52 private Collection roles_; 53 private PortletSessionImp session_; 54 private Portlet portletDatas_; 55 protected Input input_; 56 private PortletWindowInternal portletWindowInternal_; 57 private PortalContext portalContext_; 58 private PortletContext portletContext_; 59 private List securityContraints_; 60 private List userAttributes_; 61 private List customPortletModes_; 62 protected List customWindowStates_; 63 private Collection supportedContents_; 64 private Log log_; 65 66 76 public PortletRequestImp(HttpServletRequest httpServletRequest) { 77 super(httpServletRequest); 78 this.log_ = LogUtil.getLog("org.exoplatform.services.portletcontainer"); 79 } 80 81 public void fillPortletRequest(HttpServletRequest httpServletRequest, 82 PortalContext portalContext, 83 PortletContext portletContext, 84 PortletSessionImp session, 85 Portlet portletDatas, Input input, 86 PortletWindowInternal portletWindowInternal, 87 List securityContraints, List userAttributes, 88 List customPortletModes, 89 List customWindowStates, Collection roles, 90 Collection supportedContents) { 91 super.setRequest(httpServletRequest); 92 this.portalContext_ = portalContext; 93 this.portletContext_ = portletContext; 94 this.session_ = session; 95 this.portletDatas_ = portletDatas; 96 this.input_ = input; 97 this.portletWindowInternal_ = portletWindowInternal; 98 this.securityContraints_ = securityContraints; 99 this.userAttributes_ = userAttributes; 100 this.customPortletModes_ = customPortletModes; 101 this.customWindowStates_ = customWindowStates; 102 this.roles_ = roles; 103 this.supportedContents_ = supportedContents; 104 init(); 105 } 106 107 public void emptyPortletRequest() { 108 super.setRequest(new EmptyRequest()); 109 } 110 111 public void init() { 112 } 113 114 public Input getInput() { 115 return input_; 116 } 117 118 public boolean isWindowStateAllowed(WindowState windowState) { 119 Enumeration e = portalContext_.getSupportedWindowStates(); 120 while (e.hasMoreElements()) { 121 WindowState supportedWindowState = (WindowState) e.nextElement(); 122 if (supportedWindowState.equals(windowState)) 123 return true; 124 } 125 return false; 126 } 127 128 public boolean isPortletModeAllowed(PortletMode portletMode) { 129 Enumeration e = portalContext_.getSupportedPortletModes(); 130 while (e.hasMoreElements()) { 131 PortletMode supportedPortletMode = (PortletMode) e.nextElement(); 132 if (supportedPortletMode.toString().toLowerCase().equals(portletMode.toString().toLowerCase())) 133 return true; 134 } 135 136 return false; 137 } 138 139 public PortletConfig getPortletConfig(){ 140 PortalContainer manager = PortalContainer.getInstance(); 141 String portletAppName = portletWindowInternal_.getWindowID().getPortletApplicationName() ; 142 PortletApplicationProxy proxy = 143 (PortletApplicationProxy) manager.getComponentInstance(portletAppName); 144 return proxy.getPortletConfig(portletWindowInternal_.getWindowID().getPortletName()); 145 } 146 147 public PortletMode getPortletMode() { 148 return input_.getPortletMode(); 149 } 150 151 public WindowState getWindowState() { 152 return input_.getWindowState(); 153 } 154 155 public PortletPreferences getPreferences() { 156 return portletWindowInternal_.getPreferences(); 157 } 158 159 public PortletSession getPortletSession() { 160 return getPortletSession(true); 161 } 162 163 public PortletSession getPortletSession(boolean create) { 164 PortletContainerConf conf = (PortletContainerConf)PortalContainer.getInstance(). 165 getComponentInstanceOfType(PortletContainerConf.class); 166 boolean isSharedSessionEnable = conf.isSharedSessionEnable(); 167 if(create){ 168 if(session_.isSessionValid()){ 169 return session_; 170 } 171 if(isSharedSessionEnable){ 172 ((SharedSessionWrapper)session_.getSession()).init(); 173 } else{ 174 session_.setSession(((HttpServletRequest )super.getRequest()).getSession()); 175 } 176 return session_; 177 } 178 if(isSharedSessionEnable){ 179 if(session_.isSessionValid()){ 180 return session_; 181 } 182 } else{ 183 HttpSession tmpSession = ((HttpServletRequest )super.getRequest()).getSession(false); 184 if(tmpSession == null) 185 return null; 186 try{ 188 tmpSession.getLastAccessedTime(); 189 } catch (IllegalStateException e){ 190 log_.error("IllegalStateExcetion sent in PortletRequestImp getPortletSession()", e); 191 return null; 192 } 193 return session_; 194 } 195 return null; 196 } 197 198 public boolean isRequestedSessionIdValid() { 199 PortletContainerConf conf = 200 (PortletContainerConf) PortalContainer.getInstance(). 201 getComponentInstanceOfType(PortletContainerConf.class); 202 boolean isSharedSessionEnable = conf.isSharedSessionEnable(); 203 if (isSharedSessionEnable) { 204 return this.session_.isSessionValid(); 205 } 206 try { 208 ((HttpServletRequest ) super.getRequest()).getSession() 209 .getLastAccessedTime(); 210 } catch (IllegalStateException e) { 211 log_.error("IllegalStateExcetion sent in PortletRequestImp isRequestedSessionIdValid()", e); 212 return false; 213 } 214 return ((HttpServletRequest ) super.getRequest()).isRequestedSessionIdValid(); 215 } 216 217 public String getProperty(String s) { 218 String header = ((HttpServletRequest )super.getRequest()).getHeader(s); 219 if(header != null) 220 return header; 221 return portalContext_.getProperty(s); 222 } 223 224 public Enumeration getProperties(String s) { 225 Enumeration header = ((HttpServletRequest )super.getRequest()).getHeaders(s); 226 return header; 227 } 228 229 public Enumeration getPropertyNames() { 230 Enumeration headerNames = ((HttpServletRequest )super.getRequest()).getHeaderNames(); 231 Enumeration portalPropertyNames = portalContext_.getPropertyNames(); 232 Collection global = new ArrayList(); 233 while (portalPropertyNames.hasMoreElements()) { 234 String s = (String ) portalPropertyNames.nextElement(); 235 global.add(s); 236 } 237 while (headerNames.hasMoreElements()) { 238 String s = (String ) headerNames.nextElement(); 239 global.add(s); 240 } 241 return Collections.enumeration(global); 242 } 243 244 public PortalContext getPortalContext() { 245 return portalContext_; 246 } 247 248 public String getResponseContentType() { 249 List l = portletDatas_.getSupports(); 250 String markup = input_.getMarkup() ; 251 String inputPortletMode = input_.getPortletMode().toString() ; 252 for (int i = 0; i < l.size(); i++) { 253 Supports supportsType = (Supports) l.get(i); 254 String mimeType = supportsType.getMimeType(); 255 if (mimeType.equals(markup)) { 256 List portletModes = supportsType.getPortletMode(); 257 for (int modeIdx = 0; modeIdx < portletModes.size(); modeIdx++) { 258 String portletMode = (String ) portletModes.get(modeIdx); 259 if(portletMode.equals(inputPortletMode)) return mimeType; 260 } 261 } 262 } 263 return "text/html"; 264 } 265 266 public Enumeration getResponseContentTypes() { 267 List c = new ArrayList(); 268 c.add(getResponseContentType()); 269 String markup = input_.getMarkup() ; 270 String inputPortletMode = input_.getPortletMode().toString() ; 271 for (Iterator iter = supportedContents_.iterator(); iter.hasNext();) { 272 String supportedContent = (String ) iter.next(); 273 List l = portletDatas_.getSupports(); 274 for (int i = 0; i < l.size(); i++) { 275 Supports supportsType = (Supports) l.get(i); 276 String mimeType = supportsType.getMimeType(); 277 if(supportedContent.equals(mimeType) && !supportedContent.equals(markup)){ 278 List portletModes = supportsType.getPortletMode(); 279 for (Iterator iter2 = portletModes.iterator(); iter2.hasNext();) { 280 String portletMode = (String ) iter2.next(); 281 if(portletMode.equals(inputPortletMode)){ 282 c.add(mimeType); 283 break; 284 } 285 } 286 } 287 } 288 } 289 return Collections.enumeration(c); 290 } 291 292 public boolean isUserInRole(String role){ 293 List l = portletDatas_.getSecurityRoleRef(); 294 for (Iterator iterator = l.iterator(); iterator.hasNext();) { 295 SecurityRoleRef securityRoleRef = (SecurityRoleRef) iterator.next(); 296 if(securityRoleRef.getRoleName().equals(role)){ 297 String roleLink = securityRoleRef.getRoleLink(); 298 if(roleLink == null || "".equals(roleLink)){ 299 if(isRoleDefinedInWebXML(role)) return super.isUserInRole(role); 300 return false; 301 } 302 if(isRoleDefinedInWebXML(roleLink)) return super.isUserInRole(roleLink); 303 return false; 304 } 305 } 306 return false; 307 } 308 309 private boolean isRoleDefinedInWebXML(String role){ 310 for (Iterator iter = roles_.iterator(); iter.hasNext();) { 311 String roleDefined = (String ) iter.next(); 312 if(roleDefined.equals(role)) 313 return true; 314 } 315 return false; 316 } 317 318 public Portlet getPortletDatas() { 319 return portletDatas_; 320 } 321 322 public PortletWindowInternal getPortletWindowInternal() { 323 return portletWindowInternal_; 324 } 325 326 public boolean needsSecurityContraints(String portletName) { 327 for (Iterator iterator = securityContraints_.iterator(); iterator.hasNext();) { 328 SecurityConstraint securityConstraint = (SecurityConstraint) iterator.next(); 329 List l = securityConstraint.getPortletCollection().getPortletName(); 330 for (Iterator iterator2 = l.iterator(); iterator2.hasNext();) { 331 String portletN = (String ) iterator2.next(); 332 if(portletN.equals(portletName)) return true; 333 } 334 } 335 return false; 336 } 337 338 public String getAuthType(){ 339 String type = super.getAuthType(); 340 if(HttpServletRequest.BASIC_AUTH.equals(type)) 341 return PortletRequest.BASIC_AUTH; 342 else if (HttpServletRequest.DIGEST_AUTH.equals(type)) 343 return PortletRequest.DIGEST_AUTH; 344 else if (HttpServletRequest.CLIENT_CERT_AUTH.equals(type)) 345 return PortletRequest.CLIENT_CERT_AUTH; 346 else if (HttpServletRequest.FORM_AUTH.equals(type)) 347 return PortletRequest.FORM_AUTH; 348 else 349 return type; 350 } 351 352 public Object getAttribute(String name) { 353 if (name == null) { 354 throw new IllegalArgumentException ("The attribute name cannot be null") ; 355 } 356 return super.getAttribute(name) ; 357 } 358 359 public void setAttribute(String name, Object value) { 360 if (name == null) { 361 throw new IllegalArgumentException ("The attribute name cannot be null") ; 362 } 363 if(value == null) { 365 super.removeAttribute(name) ; 366 } else { 367 super.setAttribute(name, value) ; 368 } 369 } 370 371 public void removeAttribute(String name) { 372 if (name == null) { 373 throw new IllegalArgumentException ("The attribute name cannot be null") ; 374 } 375 super.removeAttribute(name) ; 376 } 377 378 public String getContextPath() { 379 return "/" + this.portletContext_.getPortletContextName() ; 380 } 381 382 384 public int size() { 385 int n = 0; 386 Enumeration keys = getAttributeNames(); 387 while (keys.hasMoreElements()) { 388 n++; 390 } 391 return n; 392 } 393 394 public boolean isEmpty() { 395 return !getAttributeNames().hasMoreElements(); 396 } 397 398 public boolean containsKey( Object key ) { 399 return (getAttribute( (String ) key ) != null); 400 } 401 402 public boolean containsValue( Object value ) { 403 boolean match = false; 404 Enumeration keys = getAttributeNames(); 405 while (keys.hasMoreElements()) { 406 String key = (String ) keys.nextElement(); 407 Object val = getAttribute( key ); 408 if (value.equals( val )) { 409 match = true; 410 break; 411 } 412 } 413 return match; 414 } 415 416 public Object get( Object key ) { 417 return getAttribute( (String ) key ); 418 } 419 420 public Object put( Object key, Object value ) { 421 Object result = null; 422 if (containsKey( key )) result = getAttribute( (String ) key ); 423 setAttribute( (String ) key, value ); 424 return result; 425 } 426 427 public Object remove( Object key ) { 428 Object result = null; 429 if (containsKey( key )) result = getAttribute( (String ) key ); 430 removeAttribute( (String ) key ); 431 return result; 432 } 433 434 public void putAll( Map t ) { 435 for (Iterator i = t.keySet().iterator(); i.hasNext();) { 436 String key = (String ) i.next(); 437 Object value = t.get( key ); 438 put( key, value ); 439 } 440 } 441 442 public void clear() { 443 throw new UnsupportedOperationException (); 444 } 445 446 public Set keySet() { 447 throw new UnsupportedOperationException (); 448 } 449 450 public Collection values() { 451 throw new UnsupportedOperationException (); 452 } 453 454 public Set entrySet() { 455 throw new UnsupportedOperationException (); 456 } 457 458 public Locale getLocale(){ 459 List locales = input_.getLocales(); 460 if(locales != null && !locales.isEmpty()){ 461 return (Locale) locales.iterator().next(); 462 } 463 return super.getLocale(); 464 } 465 466 public Enumeration getLocales(){ 467 List locales = input_.getLocales(); 468 if(locales != null){ 469 return Collections.enumeration(locales); 470 } 471 return super.getLocales(); 472 473 } 474 }
| Popular Tags
|