KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > web > servlet > MockHttpServletRequest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
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 package org.apache.commons.chain.web.servlet;
17
18
19 import org.apache.commons.chain.web.MockEnumeration;
20 import org.apache.commons.chain.web.MockPrincipal;
21
22 import javax.servlet.RequestDispatcher JavaDoc;
23 import javax.servlet.ServletInputStream JavaDoc;
24 import javax.servlet.http.Cookie JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import java.io.BufferedReader JavaDoc;
28 import java.security.Principal JavaDoc;
29 import java.util.*;
30
31
32 // Mock Object for HttpServletRequest (Version 2.3)
33
public class MockHttpServletRequest implements HttpServletRequest JavaDoc {
34
35
36     public MockHttpServletRequest() {
37         super();
38     }
39
40
41     public MockHttpServletRequest(HttpSession JavaDoc session) {
42         super();
43         setHttpSession(session);
44     }
45
46
47     public MockHttpServletRequest(String JavaDoc contextPath, String JavaDoc servletPath,
48                                   String JavaDoc pathInfo, String JavaDoc queryString) {
49         super();
50         setPathElements(contextPath, servletPath, pathInfo, queryString);
51     }
52
53
54
55     public MockHttpServletRequest(String JavaDoc contextPath, String JavaDoc servletPath,
56                                   String JavaDoc pathInfo, String JavaDoc queryString,
57                                   HttpSession JavaDoc session) {
58         super();
59         setPathElements(contextPath, servletPath, pathInfo, queryString);
60         setHttpSession(session);
61     }
62
63
64
65     protected HashMap attributes = new HashMap();
66     protected String JavaDoc contextPath = null;
67     protected HashMap headers = new HashMap();
68     protected Locale locale = null;
69     protected HashMap parameters = new HashMap();
70     protected String JavaDoc pathInfo = null;
71     protected Principal principal = null;
72     protected String JavaDoc queryString = null;
73     protected String JavaDoc servletPath = null;
74     protected HttpSession JavaDoc session = null;
75
76
77     // --------------------------------------------------------- Public Methods
78

79
80     public void addHeader(String JavaDoc name, String JavaDoc value) {
81         String JavaDoc values[] = (String JavaDoc[]) headers.get(name);
82         if (values == null) {
83             String JavaDoc results[] = new String JavaDoc[] { value };
84             headers.put(name, results);
85             return;
86         }
87         String JavaDoc results[] = new String JavaDoc[values.length + 1];
88         System.arraycopy(values, 0, results, 0, values.length);
89         results[values.length] = value;
90         headers.put(name, results);
91     }
92
93
94     public void addParameter(String JavaDoc name, String JavaDoc value) {
95         String JavaDoc values[] = (String JavaDoc[]) parameters.get(name);
96         if (values == null) {
97             String JavaDoc results[] = new String JavaDoc[] { value };
98             parameters.put(name, results);
99             return;
100         }
101         String JavaDoc results[] = new String JavaDoc[values.length + 1];
102         System.arraycopy(values, 0, results, 0, values.length);
103         results[values.length] = value;
104         parameters.put(name, results);
105     }
106
107
108     public void setHttpSession(HttpSession JavaDoc session) {
109         this.session = session;
110     }
111
112
113     public void setLocale(Locale locale) {
114         this.locale = locale;
115     }
116
117
118     public void setPathElements(String JavaDoc contextPath, String JavaDoc servletPath,
119                                 String JavaDoc pathInfo, String JavaDoc queryString) {
120
121         this.contextPath = contextPath;
122         this.servletPath = servletPath;
123         this.pathInfo = pathInfo;
124         this.queryString = queryString;
125
126     }
127
128
129     public void setUserPrincipal(Principal principal) {
130         this.principal = principal;
131     }
132
133
134
135     // --------------------------------------------- HttpServletRequest Methods
136

137
138     public String JavaDoc getAuthType() {
139         throw new UnsupportedOperationException JavaDoc();
140     }
141
142
143     public String JavaDoc getContextPath() {
144         return (contextPath);
145     }
146
147
148     public Cookie JavaDoc[] getCookies() {
149         throw new UnsupportedOperationException JavaDoc();
150     }
151
152
153     public long getDateHeader(String JavaDoc name) {
154         throw new UnsupportedOperationException JavaDoc();
155     }
156
157
158     public String JavaDoc getHeader(String JavaDoc name) {
159         String JavaDoc values[] = (String JavaDoc[]) headers.get(name);
160         if (values != null) {
161             return (values[0]);
162         } else {
163             return (null);
164         }
165     }
166
167
168     public Enumeration getHeaderNames() {
169         return (new MockEnumeration(headers.keySet().iterator()));
170     }
171
172
173     public Enumeration getHeaders(String JavaDoc name) {
174         String JavaDoc headers[] = (String JavaDoc[]) this.headers.get(name);
175         if (headers == null) {
176             headers = new String JavaDoc[0];
177         }
178         List list = new ArrayList();
179         for (int i = 0; i < headers.length; i++) {
180             list.add(headers[i]);
181         }
182         return (new MockEnumeration(list.iterator()));
183     }
184
185
186     public int getIntHeader(String JavaDoc name) {
187         throw new UnsupportedOperationException JavaDoc();
188     }
189
190
191     public String JavaDoc getMethod() {
192         throw new UnsupportedOperationException JavaDoc();
193     }
194
195
196     public String JavaDoc getPathInfo() {
197         return (pathInfo);
198     }
199
200
201     public String JavaDoc getPathTranslated() {
202         throw new UnsupportedOperationException JavaDoc();
203     }
204
205
206     public String JavaDoc getQueryString() {
207         return (queryString);
208     }
209
210
211     public String JavaDoc getRemoteUser() {
212         if (principal != null) {
213             return (principal.getName());
214         } else {
215             return (null);
216         }
217     }
218
219
220     public String JavaDoc getRequestedSessionId() {
221         throw new UnsupportedOperationException JavaDoc();
222     }
223
224
225     public String JavaDoc getRequestURI() {
226         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
227         if (contextPath != null) {
228             sb.append(contextPath);
229         }
230         if (servletPath != null) {
231             sb.append(servletPath);
232         }
233         if (pathInfo != null) {
234             sb.append(pathInfo);
235         }
236         if (sb.length() > 0) {
237             return (sb.toString());
238         }
239         throw new UnsupportedOperationException JavaDoc();
240     }
241
242
243     public StringBuffer JavaDoc getRequestURL() {
244         throw new UnsupportedOperationException JavaDoc();
245     }
246
247
248     public String JavaDoc getServletPath() {
249         return (servletPath);
250     }
251
252
253     public HttpSession JavaDoc getSession() {
254         return (getSession(true));
255     }
256
257
258     public HttpSession JavaDoc getSession(boolean create) {
259         if (create && (session == null)) {
260             throw new UnsupportedOperationException JavaDoc();
261         }
262         return (session);
263     }
264
265
266     public Principal getUserPrincipal() {
267         return (principal);
268     }
269
270
271     public boolean isRequestedSessionIdFromCookie() {
272         throw new UnsupportedOperationException JavaDoc();
273     }
274
275
276     public boolean isRequestedSessionIdFromUrl() {
277         throw new UnsupportedOperationException JavaDoc();
278     }
279
280
281     public boolean isRequestedSessionIdFromURL() {
282         throw new UnsupportedOperationException JavaDoc();
283     }
284
285
286     public boolean isRequestedSessionIdValid() {
287         throw new UnsupportedOperationException JavaDoc();
288     }
289
290
291     public boolean isUserInRole(String JavaDoc role) {
292         if ((principal != null) && (principal instanceof MockPrincipal)) {
293             return (((MockPrincipal) principal).isUserInRole(role));
294         } else {
295             return (false);
296         }
297     }
298
299
300     // ------------------------------------------------- ServletRequest Methods
301

302
303     public Object JavaDoc getAttribute(String JavaDoc name) {
304         return (attributes.get(name));
305     }
306
307
308     public Enumeration getAttributeNames() {
309         return (new MockEnumeration(attributes.keySet().iterator()));
310     }
311
312
313     public String JavaDoc getCharacterEncoding() {
314         throw new UnsupportedOperationException JavaDoc();
315     }
316
317
318     public int getContentLength() {
319         throw new UnsupportedOperationException JavaDoc();
320     }
321
322
323     public String JavaDoc getContentType() {
324         throw new UnsupportedOperationException JavaDoc();
325     }
326
327
328     public ServletInputStream JavaDoc getInputStream() {
329         throw new UnsupportedOperationException JavaDoc();
330     }
331
332
333     public Locale getLocale() {
334         return (locale);
335     }
336
337
338     public Enumeration getLocales() {
339         throw new UnsupportedOperationException JavaDoc();
340     }
341
342
343     public String JavaDoc getLocalAddr() {
344         throw new UnsupportedOperationException JavaDoc();
345     }
346
347
348     public String JavaDoc getLocalName() {
349     throw new UnsupportedOperationException JavaDoc();
350     }
351
352
353     public int getLocalPort() {
354     throw new UnsupportedOperationException JavaDoc();
355     }
356
357
358     public String JavaDoc getParameter(String JavaDoc name) {
359         String JavaDoc values[] = (String JavaDoc[]) parameters.get(name);
360         if (values != null) {
361             return (values[0]);
362         } else {
363             return (null);
364         }
365     }
366
367
368     public Map getParameterMap() {
369         return (parameters);
370     }
371
372
373     public Enumeration getParameterNames() {
374         return (new MockEnumeration(parameters.keySet().iterator()));
375     }
376
377
378     public String JavaDoc[] getParameterValues(String JavaDoc name) {
379         return ((String JavaDoc[]) parameters.get(name));
380     }
381
382
383     public String JavaDoc getProtocol() {
384         throw new UnsupportedOperationException JavaDoc();
385     }
386
387
388     public BufferedReader JavaDoc getReader() {
389         throw new UnsupportedOperationException JavaDoc();
390     }
391
392
393     public String JavaDoc getRealPath(String JavaDoc path) {
394         throw new UnsupportedOperationException JavaDoc();
395     }
396
397
398     public String JavaDoc getRemoteAddr() {
399         throw new UnsupportedOperationException JavaDoc();
400     }
401
402
403     public String JavaDoc getRemoteHost() {
404         throw new UnsupportedOperationException JavaDoc();
405     }
406
407
408     public int getRemotePort() {
409     throw new UnsupportedOperationException JavaDoc();
410     }
411
412
413     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
414         throw new UnsupportedOperationException JavaDoc();
415     }
416
417
418     public String JavaDoc getScheme() {
419         return ("http");
420     }
421
422
423     public String JavaDoc getServerName() {
424         return ("localhost");
425     }
426
427
428     public int getServerPort() {
429         return (8080);
430     }
431
432
433     public boolean isSecure() {
434         return (false);
435     }
436
437
438     public void removeAttribute(String JavaDoc name) {
439         attributes.remove(name);
440     }
441
442
443     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
444         if (value == null) {
445             attributes.remove(name);
446         } else {
447             attributes.put(name, value);
448         }
449     }
450
451
452     public void setCharacterEncoding(String JavaDoc name) {
453         throw new UnsupportedOperationException JavaDoc();
454     }
455
456
457 }
458
Popular Tags