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.servlet;
18  
19  
20  import org.apache.commons.chain.web.MockEnumeration;
21  import org.apache.commons.chain.web.MockPrincipal;
22  
23  import javax.servlet.RequestDispatcher;
24  import javax.servlet.ServletInputStream;
25  import javax.servlet.http.Cookie;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpSession;
28  import java.io.BufferedReader;
29  import java.security.Principal;
30  import java.util.*;
31  
32  
33  // Mock Object for HttpServletRequest (Version 2.3)
34  public class MockHttpServletRequest implements HttpServletRequest {
35  
36  
37      public MockHttpServletRequest() {
38          super();
39      }
40  
41  
42      public MockHttpServletRequest(HttpSession session) {
43          super();
44          setHttpSession(session);
45      }
46  
47  
48      public MockHttpServletRequest(String contextPath, String servletPath,
49                                    String pathInfo, String queryString) {
50          super();
51          setPathElements(contextPath, servletPath, pathInfo, queryString);
52      }
53  
54  
55  
56      public MockHttpServletRequest(String contextPath, String servletPath,
57                                    String pathInfo, String queryString,
58                                    HttpSession session) {
59          super();
60          setPathElements(contextPath, servletPath, pathInfo, queryString);
61          setHttpSession(session);
62      }
63  
64  
65  
66      protected HashMap attributes = new HashMap();
67      protected String contextPath = null;
68      protected HashMap headers = new HashMap();
69      protected Cookie[] cookies = new Cookie[0];
70      protected Locale locale = null;
71      protected HashMap parameters = new HashMap();
72      protected String pathInfo = null;
73      protected Principal principal = null;
74      protected String queryString = null;
75      protected String servletPath = null;
76      protected HttpSession session = null;
77  
78  
79      // --------------------------------------------------------- Public Methods
80  
81  
82      public void addHeader(String name, String value) {
83          String values[] = (String[]) headers.get(name);
84          if (values == null) {
85              String results[] = new String[] { value };
86              headers.put(name, results);
87              return;
88          }
89          String results[] = new String[values.length + 1];
90          System.arraycopy(values, 0, results, 0, values.length);
91          results[values.length] = value;
92          headers.put(name, results);
93      }
94  
95  
96      public void addParameter(String name, String value) {
97          String values[] = (String[]) parameters.get(name);
98          if (values == null) {
99              String results[] = new String[] { value };
100             parameters.put(name, results);
101             return;
102         }
103         String results[] = new String[values.length + 1];
104         System.arraycopy(values, 0, results, 0, values.length);
105         results[values.length] = value;
106         parameters.put(name, results);
107     }
108 
109     public void addCookie(String name, String value) {
110         addCookie(new Cookie(name, value));
111     }
112 
113     public void addCookie(Cookie cookie) {
114         Cookie[] newValues = new Cookie[cookies.length + 1];
115         System.arraycopy(cookies, 0, newValues, 0, cookies.length);
116         cookies = newValues;
117         cookies[cookies.length - 1] = cookie;
118     }
119 
120 
121     public void setHttpSession(HttpSession session) {
122         this.session = session;
123     }
124 
125 
126     public void setLocale(Locale locale) {
127         this.locale = locale;
128     }
129 
130 
131     public void setPathElements(String contextPath, String servletPath,
132                                 String pathInfo, String queryString) {
133 
134         this.contextPath = contextPath;
135         this.servletPath = servletPath;
136         this.pathInfo = pathInfo;
137         this.queryString = queryString;
138 
139     }
140 
141 
142     public void setUserPrincipal(Principal principal) {
143         this.principal = principal;
144     }
145 
146 
147 
148     // --------------------------------------------- HttpServletRequest Methods
149 
150 
151     public String getAuthType() {
152         throw new UnsupportedOperationException();
153     }
154 
155 
156     public String getContextPath() {
157         return (contextPath);
158     }
159 
160 
161     public Cookie[] getCookies() {
162         return cookies;
163     }
164 
165 
166     public long getDateHeader(String name) {
167         throw new UnsupportedOperationException();
168     }
169 
170 
171     public String getHeader(String name) {
172         String values[] = (String[]) headers.get(name);
173         if (values != null) {
174             return (values[0]);
175         } else {
176             return (null);
177         }
178     }
179 
180 
181     public Enumeration getHeaderNames() {
182         return (new MockEnumeration(headers.keySet().iterator()));
183     }
184 
185 
186     public Enumeration getHeaders(String name) {
187         String headers[] = (String[]) this.headers.get(name);
188         if (headers == null) {
189             headers = new String[0];
190         }
191         List list = new ArrayList();
192         for (int i = 0; i < headers.length; i++) {
193             list.add(headers[i]);
194         }
195         return (new MockEnumeration(list.iterator()));
196     }
197 
198 
199     public int getIntHeader(String name) {
200         throw new UnsupportedOperationException();
201     }
202 
203 
204     public String getMethod() {
205         throw new UnsupportedOperationException();
206     }
207 
208 
209     public String getPathInfo() {
210         return (pathInfo);
211     }
212 
213 
214     public String getPathTranslated() {
215         throw new UnsupportedOperationException();
216     }
217 
218 
219     public String getQueryString() {
220         return (queryString);
221     }
222 
223 
224     public String getRemoteUser() {
225         if (principal != null) {
226             return (principal.getName());
227         } else {
228             return (null);
229         }
230     }
231 
232 
233     public String getRequestedSessionId() {
234         throw new UnsupportedOperationException();
235     }
236 
237 
238     public String getRequestURI() {
239         StringBuffer sb = new StringBuffer();
240         if (contextPath != null) {
241             sb.append(contextPath);
242         }
243         if (servletPath != null) {
244             sb.append(servletPath);
245         }
246         if (pathInfo != null) {
247             sb.append(pathInfo);
248         }
249         if (sb.length() > 0) {
250             return (sb.toString());
251         }
252         throw new UnsupportedOperationException();
253     }
254 
255 
256     public StringBuffer getRequestURL() {
257         throw new UnsupportedOperationException();
258     }
259 
260 
261     public String getServletPath() {
262         return (servletPath);
263     }
264 
265 
266     public HttpSession getSession() {
267         return (getSession(true));
268     }
269 
270 
271     public HttpSession getSession(boolean create) {
272         if (create && (session == null)) {
273             throw new UnsupportedOperationException();
274         }
275         return (session);
276     }
277 
278 
279     public Principal getUserPrincipal() {
280         return (principal);
281     }
282 
283 
284     public boolean isRequestedSessionIdFromCookie() {
285         throw new UnsupportedOperationException();
286     }
287 
288 
289     public boolean isRequestedSessionIdFromUrl() {
290         throw new UnsupportedOperationException();
291     }
292 
293 
294     public boolean isRequestedSessionIdFromURL() {
295         throw new UnsupportedOperationException();
296     }
297 
298 
299     public boolean isRequestedSessionIdValid() {
300         throw new UnsupportedOperationException();
301     }
302 
303 
304     public boolean isUserInRole(String role) {
305         if ((principal != null) && (principal instanceof MockPrincipal)) {
306             return (((MockPrincipal) principal).isUserInRole(role));
307         } else {
308             return (false);
309         }
310     }
311 
312 
313     // ------------------------------------------------- ServletRequest Methods
314 
315 
316     public Object getAttribute(String name) {
317         return (attributes.get(name));
318     }
319 
320 
321     public Enumeration getAttributeNames() {
322         return (new MockEnumeration(attributes.keySet().iterator()));
323     }
324 
325 
326     public String getCharacterEncoding() {
327         throw new UnsupportedOperationException();
328     }
329 
330 
331     public int getContentLength() {
332         throw new UnsupportedOperationException();
333     }
334 
335 
336     public String getContentType() {
337         throw new UnsupportedOperationException();
338     }
339 
340 
341     public ServletInputStream getInputStream() {
342         throw new UnsupportedOperationException();
343     }
344 
345 
346     public Locale getLocale() {
347         return (locale);
348     }
349 
350 
351     public Enumeration getLocales() {
352         throw new UnsupportedOperationException();
353     }
354 
355 
356     public String getLocalAddr() {
357         throw new UnsupportedOperationException();
358     }
359 
360 
361     public String getLocalName() {
362     throw new UnsupportedOperationException();
363     }
364 
365 
366     public int getLocalPort() {
367     throw new UnsupportedOperationException();
368     }
369 
370 
371     public String getParameter(String name) {
372         String values[] = (String[]) parameters.get(name);
373         if (values != null) {
374             return (values[0]);
375         } else {
376             return (null);
377         }
378     }
379 
380 
381     public Map getParameterMap() {
382         return (parameters);
383     }
384 
385 
386     public Enumeration getParameterNames() {
387         return (new MockEnumeration(parameters.keySet().iterator()));
388     }
389 
390 
391     public String[] getParameterValues(String name) {
392         return ((String[]) parameters.get(name));
393     }
394 
395 
396     public String getProtocol() {
397         throw new UnsupportedOperationException();
398     }
399 
400 
401     public BufferedReader getReader() {
402         throw new UnsupportedOperationException();
403     }
404 
405 
406     public String getRealPath(String path) {
407         throw new UnsupportedOperationException();
408     }
409 
410 
411     public String getRemoteAddr() {
412         throw new UnsupportedOperationException();
413     }
414 
415 
416     public String getRemoteHost() {
417         throw new UnsupportedOperationException();
418     }
419 
420 
421     public int getRemotePort() {
422     throw new UnsupportedOperationException();
423     }
424 
425 
426     public RequestDispatcher getRequestDispatcher(String path) {
427         throw new UnsupportedOperationException();
428     }
429 
430 
431     public String getScheme() {
432         return ("http");
433     }
434 
435 
436     public String getServerName() {
437         return ("localhost");
438     }
439 
440 
441     public int getServerPort() {
442         return (8080);
443     }
444 
445 
446     public boolean isSecure() {
447         return (false);
448     }
449 
450 
451     public void removeAttribute(String name) {
452         attributes.remove(name);
453     }
454 
455 
456     public void setAttribute(String name, Object value) {
457         if (value == null) {
458             attributes.remove(name);
459         } else {
460             attributes.put(name, value);
461         }
462     }
463 
464 
465     public void setCharacterEncoding(String name) {
466         throw new UnsupportedOperationException();
467     }
468 
469 
470 }