001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
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.apache.commons.chain.web.servlet;
017:
018: import org.apache.commons.chain.web.MockEnumeration;
019: import org.apache.commons.chain.web.MockPrincipal;
020:
021: import javax.servlet.RequestDispatcher;
022: import javax.servlet.ServletInputStream;
023: import javax.servlet.http.Cookie;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpSession;
026: import java.io.BufferedReader;
027: import java.security.Principal;
028: import java.util.*;
029:
030: // Mock Object for HttpServletRequest (Version 2.3)
031: public class MockHttpServletRequest implements HttpServletRequest {
032:
033: public MockHttpServletRequest() {
034: super ();
035: }
036:
037: public MockHttpServletRequest(HttpSession session) {
038: super ();
039: setHttpSession(session);
040: }
041:
042: public MockHttpServletRequest(String contextPath,
043: String servletPath, String pathInfo, String queryString) {
044: super ();
045: setPathElements(contextPath, servletPath, pathInfo, queryString);
046: }
047:
048: public MockHttpServletRequest(String contextPath,
049: String servletPath, String pathInfo, String queryString,
050: HttpSession session) {
051: super ();
052: setPathElements(contextPath, servletPath, pathInfo, queryString);
053: setHttpSession(session);
054: }
055:
056: protected HashMap attributes = new HashMap();
057: protected String contextPath = null;
058: protected HashMap headers = new HashMap();
059: protected Cookie[] cookies = new Cookie[0];
060: protected Locale locale = null;
061: protected HashMap parameters = new HashMap();
062: protected String pathInfo = null;
063: protected Principal principal = null;
064: protected String queryString = null;
065: protected String servletPath = null;
066: protected HttpSession session = null;
067:
068: // --------------------------------------------------------- Public Methods
069:
070: public void addHeader(String name, String value) {
071: String values[] = (String[]) headers.get(name);
072: if (values == null) {
073: String results[] = new String[] { value };
074: headers.put(name, results);
075: return;
076: }
077: String results[] = new String[values.length + 1];
078: System.arraycopy(values, 0, results, 0, values.length);
079: results[values.length] = value;
080: headers.put(name, results);
081: }
082:
083: public void addParameter(String name, String value) {
084: String values[] = (String[]) parameters.get(name);
085: if (values == null) {
086: String results[] = new String[] { value };
087: parameters.put(name, results);
088: return;
089: }
090: String results[] = new String[values.length + 1];
091: System.arraycopy(values, 0, results, 0, values.length);
092: results[values.length] = value;
093: parameters.put(name, results);
094: }
095:
096: public void addCookie(String name, String value) {
097: addCookie(new Cookie(name, value));
098: }
099:
100: public void addCookie(Cookie cookie) {
101: Cookie[] newValues = new Cookie[cookies.length + 1];
102: System.arraycopy(cookies, 0, newValues, 0, cookies.length);
103: cookies = newValues;
104: cookies[cookies.length - 1] = cookie;
105: }
106:
107: public void setHttpSession(HttpSession session) {
108: this .session = session;
109: }
110:
111: public void setLocale(Locale locale) {
112: this .locale = locale;
113: }
114:
115: public void setPathElements(String contextPath, String servletPath,
116: String pathInfo, String queryString) {
117:
118: this .contextPath = contextPath;
119: this .servletPath = servletPath;
120: this .pathInfo = pathInfo;
121: this .queryString = queryString;
122:
123: }
124:
125: public void setUserPrincipal(Principal principal) {
126: this .principal = principal;
127: }
128:
129: // --------------------------------------------- HttpServletRequest Methods
130:
131: public String getAuthType() {
132: throw new UnsupportedOperationException();
133: }
134:
135: public String getContextPath() {
136: return (contextPath);
137: }
138:
139: public Cookie[] getCookies() {
140: return cookies;
141: }
142:
143: public long getDateHeader(String name) {
144: throw new UnsupportedOperationException();
145: }
146:
147: public String getHeader(String name) {
148: String values[] = (String[]) headers.get(name);
149: if (values != null) {
150: return (values[0]);
151: } else {
152: return (null);
153: }
154: }
155:
156: public Enumeration getHeaderNames() {
157: return (new MockEnumeration(headers.keySet().iterator()));
158: }
159:
160: public Enumeration getHeaders(String name) {
161: String headers[] = (String[]) this .headers.get(name);
162: if (headers == null) {
163: headers = new String[0];
164: }
165: List list = new ArrayList();
166: for (int i = 0; i < headers.length; i++) {
167: list.add(headers[i]);
168: }
169: return (new MockEnumeration(list.iterator()));
170: }
171:
172: public int getIntHeader(String name) {
173: throw new UnsupportedOperationException();
174: }
175:
176: public String getMethod() {
177: throw new UnsupportedOperationException();
178: }
179:
180: public String getPathInfo() {
181: return (pathInfo);
182: }
183:
184: public String getPathTranslated() {
185: throw new UnsupportedOperationException();
186: }
187:
188: public String getQueryString() {
189: return (queryString);
190: }
191:
192: public String getRemoteUser() {
193: if (principal != null) {
194: return (principal.getName());
195: } else {
196: return (null);
197: }
198: }
199:
200: public String getRequestedSessionId() {
201: throw new UnsupportedOperationException();
202: }
203:
204: public String getRequestURI() {
205: StringBuffer sb = new StringBuffer();
206: if (contextPath != null) {
207: sb.append(contextPath);
208: }
209: if (servletPath != null) {
210: sb.append(servletPath);
211: }
212: if (pathInfo != null) {
213: sb.append(pathInfo);
214: }
215: if (sb.length() > 0) {
216: return (sb.toString());
217: }
218: throw new UnsupportedOperationException();
219: }
220:
221: public StringBuffer getRequestURL() {
222: throw new UnsupportedOperationException();
223: }
224:
225: public String getServletPath() {
226: return (servletPath);
227: }
228:
229: public HttpSession getSession() {
230: return (getSession(true));
231: }
232:
233: public HttpSession getSession(boolean create) {
234: if (create && (session == null)) {
235: throw new UnsupportedOperationException();
236: }
237: return (session);
238: }
239:
240: public Principal getUserPrincipal() {
241: return (principal);
242: }
243:
244: public boolean isRequestedSessionIdFromCookie() {
245: throw new UnsupportedOperationException();
246: }
247:
248: public boolean isRequestedSessionIdFromUrl() {
249: throw new UnsupportedOperationException();
250: }
251:
252: public boolean isRequestedSessionIdFromURL() {
253: throw new UnsupportedOperationException();
254: }
255:
256: public boolean isRequestedSessionIdValid() {
257: throw new UnsupportedOperationException();
258: }
259:
260: public boolean isUserInRole(String role) {
261: if ((principal != null) && (principal instanceof MockPrincipal)) {
262: return (((MockPrincipal) principal).isUserInRole(role));
263: } else {
264: return (false);
265: }
266: }
267:
268: // ------------------------------------------------- ServletRequest Methods
269:
270: public Object getAttribute(String name) {
271: return (attributes.get(name));
272: }
273:
274: public Enumeration getAttributeNames() {
275: return (new MockEnumeration(attributes.keySet().iterator()));
276: }
277:
278: public String getCharacterEncoding() {
279: throw new UnsupportedOperationException();
280: }
281:
282: public int getContentLength() {
283: throw new UnsupportedOperationException();
284: }
285:
286: public String getContentType() {
287: throw new UnsupportedOperationException();
288: }
289:
290: public ServletInputStream getInputStream() {
291: throw new UnsupportedOperationException();
292: }
293:
294: public Locale getLocale() {
295: return (locale);
296: }
297:
298: public Enumeration getLocales() {
299: throw new UnsupportedOperationException();
300: }
301:
302: public String getLocalAddr() {
303: throw new UnsupportedOperationException();
304: }
305:
306: public String getLocalName() {
307: throw new UnsupportedOperationException();
308: }
309:
310: public int getLocalPort() {
311: throw new UnsupportedOperationException();
312: }
313:
314: public String getParameter(String name) {
315: String values[] = (String[]) parameters.get(name);
316: if (values != null) {
317: return (values[0]);
318: } else {
319: return (null);
320: }
321: }
322:
323: public Map getParameterMap() {
324: return (parameters);
325: }
326:
327: public Enumeration getParameterNames() {
328: return (new MockEnumeration(parameters.keySet().iterator()));
329: }
330:
331: public String[] getParameterValues(String name) {
332: return ((String[]) parameters.get(name));
333: }
334:
335: public String getProtocol() {
336: throw new UnsupportedOperationException();
337: }
338:
339: public BufferedReader getReader() {
340: throw new UnsupportedOperationException();
341: }
342:
343: public String getRealPath(String path) {
344: throw new UnsupportedOperationException();
345: }
346:
347: public String getRemoteAddr() {
348: throw new UnsupportedOperationException();
349: }
350:
351: public String getRemoteHost() {
352: throw new UnsupportedOperationException();
353: }
354:
355: public int getRemotePort() {
356: throw new UnsupportedOperationException();
357: }
358:
359: public RequestDispatcher getRequestDispatcher(String path) {
360: throw new UnsupportedOperationException();
361: }
362:
363: public String getScheme() {
364: return ("http");
365: }
366:
367: public String getServerName() {
368: return ("localhost");
369: }
370:
371: public int getServerPort() {
372: return (8080);
373: }
374:
375: public boolean isSecure() {
376: return (false);
377: }
378:
379: public void removeAttribute(String name) {
380: attributes.remove(name);
381: }
382:
383: public void setAttribute(String name, Object value) {
384: if (value == null) {
385: attributes.remove(name);
386: } else {
387: attributes.put(name, value);
388: }
389: }
390:
391: public void setCharacterEncoding(String name) {
392: throw new UnsupportedOperationException();
393: }
394:
395: }
|