KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2005 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 java.io.IOException JavaDoc;
19
20 import javax.servlet.Filter JavaDoc;
21 import javax.servlet.FilterChain JavaDoc;
22 import javax.servlet.FilterConfig JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.ServletResponse JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.commons.fileupload.FileUpload;
30
31
32 /**
33  * This filters is mandatory for the use of many components.
34  * It handles the Multipart requests (for file upload)
35  * It's used by the components that need javascript libraries
36  *
37  * @author Sylvain Vieujot (latest modification by $Author: oros $)
38  * @author <a HREF="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
39  * @version $Revision: 1.1 $ $Date: 2005/03/20 23:16:08 $
40  */

41 public class MultipartFilter implements Filter JavaDoc
42 {
43
44     private int uploadMaxFileSize = 100 * 1024 * 1024; // 10 MB
45

46     private int uploadThresholdSize = 1 * 1024 * 1024; // 1 MB
47

48     private String JavaDoc uploadRepositoryPath = null; //standard temp directory
49

50
51     public void init(FilterConfig JavaDoc filterConfig)
52     {
53         uploadMaxFileSize = resolveSize(filterConfig.getInitParameter("uploadMaxFileSize"), uploadMaxFileSize);
54         uploadThresholdSize = resolveSize(filterConfig.getInitParameter("uploadThresholdSize"), uploadThresholdSize);
55         uploadRepositoryPath = filterConfig.getInitParameter("uploadRepositoryPath");
56     }
57
58
59     private int resolveSize(String JavaDoc param, int defaultValue)
60     {
61         int numberParam = defaultValue;
62
63         if (param != null)
64         {
65             param = param.toLowerCase();
66             int factor = 1;
67             String JavaDoc number = param;
68
69             if (param.endsWith("g"))
70             {
71                 factor = 1024 * 1024 * 1024;
72                 number = param.substring(0, param.length() - 1);
73             } else if (param.endsWith("m"))
74             {
75                 factor = 1024 * 1024;
76                 number = param.substring(0, param.length() - 1);
77             } else if (param.endsWith("k"))
78             {
79                 factor = 1024;
80                 number = param.substring(0, param.length() - 1);
81             }
82
83             numberParam = Integer.parseInt(number) * factor;
84         }
85         return numberParam;
86     }
87
88
89     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc
90     {
91         if (!(response instanceof HttpServletResponse JavaDoc))
92         {
93             chain.doFilter(request, response);
94             return;
95         }
96
97         HttpServletRequest JavaDoc httpRequest = (HttpServletRequest JavaDoc) request;
98
99         // For multipart/form-data requests
100
if (FileUpload.isMultipartContent(httpRequest))
101         {
102             chain.doFilter(new MultipartRequestWrapper(httpRequest, uploadMaxFileSize, uploadThresholdSize, uploadRepositoryPath), response);
103         } else
104         {
105             chain.doFilter(request, response);
106         }
107     }
108
109
110     public void destroy()
111     {
112         // NoOp
113
}
114 }
115
Popular Tags