1 9 package org.jboss.portal.portlet.impl; 10 11 import java.security.Principal ; 12 import java.util.ArrayList ; 13 import java.util.Collections ; 14 import java.util.Enumeration ; 15 import java.util.Locale ; 16 import java.util.Map ; 17 import java.util.Vector ; 18 import java.util.List ; 19 import java.util.Set ; 20 21 import javax.portlet.PortalContext; 22 import javax.portlet.PortletMode; 23 import javax.portlet.PortletPreferences; 24 import javax.portlet.PortletRequest; 25 import javax.portlet.PortletSession; 26 import javax.portlet.WindowState; 27 import javax.servlet.http.HttpServletRequest ; 28 import javax.servlet.http.HttpSession ; 29 30 import org.apache.log4j.Logger; 31 import org.jboss.portal.common.util.Tools; 32 import org.jboss.portal.portlet.PortletApplication; 33 import org.jboss.portal.portlet.PortletContainer; 34 import org.jboss.portal.portlet.PortletUtils; 35 import org.jboss.portal.server.Component; 36 import org.jboss.portal.server.Instance; 37 import org.jboss.portal.server.InstanceContext; 38 import org.jboss.portal.server.PortalRequest; 39 import org.jboss.portal.server.PortalResponse; 40 import org.jboss.portal.server.Window; 41 import org.jboss.portal.server.WindowContext; 42 import org.jboss.portal.server.util.Properties; 43 import org.jboss.portal.server.plugins.mode.ContentTypes; 44 import org.jboss.portal.server.user.UserContext; 45 46 53 public abstract class PortletRequestImpl implements PortletRequest 54 { 55 56 protected static final Logger log = Logger.getLogger(PortletRequestImpl.class); 57 58 protected UserContext userContext; 59 60 protected Window window; 61 protected WindowContext windowCtx; 62 protected Instance instance; 63 protected InstanceContext instanceCtx; 64 protected Component component; 65 66 protected ContentTypes contentTypes; 67 68 protected PortalRequest req; 69 protected PortalResponse resp; 70 71 protected Map userInfo; 72 73 protected HttpServletRequest dreq; 74 75 protected PortalContext portalContext; 76 protected PortletSessionImpl psession; 77 protected PortletPreferences preferences; 78 79 protected int sessionStatus; 80 81 public PortletRequestImpl( 82 UserContext userContext, 83 PortletPreferences preferences, 84 Window window, 85 WindowContext windowCtx, 86 PortalRequest req, 87 PortalResponse resp, 88 HttpServletRequest dreq) 89 { 90 this.userContext = userContext; 91 this.window = window; 92 this.windowCtx = windowCtx; 93 this.instance = window.getInstance(); 94 this.instanceCtx = (InstanceContext)userContext.getContext(instance); 95 this.component = instance.getComponent(); 96 this.contentTypes = component.getContentTypes(); 97 this.portalContext = new PortalContextImpl(window.getPortal()); 98 this.req = req; 99 this.resp = resp; 100 this.dreq = dreq; 101 this.preferences = preferences; 102 } 103 104 106 public Object getAttribute(String name) 107 { 108 if (name == null) 109 { 110 throw new IllegalArgumentException ("name must not be null"); 111 } 112 if (PortletRequest.USER_INFO.equals(name)) 113 { 114 Map infos = userContext.getInformations(); 125 return infos != null ? Collections.unmodifiableMap(infos) : null; 126 } 127 else 128 { 129 return req.getAttribute(name); 130 } 131 } 132 133 public Enumeration getAttributeNames() 134 { 135 ArrayList list = new ArrayList (); 140 for (Enumeration e = req.getAttributeNames();e.hasMoreElements();) 141 { 142 list.add(e.nextElement()); 143 } 144 return Tools.toEnumeration(list.iterator()); 145 } 146 147 public void setAttribute(String name, Object value) 148 { 149 if (name == null) 150 { 151 throw new IllegalArgumentException ("name must not be null"); 152 } 153 if (!PortletRequest.USER_INFO.equals(name)) 154 { 155 if (value != null) 156 { 157 req.setAttribute(name, value); 158 } 159 else 160 { 161 req.removeAttribute(name); 162 } 163 } 164 } 165 166 public void removeAttribute(String name) 167 { 168 if (name == null) 169 { 170 throw new IllegalArgumentException ("name must not be null"); 171 } 172 if (!PortletRequest.USER_INFO.equals(name)) 173 { 174 req.removeAttribute(name); 175 } 176 } 177 178 180 public String getProperty(String name) 181 { 182 Properties properties = req.getProperties(); 184 String property = properties.getProperty(name); 185 if (property != null) 186 { 187 return property; 188 } 189 190 return req.getHeader(name); 192 } 193 194 public Enumeration getProperties(String name) 195 { 196 Properties properties = req.getProperties(); 198 List list = properties.getProperties(name); 199 if (list.size() > 0) 200 { 201 return Tools.toEnumeration(list.iterator()); 202 } 203 204 return req.getHeaders(name); 206 } 207 208 public Enumeration getPropertyNames() 209 { 210 Properties properties = req.getProperties(); 212 Set names = properties.getPropertyNames(); 213 Set headers = Tools.toSet(req.getHeaderNames()); 214 headers.addAll(names); 215 return Tools.toEnumeration(names.iterator()); 216 } 217 218 220 public String getContextPath() 221 { 222 return instance.getComponent().getApplication().getContextPath(); 224 } 225 226 228 public String getAuthType() 229 { 230 return req.getAuthType(); 231 } 232 233 public String getRemoteUser() 234 { 235 return req.getRemoteUser(); 236 } 237 238 public Principal getUserPrincipal() 239 { 240 return req.getUserPrincipal(); 241 } 242 243 public boolean isUserInRole(String roleName) 244 { 245 PortletContainer portletContainer = (PortletContainer)instance.getComponent(); 247 Map securityRoleRefsMap = portletContainer.getSecurityRoleRefsMap(); 248 249 String roleLink = (String )securityRoleRefsMap.get(roleName); 251 if (roleLink == null) 252 { 253 if (securityRoleRefsMap.containsKey(roleName)) 254 { 255 return req.isUserInRole(roleName); 257 } 258 else 259 { 260 return false; 262 } 263 } 264 else 265 { 266 return req.isUserInRole(roleLink); 268 } 269 } 270 271 public boolean isSecure() 272 { 273 return req.isSecure(); 274 } 275 276 278 public String getResponseContentType() 279 { 280 return resp.getContentType(); 281 } 282 283 public Enumeration getResponseContentTypes() 284 { 285 Vector v = new Vector (); 286 v.add(getResponseContentType()); 287 return v.elements(); 288 } 289 290 292 public Locale getLocale() 293 { 294 return req.getLocale(); 295 } 296 297 299 public boolean isPortletModeAllowed(PortletMode portletMode) 300 { 301 if (portletMode == null) 302 { 303 log.warn("Try to test a null portlet mode"); 305 return false; 306 } 307 else 308 { 309 String contentType = resp.getContentType(); 310 return window.isSupportedMode( 311 contentType, 312 org.jboss.portal.server.plugins.mode.Mode.create(portletMode.toString())); 313 } 314 } 315 316 public PortletMode getPortletMode() 317 { 318 return PortletUtils.decodePortletMode(windowCtx.getMode().toString()); 319 } 320 321 323 public boolean isWindowStateAllowed(WindowState windowState) 324 { 325 return window.isSupportedWindowState(org.jboss.portal.server.plugins.windowstate.WindowState.create(windowState.toString())); 326 } 327 328 public WindowState getWindowState() 329 { 330 return PortletUtils.decodeWindowState(windowCtx.getWindowState().toString()); 331 } 332 333 335 public PortletSession getPortletSession() 336 { 337 return getPortletSession(true); 338 } 339 340 public PortletSession getPortletSession(boolean create) 341 { 342 if (!create) 344 { 345 create = psession == null; 347 if (!create) 348 { 349 try 350 { 351 psession.getSession().isNew(); 352 } 353 catch (IllegalStateException e) 354 { 355 create = true; 357 } 358 } 359 } 360 361 if (create) 362 { 363 HttpSession hsession = dreq.getSession(create); 365 366 String id = instance.getName() + "." + window.getName(); 368 369 PortletContainer portletContainer = (PortletContainer)instance.getComponent(); 371 PortletApplication application = (PortletApplication)portletContainer.getApplication(); 372 psession = new PortletSessionImpl( 373 hsession, 374 id, 375 application.getPortletContext()); 376 } 377 return psession; 378 } 379 380 public PortalContext getPortalContext() 381 { 382 return portalContext; 383 } 384 385 public String getRequestedSessionId() 386 { 387 return dreq.getRequestedSessionId(); 388 } 389 390 public boolean isRequestedSessionIdValid() 391 { 392 return dreq.isRequestedSessionIdValid(); 393 } 394 395 public Enumeration getLocales() 396 { 397 return req.getLocales(); 398 } 399 400 public String getScheme() 401 { 402 return req.getScheme(); 403 } 404 405 public String getServerName() 406 { 407 return req.getServerName(); 408 } 409 410 public int getServerPort() 411 { 412 return req.getServerPort(); 413 } 414 415 public PortletPreferences getPreferences() 416 { 417 return preferences; 418 } 419 } 420 | Popular Tags |