KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > mock > web > portlet > MockPortletRequest


1 /*
2  * Copyright 2002-2006 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 JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.Vector JavaDoc;
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 /**
45  * Mock implementation of the {@link javax.portlet.PortletRequest} interface.
46  *
47  * @author John A. Lewis
48  * @author Juergen Hoeller
49  * @since 2.0
50  */

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 JavaDoc properties = CollectionFactory.createLinkedMapIfPossible(16);
68
69     private final Hashtable JavaDoc attributes = new Hashtable JavaDoc();
70
71     private final Map JavaDoc parameters = CollectionFactory.createLinkedMapIfPossible(16);
72
73     private String JavaDoc authType = null;
74
75     private String JavaDoc contextPath = "";
76
77     private String JavaDoc remoteUser = null;
78
79     private Principal JavaDoc userPrincipal = null;
80
81     private final Set JavaDoc userRoles = new HashSet JavaDoc();
82
83     private boolean secure = false;
84
85     private boolean requestedSessionIdValid = true;
86
87     private final Vector JavaDoc responseContentTypes = new Vector JavaDoc();
88
89     private final Vector JavaDoc locales = new Vector JavaDoc();
90
91     private String JavaDoc scheme = "http";
92
93     private String JavaDoc serverName = "localhost";
94
95     private int serverPort = 80;
96
97
98     /**
99      * Create a new MockPortletRequest with a default {@link MockPortalContext}
100      * and a default {@link MockPortletContext}.
101      * @see MockPortalContext
102      * @see MockPortletContext
103      */

104     public MockPortletRequest() {
105         this(null, null);
106     }
107
108     /**
109      * Create a new MockPortletRequest with a default {@link MockPortalContext}.
110      * @param portletContext the PortletContext that the request runs in
111      * @see MockPortalContext
112      */

113     public MockPortletRequest(PortletContext portletContext) {
114         this(null, portletContext);
115     }
116
117     /**
118      * Create a new MockPortletRequest.
119      * @param portalContext the PortalContext that the request runs in
120      * @param portletContext the PortletContext that the request runs in
121      */

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     //---------------------------------------------------------------------
131
// Lifecycle methods
132
//---------------------------------------------------------------------
133

134     /**
135      * Return whether this request is still active (that is, not completed yet).
136      */

137     public boolean isActive() {
138         return this.active;
139     }
140
141     /**
142      * Mark this request as completed.
143      */

144     public void close() {
145         this.active = false;
146     }
147
148     /**
149      * Check whether this request is still active (that is, not completed yet),
150      * throwing an IllegalStateException if not active anymore.
151      */

152     protected void checkActive() throws IllegalStateException JavaDoc {
153         if (!this.active) {
154             throw new IllegalStateException JavaDoc("Request is not active anymore");
155         }
156     }
157
158
159     //---------------------------------------------------------------------
160
// PortletRequest methods
161
//---------------------------------------------------------------------
162

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         // Reset session if invalidated.
210
if (this.session instanceof MockPortletSession && ((MockPortletSession) this.session).isInvalid()) {
211             this.session = null;
212         }
213         // Create new session if necessary.
214
if (this.session == null && create) {
215             this.session = new MockPortletSession(this.portletContext);
216         }
217         return this.session;
218     }
219
220     /**
221      * Set a single value for the specified property.
222      * <p>If there are already one or more values registered for the given
223      * property key, they will be replaced.
224      */

225     public void setProperty(String JavaDoc key, String JavaDoc value) {
226         Assert.notNull(key, "Property key must not be null");
227         List JavaDoc list = new LinkedList JavaDoc();
228         list.add(value);
229         this.properties.put(key, list);
230     }
231
232     /**
233      * Add a single value for the specified property.
234      * <p>If there are already one or more values registered for the given
235      * property key, the given value will be added to the end of the list.
236      */

237     public void addProperty(String JavaDoc key, String JavaDoc value) {
238         Assert.notNull(key, "Property key must not be null");
239         List JavaDoc oldList = (List JavaDoc) this.properties.get(key);
240         if (oldList != null) {
241             oldList.add(value);
242         }
243         else {
244             List JavaDoc list = new LinkedList JavaDoc();
245             list.add(value);
246             this.properties.put(key, list);
247         }
248     }
249
250     public String JavaDoc getProperty(String JavaDoc key) {
251         Assert.notNull(key, "Property key must not be null");
252         List JavaDoc list = (List JavaDoc) this.properties.get(key);
253         return (list != null && list.size() > 0 ? (String JavaDoc) list.get(0) : null);
254     }
255
256     public Enumeration JavaDoc getProperties(String JavaDoc key) {
257         Assert.notNull(key, "property key must not be null");
258         return Collections.enumeration((List JavaDoc) this.properties.get(key));
259     }
260
261     public Enumeration JavaDoc 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 JavaDoc authType) {
270         this.authType = authType;
271     }
272
273     public String JavaDoc getAuthType() {
274         return this.authType;
275     }
276
277     public void setContextPath(String JavaDoc contextPath) {
278         this.contextPath = contextPath;
279     }
280
281     public String JavaDoc getContextPath() {
282         return this.contextPath;
283     }
284
285     public void setRemoteUser(String JavaDoc remoteUser) {
286         this.remoteUser = remoteUser;
287     }
288
289     public String JavaDoc getRemoteUser() {
290         return this.remoteUser;
291     }
292
293     public void setUserPrincipal(Principal JavaDoc userPrincipal) {
294         this.userPrincipal = userPrincipal;
295     }
296
297     public Principal JavaDoc getUserPrincipal() {
298         return this.userPrincipal;
299     }
300
301     public void addUserRole(String JavaDoc role) {
302         this.userRoles.add(role);
303     }
304
305     public boolean isUserInRole(String JavaDoc role) {
306         return this.userRoles.contains(role);
307     }
308
309     public Object JavaDoc getAttribute(String JavaDoc name) {
310         checkActive();
311         return this.attributes.get(name);
312     }
313
314     public Enumeration JavaDoc getAttributeNames() {
315         checkActive();
316         return this.attributes.keys();
317     }
318
319     public void setParameters(Map JavaDoc parameters) {
320         Assert.notNull(parameters, "Parameters Map must not be null");
321         this.parameters.clear();
322         for (Iterator JavaDoc it = parameters.entrySet().iterator(); it.hasNext();) {
323             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
324             Assert.isTrue(entry.getKey() instanceof String JavaDoc, "Key must be of type String");
325             Assert.isTrue(entry.getValue() instanceof String JavaDoc[], "Value must be of type String[]");
326             this.parameters.put(entry.getKey(), entry.getValue());
327         }
328     }
329
330     public void setParameter(String JavaDoc key, String JavaDoc 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 JavaDoc[] {value});
334     }
335
336     public void setParameter(String JavaDoc key, String JavaDoc[] 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 JavaDoc name, String JavaDoc value) {
343         addParameter(name, new String JavaDoc[] {value});
344     }
345
346     public void addParameter(String JavaDoc name, String JavaDoc[] values) {
347         String JavaDoc[] oldArr = (String JavaDoc[]) this.parameters.get(name);
348         if (oldArr != null) {
349             String JavaDoc[] newArr = new String JavaDoc[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 JavaDoc getParameter(String JavaDoc name) {
360         String JavaDoc[] arr = (String JavaDoc[]) this.parameters.get(name);
361         return (arr != null && arr.length > 0 ? arr[0] : null);
362     }
363
364     public Enumeration JavaDoc getParameterNames() {
365         return Collections.enumeration(this.parameters.keySet());
366     }
367
368     public String JavaDoc[] getParameterValues(String JavaDoc name) {
369         return (String JavaDoc[]) this.parameters.get(name);
370     }
371
372     public Map JavaDoc 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 JavaDoc name, Object JavaDoc 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 JavaDoc name) {
395         checkActive();
396         this.attributes.remove(name);
397     }
398
399     public String JavaDoc 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 JavaDoc responseContentType) {
413         this.responseContentTypes.add(responseContentType);
414     }
415
416     public void addPreferredResponseContentType(String JavaDoc responseContentType) {
417         this.responseContentTypes.add(0, responseContentType);
418     }
419
420     public String JavaDoc getResponseContentType() {
421         return (String JavaDoc) this.responseContentTypes.get(0);
422     }
423
424     public Enumeration JavaDoc getResponseContentTypes() {
425         return this.responseContentTypes.elements();
426     }
427
428     public void addLocale(Locale JavaDoc locale) {
429         this.locales.add(locale);
430     }
431
432     public void addPreferredLocale(Locale JavaDoc locale) {
433         this.locales.add(0, locale);
434     }
435
436     public Locale JavaDoc getLocale() {
437         return (Locale JavaDoc) this.locales.get(0);
438     }
439
440     public Enumeration JavaDoc getLocales() {
441         return this.locales.elements();
442     }
443
444     public void setScheme(String JavaDoc scheme) {
445         this.scheme = scheme;
446     }
447
448     public String JavaDoc getScheme() {
449         return scheme;
450     }
451
452     public void setServerName(String JavaDoc serverName) {
453         this.serverName = serverName;
454     }
455
456     public String JavaDoc 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