001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package de.schlund.pfixcore.scriptedflow.vm;
020:
021: import java.io.BufferedReader;
022: import java.io.IOException;
023: import java.io.StringReader;
024: import java.io.UnsupportedEncodingException;
025: import java.security.Principal;
026: import java.util.Collections;
027: import java.util.Enumeration;
028: import java.util.HashMap;
029: import java.util.Locale;
030: import java.util.Map;
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: public class VirtualHttpServletRequest implements HttpServletRequest {
039: private HttpServletRequest orig;
040:
041: private String pagename = null;
042:
043: private Map<String, String[]> params;
044:
045: private String queryString;
046:
047: // Constructor is intenionally in default scope
048: // as it should only be used within this package
049: VirtualHttpServletRequest(HttpServletRequest originalRequest,
050: String pagename, Map<String, String[]> params) {
051: this .orig = originalRequest;
052: this .pagename = pagename;
053: this .params = params;
054:
055: StringBuffer qs = new StringBuffer();
056: for (String key : params.keySet()) {
057: String[] values = params.get(key);
058: if (values != null) {
059: for (int i = 0; i < values.length; i++) {
060: qs.append('&');
061: qs.append(key);
062: if (values[i] != null && values[i].length() > 0) {
063: qs.append('=');
064: qs.append(values[i]);
065: }
066: }
067: }
068: }
069: if (qs.length() > 0) {
070: // Remove leading '&'
071: qs.deleteCharAt(0);
072: }
073: queryString = qs.toString();
074: }
075:
076: public String getAuthType() {
077: return orig.getAuthType();
078: }
079:
080: public Cookie[] getCookies() {
081: return orig.getCookies();
082: }
083:
084: public long getDateHeader(String arg0) {
085: return orig.getDateHeader(arg0);
086: }
087:
088: public String getHeader(String arg0) {
089: return orig.getHeader(arg0);
090: }
091:
092: @SuppressWarnings("unchecked")
093: public Enumeration getHeaders(String arg0) {
094: return orig.getHeaders(arg0);
095: }
096:
097: @SuppressWarnings("unchecked")
098: public Enumeration getHeaderNames() {
099: return orig.getHeaderNames();
100: }
101:
102: public int getIntHeader(String arg0) {
103: return orig.getIntHeader(arg0);
104: }
105:
106: public String getMethod() {
107: // A virtual request is always a GET request
108: return "GET";
109: }
110:
111: public String getPathInfo() {
112: if (pagename == null) {
113: return null;
114: } else {
115: return "/" + pagename;
116: }
117: }
118:
119: public String getPathTranslated() {
120: if (getPathInfo() != null) {
121: return getRealPath(getPathInfo());
122: } else {
123: return null;
124: }
125: }
126:
127: public String getContextPath() {
128: return orig.getContextPath();
129: }
130:
131: public String getQueryString() {
132: return queryString;
133: }
134:
135: public String getRemoteUser() {
136: return orig.getRemoteUser();
137: }
138:
139: public boolean isUserInRole(String arg0) {
140: return orig.isUserInRole(arg0);
141: }
142:
143: public Principal getUserPrincipal() {
144: return orig.getUserPrincipal();
145: }
146:
147: public String getRequestedSessionId() {
148: return orig.getRequestedSessionId();
149: }
150:
151: public String getRequestURI() {
152: // Construct the request URI using servlet path
153: // and path info
154: String contextpath = getContextPath();
155: String servletpath = getServletPath();
156: String pathinfo = getPathInfo();
157: if (pathinfo == null && servletpath == null) {
158: if (contextpath.length() == 0) {
159: return "/";
160: } else {
161: return contextpath;
162: }
163: } else if (servletpath == null) {
164: return contextpath + pathinfo;
165: } else if (pathinfo == null) {
166: return contextpath + servletpath;
167: } else {
168: return contextpath + servletpath + pathinfo;
169: }
170: }
171:
172: public StringBuffer getRequestURL() {
173: // Construct the URL
174: StringBuffer buffer = new StringBuffer();
175: String scheme = getScheme();
176: buffer.append(scheme);
177: buffer.append("://");
178: buffer.append(getLocalName());
179: int port = getLocalPort();
180: if (scheme.equals("http") && port != 80) {
181: buffer.append(":" + String.valueOf(port));
182: } else if (scheme.equals("https") && port != 443) {
183: buffer.append(":" + String.valueOf(port));
184: }
185: buffer.append(getRequestURI());
186: return buffer;
187: }
188:
189: public String getServletPath() {
190: return orig.getServletPath();
191: }
192:
193: public HttpSession getSession(boolean arg0) {
194: return orig.getSession(arg0);
195: }
196:
197: public HttpSession getSession() {
198: return orig.getSession();
199: }
200:
201: public boolean isRequestedSessionIdValid() {
202: return orig.isRequestedSessionIdValid();
203: }
204:
205: public boolean isRequestedSessionIdFromCookie() {
206: return orig.isRequestedSessionIdFromCookie();
207: }
208:
209: public boolean isRequestedSessionIdFromURL() {
210: return orig.isRequestedSessionIdFromURL();
211: }
212:
213: @SuppressWarnings("deprecation")
214: public boolean isRequestedSessionIdFromUrl() {
215: return orig.isRequestedSessionIdFromUrl();
216: }
217:
218: public Object getAttribute(String arg0) {
219: return orig.getAttribute(arg0);
220: }
221:
222: @SuppressWarnings("unchecked")
223: public Enumeration getAttributeNames() {
224: return orig.getAttributeNames();
225: }
226:
227: public String getCharacterEncoding() {
228: return orig.getCharacterEncoding();
229: }
230:
231: public void setCharacterEncoding(String arg0)
232: throws UnsupportedEncodingException {
233: throw new RuntimeException(
234: "Cannot set encoding on a virtual request!");
235: }
236:
237: public int getContentLength() {
238: // Virtual requests have no request body
239: return -1;
240: }
241:
242: public String getContentType() {
243: // Virtual requests have no request body
244: return null;
245: }
246:
247: public ServletInputStream getInputStream() throws IOException {
248: // Return empty stream
249: return new ServletInputStream() {
250: public int read() throws IOException {
251: return -1;
252: }
253: };
254: }
255:
256: public String getParameter(String arg0) {
257: String[] values = params.get(arg0);
258: if (values == null || values.length < 1) {
259: return null;
260: } else {
261: return values[0];
262: }
263: }
264:
265: @SuppressWarnings("unchecked")
266: public Enumeration getParameterNames() {
267: return Collections.enumeration(params.keySet());
268: }
269:
270: public String[] getParameterValues(String arg0) {
271: return params.get(arg0);
272: }
273:
274: @SuppressWarnings("unchecked")
275: public Map getParameterMap() {
276: return Collections.unmodifiableMap(params);
277: }
278:
279: public String getProtocol() {
280: return orig.getProtocol();
281: }
282:
283: public String getScheme() {
284: return orig.getScheme();
285: }
286:
287: public String getServerName() {
288: return orig.getServerName();
289: }
290:
291: public int getServerPort() {
292: return orig.getServerPort();
293: }
294:
295: public BufferedReader getReader() throws IOException {
296: // Return empty reader
297: return new BufferedReader(new StringReader(""));
298: }
299:
300: public String getRemoteAddr() {
301: return orig.getRemoteAddr();
302: }
303:
304: public String getRemoteHost() {
305: return orig.getRemoteHost();
306: }
307:
308: public void setAttribute(String arg0, Object arg1) {
309: orig.setAttribute(arg0, arg1);
310: }
311:
312: public void removeAttribute(String arg0) {
313: orig.removeAttribute(arg0);
314: }
315:
316: public Locale getLocale() {
317: return orig.getLocale();
318: }
319:
320: @SuppressWarnings("unchecked")
321: public Enumeration getLocales() {
322: return orig.getLocales();
323: }
324:
325: public boolean isSecure() {
326: return orig.isSecure();
327: }
328:
329: public RequestDispatcher getRequestDispatcher(String arg0) {
330: return orig.getRequestDispatcher(arg0);
331: }
332:
333: @SuppressWarnings("deprecation")
334: public String getRealPath(String arg0) {
335: return orig.getRealPath(arg0);
336: }
337:
338: public int getRemotePort() {
339: return orig.getRemotePort();
340: }
341:
342: public String getLocalName() {
343: return orig.getLocalName();
344: }
345:
346: public String getLocalAddr() {
347: return orig.getLocalAddr();
348: }
349:
350: public int getLocalPort() {
351: return orig.getLocalPort();
352: }
353:
354: /**
355: * Create a new HttpServletRequest based on the given request, but without
356: * any request parameters or path info.
357: *
358: * @param orig Original request to use as a base for the new reqeust
359: * @return new request which simply queries the servlet without giving
360: * any parameters or path info
361: */
362: public static HttpServletRequest getVoidRequest(
363: HttpServletRequest orig) {
364: return new VirtualHttpServletRequest(orig, null,
365: new HashMap<String, String[]>());
366: }
367:
368: }
|