Revision
11640 <http://svn.magnolia.info/view/?rev=11640&view=rev>
Author
fgiust
Date
2007-09-30 22:53:46 +0200 (Sun, 30 Sep 2007)
Log Message
MAGNOLIA-540 <http://jira.magnolia.info/browse/MAGNOLIA-540> Replacing default
multipart filter implementation: now commons-fileupload is enabled by default and cos
is optional
Modified Paths
* magnolia/trunk/magnolia-core/pom.xml
<#magnoliatrunkmagnoliacorepomxml>
* magnolia/trunk/magnolia-module-admininterface/pom.xml
<#magnoliatrunkmagnoliamoduleadmininterfacepomxml>
* magnolia/trunk/pom.xml <#magnoliatrunkpomxml>
Added Paths
*
magnolia/trunk/magnolia-core/src/main/java/info/magnolia/cms/filters/CosMultipartRequestFilter.java
<#magnoliatrunkmagnoliacoresrcmainjavainfomagnoliacmsfiltersCosMultipartRequestFilterjava>
Removed Paths
*
magnolia/trunk/magnolia-core/src/main/java/info/magnolia/cms/filters/CommonsFileUploadMultipartRequestFilter.java
<#magnoliatrunkmagnoliacoresrcmainjavainfomagnoliacmsfiltersCommonsFileUploadMultipartRequestFilterjava>
Diff
Modified: magnolia/trunk/magnolia-core/pom.xml (11639 => 11640)
--- magnolia/trunk/magnolia-core/pom.xml 2007-09-30 20:52:06 UTC (rev
11639)
+++ magnolia/trunk/magnolia-core/pom.xml 2007-09-30 20:53:46 UTC (rev
11640)
@@ -125,7 +125,7 @@
<dependency>
<groupId>servlets.com</groupId>
<artifactId>cos</artifactId>
- <version>05Nov2002</version>
+ <optional>true</optional>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
Deleted:
magnolia/trunk/magnolia-core/src/main/java/info/magnolia/cms/filters/CommonsFileUploadMultipartRequestFilter.java
(11639 => 11640)
---
magnolia/trunk/magnolia-core/src/main/java/info/magnolia/cms/filters/CommonsFileUploadMultipartRequestFilter.java
2007-09-30 20:52:06 UTC (rev 11639)
+++
magnolia/trunk/magnolia-core/src/main/java/info/magnolia/cms/filters/CommonsFileUploadMultipartRequestFilter.java
2007-09-30 20:53:46 UTC (rev 11640)
@@ -1,249 +0,0 @@
-/**
- *
- * Magnolia and its source-code is licensed under the LGPL.
- * You may copy, adapt, and redistribute this file for commercial or
non-commercial use.
- * When copying, adapting, or redistributing this document in keeping with the
guidelines above,
- * you are required to provide proper attribution to obinary.
- * If you reproduce or distribute the document without making any substantive
modifications to its content,
- * please use the following attribution line:
- *
- * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights
reserved.
- *
- */
-package info.magnolia.cms.filters;
-
-import info.magnolia.cms.beans.runtime.MultipartForm;
-import info.magnolia.cms.core.Path;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.util.Enumeration;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import javax.servlet.FilterChain;
-import javax.servlet.FilterConfig;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletRequestWrapper;
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileUploadBase;
-import org.apache.commons.fileupload.disk.DiskFileItemFactory;
-import org.apache.commons.fileupload.servlet.ServletFileUpload;
-import org.apache.commons.fileupload.servlet.ServletRequestContext;
-import org.apache.commons.lang.ArrayUtils;
-import org.apache.commons.lang.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-
-/**
- * A <code>Filter</code> that determines if a <code>HttpServletRequest</code>
contains multipart content and if so
- * parses it into a request attribute for further processing. This
implementation uses jakarta commons-fileupload for
- * parsing multipart requests. Maximum file size can be configured using the
"maxFileSize" init parameter, defaulting to
- * 2 GB.
- * @author Andreas Brenk
- * @author Fabrizio Giustina
- * @version $Id$
- */
-public class CommonsFileUploadMultipartRequestFilter extends
AbstractMagnoliaFilter {
-
- /**
- * Logger.
- */
- protected static Logger log =
LoggerFactory.getLogger(MultipartRequestFilter.class);
-
- /**
- * Default max file upload size (2 GB).
- */
- private static final int DEFAULT_MAX_FILE_SIZE = 2000000000; // 2GB
-
- /**
- * Config parameter name for max file size.
- */
- private static final String PARAM_MAX_FILE_SIZE = "maxFileSize";
-
- /**
- * The maximum size a file upload may have.
- */
- private long maxFileSize = DEFAULT_MAX_FILE_SIZE;
-
- /**
- * The directory for temporary storage of uploaded files.
- */
- private File tempDir;
-
- /**
- * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
- */
-
- public void init(FilterConfig config) throws ServletException {
- super.init(config);
- String maxFileSize = config.getInitParameter(PARAM_MAX_FILE_SIZE);
- if (maxFileSize != null) {
- this.maxFileSize = Long.parseLong(maxFileSize);
- }
-
- this.tempDir = new File(Path.getTempDirectoryPath());
- }
-
- /**
- * Determine if the request has multipart content and if so parse it into a
<code>MultipartForm</code> and store
- * it as a request attribute.
- */
-
- public void doFilter(HttpServletRequest request, HttpServletResponse
response, FilterChain chain)
- throws IOException, ServletException {
-
- boolean isMultipartContent = FileUploadBase.isMultipartContent(new
ServletRequestContext(request));
- if (isMultipartContent) {
- try {
- MultipartForm mpf = parseRequest(request);
-
- // wrap the request
- MultipartRequestWrapper mrw = new
MultipartRequestWrapper(request, mpf);
- chain.doFilter(mrw, response);
- return;
-
- }
- catch (IOException e) {
- throw e;
- }
- catch (Exception e) {
- throw new ServletException(e);
- }
- }
-
- chain.doFilter(request, response);
- }
-
- /**
- * Parse the request and store it as a request attribute.
- */
- private MultipartForm parseRequest(HttpServletRequest request) throws
Exception {
- MultipartForm form = new MultipartForm();
-
- ServletFileUpload upload = newServletFileUpload();
-
- List fileItems = upload.parseRequest(request);
-
- for (Iterator fileItemIterator = fileItems.iterator();
fileItemIterator.hasNext();) {
- FileItem item = (FileItem) fileItemIterator.next();
- if (item.isFormField()) {
- addField(request, item, form);
- }
- else {
- addFile(item, form);
- }
- }
-
- request.setAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME, form);
- return form;
- }
-
- /**
- * Create a new <code>DiskFileUpload</code>.
- */
- private ServletFileUpload newServletFileUpload() {
- ServletFileUpload upload = new ServletFileUpload();
- upload.setSizeMax(this.maxFileSize);
- DiskFileItemFactory fif = new DiskFileItemFactory();
- fif.setRepository(Path.getTempDirectory());
- upload.setFileItemFactory(fif);
-
- return upload;
- }
-
- /**
- * Add the <code>FileItem</code> as a paramter into the
<code>MultipartForm</code>.
- */
- private void addField(HttpServletRequest request, FileItem item,
MultipartForm form) {
- String name = item.getFieldName();
-
- String value;
- try {
- String encoding = StringUtils.defaultString(request.getCharacterEncoding(),
"UTF-8");
- value = item.getString(encoding);
- }
- catch (UnsupportedEncodingException ex) {
- value = item.getString();
- }
-
- form.addParameter(name, value);
-
- String[] values = form.getParameterValues(name);
- if (values == null) {
- form.addparameterValues(name, new String[]{value});
- }
- else {
- form.addparameterValues(name, (String[]) ArrayUtils.add(values,
value));
- }
- }
-
- /**
- * Add the <code>FileItem</code> as a document into the
<code>MultipartForm</code>.
- */
- private void addFile(FileItem item, MultipartForm form) throws Exception {
- String atomName = item.getFieldName();
- String fileName = item.getName();
- String type = item.getContentType();
- File file = File.createTempFile(atomName, null, this.tempDir);
- item.write(file);
-
- form.addDocument(atomName, fileName, type, file);
- }
-
- static class MultipartRequestWrapper extends HttpServletRequestWrapper {
-
- private MultipartForm form;
-
- /**
- * @param request
- */
- public MultipartRequestWrapper(HttpServletRequest request,
MultipartForm form) {
- super(request);
- this.form = form;
- }
-
- /**
- * [EMAIL PROTECTED]
- */
-
- public String getParameter(String name) {
- String value = form.getParameter(name);
- log.debug("getParameter: {}={}", name, value);
- return value;
- }
-
- /**
- * [EMAIL PROTECTED]
- */
-
- public Map getParameterMap() {
- return form.getParameters();
- }
-
- /**
- * [EMAIL PROTECTED]
- */
-
- public Enumeration getParameterNames() {
- return form.getParameterNames();
- }
-
- /**
- * [EMAIL PROTECTED]
- */
-
- public String[] getParameterValues(String name) {
- String[] value = form.getParameterValues(name);
- log.debug("getParameterValues: {}={}", name, value);
- return value;
- }
-
- }
-}
Copied:
magnolia/trunk/magnolia-core/src/main/java/info/magnolia/cms/filters/CosMultipartRequestFilter.java
(from rev 11541,
magnolia/trunk/magnolia-core/src/main/java/info/magnolia/cms/filters/MultipartRequestFilter.java)
(11541 => 11640)
---
magnolia/trunk/magnolia-core/src/main/java/info/magnolia/cms/filters/MultipartRequestFilter.java
2007-09-26 13:59:55 UTC (rev 11541)
+++
magnolia/trunk/magnolia-core/src/main/java/info/magnolia/cms/filters/CosMultipartRequestFilter.java
2007-09-30 20:53:46 UTC (rev 11640)
@@ -0,0 +1,92 @@
+/**
+ *
+ * Magnolia and its source-code is licensed under the LGPL.
+ * You may copy, adapt, and redistribute this file for commercial or
non-commercial use.
+ * When copying, adapting, or redistributing this document in keeping with the
guidelines above,
+ * you are required to provide proper attribution to obinary.
+ * If you reproduce or distribute the document without making any substantive
modifications to its content,
+ * please use the following attribution line:
+ *
+ * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights
reserved.
+ *
+ */
+package info.magnolia.cms.filters;
+
+import info.magnolia.cms.beans.runtime.MultipartForm;
+import info.magnolia.cms.core.Path;
+
+import java.io.IOException;
+import java.util.Enumeration;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.lang.StringUtils;
+
+import com.oreilly.servlet.MultipartRequest;
+
+
+/**
+ * @author Sameer Charles
+ * @version $Id$
+ */
+public class CosMultipartRequestFilter extends AbstractMagnoliaFilter {
+
+ /**
+ * Max file upload size.
+ */
+ private static final int MAX_FILE_SIZE = 2000000000; // 2GB
+
+ public void doFilter(HttpServletRequest request, HttpServletResponse
response, FilterChain chain) throws IOException, ServletException{
+
+ String type = null;
+ String type1 = request.getHeader("Content-Type"); //$NON-NLS-1$
+ String type2 = request.getContentType();
+ if (type1 == null && type2 != null) {
+ type = type2;
+ }
+ else if (type2 == null && type1 != null) {
+ type = type1;
+ }
+ else if (type1 != null) {
+ type = (type1.length() > type2.length() ? type1 : type2);
+ }
+ if ((type != null) &&
type.toLowerCase().startsWith("multipart/form-data")) { //$NON-NLS-1$
+ parseParameters(request);
+ }
+ chain.doFilter(request, response);
+ }
+
+ /**
+ * Adds all request paramaters as request attributes.
+ * @param request HttpServletRequest
+ */
+ private static void parseParameters(HttpServletRequest request) throws
IOException {
+ MultipartForm form = new MultipartForm();
+ String encoding = StringUtils.defaultString(request.getCharacterEncoding(),
"UTF-8"); //$NON-NLS-1$
+ MultipartRequest multi = new MultipartRequest(
+ request,
+ Path.getTempDirectoryPath(),
+ MAX_FILE_SIZE,
+ encoding,
+ null);
+ Enumeration params = multi.getParameterNames();
+ while (params.hasMoreElements()) {
+ String name = (String) params.nextElement();
+ String value = multi.getParameter(name);
+ form.addParameter(name, value);
+ String[] s = multi.getParameterValues(name);
+ if (s != null) {
+ form.addparameterValues(name, s);
+ }
+ }
+ Enumeration files = multi.getFileNames();
+ while (files.hasMoreElements()) {
+ String name = (String) files.nextElement();
+ form.addDocument(name, multi.getFilesystemName(name),
multi.getContentType(name), multi.getFile(name));
+ }
+ request.setAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME, form);
+ }
+}
Modified:
magnolia/trunk/magnolia-module-admininterface/pom.xml (11639
=> 11640)
--- magnolia/trunk/magnolia-module-admininterface/pom.xml 2007-09-30
20:52:06 UTC (rev 11639)
+++ magnolia/trunk/magnolia-module-admininterface/pom.xml 2007-09-30
20:53:46 UTC (rev 11640)
@@ -70,11 +70,6 @@
<artifactId>jdom</artifactId>
</dependency>
<dependency>
- <groupId>servlets.com</groupId>
- <artifactId>cos</artifactId>
- <version>05Nov2002</version>
- </dependency>
- <dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</dependency>
Modified: magnolia/trunk/pom.xml (11639 => 11640)
--- magnolia/trunk/pom.xml 2007-09-30 20:52:06 UTC (rev 11639)
+++ magnolia/trunk/pom.xml 2007-09-30 20:53:46 UTC (rev 11640)
@@ -755,6 +755,12 @@
<version>2.2.1</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>servlets.com</groupId>
+ <artifactId>cos</artifactId>
+ <version>05Nov2002</version>
+ <optional>true</optional>
+ </dependency>
</dependencies>
</dependencyManagement>
<distributionManagement>