2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.test.mock;
19  
20  import java.security.Principal;
21  import java.util.Enumeration;
22  import java.util.HashMap;
23  import java.util.Locale;
24  import java.util.Map;
25  
26  import javax.portlet.PortalContext;
27  import javax.portlet.PortletMode;
28  import javax.portlet.PortletPreferences;
29  import javax.portlet.PortletRequest;
30  import javax.portlet.PortletSession;
31  import javax.portlet.WindowState;
32  
33  /***
34   * <p> Mock implementation of <code>PortletRequest</code>. </p>
35   *
36   * $Id: MockPortletRequest.java 516091 2007-03-08 16:25:17Z greddin $
37   */
38  
39  public class MockPortletRequest implements PortletRequest {
40  
41      // ------------------------------------------------------------ Constructors
42  
43      public MockPortletRequest() {
44  
45          super();
46  
47      }
48  
49  
50      public MockPortletRequest(PortletSession session) {
51  
52          super();
53          this.session = session;
54  
55      }
56  
57  
58      // ----------------------------------------------------- Mock Object Methods
59  
60      /***
61       * <p> Add a request parameter for this request. </p>
62       *
63       * @param name Parameter name
64       * @param value Parameter value
65       */
66      public void addParameter(String name, String value) {
67  
68          String[] values = (String[]) parameters.get(name);
69          if (values == null) {
70              String[] results = new String[] { value };
71              parameters.put(name, results);
72              return;
73          }
74          String[] results = new String[values.length + 1];
75          System.arraycopy(values, 0, results, 0, values.length);
76          results[values.length] = value;
77          parameters.put(name, results);
78  
79      }
80  
81  
82      /***
83       * <p> Set the <code>PortletSession</code> associated with this request.
84       * </p>
85       *
86       * @param session The new session
87       */
88      public void setPortletSession(PortletSession session) {
89  
90          this.session = session;
91      }
92  
93  
94      /***
95       * <p> Set the <code>Locale</code> associated with this request. </p>
96       *
97       * @param locale The new locale
98       */
99      public void setLocale(Locale locale) {
100 
101         this.locale = locale;
102 
103     }
104 
105 
106     /***
107      * <p> Set the <code>Principal</code> associated with this request. </p>
108      *
109      * @param principal The new Principal
110      */
111     public void setUserPrincipal(Principal principal) {
112 
113         this.principal = principal;
114 
115     }
116 
117     // ------------------------------------------------------ Instance Variables
118 
119     private Map attributes = new HashMap();
120     private String contextPath = null;
121     private Locale locale = null;
122     private Map parameters = new HashMap();
123     private Principal principal = null;
124     private PortletSession session = null;
125 
126 
127     // -------------------------------------------------- PortletRequest Methods
128 
129 
130     /*** {@inheritDoc} */
131     public String getAuthType() {
132 
133         throw new UnsupportedOperationException();
134 
135     }
136 
137 
138     /*** {@inheritDoc} */
139     public String getContextPath() {
140 
141         return contextPath;
142 
143     }
144 
145 
146     /*** {@inheritDoc} */
147     public Object getAttribute(String name) {
148 
149         return attributes.get(name);
150 
151     }
152 
153 
154     /*** {@inheritDoc} */
155     public Enumeration getAttributeNames() {
156 
157         return new MockEnumeration(attributes.keySet().iterator());
158 
159     }
160 
161 
162     /*** {@inheritDoc} */
163     public Locale getLocale() {
164 
165         return locale;
166     }
167 
168 
169     /*** {@inheritDoc} */
170     public Enumeration getLocales() {
171 
172         throw new UnsupportedOperationException();
173 
174     }
175 
176 
177     /*** {@inheritDoc} */
178     public String getParameter(String name) {
179 
180         String[] values = (String[]) parameters.get(name);
181         if (values != null) {
182             return values[0];
183         } else {
184             return null;
185         }
186 
187     }
188 
189 
190     /*** {@inheritDoc} */
191     public Map getParameterMap() {
192 
193         return parameters;
194 
195     }
196 
197 
198     /*** {@inheritDoc} */
199     public Enumeration getParameterNames() {
200 
201         return new MockEnumeration(parameters.keySet().iterator());
202 
203     }
204 
205 
206     /*** {@inheritDoc} */
207     public String[] getParameterValues(String name) {
208 
209         return (String[]) parameters.get(name);
210 
211     }
212 
213 
214     /*** {@inheritDoc} */
215     public PortalContext getPortalContext() {
216 
217         throw new UnsupportedOperationException();
218 
219     }
220 
221 
222     /*** {@inheritDoc} */
223     public PortletMode getPortletMode() {
224 
225         throw new UnsupportedOperationException();
226 
227     }
228 
229 
230     /*** {@inheritDoc} */
231     public PortletSession getPortletSession() {
232 
233         return getPortletSession(true);
234 
235     }
236 
237 
238     /*** {@inheritDoc} */
239     public PortletSession getPortletSession(boolean create) {
240 
241         if (create && (session == null)) {
242             throw new UnsupportedOperationException();
243         }
244         return session;
245 
246     }
247 
248 
249     /*** {@inheritDoc} */
250     public PortletPreferences getPreferences() {
251 
252         throw new UnsupportedOperationException();
253 
254     }
255 
256 
257     /*** {@inheritDoc} */
258     public Enumeration getProperties(String arg0) {
259 
260         throw new UnsupportedOperationException();
261 
262     }
263 
264 
265     /*** {@inheritDoc} */
266     public String getProperty(String arg0) {
267 
268         throw new UnsupportedOperationException();
269 
270     }
271 
272 
273     /*** {@inheritDoc} */
274     public Enumeration getPropertyNames() {
275 
276         throw new UnsupportedOperationException();
277 
278     }
279 
280 
281     /*** {@inheritDoc} */
282     public String getRemoteUser() {
283 
284         if (principal != null) {
285             return principal.getName();
286         } else {
287             return null;
288         }
289 
290     }
291 
292 
293     /*** {@inheritDoc} */
294     public String getRequestedSessionId() {
295 
296         throw new UnsupportedOperationException();
297 
298     }
299 
300 
301     /*** {@inheritDoc} */
302     public String getResponseContentType() {
303 
304         throw new UnsupportedOperationException();
305 
306     }
307 
308 
309     /*** {@inheritDoc} */
310     public Enumeration getResponseContentTypes() {
311 
312         throw new UnsupportedOperationException();
313 
314     }
315 
316 
317     /*** {@inheritDoc} */
318     public String getScheme() {
319 
320         return ("http");
321 
322     }
323 
324 
325     /*** {@inheritDoc} */
326     public String getServerName() {
327 
328         return ("localhost");
329 
330     }
331 
332 
333     /*** {@inheritDoc} */
334     public int getServerPort() {
335 
336         return (8080);
337 
338     }
339 
340 
341     /*** {@inheritDoc} */
342     public Principal getUserPrincipal() {
343 
344         return principal;
345 
346     }
347 
348 
349     /*** {@inheritDoc} */
350     public WindowState getWindowState() {
351 
352         throw new UnsupportedOperationException();
353 
354     }
355 
356 
357     /*** {@inheritDoc} */
358     public boolean isPortletModeAllowed(PortletMode arg0) {
359 
360         throw new UnsupportedOperationException();
361 
362     }
363 
364 
365     /*** {@inheritDoc} */
366     public boolean isRequestedSessionIdValid() {
367 
368         throw new UnsupportedOperationException();
369 
370     }
371 
372 
373     /*** {@inheritDoc} */
374     public boolean isSecure() {
375 
376         return false;
377 
378     }
379 
380 
381     /*** {@inheritDoc} */
382     public boolean isUserInRole(String arg0) {
383 
384         throw new UnsupportedOperationException();
385 
386     }
387 
388 
389     /*** {@inheritDoc} */
390     public boolean isWindowStateAllowed(WindowState arg0) {
391 
392         throw new UnsupportedOperationException();
393 
394     }
395 
396 
397     /*** {@inheritDoc} */
398     public void removeAttribute(String name) {
399 
400         if (attributes.containsKey(name)) {
401             attributes.remove(name);
402         }
403 
404     }
405 
406 
407     /*** {@inheritDoc} */
408     public void setAttribute(String name, Object value) {
409 
410         if (name == null) {
411             throw new IllegalArgumentException("Attribute name cannot be null");
412         }
413         if (value == null) {
414             removeAttribute(name);
415             return;
416         }
417         attributes.put(name, value);
418 
419     }
420 
421 }