KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > mock > MockHttpServletRequest


1 /**
2  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3  * Licensed under the Common Development and Distribution License,
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.sun.com/cddl/
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */

15
16 package com.sun.facelets.mock;
17
18 import java.io.BufferedReader JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.net.URI JavaDoc;
24 import java.security.Principal JavaDoc;
25 import java.text.DateFormat JavaDoc;
26 import java.text.ParseException JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.Vector JavaDoc;
36
37 import javax.servlet.RequestDispatcher JavaDoc;
38 import javax.servlet.ServletContext JavaDoc;
39 import javax.servlet.ServletInputStream JavaDoc;
40 import javax.servlet.http.Cookie JavaDoc;
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpSession JavaDoc;
43
44 /**
45  *
46  * @author Jacob Hookom
47  * @version $Id: MockHttpServletRequest.java,v 1.2 2005/12/21 05:51:05 jhook Exp $
48  */

49 public class MockHttpServletRequest implements HttpServletRequest JavaDoc {
50
51     private final ServletContext JavaDoc servletContext;
52
53     private final URI JavaDoc uri;
54
55     private final String JavaDoc method;
56
57     private Cookie JavaDoc[] cookies = new Cookie JavaDoc[0];
58
59     private final Hashtable JavaDoc headers = new Hashtable JavaDoc();
60
61     private String JavaDoc remoteUser;
62
63     private String JavaDoc servletPath;
64
65     private HttpSession JavaDoc session;
66
67     private final Hashtable JavaDoc attributes = new Hashtable JavaDoc();
68
69     private final Properties JavaDoc param = new Properties JavaDoc();
70
71     private String JavaDoc characterEncoding = "ISO-8859-1";
72
73     private String JavaDoc contentType = "text/html";
74
75     private int contentLength = 0;
76
77     private String JavaDoc protocol = "HTTP/1.1";
78
79     private String JavaDoc localName = "localhost";
80
81     private int localPort = 80;
82
83     private String JavaDoc remoteAddr = "127.0.0.1";
84
85     private String JavaDoc remoteHost = "localhost";
86
87     private Locale JavaDoc locale = Locale.getDefault();
88
89     private Vector JavaDoc locales = new Vector JavaDoc(Arrays.asList(Locale
90             .getAvailableLocales()));
91     
92     private boolean secure = false;
93     
94     private int remotePort = 1024;
95     
96     private String JavaDoc localAddr = "127.0.0.1";
97
98     private ServletInputStream JavaDoc inputStream = new MockServletInputStream();
99
100     public MockHttpServletRequest(ServletContext JavaDoc servletContext, URI JavaDoc uri) {
101         this(servletContext, "GET", uri);
102     }
103
104     public MockHttpServletRequest(ServletContext JavaDoc servletContext, String JavaDoc uri) {
105         this(servletContext, "GET", uri);
106     }
107
108     public MockHttpServletRequest(ServletContext JavaDoc servletContext, String JavaDoc method,
109             String JavaDoc uri) {
110         this(servletContext, method, URI.create(uri));
111     }
112
113     public MockHttpServletRequest(ServletContext JavaDoc servletContext, String JavaDoc method,
114             URI JavaDoc uri) {
115         this.servletContext = servletContext;
116         this.uri = uri;
117         this.method = method;
118
119         String JavaDoc q = this.uri.getRawQuery();
120         if (q != null) {
121             String JavaDoc[] p = q.split("(&|=)");
122             for (int i = 0; i < p.length; i += 2) {
123                 this.param.put(p[i], p[i + 1]);
124             }
125         }
126     }
127
128     public String JavaDoc getAuthType() {
129         return BASIC_AUTH;
130     }
131
132     public Cookie JavaDoc[] getCookies() {
133         return this.cookies;
134     }
135
136     public long getDateHeader(String JavaDoc name) {
137         String JavaDoc hdr = this.getHeader(name);
138         if (hdr != null) {
139             try {
140                 return DateFormat.getDateInstance(DateFormat.FULL).parse(hdr)
141                         .getTime();
142             } catch (ParseException JavaDoc e) {
143                 throw new IllegalArgumentException JavaDoc("Header " + name + ": "
144                         + hdr);
145             }
146         }
147         return -1;
148     }
149
150     public String JavaDoc getHeader(String JavaDoc name) {
151         Object JavaDoc obj = this.headers.get(name);
152         if (obj instanceof List JavaDoc) {
153             return ((List JavaDoc) obj).get(0).toString();
154         } else if (obj instanceof String JavaDoc) {
155             return (String JavaDoc) obj;
156         }
157         return null;
158     }
159
160     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
161         Object JavaDoc obj = this.headers.get(name);
162         if (obj instanceof Vector JavaDoc) {
163             return ((Vector JavaDoc) obj).elements();
164         } else if (obj instanceof String JavaDoc) {
165             Vector JavaDoc v = new Vector JavaDoc();
166             v.add(obj);
167             return v.elements();
168         }
169         return null;
170     }
171
172     public Enumeration JavaDoc getHeaderNames() {
173         return this.headers.keys();
174     }
175
176     public int getIntHeader(String JavaDoc name) {
177         String JavaDoc hdr = this.getHeader(name);
178         if (hdr != null) {
179             try {
180                 return Integer.parseInt(hdr);
181             } catch (Exception JavaDoc e) {
182                 throw new IllegalArgumentException JavaDoc("Header " + name + ": "
183                         + hdr);
184             }
185         }
186         return -1;
187     }
188
189     public String JavaDoc getMethod() {
190         return this.method;
191     }
192
193     public String JavaDoc getPathInfo() {
194         return this.uri.getPath();
195     }
196
197     public String JavaDoc getPathTranslated() {
198         return this.servletContext.getRealPath(this.uri.getPath());
199     }
200
201     public String JavaDoc getContextPath() {
202         return this.uri.getPath();
203     }
204
205     public String JavaDoc getQueryString() {
206         return this.uri.getQuery();
207     }
208
209     public String JavaDoc getRemoteUser() {
210         return this.remoteUser;
211     }
212
213     public boolean isUserInRole(String JavaDoc role) {
214         throw new UnsupportedOperationException JavaDoc();
215     }
216
217     public Principal JavaDoc getUserPrincipal() {
218         throw new UnsupportedOperationException JavaDoc();
219     }
220
221     public String JavaDoc getRequestedSessionId() {
222         return this.getParameter("jsessionid");
223     }
224
225     public String JavaDoc getRequestURI() {
226         return this.uri.getPath();
227     }
228
229     public StringBuffer JavaDoc getRequestURL() {
230         return new StringBuffer JavaDoc(this.uri.toString());
231     }
232
233     public String JavaDoc getServletPath() {
234         return this.servletPath;
235     }
236
237     public HttpSession JavaDoc getSession(boolean create) {
238         if (this.session == null && create) {
239             this.session = new MockHttpSession(this.servletContext);
240         }
241         return this.session;
242     }
243
244     public HttpSession JavaDoc getSession() {
245         return this.getSession(true);
246     }
247
248     public boolean isRequestedSessionIdValid() {
249         throw new UnsupportedOperationException JavaDoc();
250     }
251
252     public boolean isRequestedSessionIdFromCookie() {
253         throw new UnsupportedOperationException JavaDoc();
254     }
255
256     public boolean isRequestedSessionIdFromURL() {
257         throw new UnsupportedOperationException JavaDoc();
258     }
259
260     public boolean isRequestedSessionIdFromUrl() {
261         throw new UnsupportedOperationException JavaDoc();
262     }
263
264     public Object JavaDoc getAttribute(String JavaDoc name) {
265         return this.attributes.get(name);
266     }
267
268     public Enumeration JavaDoc getAttributeNames() {
269         return this.attributes.keys();
270     }
271
272     public String JavaDoc getCharacterEncoding() {
273         return this.characterEncoding;
274     }
275
276     public void setCharacterEncoding(String JavaDoc characterEncoding)
277             throws UnsupportedEncodingException JavaDoc {
278         this.characterEncoding = characterEncoding;
279     }
280
281     public int getContentLength() {
282         return this.contentLength;
283     }
284
285     public String JavaDoc getContentType() {
286         return this.contentType;
287     }
288
289     public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc {
290         return this.inputStream;
291     }
292
293     public String JavaDoc getParameter(String JavaDoc name) {
294         return this.param.getProperty(name);
295     }
296
297     public Enumeration JavaDoc getParameterNames() {
298         return this.param.keys();
299     }
300
301     public String JavaDoc[] getParameterValues(String JavaDoc name) {
302         String JavaDoc p = this.param.getProperty(name);
303         if (p != null) {
304             return p.split(",");
305         }
306         return null;
307     }
308     
309     public void setParameter(String JavaDoc name, String JavaDoc value) {
310         this.param.put(name, value);
311     }
312
313     public Map JavaDoc getParameterMap() {
314         return Collections.unmodifiableMap(this.param);
315     }
316
317     public String JavaDoc getProtocol() {
318         return this.protocol;
319     }
320
321     public String JavaDoc getScheme() {
322         return this.uri.getScheme();
323     }
324
325     public String JavaDoc getServerName() {
326         return this.localName;
327     }
328
329     public int getServerPort() {
330         return this.localPort;
331     }
332
333     public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
334         if (this.inputStream != null) {
335             Reader JavaDoc sourceReader = (this.characterEncoding != null) ? new InputStreamReader JavaDoc(
336                     this.inputStream, this.characterEncoding)
337                     : new InputStreamReader JavaDoc(this.inputStream);
338             return new BufferedReader JavaDoc(sourceReader);
339         } else {
340             return null;
341         }
342     }
343
344     public String JavaDoc getRemoteAddr() {
345         return this.remoteAddr;
346     }
347
348     public String JavaDoc getRemoteHost() {
349         return this.remoteHost;
350     }
351
352     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
353         this.attributes.put(name, value);
354     }
355
356     public void removeAttribute(String JavaDoc name) {
357         this.attributes.remove(name);
358     }
359
360     public Locale JavaDoc getLocale() {
361         return this.locale;
362     }
363
364     public Enumeration JavaDoc getLocales() {
365         return this.locales.elements();
366     }
367
368     public boolean isSecure() {
369         return this.secure;
370     }
371
372     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
373         return this.servletContext.getRequestDispatcher(path);
374     }
375
376     public String JavaDoc getRealPath(String JavaDoc path) {
377         return this.servletContext.getRealPath(path);
378     }
379
380     public int getRemotePort() {
381         return this.remotePort;
382     }
383
384     public String JavaDoc getLocalName() {
385         return this.localName;
386     }
387
388     public String JavaDoc getLocalAddr() {
389         return this.localAddr;
390     }
391
392     public int getLocalPort() {
393         return this.localPort;
394     }
395
396 }
397
Popular Tags