001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting.util;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.io.StringReader;
021: import java.io.UnsupportedEncodingException;
022: import java.security.Principal;
023: import java.util.Arrays;
024: import java.util.Collections;
025: import java.util.Enumeration;
026: import java.util.HashMap;
027: import java.util.Locale;
028: import java.util.Map;
029:
030: import javax.servlet.RequestDispatcher;
031: import javax.servlet.ServletException;
032: import javax.servlet.ServletInputStream;
033: import javax.servlet.ServletRequest;
034: import javax.servlet.ServletResponse;
035: import javax.servlet.http.Cookie;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpSession;
038:
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041:
042: /**
043: * For the benefit of anyone that wants to create a fake HttpServletRequest
044: * that doesn't do anything other than not be null.
045: * @author Joe Walker [joe at getahead dot ltd dot uk]
046: */
047: public class FakeHttpServletRequest implements HttpServletRequest {
048: /* (non-Javadoc)
049: * @see javax.servlet.http.HttpServletRequest#getAuthType()
050: */
051: public String getAuthType() {
052: return null;
053: }
054:
055: /* (non-Javadoc)
056: * @see javax.servlet.http.HttpServletRequest#getCookies()
057: */
058: public Cookie[] getCookies() {
059: return null;
060: }
061:
062: /* (non-Javadoc)
063: * @see javax.servlet.http.HttpServletRequest#getDateHeader(java.lang.String)
064: */
065: public long getDateHeader(String name) {
066: return -1;
067: }
068:
069: /* (non-Javadoc)
070: * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
071: */
072: public String getHeader(String name) {
073: return null;
074: }
075:
076: /* (non-Javadoc)
077: * @see javax.servlet.http.HttpServletRequest#getHeaders(java.lang.String)
078: */
079: public Enumeration<String[]> getHeaders(String name) {
080: return Collections.enumeration(Collections
081: .<String[]> emptySet());
082: }
083:
084: /* (non-Javadoc)
085: * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
086: */
087: public Enumeration<String> getHeaderNames() {
088: return Collections.enumeration(Collections.<String> emptySet());
089: }
090:
091: /* (non-Javadoc)
092: * @see javax.servlet.http.HttpServletRequest#getIntHeader(java.lang.String)
093: */
094: public int getIntHeader(String name) {
095: return -1;
096: }
097:
098: /* (non-Javadoc)
099: * @see javax.servlet.http.HttpServletRequest#getMethod()
100: */
101: public String getMethod() {
102: log
103: .warn("Inventing data in FakeHttpServletRequest.getMethod() to remain plausible.");
104: return "GET";
105: }
106:
107: /* (non-Javadoc)
108: * @see javax.servlet.http.HttpServletRequest#getPathInfo()
109: */
110: public String getPathInfo() {
111: return null;
112: }
113:
114: /* (non-Javadoc)
115: * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
116: */
117: public String getPathTranslated() {
118: return null;
119: }
120:
121: /* (non-Javadoc)
122: * @see javax.servlet.http.HttpServletRequest#getContextPath()
123: */
124: public String getContextPath() {
125: log
126: .warn("Inventing data in FakeHttpServletRequest.getContextPath() to remain plausible.");
127: return "";
128: }
129:
130: /* (non-Javadoc)
131: * @see javax.servlet.http.HttpServletRequest#getQueryString()
132: */
133: public String getQueryString() {
134: return null;
135: }
136:
137: /* (non-Javadoc)
138: * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
139: */
140: public String getRemoteUser() {
141: return null;
142: }
143:
144: /* (non-Javadoc)
145: * @see javax.servlet.http.HttpServletRequest#isUserInRole(java.lang.String)
146: */
147: public boolean isUserInRole(String role) {
148: return false;
149: }
150:
151: /* (non-Javadoc)
152: * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
153: */
154: public Principal getUserPrincipal() {
155: return null;
156: }
157:
158: /* (non-Javadoc)
159: * @see javax.servlet.http.HttpServletRequest#getRequestedSessionId()
160: */
161: public String getRequestedSessionId() {
162: return null;
163: }
164:
165: /* (non-Javadoc)
166: * @see javax.servlet.http.HttpServletRequest#getRequestURI()
167: */
168: public String getRequestURI() {
169: log
170: .warn("Inventing data in FakeHttpServletRequest.getRequestURI() to remain plausible.");
171: return "/";
172: }
173:
174: /* (non-Javadoc)
175: * @see javax.servlet.http.HttpServletRequest#getRequestURL()
176: */
177: public StringBuffer getRequestURL() {
178: log
179: .warn("Inventing data in FakeHttpServletRequest.getRequestURL() to remain plausible.");
180: return new StringBuffer("http://localhost/");
181: }
182:
183: /* (non-Javadoc)
184: * @see javax.servlet.http.HttpServletRequest#getServletPath()
185: */
186: public String getServletPath() {
187: log
188: .warn("Inventing data in FakeHttpServletRequest.getServletPath() to remain plausible.");
189: return "";
190: }
191:
192: /* (non-Javadoc)
193: * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
194: */
195: public HttpSession getSession(boolean create) {
196: if (!create) {
197: return null;
198: }
199:
200: return new FakeHttpSession();
201: }
202:
203: /* (non-Javadoc)
204: * @see javax.servlet.http.HttpServletRequest#getSession()
205: */
206: public HttpSession getSession() {
207: return null;
208: }
209:
210: /* (non-Javadoc)
211: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdValid()
212: */
213: public boolean isRequestedSessionIdValid() {
214: return false;
215: }
216:
217: /* (non-Javadoc)
218: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromCookie()
219: */
220: public boolean isRequestedSessionIdFromCookie() {
221: return false;
222: }
223:
224: /* (non-Javadoc)
225: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromURL()
226: */
227: public boolean isRequestedSessionIdFromURL() {
228: return false;
229: }
230:
231: /**
232: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromUrl()
233: * @deprecated
234: */
235: @Deprecated
236: public boolean isRequestedSessionIdFromUrl() {
237: return false;
238: }
239:
240: /* (non-Javadoc)
241: * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
242: */
243: public Object getAttribute(String name) {
244: return attributes.get(name);
245: }
246:
247: /* (non-Javadoc)
248: * @see javax.servlet.ServletRequest#getAttributeNames()
249: */
250: public Enumeration<String> getAttributeNames() {
251: return Collections.enumeration(attributes.keySet());
252: }
253:
254: /* (non-Javadoc)
255: * @see javax.servlet.ServletRequest#getCharacterEncoding()
256: */
257: public String getCharacterEncoding() {
258: return characterEncoding;
259: }
260:
261: /* (non-Javadoc)
262: * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
263: */
264: public void setCharacterEncoding(String characterEncoding)
265: throws UnsupportedEncodingException {
266: this .characterEncoding = characterEncoding;
267: }
268:
269: /* (non-Javadoc)
270: * @see javax.servlet.ServletRequest#getContentLength()
271: */
272: public int getContentLength() {
273: return 0;
274: }
275:
276: /* (non-Javadoc)
277: * @see javax.servlet.ServletRequest#getContentType()
278: */
279: public String getContentType() {
280: log
281: .warn("Inventing data in FakeHttpServletRequest.getContentType() to remain plausible.");
282: return "text/plain";
283: }
284:
285: /* (non-Javadoc)
286: * @see javax.servlet.ServletRequest#getInputStream()
287: */
288: public ServletInputStream getInputStream() throws IOException {
289: return new ServletInputStream() {
290: /* (non-Javadoc)
291: * @see java.io.InputStream#read()
292: */
293: @Override
294: public int read() throws IOException {
295: return -1;
296: }
297: };
298: }
299:
300: /**
301: * @return "127.0.0.1"
302: */
303: public String getLocalAddr() {
304: log
305: .warn("Inventing data in FakeHttpServletRequest.getLocalAddr() to remain plausible.");
306: return "127.0.0.1";
307: }
308:
309: /**
310: * @return "localhost"
311: */
312: public String getLocalName() {
313: log
314: .warn("Inventing data in FakeHttpServletRequest.getLocalName() to remain plausible.");
315: return "localhost";
316: }
317:
318: /**
319: * @return 80
320: */
321: public int getLocalPort() {
322: log
323: .warn("Inventing data in FakeHttpServletRequest.getLocalPort() to remain plausible.");
324: return 80;
325: }
326:
327: /* (non-Javadoc)
328: * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
329: */
330: public String getParameter(String name) {
331: return null;
332: }
333:
334: /* (non-Javadoc)
335: * @see javax.servlet.ServletRequest#getParameterNames()
336: */
337: public Enumeration<String> getParameterNames() {
338: return Collections.enumeration(Collections.<String> emptySet());
339: }
340:
341: /* (non-Javadoc)
342: * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
343: */
344: public String[] getParameterValues(String name) {
345: return null;
346: }
347:
348: /* (non-Javadoc)
349: * @see javax.servlet.ServletRequest#getParameterMap()
350: */
351: public Map<String, String> getParameterMap() {
352: return Collections.emptyMap();
353: }
354:
355: /* (non-Javadoc)
356: * @see javax.servlet.ServletRequest#getProtocol()
357: */
358: public String getProtocol() {
359: log
360: .warn("Inventing data in FakeHttpServletRequest.getProtocol() to remain plausible.");
361: return "HTTP/1.1";
362: }
363:
364: /* (non-Javadoc)
365: * @see javax.servlet.ServletRequest#getScheme()
366: */
367: public String getScheme() {
368: log
369: .warn("Inventing data in FakeHttpServletRequest.getScheme() to remain plausible.");
370: return "http";
371: }
372:
373: /* (non-Javadoc)
374: * @see javax.servlet.ServletRequest#getServerName()
375: */
376: public String getServerName() {
377: log
378: .warn("Inventing data in FakeHttpServletRequest.getServerName() to remain plausible.");
379: return "localhost";
380: }
381:
382: /* (non-Javadoc)
383: * @see javax.servlet.ServletRequest#getServerPort()
384: */
385: public int getServerPort() {
386: log
387: .warn("Inventing data in FakeHttpServletRequest.getServerPort() to remain plausible.");
388: return 80;
389: }
390:
391: /* (non-Javadoc)
392: * @see javax.servlet.ServletRequest#getReader()
393: */
394: public BufferedReader getReader() throws IOException {
395: return new BufferedReader(new StringReader(""));
396: }
397:
398: /* (non-Javadoc)
399: * @see javax.servlet.ServletRequest#getRemoteAddr()
400: */
401: public String getRemoteAddr() {
402: log
403: .warn("Inventing data in FakeHttpServletRequest.getRemoteAddr() to remain plausible.");
404: return "localhost";
405: }
406:
407: /* (non-Javadoc)
408: * @see javax.servlet.ServletRequest#getRemoteHost()
409: */
410: public String getRemoteHost() {
411: log
412: .warn("Inventing data in FakeHttpServletRequest.getRemoteHost() to remain plausible.");
413: return "localhost";
414: }
415:
416: /**
417: * @return 80
418: */
419: public int getRemotePort() {
420: log
421: .warn("Inventing data in FakeHttpServletRequest.getRemotePort() to remain plausible.");
422: return 80;
423: }
424:
425: /* (non-Javadoc)
426: * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object)
427: */
428: public void setAttribute(String name, Object o) {
429: attributes.put(name, o);
430: }
431:
432: /* (non-Javadoc)
433: * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
434: */
435: public void removeAttribute(String name) {
436: attributes.remove(name);
437: }
438:
439: /* (non-Javadoc)
440: * @see javax.servlet.ServletRequest#getLocale()
441: */
442: public Locale getLocale() {
443: return Locale.getDefault();
444: }
445:
446: /* (non-Javadoc)
447: * @see javax.servlet.ServletRequest#getLocales()
448: */
449: public Enumeration<Locale> getLocales() {
450: return Collections.enumeration(Arrays.asList(Locale
451: .getDefault()));
452: }
453:
454: /* (non-Javadoc)
455: * @see javax.servlet.ServletRequest#isSecure()
456: */
457: public boolean isSecure() {
458: return false;
459: }
460:
461: /* (non-Javadoc)
462: * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
463: */
464: public RequestDispatcher getRequestDispatcher(String path) {
465: return new RequestDispatcher() {
466: /* (non-Javadoc)
467: * @see javax.servlet.RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
468: */
469: public void include(ServletRequest request,
470: ServletResponse response) throws ServletException,
471: IOException {
472: }
473:
474: /* (non-Javadoc)
475: * @see javax.servlet.RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
476: */
477: public void forward(ServletRequest request,
478: ServletResponse response) throws ServletException,
479: IOException {
480: }
481: };
482: }
483:
484: /**
485: * @see javax.servlet.ServletRequest#getRealPath(java.lang.String)
486: * @deprecated
487: */
488: @Deprecated
489: public String getRealPath(String path) {
490: return null;
491: }
492:
493: /**
494: * The character encoding in the supposed request
495: */
496: private String characterEncoding = null;
497:
498: /**
499: * The list of attributes
500: */
501: private Map<String, Object> attributes = new HashMap<String, Object>();
502:
503: /**
504: * The log stream
505: */
506: private static final Log log = LogFactory
507: .getLog(FakeHttpServletRequest.class);
508: }
|