001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.multipart; 016 017 import java.io.IOException; 018 019 import javax.servlet.ServletException; 020 import javax.servlet.http.HttpServletRequest; 021 import javax.servlet.http.HttpServletResponse; 022 023 import org.apache.tapestry.services.ServletRequestServicer; 024 import org.apache.tapestry.services.ServletRequestServicerFilter; 025 026 /** 027 * Checks to see if the request is a file upload and, if so, uses the 028 * {@link org.apache.tapestry.multipart.MultipartDecoder} to obtain form parameters. 029 * 030 * @author Howard M. Lewis Ship 031 * @since 4.0 032 */ 033 public class MultipartDecoderFilter implements ServletRequestServicerFilter 034 { 035 private ServletMultipartDecoder _decoder; 036 037 public void service(HttpServletRequest request, HttpServletResponse response, 038 ServletRequestServicer servicer) throws IOException, ServletException 039 { 040 String contentType = request.getContentType(); 041 042 // contentType is occasionally null in testing. The browser tacks on additional 043 // information onto the contentType to indicate where the boundaries are in 044 // the stream. 045 046 boolean encoded = contentType != null && contentType.startsWith("multipart/form-data"); 047 048 try 049 { 050 HttpServletRequest newRequest = encoded ? _decoder.decode(request) : request; 051 052 servicer.service(newRequest, response); 053 } 054 finally 055 { 056 if (encoded) 057 _decoder.cleanup(); 058 } 059 } 060 061 public void setDecoder(ServletMultipartDecoder decoder) 062 { 063 _decoder = decoder; 064 } 065 }