/** * This work was created by participants in the DataONE project, and is * jointly copyrighted by participating institutions in DataONE. For * more information on DataONE, see our web site at http://dataone.org. * * Copyright ${year} * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * $Id$ */ package org.dataone.mimemultipart; import java.io.*; import java.util.*; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUpload; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.log4j.Logger; /* * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * DataOne conversion of HttpServletRequests into a DataONE MultipartRequest via * Jakarta Commons FileUpload * 1.2 or above. Inspired by MultipartResolver * but almost entirely rewritten by DataONE. * *
Provides "maxUploadSize", "maxMemorySize" and "tmpUploadDir" settings as * bean properties. See corresponding * ServletFileUpload / DiskFileItemFactory properties ("sizeMax", "sizeThreshold", * ) for details in terms of defaults and accepted values. * *
Saves temporary files to the temporary directory.
*
* @author Robert P. Waltz (DataONE)
* @author Trevor D. Cook (Original)
* @author Juergen Hoeller (Original)
* @since 29.09.2003
* @see #CommonsMultipartResolver(ServletContext)
* @see org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver
* @see org.apache.commons.fileupload.servlet.ServletFileUpload
* @see org.apache.commons.fileupload.disk.DiskFileItemFactory
*/
public class MultipartRequestResolver {
Logger logger = Logger.getLogger(MultipartRequestResolver.class.getName());
private DiskFileItemFactory factory;
private org.apache.commons.fileupload.servlet.ServletFileUpload upload;
final static int SIZE = 16384;
/**
* constructor.
* @param filePartFile a Directory path to write any file contents to.
*
*/
public MultipartRequestResolver() {
// Create a factory for disk-based file items
this.factory = new DiskFileItemFactory();
// Create a new file upload handler
this.upload = new ServletFileUpload(this.factory);
}
public MultipartRequestResolver(String tmpUploadDir) {
this.factory = new DiskFileItemFactory();
this.factory.setRepository(new File(tmpUploadDir));
this.upload = new ServletFileUpload(factory);
}
public MultipartRequestResolver(String tmpUploadDir, int maxUploadSize) {
this.factory = new DiskFileItemFactory();
// Set factory constraints
this.factory.setRepository(new File(tmpUploadDir));
// Create a new file upload handler
this.upload = new ServletFileUpload(factory);
// Set overall request size constraint
this.upload.setSizeMax(maxUploadSize);
}
public MultipartRequestResolver(String tmpUploadDir, int maxUploadSize, int maxMemorySize) {
this.factory = new DiskFileItemFactory();
// Set factory constraints
this.factory.setSizeThreshold(maxMemorySize);
this.factory.setRepository(new File(tmpUploadDir));
// Create a new file upload handler
this.upload = new ServletFileUpload(factory);
// Set overall request size constraint
this.upload.setSizeMax(maxUploadSize);
}
/**
* get the parts of a
*
* @throws IOException
*/
public MultipartRequest resolveMultipart(HttpServletRequest request) throws IOException, FileUploadException, Exception {
Map