001: // Copyright 2007 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:
015: package org.apache.tapestry.upload.services;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
018:
019: import java.io.File;
020: import java.util.List;
021: import java.util.Map;
022:
023: import javax.servlet.http.HttpServletRequest;
024:
025: import org.apache.commons.fileupload.FileItem;
026: import org.apache.commons.fileupload.FileItemFactory;
027: import org.apache.commons.fileupload.FileUploadException;
028: import org.apache.commons.fileupload.disk.DiskFileItemFactory;
029: import org.apache.commons.fileupload.servlet.ServletFileUpload;
030: import org.apache.tapestry.ioc.services.ThreadCleanupListener;
031:
032: /**
033: * Implementation of multipart decoder for servlets.
034: */
035: class MultipartDecoderImpl implements MultipartDecoder,
036: ThreadCleanupListener {
037: private final Map<String, UploadedFileItem> _uploads = newMap();
038:
039: private final String _repositoryLocation;
040:
041: private final int _repositoryThreshold;
042:
043: private final long _maxRequestSize;
044:
045: private final long _maxFileSize;
046:
047: public MultipartDecoderImpl(String repositoryLocation,
048: int repositoryThreshold, long maxRequestSize,
049: long maxFileSize) {
050: _repositoryLocation = repositoryLocation;
051: _repositoryThreshold = repositoryThreshold;
052: _maxRequestSize = maxRequestSize;
053: _maxFileSize = maxFileSize;
054: }
055:
056: public UploadedFile getFileUpload(String parameterName) {
057: return _uploads.get(parameterName);
058: }
059:
060: public HttpServletRequest decode(HttpServletRequest request) {
061: // String encoding = request.getCharacterEncoding();
062: List<FileItem> fileItems = parseRequest(request);
063:
064: return processFileItems(request, fileItems);
065: }
066:
067: public void threadDidCleanup() {
068: for (UploadedFileItem uploaded : _uploads.values()) {
069: uploaded.cleanup();
070: }
071: }
072:
073: @SuppressWarnings("unchecked")
074: protected List<FileItem> parseRequest(HttpServletRequest request) {
075: try {
076: return createFileUpload().parseRequest(request);
077: } catch (FileUploadException e) {
078: throw new RuntimeException(UploadMessages.unableToDecode(),
079: e);
080: }
081: }
082:
083: protected ServletFileUpload createFileUpload() {
084: FileItemFactory factory = new DiskFileItemFactory(
085: _repositoryThreshold, new File(_repositoryLocation));
086: ServletFileUpload upload = new ServletFileUpload(factory);
087:
088: // set maximum file upload size
089: upload.setSizeMax(_maxRequestSize);
090: upload.setFileSizeMax(_maxFileSize);
091:
092: return upload;
093: }
094:
095: protected HttpServletRequest processFileItems(
096: HttpServletRequest request, List<FileItem> fileItems) {
097: if (fileItems == null || fileItems.isEmpty()) {
098: return request;
099: }
100:
101: ParametersServletRequestWrapper wrapper = new ParametersServletRequestWrapper(
102: request);
103:
104: for (FileItem item : fileItems) {
105: if (item.isFormField()) {
106: wrapper.addParameter(item.getFieldName(), item
107: .getString());
108: } else {
109: wrapper.addParameter(item.getFieldName(), item
110: .getName());
111: addUploadedFile(item.getFieldName(),
112: new UploadedFileItem(item));
113: }
114: }
115:
116: return wrapper;
117: }
118:
119: protected void addUploadedFile(String name, UploadedFileItem file) {
120: _uploads.put(name, file);
121: }
122: }
|