001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.edl;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import javax.servlet.http.HttpServletRequest;
026:
027: import org.apache.commons.fileupload.FileItem;
028: import org.apache.commons.fileupload.FileUploadException;
029: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
030: import org.apache.commons.fileupload.servlet.ServletFileUpload;
031: import org.apache.commons.lang.ArrayUtils;
032:
033: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
034:
035: /**
036: * An abstraction that allows for switching between multipart form requests and normal requests when getting
037: * request params
038: *
039: * @author rkirkend
040: *
041: */
042: public class RequestParser {
043:
044: private static final String PARSED_MULTI_REQUEST_KEY = "__parsedRequestStruct";
045: private static final String UPLOADED_FILES_KEY = "__uploadedFiles";
046:
047: public static final String WORKFLOW_DOCUMENT_SESSION_KEY = "workflowDocument";
048: public static final String GLOBAL_ERRORS_KEY = "edu.iu.uis.eden.edl.GlobalErrors";
049: public static final String GLOBAL_MESSAGES_KEY = "edu.iu.uis.eden.edl.GlobalMessages";
050:
051: private HttpServletRequest request;
052:
053: public RequestParser(HttpServletRequest request) {
054: this .request = request;
055: // setup empty List of errors and messages
056: request.setAttribute(GLOBAL_ERRORS_KEY, new ArrayList());
057: request.setAttribute(GLOBAL_MESSAGES_KEY, new ArrayList());
058: }
059:
060: private static void parseRequest(HttpServletRequest request) {
061: if (request.getAttribute(PARSED_MULTI_REQUEST_KEY) != null) {
062: return;
063: }
064:
065: Map requestParams = new HashMap();
066: request.setAttribute(PARSED_MULTI_REQUEST_KEY, requestParams);
067:
068: DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
069: diskFileItemFactory.setSizeThreshold(100);
070: ServletFileUpload upload = new ServletFileUpload(
071: diskFileItemFactory);
072:
073: List items = null;
074: try {
075: items = upload.parseRequest(request);
076: } catch (FileUploadException fue) {
077: throw new WorkflowRuntimeException(fue);
078: }
079:
080: Iterator iter = items.iterator();
081: while (iter.hasNext()) {
082:
083: FileItem item = (FileItem) iter.next();
084: if (item.isFormField()) {
085: String fieldName = item.getFieldName();
086: String fieldValue = item.getString();
087: String[] paramVals = null;
088: if (requestParams.containsKey(fieldName)) {
089: paramVals = (String[]) requestParams.get(fieldName);
090: ArrayUtils.add(paramVals, fieldValue);
091: } else {
092: paramVals = new String[1];
093: paramVals[0] = fieldValue;
094: requestParams.put(fieldName, paramVals);
095: }
096: } else {
097: List uploadedFiles = (List) request
098: .getAttribute(UPLOADED_FILES_KEY);
099: if (uploadedFiles == null) {
100: uploadedFiles = new ArrayList();
101: request.setAttribute(UPLOADED_FILES_KEY,
102: uploadedFiles);
103: }
104: uploadedFiles.add(item);
105: }
106: }
107: }
108:
109: public String[] getParameterValues(String paramName) {
110: boolean isMultipart = ServletFileUpload.isMultipartContent(this
111: .getRequest());
112: if (isMultipart) {
113: parseRequest(this .getRequest());
114: String[] params = (String[]) ((Map) request
115: .getAttribute(PARSED_MULTI_REQUEST_KEY))
116: .get(paramName);
117: if (params == null) {
118: return null;
119: }
120: return params;
121: } else {
122: return this .getRequest().getParameterValues(paramName);
123: }
124: }
125:
126: public void setAttribute(String name, Object value) {
127: this .getRequest().setAttribute(name, value);
128: }
129:
130: public Object getAttribute(String name) {
131: return this .getRequest().getAttribute(name);
132: }
133:
134: public String getParameterValue(String paramName) {
135: String[] paramVals = getParameterValues(paramName);
136: if (paramVals != null) {
137: return paramVals[0];
138: }
139: return null;
140: }
141:
142: public void setRequest(HttpServletRequest request) {
143: this .request = request;
144: }
145:
146: public HttpServletRequest getRequest() {
147: return request;
148: }
149:
150: public List getUploadList() {
151: return (List) request.getAttribute(UPLOADED_FILES_KEY);
152: }
153: }
|