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.File;
017    import java.util.List;
018    import java.util.Map;
019    
020    import javax.portlet.ActionRequest;
021    
022    import org.apache.commons.fileupload.FileItemFactory;
023    import org.apache.commons.fileupload.FileUploadException;
024    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
025    import org.apache.commons.fileupload.portlet.PortletFileUpload;
026    import org.apache.hivemind.ApplicationRuntimeException;
027    import org.apache.tapestry.multipart.AbstractMultipartDecoder;
028    import org.apache.tapestry.multipart.MultipartMessages;
029    
030    /**
031     * @author Raphael Jean
032     */
033    public class PortletMultipartDecoderImpl extends AbstractMultipartDecoder
034            implements PortletMultipartDecoder
035    {
036    
037        public ActionRequest decode(ActionRequest request)
038        {
039            _encoding = request.getCharacterEncoding();
040    
041            PortletFileUpload upload = createFileUpload();
042    
043            try
044            {
045                List fileItems = upload.parseRequest(request);
046    
047                processFileItems(fileItems);
048            }
049            catch (FileUploadException ex)
050            {
051                throw new ApplicationRuntimeException(MultipartMessages
052                        .unableToDecode(ex), ex);
053            }
054    
055            Map parameterMap = buildParameterMap();
056    
057            return new UploadFormPortletParametersWrapper(request, parameterMap);
058        }
059    
060        private PortletFileUpload createFileUpload()
061        {
062            FileItemFactory factory = new DiskFileItemFactory(_thresholdSize,
063                    new File(_repositoryPath));
064            PortletFileUpload upload = new PortletFileUpload(factory);
065    
066            if (_encoding != null) upload.setHeaderEncoding(_encoding);
067    
068            return upload;
069        }
070    
071    }