001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019 020 package org.apache.myfaces.tobago.webapp; 021 022 import org.apache.commons.logging.Log; 023 import org.apache.commons.logging.LogFactory; 024 025 import javax.servlet.Filter; 026 import javax.servlet.FilterChain; 027 import javax.servlet.FilterConfig; 028 import javax.servlet.ServletException; 029 import javax.servlet.ServletRequest; 030 import javax.servlet.ServletResponse; 031 import javax.servlet.http.HttpServletRequest; 032 import javax.servlet.http.HttpServletResponse; 033 import java.io.IOException; 034 import java.io.File; 035 import java.util.Locale; 036 037 038 /** 039 * This filter handles multipart request. It must be enabled in the web.xml of your web application. 040 * Usage: 041 * <p/> 042 * <p><blockquote><pre> 043 * <filter> 044 * <filter-name>multipartFormdataFilter</filter-name> 045 * <filter-class>org.apache.myfaces.tobago.webapp.TobagoMultipartFormdataFilter</filter-class> 046 * <init-param> 047 * <description>Set the size limit for uploaded files. Default value is 1 MB. 048 * Format: 10 = 10 bytes 049 * 10k = 10 KB 050 * 10m = 10 MB 051 * 1g = 1 GB 052 * </description> 053 * <param-name>uploadMaxFileSize</param-name> 054 * <param-value>20m</param-value> 055 * </init-param> 056 * <init-param> 057 * <description>Set the upload repository path for uploaded files. 058 * Default value is java.io.tmpdir.</description> 059 * <param-name>uploadRepositoryPath</param-name> 060 * <param-value>/tmp</param-value> 061 * </init-param> 062 * </filter> 063 * <filter-mapping> 064 * <filter-name>multipartFormdataFilter</filter-name> 065 * <url-pattern>/faces/*</url-pattern> 066 * </filter-mapping> 067 * </pre></blockquote><p> 068 */ 069 public class TobagoMultipartFormdataFilter implements Filter { 070 071 private static final Log LOG = LogFactory.getLog(TobagoMultipartFormdataFilter.class); 072 073 private String repositoryPath = System.getProperty("java.io.tmpdir"); 074 private long maxSize = TobagoMultipartFormdataRequest.ONE_MB; 075 076 public void init(FilterConfig filterConfig) throws ServletException { 077 String repositoryPath = filterConfig.getInitParameter("uploadRepositoryPath"); 078 if (repositoryPath != null) { 079 File file = new File(repositoryPath); 080 if (!file.exists()) { 081 LOG.error("Given repository Path for " + getClass().getName() + " " + repositoryPath + " doesn't exists"); 082 } else if (!file.isDirectory()) { 083 LOG.error("Given repository Path for " + getClass().getName() + " " + repositoryPath + " is not a directory"); 084 } else { 085 this.repositoryPath = repositoryPath; 086 } 087 } 088 089 LOG.info("Configure uploadRepositryPath for " + getClass().getName() + " to " + this.repositoryPath); 090 091 maxSize = TobagoMultipartFormdataRequest.getMaxSize(filterConfig.getInitParameter("uploadMaxFileSize")); 092 093 LOG.info("Configure uploadMaxFileSize for " + getClass().getName() + " to " + this.maxSize); 094 095 } 096 097 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 098 throws IOException, ServletException { 099 ServletRequest wrapper; 100 if (request instanceof HttpServletRequest) { 101 if (request instanceof TobagoMultipartFormdataRequest) { 102 wrapper = request; 103 } else { 104 String contentType = request.getContentType(); 105 if (contentType != null 106 && contentType.toLowerCase(Locale.ENGLISH).startsWith("multipart/form-data")) { 107 if (LOG.isDebugEnabled()) { 108 LOG.debug("Wrapping " + request.getClass().getName() 109 + " with ContentType=\"" + contentType + "\" " 110 + "into TobagoMultipartFormdataRequest"); 111 } 112 wrapper = new TobagoMultipartFormdataRequest( 113 (HttpServletRequest) request, repositoryPath, maxSize); 114 } else { 115 wrapper = request; 116 } 117 } 118 } else { 119 LOG.error("Not implemented for non HttpServletRequest"); 120 wrapper = request; 121 } 122 ServletResponse wrappedResponse; 123 if (response instanceof HttpServletResponse) { 124 wrappedResponse = new TobagoResponse((HttpServletResponse) response); 125 } else { 126 wrappedResponse = response; 127 } 128 129 chain.doFilter(wrapper, wrappedResponse); 130 } 131 132 public void destroy() { 133 } 134 135 }