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