KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > test > mocks > servlet > MockServletRequest


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5
6 /**
7  * Created by The eXo Platform SARL
8  * Author : Mestrallet Benjamin
9  * benjmestrallet@users.sourceforge.net
10  * Date: Jul 29, 2003
11  * Time: 12:44:53 PM
12  */

13 package org.exoplatform.test.mocks.servlet;
14
15
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.http.Cookie JavaDoc;
18 import javax.servlet.http.HttpSession JavaDoc;
19 import javax.servlet.ServletInputStream JavaDoc;
20 import javax.servlet.RequestDispatcher JavaDoc;
21
22 import java.util.*;
23 import java.security.Principal JavaDoc;
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.BufferedReader JavaDoc;
27
28 public class MockServletRequest implements HttpServletRequest JavaDoc {
29
30   private Map parameters ;
31   private Map attributes ;
32   private HttpSession JavaDoc session;
33   private Locale locale;
34   private boolean secure;
35   private Map headers;
36   private String JavaDoc enc = "ISO-8859-1";
37
38   private String JavaDoc remoteUser = "REMOTE USER FROM MOCK";
39
40   public MockServletRequest(HttpSession JavaDoc session, Locale locale) {
41     this.session = session;
42     this.locale = locale;
43     headers = new HashMap();
44     Collection headersMultiple = new ArrayList();
45     headersMultiple.add("header-value3-1");
46     headersMultiple.add("header-value3-2");
47     headersMultiple.add("header-value3-3");
48     headers.put("header1", "header-value1");
49     headers.put("header2", "header-value2");
50     headers.put("header3", headersMultiple);
51     parameters = new HashMap();
52     attributes = new HashMap();
53   }
54   
55   public void reset() {
56     parameters = new HashMap();
57     attributes = new HashMap();
58   }
59   
60   public MockServletRequest(HttpSession JavaDoc session, Locale locale, boolean secure) {
61     this(session, locale);
62     this.secure = secure;
63   }
64
65   public String JavaDoc getAuthType() {
66     return DIGEST_AUTH;
67   }
68
69   public Cookie JavaDoc[] getCookies() {
70     return new Cookie JavaDoc[0];
71   }
72
73   public long getDateHeader(String JavaDoc s) {
74     return 0;
75   }
76
77   public String JavaDoc getHeader(String JavaDoc s) {
78     return (String JavaDoc) headers.get(s);
79   }
80
81   public Enumeration getHeaders(String JavaDoc s) {
82     if(headers.get(s) instanceof Collection)
83       return Collections.enumeration((Collection)headers.get(s));
84     else {
85       Vector v = new Vector();
86       v.add(headers.get(s));
87       return v.elements();
88     }
89   }
90
91   public Enumeration getHeaderNames() {
92     return Collections.enumeration(headers.keySet());
93   }
94
95   public int getIntHeader(String JavaDoc s) {
96     return 0;
97   }
98
99   public String JavaDoc getMethod() {
100     return null;
101   }
102
103   public String JavaDoc getPathInfo() {
104     return null;
105   }
106
107   public String JavaDoc getPathTranslated() {
108     return null;
109   }
110
111   public String JavaDoc getContextPath() {
112     return null;
113   }
114
115   public String JavaDoc getQueryString() {
116     return null;
117   }
118
119   public String JavaDoc getRemoteUser() {
120     return remoteUser;
121   }
122
123   public void setRemoteUser(String JavaDoc remoteUser) {
124     this.remoteUser = remoteUser;
125   }
126
127   public boolean isUserInRole(String JavaDoc s) {
128     if("auth-user".equals(s) )
129       return true;
130     else
131       return false;
132   }
133
134   public Principal JavaDoc getUserPrincipal() {
135     return new MockPrincipal();
136   }
137
138   public String JavaDoc getRequestedSessionId() {
139     return null;
140   }
141
142   public String JavaDoc getRequestURI() {
143     return null;
144   }
145
146   public StringBuffer JavaDoc getRequestURL() {
147     return null;
148   }
149
150   public String JavaDoc getServletPath() {
151     return null;
152   }
153
154   public HttpSession JavaDoc getSession(boolean b) {
155     return session;
156   }
157
158   public HttpSession JavaDoc getSession() {
159     return session;
160   }
161
162   public boolean isRequestedSessionIdValid() {
163     return false;
164   }
165
166   public boolean isRequestedSessionIdFromCookie() {
167     return false;
168   }
169
170   public boolean isRequestedSessionIdFromURL() {
171     return false;
172   }
173
174   public boolean isRequestedSessionIdFromUrl() {
175     return false;
176   }
177
178   public Object JavaDoc getAttribute(String JavaDoc s) {
179     return attributes.get(s);
180   }
181
182   public Enumeration getAttributeNames() {
183     return new Vector(attributes.keySet()).elements();
184   }
185
186   public String JavaDoc getCharacterEncoding() {
187     return enc;
188   }
189
190   public void setCharacterEncoding(String JavaDoc s) throws UnsupportedEncodingException JavaDoc {
191     enc = s;
192   }
193
194   public int getContentLength() {
195     return 0;
196   }
197
198   public String JavaDoc getContentType() {
199     return null;
200   }
201
202   public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc {
203     return null;
204   }
205
206   public String JavaDoc getParameter(String JavaDoc s) {
207     return (String JavaDoc) parameters.get(s);
208   }
209
210   public void setParameter(String JavaDoc s, Object JavaDoc value) {
211     parameters.put(s, value);
212   }
213
214   public Enumeration getParameterNames() {
215     return new Vector(parameters.keySet()).elements();
216   }
217
218   public String JavaDoc[] getParameterValues(String JavaDoc s) {
219     return new String JavaDoc[0];
220   }
221
222   public Map getParameterMap() {
223     return parameters;
224   }
225
226   public String JavaDoc getProtocol() {
227     return null;
228   }
229
230   public String JavaDoc getScheme() {
231     return null;
232   }
233
234   public String JavaDoc getServerName() {
235     return null;
236   }
237
238   public int getServerPort() {
239     return 0;
240   }
241
242   public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
243     return null;
244   }
245
246   public String JavaDoc getRemoteAddr() {
247     return null;
248   }
249
250   public String JavaDoc getRemoteHost() {
251     return null;
252   }
253
254   public void setAttribute(String JavaDoc s, Object JavaDoc o) {
255     attributes.put(s, o);
256   }
257
258   public void removeAttribute(String JavaDoc s) {
259     attributes.remove(s);
260   }
261
262   public Locale getLocale() {
263     return locale;
264   }
265
266   public Enumeration getLocales() {
267     System.out.println("MOCK get Locale : " + locale);
268     Vector v = new Vector();
269     v.add(locale);
270     return v.elements();
271   }
272
273   public boolean isSecure() {
274     return secure;
275   }
276
277   public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc s) {
278     return null;
279   }
280
281   public String JavaDoc getRealPath(String JavaDoc s) {
282     return null;
283   }
284
285   //servlet 2.4 method
286
public int getLocalPort(){
287     return 0;
288   }
289
290   public String JavaDoc getLocalAddr(){
291     return "127.0.0.1";
292   }
293
294   public String JavaDoc getLocalName(){
295     return "localhost";
296   }
297
298   public int getRemotePort(){
299      return 0;
300   }
301 }
302
Popular Tags