001    // Copyright 2006 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    package org.apache.tapestry.portlet.multipart;
015    
016    import java.io.IOException;
017    
018    import javax.portlet.ActionRequest;
019    import javax.portlet.ActionResponse;
020    import javax.portlet.PortletException;
021    
022    import org.apache.tapestry.portlet.ActionRequestServicer;
023    import org.apache.tapestry.portlet.ActionRequestServicerFilter;
024    
025    /**
026     * @author Raphael Jean
027     */
028    public class PortletMultipartDecoderFilter implements
029            ActionRequestServicerFilter
030    {
031    
032        private PortletMultipartDecoder _decoder;
033    
034        public void service(ActionRequest request, ActionResponse response,
035                ActionRequestServicer servicer)
036            throws IOException, PortletException
037        {
038            String contentType = request.getContentType();
039    
040            // contentType is occasionally null in testing. The browser tacks on
041            // additional
042            // information onto the contentType to indicate where the boundaries are
043            // in
044            // the stream.
045    
046            boolean encoded = contentType != null
047                    && contentType.startsWith("multipart/form-data");
048    
049            try
050            {
051                ActionRequest newRequest = encoded ? _decoder.decode(request)
052                        : request;
053    
054                servicer.service(newRequest, response);
055            }
056            finally
057            {
058                if (encoded) _decoder.cleanup();
059            }
060        }
061    
062        public void setDecoder(PortletMultipartDecoder decoder)
063        {
064            this._decoder = decoder;
065        }
066    
067    }