01: /*
02: * Created on Nov 19, 2005
03: */
04: package uk.org.ponder.rsf.servlet;
05:
06: import java.io.UnsupportedEncodingException;
07: import java.util.HashMap;
08: import java.util.Map;
09:
10: import javax.servlet.http.HttpServletRequest;
11:
12: import org.springframework.web.multipart.MultipartHttpServletRequest;
13: import org.springframework.web.multipart.MultipartResolver;
14:
15: import uk.org.ponder.rsf.request.EarlyRequestParser;
16: import uk.org.ponder.util.Logger;
17: import uk.org.ponder.util.UniversalRuntimeException;
18:
19: /**
20: * <p>
21: * Nothing should touch the HttpServletRequest before this request-scope factory
22: * bean, which parses primitive information such as the request type and
23: * parameter map, as well as resolving multipart uploads.
24: */
25:
26: public class ServletEarlyRequestParser implements EarlyRequestParser {
27: protected HttpServletRequest request;
28: private MultipartResolver multipartresolver;
29: private Map multipartmap = new HashMap();
30:
31: public void setMultipartResolver(MultipartResolver multipartresolver) {
32: this .multipartresolver = multipartresolver;
33: }
34:
35: public void setHttpServletRequest(HttpServletRequest request) {
36: this .request = request;
37: }
38:
39: public void init() {
40: Logger.log.info("begin parseRequest for " + request.getMethod()
41: + " " + request.getRequestURI());
42: if (multipartresolver.isMultipart(request)) {
43: try {
44: this .request = multipartresolver
45: .resolveMultipart(request);
46: this .multipartmap = ((MultipartHttpServletRequest) request)
47: .getFileMap();
48: } catch (Exception e) {
49: throw UniversalRuntimeException.accumulate(e,
50: "Error decoding multipart request");
51: }
52: Logger.log.info("Successfully decoded multipart request");
53: }
54: // We INSIST that all data passed in and out should be in UTF-8.
55: // TODO: Make sure we do not tread on the toes of a POST request in
56: // doing this however.
57: try {
58: request.setCharacterEncoding("UTF-8");
59: } catch (UnsupportedEncodingException uee) {
60: throw UniversalRuntimeException.accumulate(uee,
61: "Fatal internal error: UTF-8 encoding not found");
62: }
63: }
64:
65: public Map getRequestMap() {
66: return request.getParameterMap();
67: }
68:
69: public Map getMultipartMap() {
70: return multipartmap;
71: }
72:
73: /**
74: * The pathinfo as returned from the request. This INCLUDES an initial slash
75: * but no final slash.
76: */
77: public String getPathInfo() {
78: String pathinfo = request.getPathInfo();
79: return pathinfo == null ? "/" : pathinfo;
80: }
81:
82: /**
83: * A factory method for a String encoding the nature of the current request
84: * cycle, either ViewParameters.RENDER_REQUEST or
85: * ViewParameters.ACTION_REQUEST.
86: */
87: public String getRequestType() {
88: String requesttype = request.getMethod().equals("GET") ? EarlyRequestParser.RENDER_REQUEST
89: : EarlyRequestParser.ACTION_REQUEST;
90: return requesttype;
91: }
92:
93: public String getEnvironmentType() {
94: return EarlyRequestParser.SERVLET_ENVIRONMENT;
95: }
96:
97: }
|