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  
20  package org.apache.myfaces.tobago.webapp;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.apache.myfaces.tobago.internal.webapp.TobagoMultipartFormdataRequest;
25  
26  import javax.servlet.Filter;
27  import javax.servlet.FilterChain;
28  import javax.servlet.FilterConfig;
29  import javax.servlet.ServletException;
30  import javax.servlet.ServletRequest;
31  import javax.servlet.ServletResponse;
32  import javax.servlet.http.HttpServletRequest;
33  import java.io.File;
34  import java.io.IOException;
35  import java.util.Locale;
36  
37  
38  /**
39   * This filter handles multipart request. It must be enabled in the web.xml of your web application.
40   * Usage:
41   * <p/>
42   * <p><blockquote><pre>
43   * &lt;filter&gt;
44   * &lt;filter-name&gt;multipartFormdataFilter&lt;/filter-name&gt;
45   * &lt;filter-class&gt;org.apache.myfaces.tobago.webapp.TobagoMultipartFormdataFilter&lt;/filter-class&gt;
46   * &lt;init-param&gt;
47   * &lt;description&gt;Set the size limit for uploaded files. Default value is 1 MB.
48   * Format: 10 = 10 bytes
49   * 10k = 10 KB
50   * 10m = 10 MB
51   * 1g = 1 GB
52   * &lt;/description&gt;
53   * &lt;param-name&gt;uploadMaxFileSize&lt;/param-name&gt;
54   * &lt;param-value&gt;20m&lt;/param-value&gt;
55   * &lt;/init-param&gt;
56   * &lt;init-param&gt;
57   * &lt;description&gt;Set the upload repository path for uploaded files.
58   * Default value is java.io.tmpdir.&lt;/description&gt;
59   * &lt;param-name&gt;uploadRepositoryPath&lt;/param-name&gt;
60   * &lt;param-value&gt;/tmp&lt;/param-value&gt;
61   * &lt;/init-param&gt;
62   * &lt;/filter&gt;
63   * &lt;filter-mapping&gt;
64   * &lt;filter-name&gt;multipartFormdataFilter&lt;/filter-name&gt;
65   * &lt;url-pattern&gt;/faces/*&lt;/url-pattern&gt;
66   * &lt;/filter-mapping&gt;
67   * </pre></blockquote><p>
68   */
69  public class TobagoMultipartFormdataFilter implements Filter {
70  
71    private static final Logger LOG = LoggerFactory.getLogger(TobagoMultipartFormdataFilter.class);
72  
73    private String repositoryPath = System.getProperty("java.io.tmpdir");
74    private long maxSize = TobagoMultipartFormdataRequest.ONE_MB;
75  
76    public void init(FilterConfig filterConfig) throws ServletException {
77      String repositoryPath = filterConfig.getInitParameter("uploadRepositoryPath");
78      if (repositoryPath != null) {
79        File file = new File(repositoryPath);
80        if (!file.exists()) {
81          LOG.error("Given repository Path for " + getClass().getName() + " " + repositoryPath + " doesn't exists");
82        } else if (!file.isDirectory()) {
83          LOG.error("Given repository Path for " + getClass().getName() + " " + repositoryPath + " is not a directory");
84        } else {
85          this.repositoryPath = repositoryPath;
86        }
87      }
88  
89  
90      maxSize = TobagoMultipartFormdataRequest.getMaxSize(filterConfig.getInitParameter("uploadMaxFileSize"));
91      if (LOG.isInfoEnabled()) {
92        LOG.info("Configure uploadRepositryPath for " + getClass().getName() + " to " + this.repositoryPath);
93        LOG.info("Configure uploadMaxFileSize for " + getClass().getName() + " to " + this.maxSize);
94      }
95  
96    }
97  
98    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
99        throws IOException, ServletException {
100     ServletRequest wrapper;
101     if (request instanceof HttpServletRequest) {
102       if (request instanceof TobagoMultipartFormdataRequest) {
103         wrapper = request;
104       } else {
105         String contentType = request.getContentType();
106         if (contentType != null
107             && contentType.toLowerCase(Locale.ENGLISH).startsWith("multipart/form-data")) {
108           if (LOG.isDebugEnabled()) {
109             LOG.debug("Wrapping " + request.getClass().getName()
110                 + " with ContentType=\"" + contentType + "\" "
111                 + "into TobagoMultipartFormdataRequest");
112           }
113           wrapper = new TobagoMultipartFormdataRequest(
114               (HttpServletRequest) request, repositoryPath, maxSize);
115         } else {
116           wrapper = request;
117         }
118       }
119     } else {
120       LOG.error("Not implemented for non HttpServletRequest");
121       wrapper = request;
122     }
123 
124     chain.doFilter(wrapper, response);
125   }
126 
127   public void destroy() {
128   }
129 
130 }