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