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
015package org.apache.tapestry.multipart;
016
017import java.io.File;
018import java.util.List;
019import java.util.Map;
020
021import javax.servlet.http.HttpServletRequest;
022
023import org.apache.commons.fileupload.FileItemFactory;
024import org.apache.commons.fileupload.FileUploadException;
025import org.apache.commons.fileupload.disk.DiskFileItemFactory;
026import org.apache.commons.fileupload.servlet.ServletFileUpload;
027import org.apache.hivemind.ApplicationRuntimeException;
028
029/**
030 * Implementation of {@link org.apache.tapestry.multipart.MultipartDecoder}that is based on <a
031 * href="http://jakarta.apache.org/commons/fileupload/">Jakarta FileUpload </a>.
032 * 
033 * @author Howard M. Lewis Ship
034 * @author Joe Panico
035 * @since 4.0
036 */
037public class MultipartDecoderImpl extends AbstractMultipartDecoder implements ServletMultipartDecoder
038{       
039        /* maximum size of file allowed to be uploaded */
040        protected long _maxSize = 10000000;
041        
042    public HttpServletRequest decode(HttpServletRequest request)
043    {
044        _encoding = request.getCharacterEncoding();
045
046        ServletFileUpload upload = createFileUpload();
047
048        try
049        {
050            List fileItems = upload.parseRequest(request);
051
052            processFileItems(fileItems);
053        }
054        catch (FileUploadException ex)
055        {
056            throw new ApplicationRuntimeException(MultipartMessages.unableToDecode(ex), ex);
057        }
058
059        Map parameterMap = buildParameterMap();
060
061        return new UploadFormParametersWrapper(request, parameterMap);
062    }
063    
064        private ServletFileUpload createFileUpload() {
065        FileItemFactory factory = new DiskFileItemFactory(_thresholdSize, new File(_repositoryPath));
066        ServletFileUpload upload = new ServletFileUpload(factory);
067        
068        // set maximum file upload size
069        upload.setSizeMax(_maxSize);
070        
071        if (_encoding != null)
072            upload.setHeaderEncoding(_encoding);
073
074        return upload;
075        }
076        
077        /**
078         * Sets the maximum size that an uploaded file will be allowed to have.
079         * @param maxSize The maximum size, in bytes.
080         */
081        public void setMaxSize(long maxSize)
082        {
083                _maxSize = maxSize;
084        }
085}