1 16 17 package org.springframework.mock.web.portlet; 18 19 import java.security.Principal ; 20 import java.util.Collections ; 21 import java.util.Enumeration ; 22 import java.util.HashSet ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.LinkedList ; 26 import java.util.List ; 27 import java.util.Locale ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.Vector ; 31 32 import javax.portlet.PortalContext; 33 import javax.portlet.PortletContext; 34 import javax.portlet.PortletMode; 35 import javax.portlet.PortletPreferences; 36 import javax.portlet.PortletRequest; 37 import javax.portlet.PortletSession; 38 import javax.portlet.WindowState; 39 40 import org.springframework.core.CollectionFactory; 41 import org.springframework.util.Assert; 42 import org.springframework.util.CollectionUtils; 43 44 51 public class MockPortletRequest implements PortletRequest { 52 53 private boolean active = true; 54 55 private final PortalContext portalContext; 56 57 private final PortletContext portletContext; 58 59 private PortletSession session = null; 60 61 private WindowState windowState = WindowState.NORMAL; 62 63 private PortletMode portletMode = PortletMode.VIEW; 64 65 private PortletPreferences portletPreferences = new MockPortletPreferences(); 66 67 private final Map properties = CollectionFactory.createLinkedMapIfPossible(16); 68 69 private final Hashtable attributes = new Hashtable (); 70 71 private final Map parameters = CollectionFactory.createLinkedMapIfPossible(16); 72 73 private String authType = null; 74 75 private String contextPath = ""; 76 77 private String remoteUser = null; 78 79 private Principal userPrincipal = null; 80 81 private final Set userRoles = new HashSet (); 82 83 private boolean secure = false; 84 85 private boolean requestedSessionIdValid = true; 86 87 private final Vector responseContentTypes = new Vector (); 88 89 private final Vector locales = new Vector (); 90 91 private String scheme = "http"; 92 93 private String serverName = "localhost"; 94 95 private int serverPort = 80; 96 97 98 104 public MockPortletRequest() { 105 this(null, null); 106 } 107 108 113 public MockPortletRequest(PortletContext portletContext) { 114 this(null, portletContext); 115 } 116 117 122 public MockPortletRequest(PortalContext portalContext, PortletContext portletContext) { 123 this.portalContext = (portalContext != null ? portalContext : new MockPortalContext()); 124 this.portletContext = (portletContext != null ? portletContext : new MockPortletContext()); 125 this.responseContentTypes.add("text/html"); 126 this.locales.add(Locale.ENGLISH); 127 } 128 129 130 134 137 public boolean isActive() { 138 return this.active; 139 } 140 141 144 public void close() { 145 this.active = false; 146 } 147 148 152 protected void checkActive() throws IllegalStateException { 153 if (!this.active) { 154 throw new IllegalStateException ("Request is not active anymore"); 155 } 156 } 157 158 159 163 public boolean isWindowStateAllowed(WindowState windowState) { 164 return CollectionUtils.contains(this.portalContext.getSupportedWindowStates(), windowState); 165 } 166 167 public boolean isPortletModeAllowed(PortletMode portletMode) { 168 return CollectionUtils.contains(this.portalContext.getSupportedPortletModes(), portletMode); 169 } 170 171 public void setPortletMode(PortletMode portletMode) { 172 this.portletMode = portletMode; 173 } 174 175 public PortletMode getPortletMode() { 176 return this.portletMode; 177 } 178 179 public void setWindowState(WindowState windowState) { 180 this.windowState = windowState; 181 } 182 183 public WindowState getWindowState() { 184 return this.windowState; 185 } 186 187 public void setPreferences(PortletPreferences preferences) { 188 this.portletPreferences = preferences; 189 } 190 191 public PortletPreferences getPreferences() { 192 return this.portletPreferences; 193 } 194 195 public void setSession(PortletSession session) { 196 this.session = session; 197 if (session instanceof MockPortletSession) { 198 MockPortletSession mockSession = ((MockPortletSession) session); 199 mockSession.access(); 200 } 201 } 202 203 public PortletSession getPortletSession() { 204 return getPortletSession(true); 205 } 206 207 public PortletSession getPortletSession(boolean create) { 208 checkActive(); 209 if (this.session instanceof MockPortletSession && ((MockPortletSession) this.session).isInvalid()) { 211 this.session = null; 212 } 213 if (this.session == null && create) { 215 this.session = new MockPortletSession(this.portletContext); 216 } 217 return this.session; 218 } 219 220 225 public void setProperty(String key, String value) { 226 Assert.notNull(key, "Property key must not be null"); 227 List list = new LinkedList (); 228 list.add(value); 229 this.properties.put(key, list); 230 } 231 232 237 public void addProperty(String key, String value) { 238 Assert.notNull(key, "Property key must not be null"); 239 List oldList = (List ) this.properties.get(key); 240 if (oldList != null) { 241 oldList.add(value); 242 } 243 else { 244 List list = new LinkedList (); 245 list.add(value); 246 this.properties.put(key, list); 247 } 248 } 249 250 public String getProperty(String key) { 251 Assert.notNull(key, "Property key must not be null"); 252 List list = (List ) this.properties.get(key); 253 return (list != null && list.size() > 0 ? (String ) list.get(0) : null); 254 } 255 256 public Enumeration getProperties(String key) { 257 Assert.notNull(key, "property key must not be null"); 258 return Collections.enumeration((List ) this.properties.get(key)); 259 } 260 261 public Enumeration getPropertyNames() { 262 return Collections.enumeration(this.properties.keySet()); 263 } 264 265 public PortalContext getPortalContext() { 266 return this.portalContext; 267 } 268 269 public void setAuthType(String authType) { 270 this.authType = authType; 271 } 272 273 public String getAuthType() { 274 return this.authType; 275 } 276 277 public void setContextPath(String contextPath) { 278 this.contextPath = contextPath; 279 } 280 281 public String getContextPath() { 282 return this.contextPath; 283 } 284 285 public void setRemoteUser(String remoteUser) { 286 this.remoteUser = remoteUser; 287 } 288 289 public String getRemoteUser() { 290 return this.remoteUser; 291 } 292 293 public void setUserPrincipal(Principal userPrincipal) { 294 this.userPrincipal = userPrincipal; 295 } 296 297 public Principal getUserPrincipal() { 298 return this.userPrincipal; 299 } 300 301 public void addUserRole(String role) { 302 this.userRoles.add(role); 303 } 304 305 public boolean isUserInRole(String role) { 306 return this.userRoles.contains(role); 307 } 308 309 public Object getAttribute(String name) { 310 checkActive(); 311 return this.attributes.get(name); 312 } 313 314 public Enumeration getAttributeNames() { 315 checkActive(); 316 return this.attributes.keys(); 317 } 318 319 public void setParameters(Map parameters) { 320 Assert.notNull(parameters, "Parameters Map must not be null"); 321 this.parameters.clear(); 322 for (Iterator it = parameters.entrySet().iterator(); it.hasNext();) { 323 Map.Entry entry = (Map.Entry ) it.next(); 324 Assert.isTrue(entry.getKey() instanceof String , "Key must be of type String"); 325 Assert.isTrue(entry.getValue() instanceof String [], "Value must be of type String[]"); 326 this.parameters.put(entry.getKey(), entry.getValue()); 327 } 328 } 329 330 public void setParameter(String key, String value) { 331 Assert.notNull(key, "Parameter key must be null"); 332 Assert.notNull(value, "Parameter value must not be null"); 333 this.parameters.put(key, new String [] {value}); 334 } 335 336 public void setParameter(String key, String [] values) { 337 Assert.notNull(key, "Parameter key must be null"); 338 Assert.notNull(values, "Parameter values must not be null"); 339 this.parameters.put(key, values); 340 } 341 342 public void addParameter(String name, String value) { 343 addParameter(name, new String [] {value}); 344 } 345 346 public void addParameter(String name, String [] values) { 347 String [] oldArr = (String []) this.parameters.get(name); 348 if (oldArr != null) { 349 String [] newArr = new String [oldArr.length + values.length]; 350 System.arraycopy(oldArr, 0, newArr, 0, oldArr.length); 351 System.arraycopy(values, 0, newArr, oldArr.length, values.length); 352 this.parameters.put(name, newArr); 353 } 354 else { 355 this.parameters.put(name, values); 356 } 357 } 358 359 public String getParameter(String name) { 360 String [] arr = (String []) this.parameters.get(name); 361 return (arr != null && arr.length > 0 ? arr[0] : null); 362 } 363 364 public Enumeration getParameterNames() { 365 return Collections.enumeration(this.parameters.keySet()); 366 } 367 368 public String [] getParameterValues(String name) { 369 return (String []) this.parameters.get(name); 370 } 371 372 public Map getParameterMap() { 373 return Collections.unmodifiableMap(this.parameters); 374 } 375 376 public void setSecure(boolean secure) { 377 this.secure = secure; 378 } 379 380 public boolean isSecure() { 381 return this.secure; 382 } 383 384 public void setAttribute(String name, Object value) { 385 checkActive(); 386 if (value != null) { 387 this.attributes.put(name, value); 388 } 389 else { 390 this.attributes.remove(name); 391 } 392 } 393 394 public void removeAttribute(String name) { 395 checkActive(); 396 this.attributes.remove(name); 397 } 398 399 public String getRequestedSessionId() { 400 PortletSession session = this.getPortletSession(); 401 return (session != null ? session.getId() : null); 402 } 403 404 public void setRequestedSessionIdValid(boolean requestedSessionIdValid) { 405 this.requestedSessionIdValid = requestedSessionIdValid; 406 } 407 408 public boolean isRequestedSessionIdValid() { 409 return this.requestedSessionIdValid; 410 } 411 412 public void addResponseContentType(String responseContentType) { 413 this.responseContentTypes.add(responseContentType); 414 } 415 416 public void addPreferredResponseContentType(String responseContentType) { 417 this.responseContentTypes.add(0, responseContentType); 418 } 419 420 public String getResponseContentType() { 421 return (String ) this.responseContentTypes.get(0); 422 } 423 424 public Enumeration getResponseContentTypes() { 425 return this.responseContentTypes.elements(); 426 } 427 428 public void addLocale(Locale locale) { 429 this.locales.add(locale); 430 } 431 432 public void addPreferredLocale(Locale locale) { 433 this.locales.add(0, locale); 434 } 435 436 public Locale getLocale() { 437 return (Locale ) this.locales.get(0); 438 } 439 440 public Enumeration getLocales() { 441 return this.locales.elements(); 442 } 443 444 public void setScheme(String scheme) { 445 this.scheme = scheme; 446 } 447 448 public String getScheme() { 449 return scheme; 450 } 451 452 public void setServerName(String serverName) { 453 this.serverName = serverName; 454 } 455 456 public String getServerName() { 457 return serverName; 458 } 459 460 public void setServerPort(int serverPort) { 461 this.serverPort = serverPort; 462 } 463 464 public int getServerPort() { 465 return serverPort; 466 } 467 468 } 469 | Popular Tags |