Coverage Report - org.kuali.rice.edl.impl.RequestParser
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestParser
0%
0/80
0%
0/22
2.417
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * An abstraction that allows for switching between multipart form requests and normal requests when getting
 38  
  * request params
 39  
  * 
 40  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  0
         private Map<String, String[]> additionalParameters = new HashMap<String, String[]>();
 55  
         
 56  0
         public RequestParser(HttpServletRequest request) {
 57  0
                 this.request = request;
 58  
                 // setup empty List of errors and messages
 59  0
                 request.setAttribute(GLOBAL_ERRORS_KEY, new ArrayList());
 60  0
                 request.setAttribute(GLOBAL_MESSAGES_KEY, new ArrayList());
 61  0
                 request.setAttribute(GLOBAL_FIELD_ERRORS_KEY, new HashMap<String, String>());
 62  0
         }
 63  
 
 64  
         private static void parseRequest(HttpServletRequest request) {
 65  0
                 if (request.getAttribute(PARSED_MULTI_REQUEST_KEY) != null) {
 66  0
                         return;
 67  
                 }
 68  
                 
 69  0
                 Map requestParams = new HashMap();
 70  0
                 request.setAttribute(PARSED_MULTI_REQUEST_KEY, requestParams);
 71  
                 
 72  0
                 DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
 73  0
                 diskFileItemFactory.setSizeThreshold(100);
 74  0
                 ServletFileUpload upload = new ServletFileUpload(diskFileItemFactory);
 75  
                 
 76  0
                 List items = null;
 77  
                 try {
 78  0
                         items = upload.parseRequest(request);        
 79  0
                 } catch (FileUploadException fue) {
 80  0
                         throw new WorkflowRuntimeException(fue);
 81  0
                 }
 82  
                 
 83  0
                 Iterator iter = items.iterator();
 84  0
                 while (iter.hasNext()) {
 85  
                         
 86  
                         try {
 87  0
                                 FileItem item = (FileItem) iter.next();
 88  0
                                 if (item.isFormField()) {
 89  0
                                         String fieldName = item.getFieldName();
 90  0
                                         String fieldValue = item.getString("utf-8");
 91  0
                                         String[] paramVals = null;
 92  0
                                         if (requestParams.containsKey(fieldName)) {
 93  0
                                                 paramVals = (String[])requestParams.get(fieldName);
 94  0
                                                 paramVals = (String[]) ArrayUtils.add(paramVals, fieldValue);
 95  
                                         } else {
 96  0
                                                 paramVals = new String[1];
 97  0
                                                 paramVals[0] = fieldValue;
 98  
                                         }
 99  0
                                         requestParams.put(fieldName, paramVals);
 100  0
                                 } else {
 101  0
                                         List uploadedFiles = (List)request.getAttribute(UPLOADED_FILES_KEY);
 102  0
                                         if (uploadedFiles == null) {
 103  0
                                                 uploadedFiles = new ArrayList();
 104  0
                                                 request.setAttribute(UPLOADED_FILES_KEY, uploadedFiles);
 105  
                                         }
 106  0
                                         uploadedFiles.add(item);
 107  
                                 }
 108  0
                         } catch (UnsupportedEncodingException e) {
 109  0
                                 throw new WorkflowRuntimeException(e);
 110  0
                         }
 111  
                 }
 112  0
         }
 113  
         
 114  
         public String[] getParameterValues(String paramName) {
 115  0
                 boolean isMultipart = ServletFileUpload.isMultipartContent(this.getRequest());
 116  0
             String[] params = null;
 117  0
             if (isMultipart) {
 118  0
                     parseRequest(this.getRequest());
 119  0
                     params = (String[]) ((Map)request.getAttribute(PARSED_MULTI_REQUEST_KEY)).get(paramName);
 120  
             } else {
 121  0
                     params = this.getRequest().getParameterValues(paramName);
 122  
             }
 123  0
             if (params == null) {
 124  0
                     params = additionalParameters.get(paramName);
 125  
             }
 126  0
             return params;
 127  
         }
 128  
         
 129  
         public void setAttribute(String name, Object value) {
 130  0
                 this.getRequest().setAttribute(name, value);
 131  0
         }
 132  
         
 133  
         public Object getAttribute(String name) {
 134  0
                 return this.getRequest().getAttribute(name); 
 135  
         }
 136  
         
 137  
         public String getParameterValue(String paramName) {
 138  0
                 String[] paramVals = getParameterValues(paramName);
 139  0
                 if (paramVals != null) {
 140  0
                         return paramVals[0];
 141  
                 }
 142  0
                 return null;
 143  
         }
 144  
         
 145  
         public void setParameterValue(String paramName, String paramValue) {
 146  0
             additionalParameters.put(paramName, new String[] { paramValue });
 147  0
         }
 148  
         
 149  
         public void setParameterValue(String paramName, String[] paramValue) {
 150  0
             additionalParameters.put(paramName, paramValue);
 151  0
         }
 152  
         
 153  
         public List<String> getParameterNames() {
 154  0
             List<String> names = new ArrayList<String>();
 155  0
             boolean isMultipart = ServletFileUpload.isMultipartContent(this.getRequest());
 156  0
             if (isMultipart) {
 157  0
                 parseRequest(this.getRequest());
 158  0
                 Map paramMap = ((Map)request.getAttribute(PARSED_MULTI_REQUEST_KEY));
 159  0
                 for (Iterator iterator = paramMap.keySet().iterator(); iterator.hasNext();) {
 160  0
                     String parameterName = (String)iterator.next();
 161  0
                     names.add(parameterName);
 162  0
                 }
 163  0
             } else { 
 164  0
                 Enumeration<String> nameEnum = getRequest().getParameterNames();
 165  0
                 while (nameEnum.hasMoreElements()) {
 166  0
                     names.add(nameEnum.nextElement());
 167  
                 }
 168  
             }
 169  0
             return names;
 170  
         }
 171  
 
 172  
         public void setRequest(HttpServletRequest request) {
 173  0
                 this.request = request;
 174  0
         }
 175  
         
 176  
         public HttpServletRequest getRequest() {
 177  0
                 return request;
 178  
         }
 179  
 
 180  
         public List getUploadList() {
 181  0
                 return (List) request.getAttribute(UPLOADED_FILES_KEY);
 182  
         }
 183  
 }