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