001: package com.mockrunner.mock.web;
002:
003: import java.io.BufferedReader;
004: import java.io.IOException;
005: import java.io.InputStreamReader;
006: import java.io.UnsupportedEncodingException;
007: import java.security.Principal;
008: import java.text.ParseException;
009: import java.text.SimpleDateFormat;
010: import java.util.ArrayList;
011: import java.util.Collections;
012: import java.util.Date;
013: import java.util.Enumeration;
014: import java.util.HashMap;
015: import java.util.List;
016: import java.util.Locale;
017: import java.util.Map;
018: import java.util.Vector;
019:
020: import javax.servlet.RequestDispatcher;
021: import javax.servlet.ServletContext;
022: import javax.servlet.ServletInputStream; //import javax.servlet.ServletRequestAttributeEvent;
023: //import javax.servlet.ServletRequestAttributeListener;
024: import javax.servlet.http.Cookie;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpSession;
027:
028: import com.mockrunner.base.NestedApplicationException;
029: import com.mockrunner.util.common.CaseAwareMap;
030:
031: /**
032: * Mock implementation of <code>HttpServletRequest</code>.
033: */
034: public class MockHttpServletRequest implements HttpServletRequest {
035: private Map attributes;
036: private Map parameters;
037: private Vector locales;
038: private Map requestDispatchers;
039: private HttpSession session;
040: private String method;
041: private String authType;
042: private Map headers;
043: private String contextPath;
044: private String pathInfo;
045: private String pathTranslated;
046: private String queryString;
047: private StringBuffer requestUrl;
048: private String requestUri;
049: private String servletPath;
050: private Principal principal;
051: private String remoteUser;
052: private boolean requestedSessionIdIsFromCookie;
053: private String protocol;
054: private String serverName;
055: private int serverPort;
056: private String scheme;
057: private String remoteHost;
058: private String remoteAddr;
059: private Map roles;
060: private String characterEncoding;
061: private int contentLength;
062: private String contentType;
063: private List cookies;
064: private MockServletInputStream bodyContent;
065: private String localAddr;
066: private String localName;
067: private int localPort;
068: private int remotePort;
069: private boolean sessionCreated;
070:
071: //private List attributeListener;
072:
073: public MockHttpServletRequest() {
074: resetAll();
075: }
076:
077: /**
078: * Resets the state of this object to the default values
079: */
080: public void resetAll() {
081: attributes = new HashMap();
082: parameters = new HashMap();
083: locales = new Vector();
084: requestDispatchers = new HashMap();
085: method = "GET";
086: headers = new CaseAwareMap();
087: requestedSessionIdIsFromCookie = true;
088: protocol = "HTTP/1.1";
089: serverName = "localhost";
090: serverPort = 8080;
091: scheme = "http";
092: remoteHost = "localhost";
093: remoteAddr = "127.0.0.1";
094: roles = new HashMap();
095: contentLength = -1;
096: cookies = new ArrayList();
097: localAddr = "127.0.0.1";
098: localName = "localhost";
099: localPort = 8080;
100: remotePort = 5000;
101: sessionCreated = false;
102: //attributeListener = new ArrayList();
103: bodyContent = new MockServletInputStream(new byte[0]);
104: }
105:
106: /*public void addAttributeListener(ServletRequestAttributeListener listener)
107: {
108: attributeListener.add(listener);
109: }*/
110:
111: public String getParameter(String key) {
112: String[] values = getParameterValues(key);
113: if (null != values && 0 < values.length) {
114: return values[0];
115: }
116: return null;
117: }
118:
119: /**
120: * Clears the parameters.
121: */
122: public void clearParameters() {
123: parameters.clear();
124: }
125:
126: public String[] getParameterValues(String key) {
127: return (String[]) parameters.get(key);
128: }
129:
130: /**
131: * Adds a request multivalue parameter.
132: * @param key the parameter key
133: * @param values the parameters values
134: */
135: public void setupAddParameter(String key, String[] values) {
136: parameters.put(key, values);
137: }
138:
139: /**
140: * Adds a request parameter.
141: * @param key the parameter key
142: * @param value the parameters value
143: */
144: public void setupAddParameter(String key, String value) {
145: setupAddParameter(key, new String[] { value });
146: }
147:
148: public Enumeration getParameterNames() {
149: Vector parameterKeys = new Vector(parameters.keySet());
150: return parameterKeys.elements();
151: }
152:
153: public Map getParameterMap() {
154: return Collections.unmodifiableMap(parameters);
155: }
156:
157: public void clearAttributes() {
158: attributes.clear();
159: }
160:
161: public Object getAttribute(String key) {
162: return attributes.get(key);
163: }
164:
165: public Enumeration getAttributeNames() {
166: Vector attKeys = new Vector(attributes.keySet());
167: return attKeys.elements();
168: }
169:
170: public void removeAttribute(String key) {
171: Object value = attributes.get(key);
172: attributes.remove(key);
173: if (null != value) {
174: //callAttributeListenersRemovedMethod(key, value);
175: }
176: }
177:
178: public void setAttribute(String key, Object value) {
179: Object oldValue = attributes.get(key);
180: if (null == value) {
181: attributes.remove(key);
182: } else {
183: attributes.put(key, value);
184: }
185: //handleAttributeListenerCalls(key, value, oldValue);
186: }
187:
188: public HttpSession getSession() {
189: sessionCreated = true;
190: return session;
191: }
192:
193: public HttpSession getSession(boolean create) {
194: if (!create && !sessionCreated)
195: return null;
196: return getSession();
197: }
198:
199: /**
200: * Sets the <code>HttpSession</code>.
201: * @param session the <code>HttpSession</code>
202: */
203: public void setSession(HttpSession session) {
204: this .session = session;
205: }
206:
207: public RequestDispatcher getRequestDispatcher(String path) {
208: RequestDispatcher dispatcher = (RequestDispatcher) requestDispatchers
209: .get(path);
210: if (null == dispatcher) {
211: dispatcher = new MockRequestDispatcher();
212: setRequestDispatcher(path, dispatcher);
213: }
214: return dispatcher;
215: }
216:
217: /**
218: * Returns the map of <code>RequestDispatcher</code> objects. The specified path
219: * maps to the corresponding <code>RequestDispatcher</code> object.
220: * @return the map of <code>RequestDispatcher</code> objects
221: */
222: public Map getRequestDispatcherMap() {
223: return Collections.unmodifiableMap(requestDispatchers);
224: }
225:
226: /**
227: * Clears the map of <code>RequestDispatcher</code> objects.
228: */
229: public void clearRequestDispatcherMap() {
230: requestDispatchers.clear();
231: }
232:
233: /**
234: * Sets a <code>RequestDispatcher</code> that will be returned when calling
235: * {@link #getRequestDispatcher} with the specified path. If no <code>RequestDispatcher</code>
236: * is set for the specified path, {@link #getRequestDispatcher} automatically creates a
237: * new one.
238: * @param path the path for the <code>RequestDispatcher</code>
239: * @param dispatcher the <code>RequestDispatcher</code> object
240: */
241: public void setRequestDispatcher(String path,
242: RequestDispatcher dispatcher) {
243: if (dispatcher instanceof MockRequestDispatcher) {
244: ((MockRequestDispatcher) dispatcher).setPath(path);
245: }
246: requestDispatchers.put(path, dispatcher);
247: }
248:
249: public Locale getLocale() {
250: if (locales.size() < 1)
251: return Locale.getDefault();
252: return (Locale) locales.get(0);
253: }
254:
255: public Enumeration getLocales() {
256: return locales.elements();
257: }
258:
259: public void addLocale(Locale locale) {
260: locales.add(locale);
261: }
262:
263: public void addLocales(List localeList) {
264: locales.addAll(localeList);
265: }
266:
267: public String getMethod() {
268: return method;
269: }
270:
271: public void setMethod(String method) {
272: this .method = method;
273: }
274:
275: public String getAuthType() {
276: return authType;
277: }
278:
279: public void setAuthType(String authType) {
280: this .authType = authType;
281: }
282:
283: public long getDateHeader(String key) {
284: String header = getHeader(key);
285: if (null == header)
286: return -1;
287: try {
288: Date dateValue = new SimpleDateFormat(
289: WebConstants.DATE_FORMAT_HEADER, Locale.US)
290: .parse(header);
291: return dateValue.getTime();
292: } catch (ParseException exc) {
293: throw new IllegalArgumentException(exc.getMessage());
294: }
295: }
296:
297: public String getHeader(String key) {
298: List headerList = (List) headers.get(key);
299: if (null == headerList || 0 == headerList.size())
300: return null;
301: return (String) headerList.get(0);
302: }
303:
304: public Enumeration getHeaderNames() {
305: return new Vector(headers.keySet()).elements();
306: }
307:
308: public Enumeration getHeaders(String key) {
309: List headerList = (List) headers.get(key);
310: if (null == headerList)
311: return null;
312: return new Vector(headerList).elements();
313: }
314:
315: public int getIntHeader(String key) {
316: String header = getHeader(key);
317: if (null == header)
318: return -1;
319: return new Integer(header).intValue();
320: }
321:
322: public void addHeader(String key, String value) {
323: List valueList = (List) headers.get(key);
324: if (null == valueList) {
325: valueList = new ArrayList();
326: headers.put(key, valueList);
327: }
328: valueList.add(value);
329: }
330:
331: public void setHeader(String key, String value) {
332: List valueList = new ArrayList();
333: headers.put(key, valueList);
334: valueList.add(value);
335: }
336:
337: public void clearHeaders() {
338: headers.clear();
339: }
340:
341: public String getContextPath() {
342: return contextPath;
343: }
344:
345: public void setContextPath(String contextPath) {
346: this .contextPath = contextPath;
347: }
348:
349: public String getPathInfo() {
350: return pathInfo;
351: }
352:
353: public void setPathInfo(String pathInfo) {
354: this .pathInfo = pathInfo;
355: }
356:
357: public String getPathTranslated() {
358: return pathTranslated;
359: }
360:
361: public void setPathTranslated(String pathTranslated) {
362: this .pathTranslated = pathTranslated;
363: }
364:
365: public String getQueryString() {
366: return queryString;
367: }
368:
369: public void setQueryString(String queryString) {
370: this .queryString = queryString;
371: }
372:
373: public String getRequestURI() {
374: return requestUri;
375: }
376:
377: public void setRequestURI(String requestUri) {
378: this .requestUri = requestUri;
379: }
380:
381: public StringBuffer getRequestURL() {
382: return requestUrl;
383: }
384:
385: public void setRequestURL(String requestUrl) {
386: this .requestUrl = new StringBuffer(requestUrl);
387: }
388:
389: public String getServletPath() {
390: return servletPath;
391: }
392:
393: public void setServletPath(String servletPath) {
394: this .servletPath = servletPath;
395: }
396:
397: public Principal getUserPrincipal() {
398: return principal;
399: }
400:
401: public void setUserPrincipal(Principal principal) {
402: this .principal = principal;
403: }
404:
405: public String getRemoteUser() {
406: return remoteUser;
407: }
408:
409: public void setRemoteUser(String remoteUser) {
410: this .remoteUser = remoteUser;
411: }
412:
413: public Cookie[] getCookies() {
414: return (Cookie[]) cookies.toArray(new Cookie[cookies.size()]);
415: }
416:
417: public void addCookie(Cookie cookie) {
418: cookies.add(cookie);
419: }
420:
421: public String getRequestedSessionId() {
422: HttpSession session = getSession();
423: if (null == session)
424: return null;
425: return session.getId();
426: }
427:
428: public boolean isRequestedSessionIdFromCookie() {
429: return requestedSessionIdIsFromCookie;
430: }
431:
432: public boolean isRequestedSessionIdFromUrl() {
433: return isRequestedSessionIdFromURL();
434: }
435:
436: public boolean isRequestedSessionIdFromURL() {
437: return !requestedSessionIdIsFromCookie;
438: }
439:
440: public void setRequestedSessionIdFromCookie(
441: boolean requestedSessionIdIsFromCookie) {
442: this .requestedSessionIdIsFromCookie = requestedSessionIdIsFromCookie;
443: }
444:
445: public boolean isRequestedSessionIdValid() {
446: HttpSession session = getSession();
447: if (null == session)
448: return false;
449: return true;
450: }
451:
452: public boolean isUserInRole(String role) {
453: return ((Boolean) roles.get(role)).booleanValue();
454: }
455:
456: public void setUserInRole(String role, boolean isInRole) {
457: roles.put(role, new Boolean(isInRole));
458: }
459:
460: public String getCharacterEncoding() {
461: return characterEncoding;
462: }
463:
464: public void setCharacterEncoding(String characterEncoding)
465: throws UnsupportedEncodingException {
466: this .characterEncoding = characterEncoding;
467: }
468:
469: public int getContentLength() {
470: return contentLength;
471: }
472:
473: public void setContentLength(int contentLength) {
474: this .contentLength = contentLength;
475: }
476:
477: public String getContentType() {
478: return contentType;
479: }
480:
481: public void setContentType(String contentType) {
482: this .contentType = contentType;
483: }
484:
485: public String getProtocol() {
486: return protocol;
487: }
488:
489: public void setProtocol(String protocol) {
490: this .protocol = protocol;
491: }
492:
493: public String getServerName() {
494: return serverName;
495: }
496:
497: public void setServerName(String serverName) {
498: this .serverName = serverName;
499: }
500:
501: public int getServerPort() {
502: return serverPort;
503: }
504:
505: public void setServerPort(int serverPort) {
506: this .serverPort = serverPort;
507: }
508:
509: public String getScheme() {
510: return scheme;
511: }
512:
513: public void setScheme(String scheme) {
514: this .scheme = scheme;
515: }
516:
517: public String getRemoteAddr() {
518: return remoteAddr;
519: }
520:
521: public void setRemoteAddr(String remoteAddr) {
522: this .remoteAddr = remoteAddr;
523: }
524:
525: public String getRemoteHost() {
526: return remoteHost;
527: }
528:
529: public void setRemoteHost(String remoteHost) {
530: this .remoteHost = remoteHost;
531: }
532:
533: public BufferedReader getReader() throws IOException {
534: return new BufferedReader(new InputStreamReader(bodyContent));
535: }
536:
537: public ServletInputStream getInputStream() throws IOException {
538: return bodyContent;
539: }
540:
541: public void setBodyContent(byte[] data) {
542: bodyContent = new MockServletInputStream(data);
543: }
544:
545: public void setBodyContent(String bodyContent) {
546: String encoding = (null == characterEncoding) ? "ISO-8859-1"
547: : characterEncoding;
548: try {
549: setBodyContent(bodyContent.getBytes(encoding));
550: } catch (UnsupportedEncodingException exc) {
551: throw new NestedApplicationException(exc);
552: }
553: }
554:
555: public String getRealPath(String path) {
556: HttpSession session = getSession();
557: if (null == session)
558: return null;
559: return session.getServletContext().getRealPath(path);
560: }
561:
562: public boolean isSecure() {
563: String scheme = getScheme();
564: if (null == scheme)
565: return false;
566: return scheme.equals("https");
567: }
568:
569: public String getLocalAddr() {
570: return localAddr;
571: }
572:
573: public void setLocalAddr(String localAddr) {
574: this .localAddr = localAddr;
575: }
576:
577: public String getLocalName() {
578: return localName;
579: }
580:
581: public void setLocalName(String localName) {
582: this .localName = localName;
583: }
584:
585: public int getLocalPort() {
586: return localPort;
587: }
588:
589: public void setLocalPort(int localPort) {
590: this .localPort = localPort;
591: }
592:
593: public int getRemotePort() {
594: return remotePort;
595: }
596:
597: public void setRemotePort(int remotePort) {
598: this .remotePort = remotePort;
599: }
600:
601: /*private void handleAttributeListenerCalls(String key, Object value, Object oldValue)
602: {
603: if(null != oldValue)
604: {
605: if(value != null)
606: {
607: callAttributeListenersReplacedMethod(key, oldValue);
608: }
609: else
610: {
611: callAttributeListenersRemovedMethod(key, oldValue);
612: }
613: }
614: else
615: {
616: if(value != null)
617: {
618: callAttributeListenersAddedMethod(key, value);
619: }
620:
621: }
622: }*/
623:
624: /*private void callAttributeListenersAddedMethod(String key, Object value)
625: {
626: for(int ii = 0; ii < attributeListener.size(); ii++)
627: {
628: ServletRequestAttributeEvent event = new ServletRequestAttributeEvent(getServletContext(), this, key, value);
629: ((ServletRequestAttributeListener)attributeListener.get(ii)).attributeAdded(event);
630: }
631: }*/
632:
633: /*private void callAttributeListenersReplacedMethod(String key, Object value)
634: {
635: for(int ii = 0; ii < attributeListener.size(); ii++)
636: {
637: ServletRequestAttributeEvent event = new ServletRequestAttributeEvent(getServletContext(), this, key, value);
638: ((ServletRequestAttributeListener)attributeListener.get(ii)).attributeReplaced(event);
639: }
640: }*/
641:
642: /*private void callAttributeListenersRemovedMethod(String key, Object value)
643: {
644: for(int ii = 0; ii < attributeListener.size(); ii++)
645: {
646: ServletRequestAttributeEvent event = new ServletRequestAttributeEvent(getServletContext(), this, key, value);
647: ((ServletRequestAttributeListener)attributeListener.get(ii)).attributeRemoved(event);
648: }
649: }*/
650:
651: /*private ServletContext getServletContext()
652: {
653: if(null == session) return new MockServletContext();
654: return session.getServletContext();
655: }*/
656: }
|