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