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.pluto.driver.url.impl;
018:
019: import java.io.BufferedReader;
020: import java.io.IOException;
021: import java.io.UnsupportedEncodingException;
022: import java.security.Principal;
023: import java.util.ArrayList;
024: import java.util.Collections;
025: import java.util.Enumeration;
026: import java.util.HashMap;
027: import java.util.List;
028: import java.util.Locale;
029: import java.util.Map;
030: import java.util.Vector;
031:
032: import javax.servlet.RequestDispatcher;
033: import javax.servlet.ServletInputStream;
034: import javax.servlet.http.Cookie;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpSession;
037:
038: /**
039: * Copied from mockrunner 0.3.8
040: * Mock implementation of <code>HttpServletRequest</code>.
041: */
042: public class MockHttpServletRequest implements HttpServletRequest {
043: private Map attributes;
044: private Map parameters;
045: private Vector locales;
046: private Map requestDispatchers;
047: private HttpSession session;
048: private String method;
049: private String authType;
050: private Map headers;
051: private String contextPath;
052: private String pathInfo;
053: private String pathTranslated;
054: private String queryString;
055: private StringBuffer requestUrl;
056: private String requestUri;
057: private String servletPath;
058: private Principal principal;
059: private String remoteUser;
060: private boolean requestedSessionIdIsFromCookie;
061: private String protocol;
062: private String serverName;
063: private int serverPort;
064: private String scheme;
065: private String remoteHost;
066: private String remoteAddr;
067: private Map roles;
068: private String characterEncoding;
069: private int contentLength;
070: private String contentType;
071: private List cookies;
072: private String localAddr;
073: private String localName;
074: private int localPort;
075: private int remotePort;
076: private boolean sessionCreated;
077:
078: public MockHttpServletRequest() {
079: resetAll();
080: }
081:
082: /**
083: * Resets the state of this object to the default values
084: */
085: public void resetAll() {
086: this .attributes = new HashMap();
087: this .parameters = new HashMap();
088: this .locales = new Vector();
089: this .requestDispatchers = new HashMap();
090: this .method = "GET";
091: headers = new HashMap();
092: requestedSessionIdIsFromCookie = true;
093: protocol = "HTTP/1.1";
094: serverName = "localhost";
095: serverPort = 8080;
096: scheme = "http";
097: remoteHost = "localhost";
098: remoteAddr = "127.0.0.1";
099: roles = new HashMap();
100: contentLength = -1;
101: cookies = new ArrayList();
102: localAddr = "127.0.0.1";
103: localName = "localhost";
104: localPort = 8080;
105: remotePort = 5000;
106: sessionCreated = false;
107: }
108:
109: public String getParameter(String key) {
110: String[] values = getParameterValues(key);
111: if (null != values && 0 < values.length) {
112: return values[0];
113: }
114: return null;
115: }
116:
117: public void clearParameters() {
118: parameters.clear();
119: }
120:
121: public String[] getParameterValues(String key) {
122: return (String[]) parameters.get(key);
123: }
124:
125: public void setupAddParameter(String key, String[] values) {
126: parameters.put(key, values);
127: }
128:
129: public void setupAddParameter(String key, String value) {
130: setupAddParameter(key, new String[] { value });
131: }
132:
133: public Enumeration getParameterNames() {
134: Vector parameterKeys = new Vector(parameters.keySet());
135: return parameterKeys.elements();
136: }
137:
138: public Map getParameterMap() {
139: return Collections.unmodifiableMap(parameters);
140: }
141:
142: public void clearAttributes() {
143: attributes.clear();
144: }
145:
146: public Object getAttribute(String key) {
147: return attributes.get(key);
148: }
149:
150: public Enumeration getAttributeNames() {
151: Vector attKeys = new Vector(attributes.keySet());
152: return attKeys.elements();
153: }
154:
155: public void removeAttribute(String key) {
156: Object value = attributes.get(key);
157: attributes.remove(key);
158: if (null != value) {
159: //callAttributeListenersRemovedMethod(key, value);
160: }
161: }
162:
163: public void setAttribute(String key, Object value) {
164: Object oldValue = attributes.get(key);
165: if (null == value) {
166: attributes.remove(key);
167: } else {
168: attributes.put(key, value);
169: }
170: //handleAttributeListenerCalls(key, value, oldValue);
171: }
172:
173: public HttpSession getSession() {
174: sessionCreated = true;
175: return session;
176: }
177:
178: public HttpSession getSession(boolean create) {
179: if (!create && !sessionCreated)
180: return null;
181: return getSession();
182: }
183:
184: public void setSession(HttpSession session) {
185: this .session = session;
186: }
187:
188: public RequestDispatcher getRequestDispatcher(String path) {
189: return null;
190: }
191:
192: /**
193: * Returns the map of <code>RequestDispatcher</code> objects. The specified path
194: * maps to the corresponding <code>RequestDispatcher</code> object.
195: * @return the map of <code>RequestDispatcher</code> objects
196: */
197: public Map getRequestDispatcherMap() {
198: return Collections.unmodifiableMap(requestDispatchers);
199: }
200:
201: /**
202: * Clears the map of <code>RequestDispatcher</code> objects.
203: */
204: public void clearRequestDispatcherMap() {
205: requestDispatchers.clear();
206: }
207:
208: public void setRequestDispatcher(String path,
209: RequestDispatcher dispatcher) {
210: }
211:
212: public Locale getLocale() {
213: if (locales.size() < 1)
214: return Locale.getDefault();
215: return (Locale) locales.get(0);
216: }
217:
218: public Enumeration getLocales() {
219: return locales.elements();
220: }
221:
222: public void addLocale(Locale locale) {
223: locales.add(locale);
224: }
225:
226: public void addLocales(List localeList) {
227: locales.addAll(localeList);
228: }
229:
230: public String getMethod() {
231: return method;
232: }
233:
234: public void setMethod(String method) {
235: this .method = method;
236: }
237:
238: public String getAuthType() {
239: return authType;
240: }
241:
242: public void setAuthType(String authType) {
243: this .authType = authType;
244: }
245:
246: public long getDateHeader(String key) {
247: return -1;
248: }
249:
250: public String getHeader(String key) {
251: List headerList = (List) headers.get(key);
252: if (null == headerList || 0 == headerList.size())
253: return null;
254: return (String) headerList.get(0);
255: }
256:
257: public Enumeration getHeaderNames() {
258: return new Vector(headers.keySet()).elements();
259: }
260:
261: public Enumeration getHeaders(String key) {
262: List headerList = (List) headers.get(key);
263: if (null == headerList)
264: return null;
265: return new Vector(headerList).elements();
266: }
267:
268: public int getIntHeader(String key) {
269: String header = getHeader(key);
270: if (null == header)
271: return -1;
272: return new Integer(header).intValue();
273: }
274:
275: public void addHeader(String key, String value) {
276: List valueList = (List) headers.get(key);
277: if (null == valueList) {
278: valueList = new ArrayList();
279: headers.put(key, valueList);
280: }
281: valueList.add(value);
282: }
283:
284: public void setHeader(String key, String value) {
285: List valueList = new ArrayList();
286: headers.put(key, valueList);
287: valueList.add(value);
288: }
289:
290: public void clearHeaders() {
291: headers.clear();
292: }
293:
294: public String getContextPath() {
295: return contextPath;
296: }
297:
298: public void setContextPath(String contextPath) {
299: this .contextPath = contextPath;
300: }
301:
302: public String getPathInfo() {
303: return pathInfo;
304: }
305:
306: public void setPathInfo(String pathInfo) {
307: this .pathInfo = pathInfo;
308: }
309:
310: public String getPathTranslated() {
311: return pathTranslated;
312: }
313:
314: public void setPathTranslated(String pathTranslated) {
315: this .pathTranslated = pathTranslated;
316: }
317:
318: public String getQueryString() {
319: return queryString;
320: }
321:
322: public void setQueryString(String queryString) {
323: this .queryString = queryString;
324: }
325:
326: public String getRequestURI() {
327: return requestUri;
328: }
329:
330: public void setRequestURI(String requestUri) {
331: this .requestUri = requestUri;
332: }
333:
334: public StringBuffer getRequestURL() {
335: return requestUrl;
336: }
337:
338: public void setRequestURL(String requestUrl) {
339: this .requestUrl = new StringBuffer(requestUrl);
340: }
341:
342: public String getServletPath() {
343: return servletPath;
344: }
345:
346: public void setServletPath(String servletPath) {
347: this .servletPath = servletPath;
348: }
349:
350: public Principal getUserPrincipal() {
351: return principal;
352: }
353:
354: public void setUserPrincipal(Principal principal) {
355: this .principal = principal;
356: }
357:
358: public String getRemoteUser() {
359: return remoteUser;
360: }
361:
362: public void setRemoteUser(String remoteUser) {
363: this .remoteUser = remoteUser;
364: }
365:
366: public Cookie[] getCookies() {
367: return (Cookie[]) cookies.toArray(new Cookie[cookies.size()]);
368: }
369:
370: public void addCookie(Cookie cookie) {
371: cookies.add(cookie);
372: }
373:
374: public String getRequestedSessionId() {
375: HttpSession session = getSession();
376: if (null == session)
377: return null;
378: return session.getId();
379: }
380:
381: public boolean isRequestedSessionIdFromCookie() {
382: return requestedSessionIdIsFromCookie;
383: }
384:
385: public boolean isRequestedSessionIdFromUrl() {
386: return isRequestedSessionIdFromURL();
387: }
388:
389: public boolean isRequestedSessionIdFromURL() {
390: return !requestedSessionIdIsFromCookie;
391: }
392:
393: public void setRequestedSessionIdFromCookie(
394: boolean requestedSessionIdIsFromCookie) {
395: this .requestedSessionIdIsFromCookie = requestedSessionIdIsFromCookie;
396: }
397:
398: public boolean isRequestedSessionIdValid() {
399: HttpSession session = getSession();
400: if (null == session)
401: return false;
402: return true;
403: }
404:
405: public boolean isUserInRole(String role) {
406: return ((Boolean) roles.get(role)).booleanValue();
407: }
408:
409: public void setUserInRole(String role, boolean isInRole) {
410: roles.put(role, new Boolean(isInRole));
411: }
412:
413: public String getCharacterEncoding() {
414: return characterEncoding;
415: }
416:
417: public void setCharacterEncoding(String characterEncoding)
418: throws UnsupportedEncodingException {
419: this .characterEncoding = characterEncoding;
420: }
421:
422: public int getContentLength() {
423: return contentLength;
424: }
425:
426: public void setContentLength(int contentLength) {
427: this .contentLength = contentLength;
428: }
429:
430: public String getContentType() {
431: return contentType;
432: }
433:
434: public void setContentType(String contentType) {
435: this .contentType = contentType;
436: }
437:
438: public String getProtocol() {
439: return protocol;
440: }
441:
442: public void setProtocol(String protocol) {
443: this .protocol = protocol;
444: }
445:
446: public String getServerName() {
447: return serverName;
448: }
449:
450: public void setServerName(String serverName) {
451: this .serverName = serverName;
452: }
453:
454: public int getServerPort() {
455: return serverPort;
456: }
457:
458: public void setServerPort(int serverPort) {
459: this .serverPort = serverPort;
460: }
461:
462: public String getScheme() {
463: return scheme;
464: }
465:
466: public void setScheme(String scheme) {
467: this .scheme = scheme;
468: }
469:
470: public String getRemoteAddr() {
471: return remoteAddr;
472: }
473:
474: public void setRemoteAddr(String remoteAddr) {
475: this .remoteAddr = remoteAddr;
476: }
477:
478: public String getRemoteHost() {
479: return remoteHost;
480: }
481:
482: public void setRemoteHost(String remoteHost) {
483: this .remoteHost = remoteHost;
484: }
485:
486: public BufferedReader getReader() throws IOException {
487: return null;
488: }
489:
490: public ServletInputStream getInputStream() throws IOException {
491: return null;
492: }
493:
494: public void setBodyContent(byte[] data) {
495: }
496:
497: public void setBodyContent(String bodyContent) {
498: }
499:
500: public String getRealPath(String path) {
501: HttpSession session = getSession();
502: if (null == session)
503: return null;
504: return session.getServletContext().getRealPath(path);
505: }
506:
507: public boolean isSecure() {
508: String scheme = getScheme();
509: if (null == scheme)
510: return false;
511: return scheme.equals("https");
512: }
513:
514: public String getLocalAddr() {
515: return localAddr;
516: }
517:
518: public void setLocalAddr(String localAddr) {
519: this .localAddr = localAddr;
520: }
521:
522: public String getLocalName() {
523: return localName;
524: }
525:
526: public void setLocalName(String localName) {
527: this .localName = localName;
528: }
529:
530: public int getLocalPort() {
531: return localPort;
532: }
533:
534: public void setLocalPort(int localPort) {
535: this .localPort = localPort;
536: }
537:
538: public int getRemotePort() {
539: return remotePort;
540: }
541:
542: public void setRemotePort(int remotePort) {
543: this.remotePort = remotePort;
544: }
545: }
|