001: /*
002: * Copyright 2002-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.mock.web.portlet;
018:
019: import java.security.Principal;
020: import java.util.Collections;
021: import java.util.Enumeration;
022: import java.util.HashSet;
023: import java.util.Hashtable;
024: import java.util.Iterator;
025: import java.util.LinkedList;
026: import java.util.List;
027: import java.util.Locale;
028: import java.util.Map;
029: import java.util.Set;
030: import java.util.Vector;
031:
032: import javax.portlet.PortalContext;
033: import javax.portlet.PortletContext;
034: import javax.portlet.PortletMode;
035: import javax.portlet.PortletPreferences;
036: import javax.portlet.PortletRequest;
037: import javax.portlet.PortletSession;
038: import javax.portlet.WindowState;
039:
040: import org.springframework.core.CollectionFactory;
041: import org.springframework.util.Assert;
042: import org.springframework.util.CollectionUtils;
043:
044: /**
045: * Mock implementation of the {@link javax.portlet.PortletRequest} interface.
046: *
047: * @author John A. Lewis
048: * @author Juergen Hoeller
049: * @since 2.0
050: */
051: public class MockPortletRequest implements PortletRequest {
052:
053: private boolean active = true;
054:
055: private final PortalContext portalContext;
056:
057: private final PortletContext portletContext;
058:
059: private PortletSession session = null;
060:
061: private WindowState windowState = WindowState.NORMAL;
062:
063: private PortletMode portletMode = PortletMode.VIEW;
064:
065: private PortletPreferences portletPreferences = new MockPortletPreferences();
066:
067: private final Map properties = CollectionFactory
068: .createLinkedMapIfPossible(16);
069:
070: private final Hashtable attributes = new Hashtable();
071:
072: private final Map parameters = CollectionFactory
073: .createLinkedMapIfPossible(16);
074:
075: private String authType = null;
076:
077: private String contextPath = "";
078:
079: private String remoteUser = null;
080:
081: private Principal userPrincipal = null;
082:
083: private final Set userRoles = new HashSet();
084:
085: private boolean secure = false;
086:
087: private boolean requestedSessionIdValid = true;
088:
089: private final Vector responseContentTypes = new Vector();
090:
091: private final Vector locales = new Vector();
092:
093: private String scheme = "http";
094:
095: private String serverName = "localhost";
096:
097: private int serverPort = 80;
098:
099: /**
100: * Create a new MockPortletRequest with a default {@link MockPortalContext}
101: * and a default {@link MockPortletContext}.
102: * @see MockPortalContext
103: * @see MockPortletContext
104: */
105: public MockPortletRequest() {
106: this (null, null);
107: }
108:
109: /**
110: * Create a new MockPortletRequest with a default {@link MockPortalContext}.
111: * @param portletContext the PortletContext that the request runs in
112: * @see MockPortalContext
113: */
114: public MockPortletRequest(PortletContext portletContext) {
115: this (null, portletContext);
116: }
117:
118: /**
119: * Create a new MockPortletRequest.
120: * @param portalContext the PortalContext that the request runs in
121: * @param portletContext the PortletContext that the request runs in
122: */
123: public MockPortletRequest(PortalContext portalContext,
124: PortletContext portletContext) {
125: this .portalContext = (portalContext != null ? portalContext
126: : new MockPortalContext());
127: this .portletContext = (portletContext != null ? portletContext
128: : new MockPortletContext());
129: this .responseContentTypes.add("text/html");
130: this .locales.add(Locale.ENGLISH);
131: }
132:
133: //---------------------------------------------------------------------
134: // Lifecycle methods
135: //---------------------------------------------------------------------
136:
137: /**
138: * Return whether this request is still active (that is, not completed yet).
139: */
140: public boolean isActive() {
141: return this .active;
142: }
143:
144: /**
145: * Mark this request as completed.
146: */
147: public void close() {
148: this .active = false;
149: }
150:
151: /**
152: * Check whether this request is still active (that is, not completed yet),
153: * throwing an IllegalStateException if not active anymore.
154: */
155: protected void checkActive() throws IllegalStateException {
156: if (!this .active) {
157: throw new IllegalStateException(
158: "Request is not active anymore");
159: }
160: }
161:
162: //---------------------------------------------------------------------
163: // PortletRequest methods
164: //---------------------------------------------------------------------
165:
166: public boolean isWindowStateAllowed(WindowState windowState) {
167: return CollectionUtils.contains(this .portalContext
168: .getSupportedWindowStates(), windowState);
169: }
170:
171: public boolean isPortletModeAllowed(PortletMode portletMode) {
172: return CollectionUtils.contains(this .portalContext
173: .getSupportedPortletModes(), portletMode);
174: }
175:
176: public void setPortletMode(PortletMode portletMode) {
177: this .portletMode = portletMode;
178: }
179:
180: public PortletMode getPortletMode() {
181: return this .portletMode;
182: }
183:
184: public void setWindowState(WindowState windowState) {
185: this .windowState = windowState;
186: }
187:
188: public WindowState getWindowState() {
189: return this .windowState;
190: }
191:
192: public void setPreferences(PortletPreferences preferences) {
193: this .portletPreferences = preferences;
194: }
195:
196: public PortletPreferences getPreferences() {
197: return this .portletPreferences;
198: }
199:
200: public void setSession(PortletSession session) {
201: this .session = session;
202: if (session instanceof MockPortletSession) {
203: MockPortletSession mockSession = ((MockPortletSession) session);
204: mockSession.access();
205: }
206: }
207:
208: public PortletSession getPortletSession() {
209: return getPortletSession(true);
210: }
211:
212: public PortletSession getPortletSession(boolean create) {
213: checkActive();
214: // Reset session if invalidated.
215: if (this .session instanceof MockPortletSession
216: && ((MockPortletSession) this .session).isInvalid()) {
217: this .session = null;
218: }
219: // Create new session if necessary.
220: if (this .session == null && create) {
221: this .session = new MockPortletSession(this .portletContext);
222: }
223: return this .session;
224: }
225:
226: /**
227: * Set a single value for the specified property.
228: * <p>If there are already one or more values registered for the given
229: * property key, they will be replaced.
230: */
231: public void setProperty(String key, String value) {
232: Assert.notNull(key, "Property key must not be null");
233: List list = new LinkedList();
234: list.add(value);
235: this .properties.put(key, list);
236: }
237:
238: /**
239: * Add a single value for the specified property.
240: * <p>If there are already one or more values registered for the given
241: * property key, the given value will be added to the end of the list.
242: */
243: public void addProperty(String key, String value) {
244: Assert.notNull(key, "Property key must not be null");
245: List oldList = (List) this .properties.get(key);
246: if (oldList != null) {
247: oldList.add(value);
248: } else {
249: List list = new LinkedList();
250: list.add(value);
251: this .properties.put(key, list);
252: }
253: }
254:
255: public String getProperty(String key) {
256: Assert.notNull(key, "Property key must not be null");
257: List list = (List) this .properties.get(key);
258: return (list != null && list.size() > 0 ? (String) list.get(0)
259: : null);
260: }
261:
262: public Enumeration getProperties(String key) {
263: Assert.notNull(key, "property key must not be null");
264: return Collections.enumeration((List) this .properties.get(key));
265: }
266:
267: public Enumeration getPropertyNames() {
268: return Collections.enumeration(this .properties.keySet());
269: }
270:
271: public PortalContext getPortalContext() {
272: return this .portalContext;
273: }
274:
275: public void setAuthType(String authType) {
276: this .authType = authType;
277: }
278:
279: public String getAuthType() {
280: return this .authType;
281: }
282:
283: public void setContextPath(String contextPath) {
284: this .contextPath = contextPath;
285: }
286:
287: public String getContextPath() {
288: return this .contextPath;
289: }
290:
291: public void setRemoteUser(String remoteUser) {
292: this .remoteUser = remoteUser;
293: }
294:
295: public String getRemoteUser() {
296: return this .remoteUser;
297: }
298:
299: public void setUserPrincipal(Principal userPrincipal) {
300: this .userPrincipal = userPrincipal;
301: }
302:
303: public Principal getUserPrincipal() {
304: return this .userPrincipal;
305: }
306:
307: public void addUserRole(String role) {
308: this .userRoles.add(role);
309: }
310:
311: public boolean isUserInRole(String role) {
312: return this .userRoles.contains(role);
313: }
314:
315: public Object getAttribute(String name) {
316: checkActive();
317: return this .attributes.get(name);
318: }
319:
320: public Enumeration getAttributeNames() {
321: checkActive();
322: return this .attributes.keys();
323: }
324:
325: public void setParameters(Map parameters) {
326: Assert.notNull(parameters, "Parameters Map must not be null");
327: this .parameters.clear();
328: for (Iterator it = parameters.entrySet().iterator(); it
329: .hasNext();) {
330: Map.Entry entry = (Map.Entry) it.next();
331: Assert.isTrue(entry.getKey() instanceof String,
332: "Key must be of type String");
333: Assert.isTrue(entry.getValue() instanceof String[],
334: "Value must be of type String[]");
335: this .parameters.put(entry.getKey(), entry.getValue());
336: }
337: }
338:
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:
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:
351: public void addParameter(String name, String value) {
352: addParameter(name, new String[] { value });
353: }
354:
355: public void addParameter(String name, String[] values) {
356: String[] oldArr = (String[]) 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,
361: values.length);
362: this .parameters.put(name, newArr);
363: } else {
364: this .parameters.put(name, values);
365: }
366: }
367:
368: public String getParameter(String name) {
369: String[] arr = (String[]) this .parameters.get(name);
370: return (arr != null && arr.length > 0 ? arr[0] : null);
371: }
372:
373: public Enumeration getParameterNames() {
374: return Collections.enumeration(this .parameters.keySet());
375: }
376:
377: public String[] getParameterValues(String name) {
378: return (String[]) this .parameters.get(name);
379: }
380:
381: public Map getParameterMap() {
382: return Collections.unmodifiableMap(this .parameters);
383: }
384:
385: public void setSecure(boolean secure) {
386: this .secure = secure;
387: }
388:
389: public boolean isSecure() {
390: return this .secure;
391: }
392:
393: public void setAttribute(String name, Object value) {
394: checkActive();
395: if (value != null) {
396: this .attributes.put(name, value);
397: } else {
398: this .attributes.remove(name);
399: }
400: }
401:
402: public void removeAttribute(String name) {
403: checkActive();
404: this .attributes.remove(name);
405: }
406:
407: public String getRequestedSessionId() {
408: PortletSession session = this .getPortletSession();
409: return (session != null ? session.getId() : null);
410: }
411:
412: public void setRequestedSessionIdValid(
413: boolean requestedSessionIdValid) {
414: this .requestedSessionIdValid = requestedSessionIdValid;
415: }
416:
417: public boolean isRequestedSessionIdValid() {
418: return this .requestedSessionIdValid;
419: }
420:
421: public void addResponseContentType(String responseContentType) {
422: this .responseContentTypes.add(responseContentType);
423: }
424:
425: public void addPreferredResponseContentType(
426: String responseContentType) {
427: this .responseContentTypes.add(0, responseContentType);
428: }
429:
430: public String getResponseContentType() {
431: return (String) this .responseContentTypes.get(0);
432: }
433:
434: public Enumeration getResponseContentTypes() {
435: return this .responseContentTypes.elements();
436: }
437:
438: public void addLocale(Locale locale) {
439: this .locales.add(locale);
440: }
441:
442: public void addPreferredLocale(Locale locale) {
443: this .locales.add(0, locale);
444: }
445:
446: public Locale getLocale() {
447: return (Locale) this .locales.get(0);
448: }
449:
450: public Enumeration getLocales() {
451: return this .locales.elements();
452: }
453:
454: public void setScheme(String scheme) {
455: this .scheme = scheme;
456: }
457:
458: public String getScheme() {
459: return scheme;
460: }
461:
462: public void setServerName(String serverName) {
463: this .serverName = serverName;
464: }
465:
466: public String getServerName() {
467: return serverName;
468: }
469:
470: public void setServerPort(int serverPort) {
471: this .serverPort = serverPort;
472: }
473:
474: public int getServerPort() {
475: return serverPort;
476: }
477:
478: }
|