1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.kuali.rice.edl.impl;
18
19 import java.io.UnsupportedEncodingException;
20 import java.util.ArrayList;
21 import java.util.Enumeration;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletRequest;
28
29 import org.apache.commons.fileupload.FileItem;
30 import org.apache.commons.fileupload.FileUploadException;
31 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
32 import org.apache.commons.fileupload.servlet.ServletFileUpload;
33 import org.apache.commons.lang.ArrayUtils;
34 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
35
36
37
38
39
40
41
42
43
44 public class RequestParser {
45
46 private static final String PARSED_MULTI_REQUEST_KEY = "__parsedRequestStruct";
47 private static final String UPLOADED_FILES_KEY = "__uploadedFiles";
48
49 public static final String WORKFLOW_DOCUMENT_SESSION_KEY = "workflowDocument";
50 public static final String GLOBAL_ERRORS_KEY = "org.kuali.rice.edl.impl.GlobalErrors";
51 public static final String GLOBAL_MESSAGES_KEY = "org.kuali.rice.edl.impl.GlobalMessages";
52 public static final String GLOBAL_FIELD_ERRORS_KEY = "org.kuali.rice.edl.impl.GlobalFieldErrors";
53
54 private HttpServletRequest request;
55 private Map<String, String[]> additionalParameters = new HashMap<String, String[]>();
56
57 public RequestParser(HttpServletRequest request) {
58 this.request = request;
59
60 request.setAttribute(GLOBAL_ERRORS_KEY, new ArrayList());
61 request.setAttribute(GLOBAL_MESSAGES_KEY, new ArrayList());
62 request.setAttribute(GLOBAL_FIELD_ERRORS_KEY, new HashMap<String, String>());
63 }
64
65 private static void parseRequest(HttpServletRequest request) {
66 if (request.getAttribute(PARSED_MULTI_REQUEST_KEY) != null) {
67 return;
68 }
69
70 Map requestParams = new HashMap();
71 request.setAttribute(PARSED_MULTI_REQUEST_KEY, requestParams);
72
73 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
74 diskFileItemFactory.setSizeThreshold(100);
75 ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
76
77 List items = null;
78 try {
79 items = upload.parseRequest(request);
80 } catch (FileUploadException fue) {
81 throw new WorkflowRuntimeException(fue);
82 }
83
84 Iterator iter = items.iterator();
85 while (iter.hasNext()) {
86
87 try {
88 FileItem item = (FileItem) iter.next();
89 if (item.isFormField()) {
90 String fieldName = item.getFieldName();
91 String fieldValue = item.getString("utf-8");
92 String[] paramVals = null;
93 if (requestParams.containsKey(fieldName)) {
94 paramVals = (String[])requestParams.get(fieldName);
95 paramVals = (String[]) ArrayUtils.add(paramVals, fieldValue);
96 } else {
97 paramVals = new String[1];
98 paramVals[0] = fieldValue;
99 }
100 requestParams.put(fieldName, paramVals);
101 } else {
102 List uploadedFiles = (List)request.getAttribute(UPLOADED_FILES_KEY);
103 if (uploadedFiles == null) {
104 uploadedFiles = new ArrayList();
105 request.setAttribute(UPLOADED_FILES_KEY, uploadedFiles);
106 }
107 uploadedFiles.add(item);
108 }
109 } catch (UnsupportedEncodingException e) {
110 throw new WorkflowRuntimeException(e);
111 }
112 }
113 }
114
115 public String[] getParameterValues(String paramName) {
116 boolean isMultipart = ServletFileUpload.isMultipartContent(this.getRequest());
117 String[] params = null;
118 if (isMultipart) {
119 parseRequest(this.getRequest());
120 params = (String[]) ((Map)request.getAttribute(PARSED_MULTI_REQUEST_KEY)).get(paramName);
121 } else {
122 params = this.getRequest().getParameterValues(paramName);
123 }
124 if (params == null) {
125 params = additionalParameters.get(paramName);
126 }
127 return params;
128 }
129
130 public void setAttribute(String name, Object value) {
131 this.getRequest().setAttribute(name, value);
132 }
133
134 public Object getAttribute(String name) {
135 return this.getRequest().getAttribute(name);
136 }
137
138 public String getParameterValue(String paramName) {
139 String[] paramVals = getParameterValues(paramName);
140 if (paramVals != null) {
141 return paramVals[0];
142 }
143 return null;
144 }
145
146 public void setParameterValue(String paramName, String paramValue) {
147 additionalParameters.put(paramName, new String[] { paramValue });
148 }
149
150 public void setParameterValue(String paramName, String[] paramValue) {
151 additionalParameters.put(paramName, paramValue);
152 }
153
154 public List<String> getParameterNames() {
155 List<String> names = new ArrayList<String>();
156 boolean isMultipart = ServletFileUpload.isMultipartContent(this.getRequest());
157 if (isMultipart) {
158 parseRequest(this.getRequest());
159 Map paramMap = ((Map)request.getAttribute(PARSED_MULTI_REQUEST_KEY));
160 for (Iterator iterator = paramMap.keySet().iterator(); iterator.hasNext();) {
161 String parameterName = (String)iterator.next();
162 names.add(parameterName);
163 }
164 } else {
165 Enumeration<String> nameEnum = getRequest().getParameterNames();
166 while (nameEnum.hasMoreElements()) {
167 names.add(nameEnum.nextElement());
168 }
169 }
170 return names;
171 }
172
173 public void setRequest(HttpServletRequest request) {
174 this.request = request;
175 }
176
177 public HttpServletRequest getRequest() {
178 return request;
179 }
180
181 public List getUploadList() {
182 return (List) request.getAttribute(UPLOADED_FILES_KEY);
183 }
184 }