View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.component.html.util;
20  
21  import java.io.IOException;
22  
23  import javax.servlet.Filter;
24  import javax.servlet.FilterChain;
25  import javax.servlet.FilterConfig;
26  import javax.servlet.ServletException;
27  import javax.servlet.ServletRequest;
28  import javax.servlet.ServletResponse;
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.commons.fileupload.servlet.ServletFileUpload;
33  import org.apache.myfaces.webapp.filter.MultipartRequestWrapper;
34  
35  
36  /**
37   * This filters is mandatory for the use of many components.
38   * It handles the Multipart requests (for file upload)
39   * It's used by the components that need javascript libraries
40   *
41   * @author Sylvain Vieujot (latest modification by $Author: lu4242 $)
42   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a>
43   * @version $Revision: 782515 $ $Date: 2009-06-07 22:28:42 -0500 (Sun, 07 Jun 2009) $
44   */
45  public class MultipartFilter implements Filter
46  {
47  
48      private int uploadMaxSize = 100 * 1024 * 1024; // 100 MB
49      
50      private int uploadMaxFileSize = 100 * 1024 * 1024; // 10 MB
51  
52      private int uploadThresholdSize = 1 * 1024 * 1024; // 1 MB
53  
54      private String uploadRepositoryPath = null; //standard temp directory
55      
56      private boolean cacheFileSizeErrors = false;
57  
58      public void init(FilterConfig filterConfig)
59      {
60          uploadMaxFileSize = resolveSize(filterConfig.getInitParameter("uploadMaxFileSize"), uploadMaxFileSize);
61          String param = filterConfig.getInitParameter("uploadMaxSize");
62          if (param != null)
63          {
64              uploadMaxSize = resolveSize(param, uploadMaxSize);
65          }
66          else
67          {
68              //If not set, default to uploadMaxFileSize
69              uploadMaxSize = resolveSize(param, uploadMaxFileSize);
70          }
71          uploadThresholdSize = resolveSize(filterConfig.getInitParameter("uploadThresholdSize"), uploadThresholdSize);
72          uploadRepositoryPath = filterConfig.getInitParameter("uploadRepositoryPath");
73          cacheFileSizeErrors = getBooleanValue(filterConfig.getInitParameter("cacheFileSizeErrors"), false);
74      }
75  
76  
77      private int resolveSize(String param, int defaultValue)
78      {
79          int numberParam = defaultValue;
80  
81          if (param != null)
82          {
83              param = param.toLowerCase();
84              int factor = 1;
85              String number = param;
86  
87              if (param.endsWith("g"))
88              {
89                  factor = 1024 * 1024 * 1024;
90                  number = param.substring(0, param.length() - 1);
91              } else if (param.endsWith("m"))
92              {
93                  factor = 1024 * 1024;
94                  number = param.substring(0, param.length() - 1);
95              } else if (param.endsWith("k"))
96              {
97                  factor = 1024;
98                  number = param.substring(0, param.length() - 1);
99              }
100 
101             numberParam = Integer.parseInt(number) * factor;
102         }
103         return numberParam;
104     }
105     
106     private static boolean getBooleanValue(String initParameter, boolean defaultVal)
107     {
108         if(initParameter == null || initParameter.trim().length()==0)
109             return defaultVal;
110 
111         return (initParameter.equalsIgnoreCase("on") || initParameter.equals("1") || initParameter.equalsIgnoreCase("true"));
112     }
113 
114     public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
115     {
116         if (!(response instanceof HttpServletResponse))
117         {
118             chain.doFilter(request, response);
119             return;
120         }
121 
122         HttpServletRequest httpRequest = (HttpServletRequest) request;
123 
124         // For multipart/form-data requests
125         if (ServletFileUpload.isMultipartContent(httpRequest))
126         {
127             chain.doFilter(new MultipartRequestWrapper(httpRequest, uploadMaxFileSize, 
128                     uploadThresholdSize, uploadRepositoryPath, uploadMaxSize , cacheFileSizeErrors), response);
129         } else
130         {
131             chain.doFilter(request, response);
132         }
133     }
134 
135 
136     public void destroy()
137     {
138         // NoOp
139     }
140 }