001: package com.ecyrd.jspwiki;
002:
003: import java.io.BufferedReader;
004: import java.io.IOException;
005: import java.io.UnsupportedEncodingException;
006: import java.security.Principal;
007: import java.util.Enumeration;
008: import java.util.HashSet;
009: import java.util.Hashtable;
010: import java.util.Locale;
011: import java.util.Map;
012: import java.util.Set;
013:
014: import javax.servlet.RequestDispatcher;
015: import javax.servlet.ServletInputStream;
016: import javax.servlet.http.Cookie;
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpSession;
019:
020: /**
021: * A mock HttpServletRequest object, used for testing. The following methods
022: * work as they should: {@link #getCookies()},{@link #getUserPrincipal()},
023: * {@link #getRemoteUser()},{@link #getRemoteAddr()},
024: * {@link #isUserInRole(String)},{@link #getSession()},
025: * {@link #getSession(boolean)}. All others either return null, or don't work
026: * they way they should.
027: * @author Andrew R. Jaquith
028: */
029: public class TestHttpServletRequest implements HttpServletRequest {
030:
031: protected Cookie[] m_cookies = new Cookie[0];
032:
033: protected Map m_params = new Hashtable();
034:
035: protected String m_remoteAddr = "127.0.0.1";
036:
037: protected String m_remoteUser = null;
038:
039: protected Set m_roles = new HashSet();
040:
041: protected Principal m_userPrincipal = null;
042:
043: /** Everything to right of servlet root */
044: protected String m_servletPath = "/";
045:
046: protected HttpSession m_session = null;
047:
048: /**
049: * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
050: */
051: public Object getAttribute(String arg0) {
052: return null;
053: }
054:
055: /**
056: * @see javax.servlet.ServletRequest#getAttributeNames()
057: */
058: public Enumeration getAttributeNames() {
059: return null;
060: }
061:
062: /**
063: * @see javax.servlet.http.HttpServletRequest#getAuthType()
064: */
065: public String getAuthType() {
066: return null;
067: }
068:
069: /**
070: * @see javax.servlet.ServletRequest#getCharacterEncoding()
071: */
072: public String getCharacterEncoding() {
073: return null;
074: }
075:
076: /**
077: * @see javax.servlet.ServletRequest#getContentLength()
078: */
079: public int getContentLength() {
080: return 0;
081: }
082:
083: /**
084: * @see javax.servlet.ServletRequest#getContentType()
085: */
086: public String getContentType() {
087: return null;
088: }
089:
090: /**
091: * @see javax.servlet.http.HttpServletRequest#getContextPath()
092: */
093: public String getContextPath() {
094: return null;
095: }
096:
097: /**
098: * @see javax.servlet.http.HttpServletRequest#getCookies()
099: */
100: public Cookie[] getCookies() {
101: return m_cookies;
102: }
103:
104: /**
105: * @see javax.servlet.http.HttpServletRequest#getDateHeader(java.lang.String)
106: */
107: public long getDateHeader(String arg0) {
108: return 0;
109: }
110:
111: /**
112: * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
113: */
114: public String getHeader(String arg0) {
115: return null;
116: }
117:
118: /**
119: * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
120: */
121: public Enumeration getHeaderNames() {
122: return null;
123: }
124:
125: /**
126: * @see javax.servlet.http.HttpServletRequest#getHeaders(java.lang.String)
127: */
128: public Enumeration getHeaders(String arg0) {
129: return null;
130: }
131:
132: /**
133: * @see javax.servlet.ServletRequest#getInputStream()
134: */
135: public ServletInputStream getInputStream() throws IOException {
136: return null;
137: }
138:
139: /**
140: * @see javax.servlet.http.HttpServletRequest#getIntHeader(java.lang.String)
141: */
142: public int getIntHeader(String arg0) {
143: return 0;
144: }
145:
146: /**
147: * @see javax.servlet.ServletRequest#getLocale()
148: */
149: public Locale getLocale() {
150: return Locale.ENGLISH;
151: }
152:
153: /**
154: * @see javax.servlet.ServletRequest#getLocales()
155: */
156: public Enumeration getLocales() {
157: return null;
158: }
159:
160: /**
161: * @see javax.servlet.http.HttpServletRequest#getMethod()
162: */
163: public String getMethod() {
164: return null;
165: }
166:
167: /**
168: * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
169: */
170: public String getParameter(String arg0) {
171: return (String) m_params.get(arg0);
172: }
173:
174: /**
175: * @see javax.servlet.ServletRequest#getParameterMap()
176: */
177: public Map getParameterMap() {
178: return m_params;
179: }
180:
181: /**
182: * @see javax.servlet.ServletRequest#getParameterNames()
183: */
184: public Enumeration getParameterNames() {
185: return ((Hashtable) m_params).keys();
186: }
187:
188: /**
189: * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
190: */
191: public String[] getParameterValues(String arg0) {
192: return (String[]) m_params.entrySet().toArray(
193: new String[m_params.size()]);
194: }
195:
196: /**
197: * @see javax.servlet.http.HttpServletRequest#getPathInfo()
198: */
199: public String getPathInfo() {
200: return null;
201: }
202:
203: /**
204: * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
205: */
206: public String getPathTranslated() {
207: return null;
208: }
209:
210: /**
211: * @see javax.servlet.ServletRequest#getProtocol()
212: */
213: public String getProtocol() {
214: return null;
215: }
216:
217: /**
218: * @see javax.servlet.http.HttpServletRequest#getQueryString()
219: */
220: public String getQueryString() {
221: return null;
222: }
223:
224: /**
225: * @see javax.servlet.ServletRequest#getReader()
226: */
227: public BufferedReader getReader() throws IOException {
228: return null;
229: }
230:
231: /**
232: * @see javax.servlet.ServletRequest#getRealPath(java.lang.String)
233: * @deprecated
234: */
235: public String getRealPath(String arg0) {
236: return null;
237: }
238:
239: /**
240: * @see javax.servlet.ServletRequest#getRemoteAddr()
241: */
242: public String getRemoteAddr() {
243: return m_remoteAddr;
244: }
245:
246: /**
247: * @see javax.servlet.ServletRequest#getRemoteHost()
248: */
249: public String getRemoteHost() {
250: return null;
251: }
252:
253: /**
254: * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
255: */
256: public String getRemoteUser() {
257: return m_remoteUser;
258: }
259:
260: /**
261: * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
262: */
263: public RequestDispatcher getRequestDispatcher(String arg0) {
264: return null;
265: }
266:
267: /**
268: * @see javax.servlet.http.HttpServletRequest#getRequestedSessionId()
269: */
270: public String getRequestedSessionId() {
271: return null;
272: }
273:
274: /**
275: * @see javax.servlet.http.HttpServletRequest#getRequestURI()
276: */
277: public String getRequestURI() {
278: return null;
279: }
280:
281: /**
282: * @see javax.servlet.http.HttpServletRequest#getRequestURL()
283: */
284: public StringBuffer getRequestURL() {
285: return null;
286: }
287:
288: /**
289: * @see javax.servlet.ServletRequest#getScheme()
290: */
291: public String getScheme() {
292: return null;
293: }
294:
295: /**
296: * @see javax.servlet.ServletRequest#getServerName()
297: */
298: public String getServerName() {
299: return null;
300: }
301:
302: /**
303: * @see javax.servlet.ServletRequest#getServerPort()
304: */
305: public int getServerPort() {
306: return 0;
307: }
308:
309: /**
310: * @see javax.servlet.http.HttpServletRequest#getServletPath()
311: */
312: public String getServletPath() {
313: return m_servletPath;
314: }
315:
316: /**
317: * @see javax.servlet.http.HttpServletRequest#getSession()
318: */
319: public HttpSession getSession() {
320: return getSession(true);
321: }
322:
323: /**
324: * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
325: */
326: public HttpSession getSession(boolean arg0) {
327: if (arg0 && m_session == null) {
328: m_session = new TestHttpSession();
329: }
330: return m_session;
331: }
332:
333: /**
334: * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
335: */
336: public Principal getUserPrincipal() {
337: return m_userPrincipal;
338: }
339:
340: /**
341: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromCookie()
342: */
343: public boolean isRequestedSessionIdFromCookie() {
344: return false;
345: }
346:
347: /**
348: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromUrl()
349: * @deprecated
350: */
351: public boolean isRequestedSessionIdFromUrl() {
352: return false;
353: }
354:
355: /**
356: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromURL()
357: */
358: public boolean isRequestedSessionIdFromURL() {
359: return false;
360: }
361:
362: /**
363: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdValid()
364: */
365: public boolean isRequestedSessionIdValid() {
366: return false;
367: }
368:
369: /**
370: * @see javax.servlet.ServletRequest#isSecure()
371: */
372: public boolean isSecure() {
373: return false;
374: }
375:
376: /**
377: * @see javax.servlet.http.HttpServletRequest#isUserInRole(java.lang.String)
378: */
379: public boolean isUserInRole(String arg0) {
380: return m_roles.contains(arg0);
381: }
382:
383: /**
384: * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
385: */
386: public void removeAttribute(String arg0) {
387: }
388:
389: /**
390: * @see javax.servlet.ServletRequest#setAttribute(java.lang.String,
391: * java.lang.Object)
392: */
393: public void setAttribute(String arg0, Object arg1) {
394: }
395:
396: /**
397: * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
398: */
399: public void setCharacterEncoding(String arg0)
400: throws UnsupportedEncodingException {
401: }
402:
403: /**
404: * @param cookies The cookies to set.
405: */
406: public void setCookies(Cookie[] cookies) {
407: m_cookies = cookies;
408: }
409:
410: public void setParameter(String key, String value) {
411: m_params.put(key, value);
412: }
413:
414: /**
415: * @param remoteAddr The remoteAddr to set.
416: */
417: public void setRemoteAddr(String remoteAddr) {
418: m_remoteAddr = remoteAddr;
419: }
420:
421: /**
422: * @param remoteUser The remoteUser to set.
423: */
424: public void setRemoteUser(String remoteUser) {
425: m_remoteUser = remoteUser;
426: }
427:
428: /**
429: * Sets the roles that this request's user should be considered a member of.
430: * The
431: * @param roles The roles to set.
432: */
433: public void setRoles(String[] roles) {
434: for (int i = 0; i < roles.length; i++) {
435: m_roles.add(roles[i]);
436: }
437: }
438:
439: /**
440: * Sets the servlet path; e.g., /JSPWiki/Wiki.jsp.
441: * @param path the servlet path
442: */
443: public void setServletPath(String path) {
444: m_servletPath = path;
445: }
446:
447: /**
448: * @param userPrincipal The userPrincipal to set.
449: */
450: public void setUserPrincipal(Principal principal) {
451: m_userPrincipal = principal;
452: }
453:
454: public int getRemotePort() {
455: // TODO Auto-generated method stub
456: return 0;
457: }
458:
459: public String getLocalName() {
460: // TODO Auto-generated method stub
461: return null;
462: }
463:
464: public String getLocalAddr() {
465: // TODO Auto-generated method stub
466: return null;
467: }
468:
469: public int getLocalPort() {
470: // TODO Auto-generated method stub
471: return 0;
472: }
473:
474: }
|