KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > component > html > util > MultipartRequestWrapper


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.apache.myfaces.component.html.util;
17
18 import org.apache.commons.fileupload.*;
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletRequestWrapper JavaDoc;
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import java.util.*;
26
27 /**
28  * @author Sylvain Vieujot (latest modification by $Author: svieujot $)
29  * @version $Revision: 1.2 $ $Date: 2004/12/11 21:41:33 $
30  * $Log: MultipartRequestWrapper.java,v $
31  * Revision 1.2 2004/12/11 21:41:33 svieujot
32  * Add method to get the FileItems.
33  *
34  * Revision 1.1 2004/12/01 16:32:03 svieujot
35  * Convert the Multipart filter in an ExtensionsFilter that provides an additional facility to include resources in a page.
36  * Tested only with javascript resources right now, but should work fine with images too.
37  * Some work to do to include css resources.
38  * The popup component has been converted to use this new Filter.
39  *
40  * Revision 1.8 2004/11/16 16:25:52 mmarinschek
41  * new popup - component; not yet finished
42  *
43  * Revision 1.7 2004/10/13 11:50:57 matze
44  * renamed packages to org.apache
45  *
46  * Revision 1.6 2004/09/09 13:43:59 manolito
47  * query string parameters where missing in the parameter map
48  *
49  * Revision 1.5 2004/08/16 18:06:47 svieujot
50  * Another bug fix for bug #1001511. Patch submitted by Takashi Okamoto.
51  *
52  * Revision 1.4 2004/08/02 04:26:06 svieujot
53  * Fix for bug #1001511 : setHeaderEncoding
54  *
55  */

56 public class MultipartRequestWrapper
57         extends HttpServletRequestWrapper JavaDoc
58 {
59     private static Log log = LogFactory.getLog(MultipartRequestWrapper.class);
60
61     HttpServletRequest JavaDoc request = null;
62     HashMap parametersMap = null;
63     DiskFileUpload fileUpload = null;
64     HashMap fileItems = null;
65     int maxSize;
66     int thresholdSize;
67     String JavaDoc repositoryPath;
68
69     public MultipartRequestWrapper(HttpServletRequest JavaDoc request,
70                                    int maxSize, int thresholdSize,
71                                    String JavaDoc repositoryPath){
72         super( request );
73         this.request = request;
74         this.maxSize = maxSize;
75         this.thresholdSize = thresholdSize;
76         this.repositoryPath = repositoryPath;
77     }
78     
79     private void parseRequest() {
80         fileUpload = new DiskFileUpload();
81         fileUpload.setFileItemFactory(new DefaultFileItemFactory());
82         fileUpload.setSizeMax(maxSize);
83
84         fileUpload.setSizeThreshold(thresholdSize);
85
86         if(repositoryPath != null && repositoryPath.trim().length()>0)
87             fileUpload.setRepositoryPath(repositoryPath);
88
89         String JavaDoc charset = request.getCharacterEncoding();
90         fileUpload.setHeaderEncoding(charset);
91
92
93         List requestParameters = null;
94         try{
95             requestParameters = fileUpload.parseRequest(request);
96         } catch (FileUploadBase.SizeLimitExceededException e) {
97
98             // TODO: find a way to notify the user about the fact that the uploaded file exceeded size limit
99

100             if(log.isInfoEnabled())
101                 log.info("user tried to upload a file that exceeded file-size limitations.",e);
102
103             requestParameters = Collections.EMPTY_LIST;
104
105         }catch(FileUploadException fue){
106             log.error("Exception while uploading file.", fue);
107             requestParameters = Collections.EMPTY_LIST;
108         }
109
110         parametersMap = new HashMap( requestParameters.size() );
111         fileItems = new HashMap();
112
113         for (Iterator iter = requestParameters.iterator(); iter.hasNext(); ){
114             FileItem fileItem = (FileItem) iter.next();
115
116             if (fileItem.isFormField()) {
117                 String JavaDoc name = fileItem.getFieldName();
118
119                 // The following code avoids commons-fileupload charset problem.
120
// After fixing commons-fileupload, this code should be
121
//
122
// String value = fileItem.getString();
123
//
124
String JavaDoc value = null;
125                 if ( charset == null) {
126                     value = fileItem.getString();
127                 } else {
128                     try {
129                         value = new String JavaDoc(fileItem.get(), charset);
130                     } catch (UnsupportedEncodingException JavaDoc e){
131                         value = fileItem.getString();
132                     }
133                 }
134
135                 addTextParameter(name, value);
136             } else { // fileItem is a File
137
if (fileItem.getName() != null) {
138                     fileItems.put(fileItem.getFieldName(), fileItem);
139                 }
140             }
141         }
142
143         //Add the query string paramters
144
for (Iterator it = request.getParameterMap().entrySet().iterator(); it.hasNext(); )
145         {
146             Map.Entry entry = (Map.Entry)it.next();
147             String JavaDoc[] valuesArray = (String JavaDoc[])entry.getValue();
148             for (int i = 0; i < valuesArray.length; i++)
149             {
150                 addTextParameter((String JavaDoc)entry.getKey(), valuesArray[i]);
151             }
152         }
153     }
154     
155     private void addTextParameter(String JavaDoc name, String JavaDoc value){
156         if( ! parametersMap.containsKey( name ) ){
157             String JavaDoc[] valuesArray = {value};
158             parametersMap.put(name, valuesArray);
159         }else{
160             String JavaDoc[] storedValues = (String JavaDoc[])parametersMap.get( name );
161             int lengthSrc = storedValues.length;
162             String JavaDoc[] valuesArray = new String JavaDoc[lengthSrc+1];
163             System.arraycopy(storedValues, 0, valuesArray, 0, lengthSrc);
164             valuesArray[lengthSrc] = value;
165             parametersMap.put(name, valuesArray);
166         }
167     }
168     
169     public Enumeration getParameterNames() {
170         if( parametersMap == null ) parseRequest();
171         
172         return Collections.enumeration( parametersMap.keySet() );
173     }
174     
175     public String JavaDoc getParameter(String JavaDoc name) {
176         if( parametersMap == null ) parseRequest();
177         
178         String JavaDoc[] values = (String JavaDoc[])parametersMap.get( name );
179         if( values == null )
180             return null;
181         return values[0];
182     }
183     
184     public String JavaDoc[] getParameterValues(String JavaDoc name) {
185         if( parametersMap == null ) parseRequest();
186         
187         return (String JavaDoc[])parametersMap.get( name );
188     }
189     
190     public Map getParameterMap() {
191         if( parametersMap == null ) parseRequest();
192         
193         return parametersMap;
194     }
195     
196     // Hook for the x:inputFileUpload tag.
197
public FileItem getFileItem(String JavaDoc fieldName) {
198         if( fileItems == null ) parseRequest();
199         
200         return (FileItem) fileItems.get( fieldName );
201     }
202     
203     /**
204      * Not used internaly by MyFaces, but provides a way to handle the uploaded files
205      * out of MyFaces.
206      */

207     public Map getFileItems(){
208         return fileItems;
209     }
210 }
211
Popular Tags