svnno****@sourc*****
svnno****@sourc*****
2007年 9月 1日 (土) 08:14:01 JST
Revision: 515 http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=pal&view=rev&rev=515 Author: shinsuke Date: 2007-09-01 08:14:00 +0900 (Sat, 01 Sep 2007) Log Message: ----------- added servlet filter Added Paths: ----------- libraries/jsf4portlet/trunk/src/main/java/jp/sf/pal/jsf/filter/FileUploadFilter.java -------------- next part -------------- Added: libraries/jsf4portlet/trunk/src/main/java/jp/sf/pal/jsf/filter/FileUploadFilter.java =================================================================== --- libraries/jsf4portlet/trunk/src/main/java/jp/sf/pal/jsf/filter/FileUploadFilter.java 2007-08-31 23:13:25 UTC (rev 514) +++ libraries/jsf4portlet/trunk/src/main/java/jp/sf/pal/jsf/filter/FileUploadFilter.java 2007-08-31 23:14:00 UTC (rev 515) @@ -0,0 +1,125 @@ +/* + * Copyright 2005-2007 Portal Application Laboratory project. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language + * governing permissions and limitations under the License. + */ +package jp.sf.pal.jsf.filter; + +import java.io.IOException; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import jp.sf.pal.jsf.multipart.MultipartRequestWrapper; + +import org.apache.commons.fileupload.servlet.ServletFileUpload; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class FileUploadFilter implements Filter { + + private Log log = LogFactory.getLog(FileUploadFilter.class); + + public static final String FILE_UPLOAD_FILTER_CALLED = "jp.sf.pal.jsf.filter.FileUploadFilter.Called"; + + private int uploadMaxFileSize = 100 * 1024 * 1024; // 10 MB + + private int uploadThresholdSize = 1 * 1024 * 1024; // 1 MB + + private String uploadRepositoryPath = null; //standard temp directory + + /** + * Init method for this filter + */ + public void init(FilterConfig filterConfig) { + + String param = filterConfig.getInitParameter("uploadMaxFileSize"); + + uploadMaxFileSize = resolveSize(param, uploadMaxFileSize); + + param = filterConfig.getInitParameter("uploadThresholdSize"); + + uploadThresholdSize = resolveSize(param, uploadThresholdSize); + + uploadRepositoryPath = filterConfig + .getInitParameter("uploadRepositoryPath"); + + } + + public void doFilter(ServletRequest request, ServletResponse response, + FilterChain chain) throws IOException, ServletException { + if (log.isDebugEnabled()) { + log.debug("called doFilter."); + } + + if (request.getAttribute(FILE_UPLOAD_FILTER_CALLED) != null) { + chain.doFilter(request, response); + return; + } + + request.setAttribute(FILE_UPLOAD_FILTER_CALLED, Boolean.TRUE); + + if (!(response instanceof HttpServletResponse) + || !(request instanceof HttpServletRequest)) { + chain.doFilter(request, response); + return; + } + + HttpServletRequest httpServletRequest = (HttpServletRequest) request; + + // check multipart/form-data + if (ServletFileUpload.isMultipartContent(httpServletRequest)) { + httpServletRequest = new MultipartRequestWrapper( + httpServletRequest, uploadMaxFileSize, uploadThresholdSize, + uploadRepositoryPath); + } + + chain.doFilter(httpServletRequest, response); + + } + + private int resolveSize(String param, int defaultValue) { + int numberParam = defaultValue; + + if (param != null) { + param = param.toLowerCase(); + int factor = 1; + String number = param; + + if (param.endsWith("g")) { + factor = 1024 * 1024 * 1024; + number = param.substring(0, param.length() - 1); + } else if (param.endsWith("m")) { + factor = 1024 * 1024; + number = param.substring(0, param.length() - 1); + } else if (param.endsWith("k")) { + factor = 1024; + number = param.substring(0, param.length() - 1); + } + + numberParam = Integer.parseInt(number) * factor; + } + return numberParam; + } + + public void destroy() { + // nothing + } + +} Property changes on: libraries/jsf4portlet/trunk/src/main/java/jp/sf/pal/jsf/filter/FileUploadFilter.java ___________________________________________________________________ Name: svn:eol-style + native