001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: HttpRequest.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.servlet;
009:
010: import com.uwyn.rife.config.RifeConfig;
011: import com.uwyn.rife.engine.Request;
012: import com.uwyn.rife.engine.RequestMethod;
013: import com.uwyn.rife.engine.StateStore;
014: import com.uwyn.rife.engine.UploadedFile;
015: import com.uwyn.rife.engine.exceptions.EngineException;
016: import com.uwyn.rife.servlet.MultipartRequest;
017: import com.uwyn.rife.tools.StringUtils;
018: import java.io.UnsupportedEncodingException;
019: import java.util.Enumeration;
020: import java.util.HashMap;
021: import java.util.Locale;
022: import java.util.Map;
023: import javax.servlet.RequestDispatcher;
024: import javax.servlet.http.Cookie;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpSession;
027:
028: public class HttpRequest implements Request {
029: private HttpServletRequest mHttpServletRequest = null;
030:
031: private Map<String, String[]> mParameters = null;
032: private Map<String, UploadedFile[]> mFiles = null;
033:
034: public HttpRequest(HttpServletRequest request)
035: throws EngineException {
036: assert request != null;
037:
038: mHttpServletRequest = request;
039: }
040:
041: public void init(StateStore stateStore) {
042: assert stateStore != null;
043:
044: if (MultipartRequest.isValidContentType(mHttpServletRequest
045: .getContentType())) {
046: MultipartRequest multipart_request = new MultipartRequest(
047: mHttpServletRequest);
048: mParameters = multipart_request.getParameterMap();
049: mFiles = multipart_request.getFileMap();
050: } else {
051: mParameters = new HashMap<String, String[]>();
052:
053: try {
054: mHttpServletRequest
055: .setCharacterEncoding(RifeConfig.Engine
056: .getRequestEncoding());
057: } catch (UnsupportedEncodingException e) {
058: // should never happen
059: }
060:
061: Enumeration<String> parameter_names = mHttpServletRequest
062: .getParameterNames();
063: String parameter_name = null;
064: String[] parameter_values = null;
065: while (parameter_names.hasMoreElements()) {
066: parameter_name = parameter_names.nextElement();
067: if (StringUtils
068: .doesUrlValueNeedDecoding(parameter_name)) {
069: parameter_name = StringUtils
070: .decodeUrlValue(parameter_name);
071: }
072:
073: parameter_values = mHttpServletRequest
074: .getParameterValues(parameter_name);
075: for (int i = 0; i < parameter_values.length; i++) {
076: if (StringUtils
077: .doesUrlValueNeedDecoding(parameter_values[i])) {
078: parameter_values[i] = StringUtils
079: .decodeUrlValue(parameter_values[i]);
080: }
081: }
082:
083: mParameters.put(parameter_name, parameter_values);
084: }
085: }
086:
087: Map<String, String[]> parameters = stateStore
088: .restoreParameters(this );
089: if (parameters != null) {
090: mParameters = parameters;
091: }
092: }
093:
094: public RequestMethod getMethod() {
095: return RequestMethod.getMethod(mHttpServletRequest.getMethod());
096: }
097:
098: public Map<String, String[]> getParameters() {
099: return mParameters;
100: }
101:
102: public Map<String, UploadedFile[]> getFiles() {
103: return mFiles;
104: }
105:
106: public boolean hasFile(String name) {
107: assert name != null;
108: assert name.length() > 0;
109:
110: if (null == getFiles()) {
111: return false;
112: }
113:
114: if (!getFiles().containsKey(name)) {
115: return false;
116: }
117:
118: UploadedFile[] uploaded_files = getFiles().get(name);
119:
120: if (0 == uploaded_files.length) {
121: return false;
122: }
123:
124: for (UploadedFile uploaded_file : uploaded_files) {
125: if (uploaded_file != null
126: && uploaded_file.getName() != null) {
127: return true;
128: }
129: }
130:
131: return false;
132: }
133:
134: public UploadedFile getFile(String name) {
135: assert name != null;
136: assert name.length() > 0;
137:
138: if (null == getFiles()) {
139: return null;
140: }
141:
142: UploadedFile[] files = getFiles().get(name);
143: if (null == files) {
144: return null;
145: }
146:
147: return files[0];
148: }
149:
150: public UploadedFile[] getFiles(String name) {
151: assert name != null;
152: assert name.length() > 0;
153:
154: if (null == getFiles()) {
155: return null;
156: }
157:
158: return getFiles().get(name);
159: }
160:
161: public boolean hasCookie(String name) {
162: assert name != null;
163: assert name.length() > 0;
164:
165: Cookie[] cookies = mHttpServletRequest.getCookies();
166:
167: if (null == cookies) {
168: return false;
169: }
170:
171: for (Cookie cookie : cookies) {
172: if (cookie.getName().equals(name)) {
173: return true;
174: }
175: }
176:
177: return false;
178: }
179:
180: public Cookie getCookie(String name) {
181: assert name != null;
182: assert name.length() > 0;
183:
184: Cookie[] cookies = mHttpServletRequest.getCookies();
185:
186: if (null == cookies) {
187: return null;
188: }
189:
190: for (Cookie cookie : cookies) {
191: if (cookie.getName().equals(name)) {
192: return cookie;
193: }
194: }
195:
196: return null;
197: }
198:
199: public Cookie[] getCookies() {
200: return mHttpServletRequest.getCookies();
201: }
202:
203: // simply wrapped methods
204: public Object getAttribute(String name) {
205: return mHttpServletRequest.getAttribute(name);
206: }
207:
208: public boolean hasAttribute(String name) {
209: return getAttribute(name) != null;
210: }
211:
212: public Enumeration getAttributeNames() {
213: return mHttpServletRequest.getAttributeNames();
214: }
215:
216: public String getCharacterEncoding() {
217: return mHttpServletRequest.getCharacterEncoding();
218: }
219:
220: public String getContentType() {
221: return mHttpServletRequest.getContentType();
222: }
223:
224: public long getDateHeader(String name) {
225: return mHttpServletRequest.getDateHeader(name);
226: }
227:
228: public String getHeader(String name) {
229: return mHttpServletRequest.getHeader(name);
230: }
231:
232: public Enumeration getHeaderNames() {
233: return mHttpServletRequest.getHeaderNames();
234: }
235:
236: public Enumeration getHeaders(String name) {
237: return mHttpServletRequest.getHeaders(name);
238: }
239:
240: public int getIntHeader(String name) {
241: return mHttpServletRequest.getIntHeader(name);
242: }
243:
244: public Locale getLocale() {
245: return mHttpServletRequest.getLocale();
246: }
247:
248: public Enumeration getLocales() {
249: return mHttpServletRequest.getLocales();
250: }
251:
252: public String getProtocol() {
253: return mHttpServletRequest.getProtocol();
254: }
255:
256: public String getRemoteAddr() {
257: return mHttpServletRequest.getRemoteAddr();
258: }
259:
260: public String getRemoteUser() {
261: return mHttpServletRequest.getRemoteUser();
262: }
263:
264: public String getRemoteHost() {
265: return mHttpServletRequest.getRemoteHost();
266: }
267:
268: public RequestDispatcher getRequestDispatcher(String url) {
269: return mHttpServletRequest.getRequestDispatcher(url);
270: }
271:
272: public HttpSession getSession() {
273: return mHttpServletRequest.getSession();
274: }
275:
276: public HttpSession getSession(boolean create) {
277: return mHttpServletRequest.getSession(create);
278: }
279:
280: public int getServerPort() {
281: if (null == mHttpServletRequest || null == mHttpServletRequest) {
282: return -1;
283: }
284:
285: return mHttpServletRequest.getServerPort();
286: }
287:
288: public String getScheme() {
289: return mHttpServletRequest.getScheme();
290: }
291:
292: public String getServerName() {
293: if (null == mHttpServletRequest || null == mHttpServletRequest) {
294: return null;
295: }
296:
297: return mHttpServletRequest.getServerName();
298: }
299:
300: public String getContextPath() {
301: if (null == mHttpServletRequest || null == mHttpServletRequest) {
302: return null;
303: }
304:
305: return mHttpServletRequest.getContextPath();
306: }
307:
308: public boolean isSecure() {
309: return mHttpServletRequest.isSecure();
310: }
311:
312: public void removeAttribute(String name) {
313: mHttpServletRequest.removeAttribute(name);
314: }
315:
316: public void setAttribute(String name, Object object) {
317: mHttpServletRequest.setAttribute(name, object);
318: }
319:
320: public String getServerRootUrl(int port) {
321: StringBuilder server_root = new StringBuilder();
322: server_root.append(getScheme());
323: server_root.append("://");
324: server_root.append(getServerName());
325: if (port <= -1) {
326: port = getServerPort();
327: }
328: if (port != 80) {
329: server_root.append(":");
330: server_root.append(port);
331: }
332: return server_root.toString();
333: }
334:
335: public HttpServletRequest getHttpServletRequest() {
336: return mHttpServletRequest;
337: }
338: }
|