1 /*
2  * Copyright 2002-2009 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
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.LinkedHashMap;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.Set;
29 import javax.portlet.PortalContext;
30 import javax.portlet.PortletContext;
31 import javax.portlet.PortletMode;
32 import javax.portlet.PortletPreferences;
33 import javax.portlet.PortletRequest;
34 import javax.portlet.PortletSession;
35 import javax.portlet.WindowState;
36 import javax.servlet.http.Cookie;
37 
38 import org.springframework.util.Assert;
39 import org.springframework.util.CollectionUtils;
40 
41 /**
42  * Mock implementation of the {@link javax.portlet.PortletRequest} interface.
43  *
44  * @author John A. Lewis
45  * @author Juergen Hoeller
46  * @since 2.0
47  */
48 public class MockPortletRequest implements PortletRequest {
49 
50 	private boolean active = true;
51 
52 	private final PortalContext portalContext;
53 
54 	private final PortletContext portletContext;
55 
56 	private PortletSession session;
57 
58 	private WindowState windowState = WindowState.NORMAL;
59 
60 	private PortletMode portletMode = PortletMode.VIEW;
61 
62 	private PortletPreferences portletPreferences = new MockPortletPreferences();
63 
64 	private final Map<String, List<String>> properties = new LinkedHashMap<String, List<String>>();
65 
66 	private final Map<String, Object> attributes = new LinkedHashMap<String, Object>();
67 
68 	private final Map<String, String[]> parameters = new LinkedHashMap<String, String[]>();
69 
70 	private String authType = null;
71 
72 	private String contextPath = "";
73 
74 	private String remoteUser = null;
75 
76 	private Principal userPrincipal = null;
77 
78 	private final Set<String> userRoles = new HashSet<String>();
79 
80 	private boolean secure = false;
81 
82 	private boolean requestedSessionIdValid = true;
83 
84 	private final List<String> responseContentTypes = new LinkedList<String>();
85 
86 	private final List<Locale> locales = new LinkedList<Locale>();
87 
88 	private String scheme = "http";
89 
90 	private String serverName = "localhost";
91 
92 	private int serverPort = 80;
93 
94 	private String windowID;
95 
96 	private Cookie[] cookies;
97 
98 	private final Set<String> publicParameterNames = new HashSet<String>();
99 
100 
101 	/**
102 	 * Create a new MockPortletRequest with a default {@link MockPortalContext}
103 	 * and a default {@link MockPortletContext}.
104 	 * @see MockPortalContext
105 	 * @see MockPortletContext
106 	 */
MockPortletRequest()107 	public MockPortletRequest() {
108 		this(null, null);
109 	}
110 
111 	/**
112 	 * Create a new MockPortletRequest with a default {@link MockPortalContext}.
113 	 * @param portletContext the PortletContext that the request runs in
114 	 * @see MockPortalContext
115 	 */
MockPortletRequest(PortletContext portletContext)116 	public MockPortletRequest(PortletContext portletContext) {
117 		this(null, portletContext);
118 	}
119 
120 	/**
121 	 * Create a new MockPortletRequest.
122 	 * @param portalContext the PortalContext that the request runs in
123 	 * @param portletContext the PortletContext that the request runs in
124 	 */
MockPortletRequest(PortalContext portalContext, PortletContext portletContext)125 	public MockPortletRequest(PortalContext portalContext, PortletContext portletContext) {
126 		this.portalContext = (portalContext != null ? portalContext : new MockPortalContext());
127 		this.portletContext = (portletContext != null ? portletContext : new MockPortletContext());
128 		this.responseContentTypes.add("text/html");
129 		this.locales.add(Locale.ENGLISH);
130 		this.attributes.put(LIFECYCLE_PHASE, getLifecyclePhase());
131 	}
132 
133 
134 	//---------------------------------------------------------------------
135 	// Lifecycle methods
136 	//---------------------------------------------------------------------
137 
138 	/**
139 	 * Return the Portlet 2.0 lifecycle id for the current phase.
140 	 */
getLifecyclePhase()141 	protected String getLifecyclePhase() {
142 		return null;
143 	}
144 
145 	/**
146 	 * Return whether this request is still active (that is, not completed yet).
147 	 */
isActive()148 	public boolean isActive() {
149 		return this.active;
150 	}
151 
152 	/**
153 	 * Mark this request as completed.
154 	 */
close()155 	public void close() {
156 		this.active = false;
157 	}
158 
159 	/**
160 	 * Check whether this request is still active (that is, not completed yet),
161 	 * throwing an IllegalStateException if not active anymore.
162 	 */
checkActive()163 	protected void checkActive() throws IllegalStateException {
164 		if (!this.active) {
165 			throw new IllegalStateException("Request is not active anymore");
166 		}
167 	}
168 
169 
170 	//---------------------------------------------------------------------
171 	// PortletRequest methods
172 	//---------------------------------------------------------------------
173 
isWindowStateAllowed(WindowState windowState)174 	public boolean isWindowStateAllowed(WindowState windowState) {
175 		return CollectionUtils.contains(this.portalContext.getSupportedWindowStates(), windowState);
176 	}
177 
isPortletModeAllowed(PortletMode portletMode)178 	public boolean isPortletModeAllowed(PortletMode portletMode) {
179 		return CollectionUtils.contains(this.portalContext.getSupportedPortletModes(), portletMode);
180 	}
181 
setPortletMode(PortletMode portletMode)182 	public void setPortletMode(PortletMode portletMode) {
183 		Assert.notNull(portletMode, "PortletMode must not be null");
184 		this.portletMode = portletMode;
185 	}
186 
getPortletMode()187 	public PortletMode getPortletMode() {
188 		return this.portletMode;
189 	}
190 
setWindowState(WindowState windowState)191 	public void setWindowState(WindowState windowState) {
192 		Assert.notNull(windowState, "WindowState must not be null");
193 		this.windowState = windowState;
194 	}
195 
getWindowState()196 	public WindowState getWindowState() {
197 		return this.windowState;
198 	}
199 
setPreferences(PortletPreferences preferences)200 	public void setPreferences(PortletPreferences preferences) {
201 		Assert.notNull(preferences, "PortletPreferences must not be null");
202 		this.portletPreferences = preferences;
203 	}
204 
getPreferences()205 	public PortletPreferences getPreferences() {
206 		return this.portletPreferences;
207 	}
208 
setSession(PortletSession session)209 	public void setSession(PortletSession session) {
210 		this.session = session;
211 		if (session instanceof MockPortletSession) {
212 			MockPortletSession mockSession = ((MockPortletSession) session);
213 			mockSession.access();
214 		}
215 	}
216 
getPortletSession()217 	public PortletSession getPortletSession() {
218 		return getPortletSession(true);
219 	}
220 
getPortletSession(boolean create)221 	public PortletSession getPortletSession(boolean create) {
222 		checkActive();
223 		// Reset session if invalidated.
224 		if (this.session instanceof MockPortletSession && ((MockPortletSession) this.session).isInvalid()) {
225 			this.session = null;
226 		}
227 		// Create new session if necessary.
228 		if (this.session == null && create) {
229 			this.session = new MockPortletSession(this.portletContext);
230 		}
231 		return this.session;
232 	}
233 
234 	/**
235 	 * Set a single value for the specified property.
236 	 * <p>If there are already one or more values registered for the given
237 	 * property key, they will be replaced.
238 	 */
setProperty(String key, String value)239 	public void setProperty(String key, String value) {
240 		Assert.notNull(key, "Property key must not be null");
241 		List<String> list = new LinkedList<String>();
242 		list.add(value);
243 		this.properties.put(key, list);
244 	}
245 
246 	/**
247 	 * Add a single value for the specified property.
248 	 * <p>If there are already one or more values registered for the given
249 	 * property key, the given value will be added to the end of the list.
250 	 */
addProperty(String key, String value)251 	public void addProperty(String key, String value) {
252 		Assert.notNull(key, "Property key must not be null");
253 		List<String> oldList = this.properties.get(key);
254 		if (oldList != null) {
255 			oldList.add(value);
256 		}
257 		else {
258 			List<String> list = new LinkedList<String>();
259 			list.add(value);
260 			this.properties.put(key, list);
261 		}
262 	}
263 
getProperty(String key)264 	public String getProperty(String key) {
265 		Assert.notNull(key, "Property key must not be null");
266 		List list = this.properties.get(key);
267 		return (list != null && list.size() > 0 ? (String) list.get(0) : null);
268 	}
269 
getProperties(String key)270 	public Enumeration<String> getProperties(String key) {
271 		Assert.notNull(key, "property key must not be null");
272 		return Collections.enumeration(this.properties.get(key));
273 	}
274 
getPropertyNames()275 	public Enumeration<String> getPropertyNames() {
276 		return Collections.enumeration(this.properties.keySet());
277 	}
278 
getPortalContext()279 	public PortalContext getPortalContext() {
280 		return this.portalContext;
281 	}
282 
setAuthType(String authType)283 	public void setAuthType(String authType) {
284 		this.authType = authType;
285 	}
286 
getAuthType()287 	public String getAuthType() {
288 		return this.authType;
289 	}
290 
setContextPath(String contextPath)291 	public void setContextPath(String contextPath) {
292 		this.contextPath = contextPath;
293 	}
294 
getContextPath()295 	public String getContextPath() {
296 		return this.contextPath;
297 	}
298 
setRemoteUser(String remoteUser)299 	public void setRemoteUser(String remoteUser) {
300 		this.remoteUser = remoteUser;
301 	}
302 
getRemoteUser()303 	public String getRemoteUser() {
304 		return this.remoteUser;
305 	}
306 
setUserPrincipal(Principal userPrincipal)307 	public void setUserPrincipal(Principal userPrincipal) {
308 		this.userPrincipal = userPrincipal;
309 	}
310 
getUserPrincipal()311 	public Principal getUserPrincipal() {
312 		return this.userPrincipal;
313 	}
314 
addUserRole(String role)315 	public void addUserRole(String role) {
316 		this.userRoles.add(role);
317 	}
318 
isUserInRole(String role)319 	public boolean isUserInRole(String role) {
320 		return this.userRoles.contains(role);
321 	}
322 
getAttribute(String name)323 	public Object getAttribute(String name) {
324 		checkActive();
325 		return this.attributes.get(name);
326 	}
327 
getAttributeNames()328 	public Enumeration<String> getAttributeNames() {
329 		checkActive();
330 		return Collections.enumeration(this.attributes.keySet());
331 	}
332 
setParameters(Map<String, String[]> parameters)333 	public void setParameters(Map<String, String[]> parameters) {
334 		Assert.notNull(parameters, "Parameters Map must not be null");
335 		this.parameters.clear();
336 		this.parameters.putAll(parameters);
337 	}
338 
setParameter(String key, String value)339 	public void setParameter(String key, String value) {
340 		Assert.notNull(key, "Parameter key must be null");
341 		Assert.notNull(value, "Parameter value must not be null");
342 		this.parameters.put(key, new String[] {value});
343 	}
344 
setParameter(String key, String[] values)345 	public void setParameter(String key, String[] values) {
346 		Assert.notNull(key, "Parameter key must be null");
347 		Assert.notNull(values, "Parameter values must not be null");
348 		this.parameters.put(key, values);
349 	}
350 
addParameter(String name, String value)351 	public void addParameter(String name, String value) {
352 		addParameter(name, new String[] {value});
353 	}
354 
addParameter(String name, String[] values)355 	public void addParameter(String name, String[] values) {
356 		String[] oldArr = this.parameters.get(name);
357 		if (oldArr != null) {
358 			String[] newArr = new String[oldArr.length + values.length];
359 			System.arraycopy(oldArr, 0, newArr, 0, oldArr.length);
360 			System.arraycopy(values, 0, newArr, oldArr.length, values.length);
361 			this.parameters.put(name, newArr);
362 		}
363 		else {
364 			this.parameters.put(name, values);
365 		}
366 	}
367 
getParameter(String name)368 	public String getParameter(String name) {
369 		String[] arr = this.parameters.get(name);
370 		return (arr != null && arr.length > 0 ? arr[0] : null);
371 	}
372 
getParameterNames()373 	public Enumeration<String> getParameterNames() {
374 		return Collections.enumeration(this.parameters.keySet());
375 	}
376 
getParameterValues(String name)377 	public String[] getParameterValues(String name) {
378 		return this.parameters.get(name);
379 	}
380 
getParameterMap()381 	public Map<String, String[]> getParameterMap() {
382 		return Collections.unmodifiableMap(this.parameters);
383 	}
384 
setSecure(boolean secure)385 	public void setSecure(boolean secure) {
386 		this.secure = secure;
387 	}
388 
isSecure()389 	public boolean isSecure() {
390 		return this.secure;
391 	}
392 
setAttribute(String name, Object value)393 	public void setAttribute(String name, Object value) {
394 		checkActive();
395 		if (value != null) {
396 			this.attributes.put(name, value);
397 		}
398 		else {
399 			this.attributes.remove(name);
400 		}
401 	}
402 
removeAttribute(String name)403 	public void removeAttribute(String name) {
404 		checkActive();
405 		this.attributes.remove(name);
406 	}
407 
getRequestedSessionId()408 	public String getRequestedSessionId() {
409 		PortletSession session = this.getPortletSession();
410 		return (session != null ? session.getId() : null);
411 	}
412 
setRequestedSessionIdValid(boolean requestedSessionIdValid)413 	public void setRequestedSessionIdValid(boolean requestedSessionIdValid) {
414 		this.requestedSessionIdValid = requestedSessionIdValid;
415 	}
416 
isRequestedSessionIdValid()417 	public boolean isRequestedSessionIdValid() {
418 		return this.requestedSessionIdValid;
419 	}
420 
addResponseContentType(String responseContentType)421 	public void addResponseContentType(String responseContentType) {
422 		this.responseContentTypes.add(responseContentType);
423 	}
424 
addPreferredResponseContentType(String responseContentType)425 	public void addPreferredResponseContentType(String responseContentType) {
426 		this.responseContentTypes.add(0, responseContentType);
427 	}
428 
getResponseContentType()429 	public String getResponseContentType() {
430 		return this.responseContentTypes.get(0);
431 	}
432 
getResponseContentTypes()433 	public Enumeration<String> getResponseContentTypes() {
434 		return Collections.enumeration(this.responseContentTypes);
435 	}
436 
addLocale(Locale locale)437 	public void addLocale(Locale locale) {
438 		this.locales.add(locale);
439 	}
440 
addPreferredLocale(Locale locale)441 	public void addPreferredLocale(Locale locale) {
442 		this.locales.add(0, locale);
443 	}
444 
getLocale()445 	public Locale getLocale() {
446 		return this.locales.get(0);
447 	}
448 
getLocales()449 	public Enumeration<Locale> getLocales() {
450 		return Collections.enumeration(this.locales);
451 	}
452 
setScheme(String scheme)453 	public void setScheme(String scheme) {
454 		this.scheme = scheme;
455 	}
456 
getScheme()457 	public String getScheme() {
458 		return this.scheme;
459 	}
460 
setServerName(String serverName)461 	public void setServerName(String serverName) {
462 		this.serverName = serverName;
463 	}
464 
getServerName()465 	public String getServerName() {
466 		return this.serverName;
467 	}
468 
setServerPort(int serverPort)469 	public void setServerPort(int serverPort) {
470 		this.serverPort = serverPort;
471 	}
472 
getServerPort()473 	public int getServerPort() {
474 		return this.serverPort;
475 	}
476 
setWindowID(String windowID)477 	public void setWindowID(String windowID) {
478 		this.windowID = windowID;
479 	}
480 
getWindowID()481 	public String getWindowID() {
482 		return this.windowID;
483 	}
484 
setCookies(Cookie... cookies)485 	public void setCookies(Cookie... cookies) {
486 		this.cookies = cookies;
487 	}
488 
getCookies()489 	public Cookie[] getCookies() {
490 		return this.cookies;
491 	}
492 
getPrivateParameterMap()493 	public Map<String, String[]> getPrivateParameterMap() {
494 		if (!this.publicParameterNames.isEmpty()) {
495 			Map<String, String[]> filtered = new LinkedHashMap<String, String[]>();
496 			for (String key : this.parameters.keySet()) {
497 				if (!this.publicParameterNames.contains(key)) {
498 					filtered.put(key, this.parameters.get(key));
499 				}
500 			}
501 			return filtered;
502 		}
503 		else {
504 			return Collections.unmodifiableMap(this.parameters);
505 		}
506 	}
507 
getPublicParameterMap()508 	public Map<String, String[]> getPublicParameterMap() {
509 		if (!this.publicParameterNames.isEmpty()) {
510 			Map<String, String[]> filtered = new LinkedHashMap<String, String[]>();
511 			for (String key : this.parameters.keySet()) {
512 				if (this.publicParameterNames.contains(key)) {
513 					filtered.put(key, this.parameters.get(key));
514 				}
515 			}
516 			return filtered;
517 		}
518 		else {
519 			return Collections.emptyMap();
520 		}
521 	}
522 
registerPublicParameter(String name)523 	public void registerPublicParameter(String name) {
524 		this.publicParameterNames.add(name);
525 	}
526 
527 }
528