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.File; 017import java.util.List; 018import java.util.Map; 019 020import javax.portlet.ActionRequest; 021 022import org.apache.commons.fileupload.FileItemFactory; 023import org.apache.commons.fileupload.FileUploadException; 024import org.apache.commons.fileupload.disk.DiskFileItemFactory; 025import org.apache.commons.fileupload.portlet.PortletFileUpload; 026import org.apache.hivemind.ApplicationRuntimeException; 027import org.apache.tapestry.multipart.AbstractMultipartDecoder; 028import org.apache.tapestry.multipart.MultipartMessages; 029 030/** 031 * @author Raphael Jean 032 * 033 */ 034public class PortletMultipartDecoderImpl extends AbstractMultipartDecoder implements PortletMultipartDecoder { 035 036 /* maximum size of file allowed to be uploaded */ 037 protected long _sizeMax = 10000000; 038 /* 039 * boolean check for whether someone has manually set 040 * the maximum upload size directly on this service 041 */ 042 protected boolean _sizeMaxSet = false; 043 044 public ActionRequest decode(ActionRequest request) { 045 _encoding = request.getCharacterEncoding(); 046 047 PortletFileUpload upload = createFileUpload(); 048 049 try 050 { 051 List fileItems = upload.parseRequest(request); 052 053 processFileItems(fileItems); 054 } 055 catch (FileUploadException ex) 056 { 057 throw new ApplicationRuntimeException(MultipartMessages.unableToDecode(ex), ex); 058 } 059 060 Map parameterMap = buildParameterMap(); 061 062 return new UploadFormPortletParametersWrapper(request, parameterMap); 063 } 064 065 private PortletFileUpload createFileUpload() { 066 FileItemFactory factory = new DiskFileItemFactory(_thresholdSize, new File(_repositoryPath)); 067 PortletFileUpload upload = new PortletFileUpload(factory); 068 069 // set maximum file upload size 070 upload.setSizeMax(_sizeMax); 071 072 if (_encoding != null) 073 upload.setHeaderEncoding(_encoding); 074 075 return upload; 076 } 077 078 /** 079 * {@inheritDoc} 080 */ 081 public void setSizeMax(long sizeMax) 082 { 083 _sizeMaxSet = true; 084 _sizeMax = sizeMax; 085 } 086 087 /** 088 * {@inheritDoc} 089 */ 090 public boolean isMaxSizeSet() 091 { 092 return _sizeMaxSet; 093 } 094}