001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.environment.mock;
018:
019: import java.security.Principal;
020: import java.util.Collections;
021: import java.util.Enumeration;
022: import java.util.Hashtable;
023: import java.util.HashMap;
024: import java.util.Locale;
025: import java.util.Map;
026: import java.text.DateFormat;
027: import java.text.SimpleDateFormat;
028: import java.text.ParseException;
029:
030: import junit.framework.AssertionFailedError;
031:
032: import org.apache.cocoon.environment.Cookie;
033: import org.apache.cocoon.environment.Request;
034: import org.apache.cocoon.environment.Session;
035:
036: /**
037: * @version $Id: MockRequest.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039: public class MockRequest implements Request {
040:
041: private Hashtable attributes = new Hashtable();
042: private String scheme;
043: private String protocol = "HTTP/1.1";
044: private String requestURI;
045: private String contextPath = "";
046: private String servletPath;
047: private String pathInfo;
048: private String queryString;
049: private String method = "GET";
050: private String contentType;
051: private Locale locale;
052: private Principal principal;
053: private String remoteAddr;
054: private String remoteHost;
055: private String remoteUser;
056: private String userRole;
057: private String reqSessionId;
058: private String authType;
059: private String charEncoding;
060: private String serverName;
061: private int port = 80;
062:
063: private Hashtable parameters = new Hashtable();
064: private Hashtable headers = new Hashtable();
065: private HashMap cookies = new HashMap();
066:
067: private MockSession session;
068:
069: public Object get(String name) {
070: return getAttribute(name);
071: }
072:
073: public Object getAttribute(String name) {
074: return attributes.get(name);
075: }
076:
077: public Enumeration getAttributeNames() {
078: return attributes.keys();
079: }
080:
081: public void setAttribute(String name, Object o) {
082: if (o == null)
083: attributes.remove(name);
084: else
085: attributes.put(name, o);
086: }
087:
088: public void removeAttribute(String name) {
089: attributes.remove(name);
090: }
091:
092: public String getAuthType() {
093: return authType;
094: }
095:
096: public String getCharacterEncoding() {
097: return charEncoding;
098: }
099:
100: public void setCharacterEncoding(String enc)
101: throws java.io.UnsupportedEncodingException {
102: charEncoding = enc;
103: }
104:
105: public int getContentLength() {
106: return -1;
107: }
108:
109: public String getContentType() {
110: return contentType;
111: }
112:
113: public String getParameter(String name) {
114: return (String) parameters.get(name);
115: }
116:
117: public Enumeration getParameterNames() {
118: return parameters.keys();
119: }
120:
121: public String[] getParameterValues(String name) {
122: Object param = parameters.get(name);
123: if (null == param)
124: return null;
125: else {
126: if (param.getClass().isArray()) {
127: return (String[]) param;
128: } else {
129: return new String[] { (String) param };
130: }
131: }
132: }
133:
134: public void addParameter(String name, String value) {
135: parameters.put(name, value);
136: }
137:
138: public String getProtocol() {
139: return protocol;
140: }
141:
142: public String getScheme() {
143: return scheme;
144: }
145:
146: public String getServerName() {
147: return serverName;
148: }
149:
150: public int getServerPort() {
151: return port;
152: }
153:
154: public String getRemoteAddr() {
155: return remoteAddr;
156: }
157:
158: public String getRemoteHost() {
159: return remoteHost;
160: }
161:
162: public Locale getLocale() {
163: return locale;
164: }
165:
166: public void setLocale(Locale loc) {
167: locale = loc;
168: }
169:
170: public Enumeration getLocales() {
171: return Collections.enumeration(Collections
172: .singleton(getLocale()));
173: }
174:
175: public boolean isSecure() {
176: if (scheme == null) {
177: return false;
178: } else {
179: return scheme.equalsIgnoreCase("HTTPS");
180: }
181: }
182:
183: public Cookie[] getCookies() {
184: if (cookies.isEmpty())
185: return null;
186: else {
187: Cookie[] cookieArray = new Cookie[cookies.size()];
188: return (Cookie[]) cookies.values().toArray(cookieArray);
189: }
190: }
191:
192: public Map getCookieMap() {
193: return cookies;
194: }
195:
196: public long getDateHeader(String name) {
197: String s1 = getHeader(name);
198: if (s1 == null)
199: return -1L;
200: try {
201: DateFormat dateFormat = new SimpleDateFormat();
202: return dateFormat.parse(s1).getTime();
203: } catch (ParseException exception) {
204: throw new IllegalArgumentException("Cannot parse date: "
205: + s1);
206: }
207: }
208:
209: public String getHeader(String name) {
210: return (String) headers.get(name);
211: }
212:
213: public Enumeration getHeaders(String name) {
214: throw new AssertionFailedError("Not implemented");
215: }
216:
217: public Enumeration getHeaderNames() {
218: return headers.keys();
219: }
220:
221: public String getMethod() {
222: return method;
223: }
224:
225: public String getPathInfo() {
226: return pathInfo;
227: }
228:
229: public String getPathTranslated() {
230: throw new AssertionFailedError("Not implemented");
231: }
232:
233: public String getContextPath() {
234: return contextPath;
235: }
236:
237: public void setContextPath(String path) {
238: contextPath = path;
239: }
240:
241: public String getQueryString() {
242: return queryString;
243: }
244:
245: public void setQueryString(String string) {
246: queryString = string;
247: }
248:
249: public String getRemoteUser() {
250: return remoteUser;
251: }
252:
253: public Principal getUserPrincipal() {
254: return principal;
255: }
256:
257: public boolean isUserInRole(String role) {
258: return userRole.equals(role);
259: }
260:
261: public String getRequestedSessionId() {
262: return reqSessionId;
263: }
264:
265: public String getRequestURI() {
266: return requestURI;
267: }
268:
269: public void setRequestURI(String uri) {
270: requestURI = uri;
271: }
272:
273: public String getSitemapURI() {
274: return requestURI;
275: }
276:
277: public String getSitemapURIPrefix() {
278: return "";
279: }
280:
281: public String getServletPath() {
282: return servletPath;
283: }
284:
285: public Session getSession(boolean create) {
286: if ((session == null) && (create))
287: this .session = new MockSession();
288: else if ((session != null) && (!(session).isValid())
289: && (create))
290: this .session = new MockSession();
291: if ((session != null) && ((session).isValid()))
292: return this .session;
293: else
294: return null;
295: }
296:
297: public Session getSession() {
298: return getSession(true);
299: }
300:
301: public boolean isRequestedSessionIdValid() {
302: if (session != null) {
303: try {
304: session.getId();
305: return true;
306: } catch (IllegalStateException e) {
307: return false;
308: }
309: } else
310: return false;
311: }
312:
313: public boolean isRequestedSessionIdFromCookie() {
314: return true;
315: }
316:
317: public boolean isRequestedSessionIdFromURL() {
318: return false;
319: }
320:
321: public void reset() {
322: attributes.clear();
323: scheme = null;
324: protocol = "HTTP/1.1";
325: requestURI = null;
326: contextPath = null;
327: servletPath = null;
328: pathInfo = null;
329: queryString = null;
330: method = "GET";
331: contentType = null;
332: locale = null;
333: principal = null;
334: remoteAddr = null;
335: remoteHost = null;
336: remoteUser = null;
337: userRole = null;
338: reqSessionId = null;
339: authType = null;
340: charEncoding = null;
341: serverName = null;
342: port = 80;
343:
344: parameters.clear();
345: headers.clear();
346: }
347:
348: public void setHeader(String key, String value) {
349: this .headers.put(key, value);
350: }
351:
352: public void setMethod(String method) {
353: this .method = method;
354: }
355:
356: public void clearSession() {
357: this.session = null;
358: }
359:
360: }
|