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  package org.apache.commons.chain.web.portlet;
18  
19  
20  import org.apache.commons.chain.web.MockEnumeration;
21  import org.apache.commons.chain.web.MockPrincipal;
22  
23  import javax.portlet.PortalContext;
24  import javax.portlet.PortletRequest;
25  import javax.portlet.PortletSession;
26  import javax.portlet.PortletContext;
27  import javax.portlet.PortletMode;
28  import javax.portlet.PortletPreferences;
29  import javax.portlet.WindowState;
30  import java.security.Principal;
31  import java.util.Map;
32  import java.util.HashMap;
33  import java.util.Enumeration;
34  import java.util.Locale;
35  
36  
37  
38  // Mock Object for PortletRequest
39  public class MockPortletRequest implements PortletRequest {
40  
41      private String contextPath;
42      private String authType;
43      private Locale locale;
44      private String scheme     = "http";
45      private String serverName = "localhost";
46      private int    serverPort = 8080;
47      private PortalContext portalContext;
48      private PortletContext context;
49      private PortletSession session;
50      private PortletMode portletMode;
51      private PortletPreferences portletPreferences;
52      private WindowState windowState;
53      private Principal principal;
54      private Map parameters = new HashMap();
55      private Map attributes = new HashMap();
56      private Map properties = new HashMap();
57  
58  
59      public MockPortletRequest() {
60          this(null, null, null);
61      }
62  
63      public MockPortletRequest(String contextPath, PortletContext context, PortletSession session) {
64          this.contextPath = contextPath;
65          this.context = (context == null ? new MockPortletContext() : context);
66          this.session = session;
67      }
68  
69      // --------------------------------------------------------- Public Methods
70  
71      public void addParameter(String name, String value) {
72          String values[] = (String[])parameters.get(name);
73          if (values == null) {
74              String results[] = new String[] { value };
75              parameters.put(name, results);
76              return;
77          }
78          String results[] = new String[values.length + 1];
79          System.arraycopy(values, 0, results, 0, values.length);
80          results[values.length] = value;
81          parameters.put(name, results);
82      }
83  
84      public void addProperty(String name, String value) {
85          String values[] = (String[])properties.get(name);
86          if (values == null) {
87              String results[] = new String[] { value };
88              properties.put(name, results);
89              return;
90          }
91          String results[] = new String[values.length + 1];
92          System.arraycopy(values, 0, results, 0, values.length);
93          results[values.length] = value;
94          properties.put(name, results);
95      }
96  
97      public void setAuthType(String authType) {
98          this.authType = authType;
99      }
100 
101     public void setContextPath(String contextPath) {
102         this.contextPath = contextPath;
103     }
104 
105     public void setLocale(Locale locale) {
106         this.locale = locale;
107     }
108 
109     public void setPortalContext(PortalContext portalContext) {
110         this.portalContext = portalContext;
111     }
112 
113     public void setPortletContext(PortletContext context) {
114         this.context = context;
115     }
116 
117     public void setPortletMode(PortletMode portletMode) {
118         this.portletMode = portletMode;
119     }
120 
121     public void setPortletPreferences(PortletPreferences portletPreferences) {
122         this.portletPreferences = portletPreferences;
123     }
124 
125     public void setPortletSession(PortletSession session) {
126         this.session = session;
127     }
128 
129     public void setScheme(String scheme) {
130         this.scheme = scheme;
131     }
132 
133     public void setServerName(String serverName) {
134         this.serverName = serverName;
135     }
136 
137     public void setServerPort(int serverPort) {
138         this.serverPort = serverPort;
139     }
140 
141     public void setUserPrincipal(Principal principal) {
142         this.principal = principal;
143     }
144 
145     public void setUserPrincipal(WindowState windowState) {
146         this.windowState = windowState;
147     }
148 
149 
150     // --------------------------------------------- PortletRequest Methods
151 
152     public Object getAttribute(String name) {
153         return attributes.get(name);
154     }
155 
156     public Enumeration getAttributeNames() {
157         return new MockEnumeration(attributes.keySet().iterator());
158     }
159 
160     public String getAuthType() {
161         return authType;
162     }
163 
164     public String getContextPath() {
165         return contextPath;
166     }
167 
168     public Locale getLocale() {
169         return locale;
170     }
171 
172     public Enumeration getLocales() {
173         throw new UnsupportedOperationException();
174     }
175 
176     public String getParameter(String name) {
177         String values[] = (String[])parameters.get(name);
178         if (values != null) {
179             return values[0];
180         } else {
181             return null;
182         }
183     }
184 
185     public Map getParameterMap() {
186         return parameters;
187     }
188 
189     public Enumeration getParameterNames() {
190         return new MockEnumeration(parameters.keySet().iterator());
191     }
192 
193     public String[] getParameterValues(String name) {
194         return (String[])parameters.get(name);
195     }
196 
197     public PortalContext getPortalContext() {
198         return portalContext; 
199     }
200 
201     public PortletMode getPortletMode() {
202         return portletMode; 
203     }
204 
205     public PortletSession getPortletSession() {
206         return getPortletSession(true);
207     }
208 
209     public PortletSession getPortletSession(boolean create) {
210         if (create && session == null) {
211             session = new MockPortletSession(context);
212         }
213         return session;
214     }
215 
216     public PortletPreferences getPreferences() {
217         return portletPreferences; 
218     }
219 
220     public Enumeration getProperties(String name) {
221         throw new UnsupportedOperationException(); 
222     }
223 
224     public String getProperty(String name) {
225         String values[] = (String[])properties.get(name);
226         if (values != null) {
227             return values[0];
228         } else {
229             return null;
230         }
231      }
232 
233     public Enumeration getPropertyNames() {
234         return new MockEnumeration(properties.keySet().iterator());
235     }
236 
237 
238     public String getRemoteUser() {
239         if (principal != null) {
240             return principal.getName();
241         } else {
242             return null;
243         }
244     }
245 
246     public String getRequestedSessionId() {
247         throw new UnsupportedOperationException();
248     }
249 
250     public String getResponseContentType() {
251         throw new UnsupportedOperationException();
252     }
253 
254     public Enumeration getResponseContentTypes() {
255         throw new UnsupportedOperationException(); 
256     }
257 
258     public String getScheme() {
259         return scheme;
260     }
261 
262     public String getServerName() {
263         return serverName;
264     }
265 
266     public int getServerPort() {
267         return serverPort;
268     }
269 
270     public Principal getUserPrincipal() {
271         return principal;
272     }
273 
274     public WindowState getWindowState() {
275         return windowState;
276     }
277 
278     public boolean isPortletModeAllowed(PortletMode mode) {
279         throw new UnsupportedOperationException();
280     }
281 
282     public boolean isRequestedSessionIdValid() {
283         throw new UnsupportedOperationException();
284     }
285 
286     public boolean isSecure() {
287         return false;
288     }
289 
290     public boolean isUserInRole(String role) {
291         if ((principal != null) && (principal instanceof MockPrincipal)) {
292             return ((MockPrincipal)principal).isUserInRole(role);
293         } else {
294             return false;
295         }
296     }
297 
298     public boolean isWindowStateAllowed(WindowState state) {
299         throw new UnsupportedOperationException();
300     }
301 
302     public void removeAttribute(String name) {
303         attributes.remove(name);
304     }
305 
306 
307     public void setAttribute(String name, Object value) {
308         if (value == null) {
309             attributes.remove(name);
310         } else {
311             attributes.put(name, value);
312         }
313     }
314 
315 }