KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > mock > MockRequest


1 /*
2  * Copyright 1999-2005 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.cocoon.environment.mock;
17
18 import java.security.Principal JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.text.DateFormat JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.text.ParseException JavaDoc;
28
29 import junit.framework.AssertionFailedError;
30
31 import org.apache.cocoon.environment.Cookie;
32 import org.apache.cocoon.environment.Request;
33 import org.apache.cocoon.environment.Session;
34
35 /**
36  * @version $Id: MockRequest.java 202262 2005-06-28 18:02:55Z vgritsenko $
37  */

38 public class MockRequest implements Request {
39
40     private Hashtable JavaDoc attributes = new Hashtable JavaDoc();
41     private String JavaDoc scheme;
42     private String JavaDoc protocol = "HTTP/1.1";
43     private String JavaDoc requestURI;
44     private String JavaDoc contextPath = "";
45     private String JavaDoc servletPath;
46     private String JavaDoc pathInfo;
47     private String JavaDoc queryString;
48     private String JavaDoc method = "GET";
49     private String JavaDoc contentType;
50     private Locale JavaDoc locale;
51     private Principal JavaDoc principal;
52     private String JavaDoc remoteAddr;
53     private String JavaDoc remoteHost;
54     private String JavaDoc remoteUser;
55     private String JavaDoc userRole;
56     private String JavaDoc reqSessionId;
57     private String JavaDoc authType;
58     private String JavaDoc charEncoding;
59     private String JavaDoc serverName;
60     private int port = 80;
61
62     private Hashtable JavaDoc parameters = new Hashtable JavaDoc();
63     private Hashtable JavaDoc headers = new Hashtable JavaDoc();
64     private HashMap JavaDoc cookies = new HashMap JavaDoc();
65
66     private MockSession session;
67
68     public Object JavaDoc get(String JavaDoc name) {
69         return getAttribute(name);
70     }
71
72     public Object JavaDoc getAttribute(String JavaDoc name) {
73         return attributes.get(name);
74     }
75
76     public Enumeration JavaDoc getAttributeNames() {
77         return attributes.keys();
78     }
79
80     public void setAttribute(String JavaDoc name, Object JavaDoc o) {
81         if (o == null)
82             attributes.remove(name);
83         else
84             attributes.put(name, o);
85     }
86
87     public void removeAttribute(String JavaDoc name) {
88         attributes.remove(name);
89     }
90
91     public String JavaDoc getAuthType() {
92         return authType;
93     }
94
95     public String JavaDoc getCharacterEncoding() {
96         return charEncoding;
97     }
98
99     public void setCharacterEncoding(String JavaDoc enc) throws java.io.UnsupportedEncodingException JavaDoc {
100         charEncoding = enc;
101     }
102
103     public int getContentLength() {
104         return -1;
105     }
106
107     public String JavaDoc getContentType() {
108         return contentType;
109     }
110
111     public String JavaDoc getParameter(String JavaDoc name) {
112         return (String JavaDoc)parameters.get(name);
113     }
114
115     public Enumeration JavaDoc getParameterNames() {
116         return parameters.keys();
117     }
118
119     public String JavaDoc[] getParameterValues(String JavaDoc name) {
120         Object JavaDoc param = parameters.get(name);
121         if( null == param )
122             return null;
123         else {
124             if (param.getClass().isArray()) {
125                 return (String JavaDoc[]) param;
126             } else {
127                 return new String JavaDoc[] {(String JavaDoc) param};
128             }
129         }
130     }
131
132     public void addParameter(String JavaDoc name, String JavaDoc value) {
133         parameters.put(name, value);
134     }
135
136     public String JavaDoc getProtocol() {
137         return protocol;
138     }
139
140     public String JavaDoc getScheme() {
141         return scheme;
142     }
143
144     public String JavaDoc getServerName() {
145         return serverName;
146     }
147
148     public int getServerPort() {
149         return port;
150     }
151
152     public String JavaDoc getRemoteAddr() {
153         return remoteAddr;
154     }
155
156     public String JavaDoc getRemoteHost() {
157         return remoteHost;
158     }
159
160     public Locale JavaDoc getLocale() {
161         return locale;
162     }
163
164     public Enumeration JavaDoc getLocales() {
165         return Collections.enumeration(Collections.singleton(getLocale()));
166     }
167
168     public boolean isSecure() {
169         if(scheme==null){
170             return false;
171         } else{
172             return scheme.equalsIgnoreCase("HTTPS");
173         }
174     }
175
176     public Cookie[] getCookies() {
177         if (cookies.isEmpty())
178             return null;
179         else {
180             Cookie[] cookieArray = new Cookie[cookies.size()];
181             return (Cookie []) cookies.values().toArray(cookieArray);
182         }
183     }
184
185     public Map JavaDoc getCookieMap() {
186         return cookies;
187     }
188
189     public long getDateHeader(String JavaDoc name) {
190         String JavaDoc s1 = getHeader(name);
191         if(s1 == null)
192             return -1L;
193         try
194         {
195             DateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc();
196             return dateFormat.parse(s1).getTime();
197         }
198         catch(ParseException JavaDoc exception) {
199             throw new IllegalArgumentException JavaDoc("Cannot parse date: " + s1);
200         }
201     }
202
203     public String JavaDoc getHeader(String JavaDoc name) {
204         return (String JavaDoc) headers.get(name);
205     }
206
207     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
208         throw new AssertionFailedError("Not implemented");
209     }
210
211     public Enumeration JavaDoc getHeaderNames() {
212         return headers.keys();
213     }
214
215     public String JavaDoc getMethod() {
216         return method;
217     }
218
219     public String JavaDoc getPathInfo() {
220         return pathInfo;
221     }
222
223     public String JavaDoc getPathTranslated() {
224         throw new AssertionFailedError("Not implemented");
225     }
226
227     public String JavaDoc getContextPath() {
228         return contextPath;
229     }
230
231     public void setContextPath(String JavaDoc path) {
232         contextPath = path;
233     }
234
235     public String JavaDoc getQueryString() {
236         return queryString;
237     }
238
239     public void setQueryString(String JavaDoc string) {
240         queryString = string;
241     }
242
243     public String JavaDoc getRemoteUser() {
244         return remoteUser;
245     }
246
247     public Principal JavaDoc getUserPrincipal() {
248         return principal;
249     }
250
251     public boolean isUserInRole(String JavaDoc role) {
252         return userRole.equals(role);
253     }
254
255     public String JavaDoc getRequestedSessionId() {
256         return reqSessionId;
257     }
258
259     public String JavaDoc getRequestURI() {
260         return requestURI;
261     }
262
263     public void setRequestURI(String JavaDoc uri) {
264         requestURI = uri;
265     }
266
267     public String JavaDoc getSitemapURI() {
268         return requestURI;
269     }
270
271     public String JavaDoc getSitemapURIPrefix() {
272         return "";
273     }
274
275     public String JavaDoc getServletPath() {
276         return servletPath;
277     }
278
279     public Session getSession(boolean create) {
280         if ((session == null) && (create))
281             this.session = new MockSession();
282         else if ((session != null) && (!(session).isValid()) && (create))
283             this.session = new MockSession();
284         if ((session != null) && ((session).isValid()))
285             return this.session;
286         else
287             return null;
288     }
289
290     public Session getSession() {
291         return getSession(true);
292     }
293
294     public boolean isRequestedSessionIdValid() {
295         if (session != null) {
296             try {
297                 session.getId();
298                 return true;
299             } catch (IllegalStateException JavaDoc e) {
300                 return false;
301             }
302         } else
303             return false;
304     }
305
306     public boolean isRequestedSessionIdFromCookie() {
307         return true;
308     }
309
310     public boolean isRequestedSessionIdFromURL() {
311         return false;
312     }
313
314     public void reset() {
315         attributes.clear();
316         scheme = null;
317         protocol = "HTTP/1.1";
318         requestURI = null;
319         contextPath = null;
320         servletPath = null;
321         pathInfo = null;
322         queryString = null;
323         method = "GET";
324         contentType = null;
325         locale = null;
326         principal = null;
327         remoteAddr = null;
328         remoteHost = null;
329         remoteUser = null;
330         userRole = null;
331         reqSessionId = null;
332         authType = null;
333         charEncoding = null;
334         serverName = null;
335         port = 80;
336
337         parameters.clear();
338         headers.clear();
339     }
340
341     public void setHeader( String JavaDoc key, String JavaDoc value ) {
342         this.headers.put(key, value );
343     }
344
345     public void setMethod( String JavaDoc method ) {
346         this.method = method;
347     }
348
349     public void clearSession() {
350         this.session = null;
351     }
352
353 }
354
Popular Tags