This is an automated email from the ASF dual-hosted git repository.
gk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/turbine-fulcrum-upload.git
The following commit(s) were added to refs/heads/master by this push:
new f1f0edb Set turbine parent 13-SNAPSHOT with java 17 baseline; use
Jakart servlet api 6.1.0 and commons-fileupload2-jakarta-servlet6, add missing
library log4j-api in test scope
f1f0edb is described below
commit f1f0edba0bd84f02d73b95164fdc77682215d2d0
Author: Georg Kallidis <[email protected]>
AuthorDate: Tue Jul 9 15:07:20 2024 +0200
Set turbine parent 13-SNAPSHOT with java 17 baseline; use Jakart servlet
api 6.1.0 and commons-fileupload2-jakarta-servlet6, add missing library
log4j-api in test scope
---
pom.xml | 26 +-
.../fulcrum/upload/DefaultUploadService.java | 636 +++++++++------------
.../org/apache/fulcrum/upload/UploadService.java | 78 +--
.../apache/fulcrum/upload/UploadServiceTest.java | 17 +-
4 files changed, 309 insertions(+), 448 deletions(-)
diff --git a/pom.xml b/pom.xml
index 21016aa..213f070 100644
--- a/pom.xml
+++ b/pom.xml
@@ -19,7 +19,7 @@
<parent>
<artifactId>turbine-parent</artifactId>
<groupId>org.apache.turbine</groupId>
- <version>12</version>
+ <version>13-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@@ -88,28 +88,28 @@
<version>4.3.1</version>
</dependency>
<dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
+ <groupId>jakarta.servlet</groupId>
+ <artifactId>jakarta.servlet-api</artifactId>
+ <version>6.1.0</version>
<scope>provided</scope>
</dependency>
- <dependency>
+ <!--dependency>
<groupId>javax.portlet</groupId>
<artifactId>portlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
- </dependency>
+ </dependency-->
<dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.5</version>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-fileupload2-jakarta-servlet6</artifactId>
+ <version>2.0.0-M2</version>
</dependency>
<!-- testing dependencies -->
<dependency>
<groupId>org.apache.fulcrum</groupId>
<artifactId>fulcrum-testcontainer</artifactId>
- <version>2.0.0</version>
+ <version>2.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -124,6 +124,12 @@
<version>${turbine.log4j2.version}</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-api</artifactId>
+ <version>${turbine.log4j2.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/src/java/org/apache/fulcrum/upload/DefaultUploadService.java
b/src/java/org/apache/fulcrum/upload/DefaultUploadService.java
index af5a9c0..b3b8b98 100644
--- a/src/java/org/apache/fulcrum/upload/DefaultUploadService.java
+++ b/src/java/org/apache/fulcrum/upload/DefaultUploadService.java
@@ -21,11 +21,12 @@ package org.apache.fulcrum.upload;
import java.io.File;
import java.io.IOException;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.List;
-import javax.portlet.ActionRequest;
-import javax.servlet.http.HttpServletRequest;
-
import org.apache.avalon.framework.activity.Initializable;
import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
@@ -34,30 +35,30 @@ import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.service.ServiceException;
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileItemIterator;
-import org.apache.commons.fileupload.FileUploadException;
-import org.apache.commons.fileupload.disk.DiskFileItemFactory;
-import org.apache.commons.fileupload.portlet.PortletFileUpload;
-import org.apache.commons.fileupload.servlet.ServletFileUpload;
+import org.apache.commons.fileupload2.core.DiskFileItemFactory;
+import org.apache.commons.fileupload2.core.FileItem;
+import org.apache.commons.fileupload2.core.FileItemInputIterator;
+import org.apache.commons.fileupload2.core.FileUploadException;
+import
org.apache.commons.fileupload2.jakarta.servlet6.JakartaServletFileUpload;
+
+import jakarta.servlet.http.HttpServletRequest;
/**
* <p>
* This class is an implementation of {@link UploadService}.
*
* <p>
- * Files will be stored in temporary disk storage on in memory, depending on
- * request size, and will be available from the
+ * Files will be stored in temporary disk storage on in memory, depending on
request size,
+ * and will be available from the
* <code>org.apache.fulcrum.util.parser.ParameterParser</code> as
* <code>org.apache.commons.fileupload.FileItem</code> objects.
*
* <p>
- * This implementation of {@link UploadService} handles multiple files per
- * single html form, sent using multipart/form-data encoding type, as specified
- * by RFC 1867. Use
- * <code>org.apache.fulcrum.parser.ParameterParser#getFileItems(String)</code>
to
- * acquire an array of <code>org.apache.commons.fileupload.FileItem</code>
objects
- * associated with given html form.
+ * This implementation of {@link UploadService} handles multiple files per
single html
+ * form, sent using multipart/form-data encoding type, as specified by RFC
1867. Use
+ * <code>org.apache.fulcrum.parser.ParameterParser#getFileItems(String)</code>
to acquire
+ * an array of <code>org.apache.commons.fileupload.FileItem</code> objects
associated with
+ * given html form.
*
* @author <a href="mailto:[email protected]">Rafal Krzewski</a>
* @author <a href="mailto:[email protected]">Daniel Rall</a>
@@ -65,343 +66,266 @@ import
org.apache.commons.fileupload.servlet.ServletFileUpload;
* @version $Id$
*/
public class DefaultUploadService extends AbstractLogEnabled
- implements UploadService, Initializable, Configurable,
Contextualizable {
- /** A File Item Factory object for the actual uploading */
- private DiskFileItemFactory itemFactory;
-
- private int sizeThreshold;
- private int sizeMax;
-
- private String repositoryPath;
- private String headerEncoding;
-
- /**
- * The application root
- */
- private String applicationRoot;
-
- /**
- * The maximum allowed upload size
- */
- @Override
- public long getSizeMax() {
- return sizeMax;
- }
-
- /**
- * The threshold beyond which files are written directly to disk.
- */
- @Override
- public long getSizeThreshold() {
- return itemFactory.getSizeThreshold();
- }
-
- /**
- * The location used to temporarily store files that are larger than
the size
- * threshold.
- */
- @Override
- public String getRepository() {
- return itemFactory.getRepository().getAbsolutePath();
- }
-
- /**
- * @return Returns the headerEncoding.
- */
- @Override
- public String getHeaderEncoding() {
- return headerEncoding;
- }
-
- /**
- * <p>
- * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
- * <code>multipart/form-data</code> stream.
- * </p>
- *
- * @param req The servlet request to be parsed.
- * @throws ServiceException Problems reading/parsing the request or
storing the
- * uploaded file(s).
- */
- @Override
- public List<FileItem> parseRequest(HttpServletRequest req) throws
ServiceException {
- return parseRequest(req, this.sizeMax, this.itemFactory);
- }
-
- /**
- * <p>
- * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
- * <code>multipart/form-data</code> stream.
- * </p>
- *
- * @param req The servlet request to be parsed.
- * @param path The location where the files should be stored.
- * @throws ServiceException Problems reading/parsing the request or
storing the
- * uploaded file(s).
- */
- @Override
- public List<FileItem> parseRequest(HttpServletRequest req, String path)
throws ServiceException {
- return parseRequest(req, this.sizeThreshold, this.sizeMax,
path);
- }
-
- /**
- * <p>
- * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
- * <code>multipart/form-data</code> stream.
- * </p>
- *
- * @param req The servlet request to be parsed.
- * @param sizeThreshold the max size in bytes to be stored in memory
- * @param sizeMax the maximum allowed upload size in bytes
- * @param path The location where the files should be stored.
- * @return list of file items
- * @throws ServiceException Problems reading/parsing the request or
storing the
- * uploaded file(s).
- */
- @Override
- public List<FileItem> parseRequest(HttpServletRequest req, int
sizeThreshold, int sizeMax, String path)
- throws ServiceException {
- return parseRequest(req, sizeMax, new
DiskFileItemFactory(sizeThreshold, new File(path)));
- }
-
- /**
- * <p>
- * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
- * <code>multipart/form-data</code> stream.
- * </p>
- *
- * @param req The servlet request to be parsed.
- * @param sizeMax the maximum allowed upload size in bytes
- * @param factory the file item factory to use
- * @return list of file items
- * @throws ServiceException Problems reading/parsing the request or
storing the
- * uploaded file(s).
- */
- protected List<FileItem> parseRequest(HttpServletRequest req, int
sizeMax, DiskFileItemFactory factory)
- throws ServiceException {
- try {
- ServletFileUpload fileUpload = new
ServletFileUpload(factory);
- fileUpload.setSizeMax(sizeMax);
- fileUpload.setHeaderEncoding(headerEncoding);
- return fileUpload.parseRequest(req);
- } catch (FileUploadException e) {
- throw new ServiceException(UploadService.ROLE,
e.getMessage(), e);
- }
- }
-
- /**
- * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC
1867</a>
- * compliant <code>multipart/form-data</code> stream.
- *
- * @param req The servlet request to be parsed.
- *
- * @return An iterator to instances of <code>FileItemStream</code>
parsed from
- * the request, in the order that they were transmitted.
- *
- * @throws ServiceException if there are problems reading/parsing the
request or
- * storing files. This may also be a network
error
- * while communicating with the client or a
problem
- * while storing the uploaded content.
- */
- @Override
- public FileItemIterator getItemIterator(HttpServletRequest req) throws
ServiceException {
- ServletFileUpload upload = new ServletFileUpload();
- try {
- return upload.getItemIterator(req);
- } catch (FileUploadException e) {
- throw new ServiceException(UploadService.ROLE,
e.getMessage(), e);
- } catch (IOException e) {
- throw new ServiceException(UploadService.ROLE,
e.getMessage(), e);
- }
- }
-
- /**
- * <p>
- * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
- * <code>multipart/form-data</code> stream.
- * </p>
- *
- * @param req The portlet request to be parsed.
- * @throws ServiceException Problems reading/parsing the request or
storing the
- * uploaded file(s).
- */
- @Override
- public List<FileItem> parseRequest(ActionRequest req) throws
ServiceException {
- return parseRequest(req, this.sizeMax, this.itemFactory);
- }
-
- /**
- * <p>
- * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
- * <code>multipart/form-data</code> stream.
- * </p>
- *
- * @param req The portlet request to be parsed.
- * @param path The location where the files should be stored.
- * @throws ServiceException Problems reading/parsing the request or
storing the
- * uploaded file(s).
- */
- @Override
- public List<FileItem> parseRequest(ActionRequest req, String path)
throws ServiceException {
- return parseRequest(req, this.sizeThreshold, this.sizeMax,
path);
- }
-
- /**
- * <p>
- * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
- * <code>multipart/form-data</code> stream.
- * </p>
- *
- * @param req The portlet request to be parsed.
- * @param sizeThreshold the max size in bytes to be stored in memory
- * @param sizeMax the maximum allowed upload size in bytes
- * @param path The location where the files should be stored.
- * @throws ServiceException Problems reading/parsing the request or
storing the
- * uploaded file(s).
- */
- @Override
- public List<FileItem> parseRequest(ActionRequest req, int
sizeThreshold, int sizeMax, String path)
- throws ServiceException {
- return parseRequest(req, sizeMax, new
DiskFileItemFactory(sizeThreshold, new File(path)));
- }
-
- /**
- * <p>
- * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
- * <code>multipart/form-data</code> stream.
- * </p>
- *
- * @param req The portlet request to be parsed.
- * @param sizeMax the maximum allowed upload size in bytes
- * @param factory the file item factory to use
- * @return The list of FileItem parts uploaded
- * @throws ServiceException Problems reading/parsing the request or
storing the
- * uploaded file(s).
- */
- protected List<FileItem> parseRequest(ActionRequest req, int sizeMax,
DiskFileItemFactory factory)
- throws ServiceException {
- try {
- PortletFileUpload fileUpload = new
PortletFileUpload(factory);
- fileUpload.setSizeMax(sizeMax);
- fileUpload.setHeaderEncoding(headerEncoding);
- return fileUpload.parseRequest(req);
- } catch (FileUploadException e) {
- throw new ServiceException(UploadService.ROLE,
e.getMessage(), e);
- }
- }
-
- /**
- * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC
1867</a>
- * compliant <code>multipart/form-data</code> stream.
- *
- * @param req The portlet request to be parsed.
- *
- * @return An iterator to instances of <code>FileItemStream</code>
parsed from
- * the request, in the order that they were transmitted.
- *
- * @throws ServiceException if there are problems reading/parsing the
request or
- * storing files. This may also be a network
error
- * while communicating with the client or a
problem
- * while storing the uploaded content.
- */
- @Override
- public FileItemIterator getItemIterator(ActionRequest req) throws
ServiceException {
- PortletFileUpload upload = new PortletFileUpload();
- try {
- return upload.getItemIterator(req);
- } catch (FileUploadException e) {
- throw new ServiceException(UploadService.ROLE,
e.getMessage(), e);
- } catch (IOException e) {
- throw new ServiceException(UploadService.ROLE,
e.getMessage(), e);
- }
- }
-
- /**
- * Utility method that determines whether the request contains multipart
- * content.
- *
- * @param req The servlet request to be evaluated. Must be non-null.
- *
- * @return <code>true</code> if the request is multipart;
<code>false</code>
- * otherwise.
- */
- @Override
- public boolean isMultipart(HttpServletRequest req) {
- return ServletFileUpload.isMultipartContent(req);
- }
-
- /**
- * Utility method that determines whether the request contains multipart
- * content.
- *
- * @param req The portlet request to be evaluated. Must be non-null.
- *
- * @return <code>true</code> if the request is multipart;
<code>false</code>
- * otherwise.
- */
- @Override
- public boolean isMultipart(ActionRequest req) {
- return PortletFileUpload.isMultipartContent(req);
- }
-
- /**
- * @see org.apache.fulcrum.ServiceBroker#getRealPath(String)
- */
- private String getRealPath(String path) {
- String absolutePath = null;
- if (applicationRoot == null) {
- absolutePath = new File(path).getAbsolutePath();
- } else {
- absolutePath = new File(applicationRoot,
path).getAbsolutePath();
- }
-
- return absolutePath;
- }
-
- // ---------------- Avalon Lifecycle Methods ---------------------
- /**
- * Avalon component lifecycle method
- */
- @Override
- public void configure(Configuration conf) {
- repositoryPath =
conf.getAttribute(UploadService.REPOSITORY_KEY,
UploadService.REPOSITORY_DEFAULT);
-
- headerEncoding =
conf.getAttribute(UploadService.HEADER_ENCODING_KEY,
UploadService.HEADER_ENCODING_DEFAULT);
-
- sizeMax =
conf.getAttributeAsInteger(UploadService.SIZE_MAX_KEY,
UploadService.SIZE_MAX_DEFAULT);
-
- sizeThreshold =
conf.getAttributeAsInteger(UploadService.SIZE_THRESHOLD_KEY,
- UploadService.SIZE_THRESHOLD_DEFAULT);
- }
-
- /**
- * Avalon component lifecycle method
- *
- * Initializes the service.
- *
- * This method processes the repository path, to make it relative to
the web
- * application root, if necessary
- */
- @Override
- public void initialize() throws Exception {
- // test for the existence of the path within the webapp
directory.
- // if it does not exist, assume the path was to be used as is.
- String testPath = getRealPath(repositoryPath);
- File testDir = new File(testPath);
- if (testDir.exists()) {
- repositoryPath = testPath;
- }
-
- getLogger().debug("Upload Service: REPOSITORY_KEY => " +
repositoryPath);
-
- itemFactory = new DiskFileItemFactory(sizeThreshold, new
File(repositoryPath));
- }
-
- /**
- * Avalon component lifecycle method
- */
- @Override
- public void contextualize(Context context) throws ContextException {
- this.applicationRoot =
context.get("urn:avalon:home").toString();
- }
+ implements UploadService, Initializable, Configurable, Contextualizable
+{
+ /** A File Item Factory object for the actual uploading */
+ private DiskFileItemFactory itemFactory;
+
+ private int sizeThreshold;
+ private int sizeMax;
+
+ private String repositoryPath;
+ private String headerEncoding;
+
+ /**
+ * The application root
+ */
+ private String applicationRoot;
+
+ /**
+ * The maximum allowed upload size
+ */
+ @Override
+ public long getSizeMax()
+ {
+ return sizeMax;
+ }
+
+ /**
+ * The threshold beyond which files are written directly to disk.
+ */
+ @Override
+ public long getSizeThreshold()
+ {
+ return itemFactory.getThreshold();
+ }
+
+ /**
+ * The location used to temporarily store files that are larger than the
size threshold.
+ */
+ @Override
+ public String getRepository()
+ {
+ return itemFactory.getRepository().toAbsolutePath().toString();
+ }
+
+ /**
+ * @return Returns the headerEncoding.
+ */
+ @Override
+ public String getHeaderEncoding()
+ {
+ return headerEncoding;
+ }
+
+ /**
+ * <p>
+ * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
+ * <code>multipart/form-data</code> stream.
+ * </p>
+ *
+ * @param req The servlet request to be parsed.
+ * @throws ServiceException Problems reading/parsing the request or
storing the uploaded
+ * file(s).
+ */
+ @Override
+ public List<FileItem> parseRequest(HttpServletRequest req) throws
ServiceException
+ {
+ return parseRequest( req, this.sizeMax, this.itemFactory );
+ }
+
+ /**
+ * <p>
+ * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
+ * <code>multipart/form-data</code> stream.
+ * </p>
+ *
+ * @param req The servlet request to be parsed.
+ * @param path The location where the files should be stored.
+ * @throws ServiceException Problems reading/parsing the request or
storing the uploaded
+ * file(s).
+ */
+ @Override
+ public List<FileItem> parseRequest(HttpServletRequest req, String path)
throws ServiceException
+ {
+ return parseRequest( req, this.sizeThreshold, this.sizeMax, path );
+ }
+
+ /**
+ * <p>
+ * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
+ * <code>multipart/form-data</code> stream.
+ * </p>
+ *
+ * @param req The servlet request to be parsed.
+ * @param sizeThreshold the max size in bytes to be stored in memory
+ * @param sizeMax the maximum allowed upload size in bytes
+ * @param path The location where the files should be stored.
+ * @return list of file items
+ * @throws ServiceException Problems reading/parsing the request or
storing the uploaded
+ * file(s).
+ */
+ @Override
+ public List<FileItem> parseRequest(HttpServletRequest req, int
sizeThreshold, int sizeMax, String path)
+ throws ServiceException
+ {
+ Path buildPath = Paths.get( path );
+ return parseRequest( req, sizeMax, DiskFileItemFactory.builder()
+ .setPath(buildPath )
+ .setBufferSize( sizeThreshold )
+ .get()
+ );
+ }
+
+ /**
+ * <p>
+ * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
+ * <code>multipart/form-data</code> stream.
+ * </p>
+ *
+ * @param req The servlet request to be parsed.
+ * @param sizeMax the maximum allowed upload size in bytes
+ * @param factory the file item factory to use
+ * @return list of file items
+ * @throws ServiceException Problems reading/parsing the request or
storing the uploaded
+ * file(s).
+ */
+ protected List<FileItem> parseRequest(HttpServletRequest req, int sizeMax,
DiskFileItemFactory factory)
+ throws ServiceException
+ {
+ try
+ {
+ JakartaServletFileUpload fileUpload = new
JakartaServletFileUpload<>( factory );
+ fileUpload.setSizeMax( sizeMax );
+ fileUpload.setHeaderCharset( null );
+
+ if (getHeaderEncoding() != null)
+ {
+ Charset uploadCharset = getHeaderEncoding().equals( "UTF-8" )
? StandardCharsets.UTF_8
+ : getHeaderEncoding().startsWith( "ISO-8859" ) ?
StandardCharsets.ISO_8859_1 : StandardCharsets.UTF_8;
+ fileUpload.setHeaderCharset( uploadCharset );
+ // fileUpload.setHeaderEncoding(headerEncoding);
+ }
+ return fileUpload.parseRequest( req );
+ } catch (FileUploadException e)
+ {
+ throw new ServiceException( UploadService.ROLE, e.getMessage(), e
);
+ }
+ }
+
+ /**
+ * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
+ * <code>multipart/form-data</code> stream.
+ *
+ * @param req The servlet request to be parsed.
+ *
+ * @return An iterator to instances of <code>FileItemStream</code> parsed
from the
+ * request, in the order that they were transmitted.
+ *
+ * @throws ServiceException if there are problems reading/parsing the
request or storing
+ * files. This may also be a network error while
communicating
+ * with the client or a problem while storing the
uploaded
+ * content.
+ */
+ @Override
+ public FileItemInputIterator getItemIterator(HttpServletRequest req)
throws ServiceException
+ {
+ JakartaServletFileUpload upload = new JakartaServletFileUpload();
+ try
+ {
+ return upload.getItemIterator( req );
+ } catch (FileUploadException e)
+ {
+ throw new ServiceException( UploadService.ROLE, e.getMessage(), e
);
+ } catch (IOException e)
+ {
+ throw new ServiceException( UploadService.ROLE, e.getMessage(), e
);
+ }
+ }
+
+ /**
+ * Utility method that determines whether the request contains multipart
content.
+ *
+ * @param req The servlet request to be evaluated. Must be non-null.
+ *
+ * @return <code>true</code> if the request is multipart;
<code>false</code> otherwise.
+ */
+ @Override
+ public boolean isMultipart(HttpServletRequest req)
+ {
+ return JakartaServletFileUpload.isMultipartContent( req );
+ }
+
+
+ /**
+ * @see org.apache.fulcrum.ServiceBroker#getRealPath(String)
+ */
+ private String getRealPath(String path)
+ {
+ String absolutePath = null;
+ if (applicationRoot == null)
+ {
+ absolutePath = new File( path ).getAbsolutePath();
+ } else
+ {
+ absolutePath = new File( applicationRoot, path ).getAbsolutePath();
+ }
+
+ return absolutePath;
+ }
+
+ // ---------------- Avalon Lifecycle Methods ---------------------
+ /**
+ * Avalon component lifecycle method
+ */
+ @Override
+ public void configure(Configuration conf)
+ {
+ repositoryPath = conf.getAttribute( UploadService.REPOSITORY_KEY,
UploadService.REPOSITORY_DEFAULT );
+
+ headerEncoding = conf.getAttribute( UploadService.HEADER_ENCODING_KEY,
UploadService.HEADER_ENCODING_DEFAULT );
+
+ sizeMax = conf.getAttributeAsInteger( UploadService.SIZE_MAX_KEY,
UploadService.SIZE_MAX_DEFAULT );
+
+ sizeThreshold = conf.getAttributeAsInteger(
UploadService.SIZE_THRESHOLD_KEY,
+ UploadService.SIZE_THRESHOLD_DEFAULT );
+ }
+
+ /**
+ * Avalon component lifecycle method
+ *
+ * Initializes the service.
+ *
+ * This method processes the repository path, to make it relative to the
web application
+ * root, if necessary
+ */
+ @Override
+ public void initialize() throws Exception
+ {
+ // test for the existence of the path within the webapp directory.
+ // if it does not exist, assume the path was to be used as is.
+ String testPath = getRealPath( repositoryPath );
+ File testDir = new File( testPath );
+ if (testDir.exists())
+ {
+ repositoryPath = testPath;
+ }
+
+ getLogger().debug( "Upload Service: REPOSITORY_KEY => " +
repositoryPath );
+
+ itemFactory =
+ DiskFileItemFactory.builder()
+ .setPath(Paths.get( repositoryPath ) )
+ .setBufferSize( sizeThreshold )
+ .get();
+ }
+
+ /**
+ * Avalon component lifecycle method
+ */
+ @Override
+ public void contextualize(Context context) throws ContextException
+ {
+ this.applicationRoot = context.get( "urn:avalon:home" ).toString();
+ }
}
diff --git a/src/java/org/apache/fulcrum/upload/UploadService.java
b/src/java/org/apache/fulcrum/upload/UploadService.java
index 2d9a993..bca743f 100644
--- a/src/java/org/apache/fulcrum/upload/UploadService.java
+++ b/src/java/org/apache/fulcrum/upload/UploadService.java
@@ -21,12 +21,11 @@ package org.apache.fulcrum.upload;
import java.util.List;
-import javax.portlet.ActionRequest;
-import javax.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletRequest;
import org.apache.avalon.framework.service.ServiceException;
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileItemIterator;
+import org.apache.commons.fileupload2.core.FileItem;
+import org.apache.commons.fileupload2.core.FileItemInputIterator;
/**
* <p>
@@ -200,66 +199,7 @@ public interface UploadService
* while communicating with the client or a
problem
* while storing the uploaded content.
*/
- FileItemIterator getItemIterator(HttpServletRequest req) throws
ServiceException;
-
- /**
- * <p>
- * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
- * <code>multipart/form-data</code> stream.
- * </p>
- *
- * @param req The portlet request to be parsed.
- * @return List of FileItem parts
- * @throws ServiceException Problems reading/parsing the request or
storing
- * the uploaded file(s).
- */
- List<FileItem> parseRequest(ActionRequest req) throws ServiceException;
-
- /**
- * <p>
- * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
- * <code>multipart/form-data</code> stream.
- * </p>
- *
- * @param req The portlet request to be parsed.
- * @param path The location where the files should be stored.
- * @return List of FileItem parts
- * @throws ServiceException Problems reading/parsing the request or
storing
- * the uploaded file(s).
- */
- List<FileItem> parseRequest(ActionRequest req, String path) throws
ServiceException;
-
- /**
- * <p>
- * Parses a <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC 1867</a>
compliant
- * <code>multipart/form-data</code> stream.
- * </p>
- *
- * @param req The portlet request to be parsed.
- * @param sizeThreshold the max size in bytes to be stored in memory
- * @param sizeMax the maximum allowed upload size in bytes
- * @param path The location where the files should be stored.
- * @return The list of FileItem parts uploaded
- * @throws ServiceException Problems reading/parsing the request or
storing
- * the uploaded file(s).
- */
- List<FileItem> parseRequest(ActionRequest req, int sizeThreshold, int
sizeMax, String path) throws ServiceException;
-
- /**
- * Processes an <a href="http://www.ietf.org/rfc/rfc1867.txt">RFC
1867</a>
- * compliant <code>multipart/form-data</code> stream.
- *
- * @param req The portlet request to be parsed.
- *
- * @return An iterator to instances of <code>FileItemStream</code>
parsed from
- * the request, in the order that they were transmitted.
- *
- * @throws ServiceException if there are problems reading/parsing the
request or
- * storing files. This may also be a network
error
- * while communicating with the client or a
problem
- * while storing the uploaded content.
- */
- FileItemIterator getItemIterator(ActionRequest req) throws
ServiceException;
+ FileItemInputIterator getItemIterator(HttpServletRequest req) throws
ServiceException;
/**
* <p>
@@ -308,14 +248,4 @@ public interface UploadService
*/
boolean isMultipart(HttpServletRequest req);
- /**
- * Utility method that determines whether the request contains multipart
- * content.
- *
- * @param req The portlet request to be evaluated. Must be non-null.
- *
- * @return <code>true</code> if the request is multipart;
<code>false</code>
- * otherwise.
- */
- boolean isMultipart(ActionRequest req);
}
diff --git a/src/test/org/apache/fulcrum/upload/UploadServiceTest.java
b/src/test/org/apache/fulcrum/upload/UploadServiceTest.java
index cafd84c..a6f8ac6 100644
--- a/src/test/org/apache/fulcrum/upload/UploadServiceTest.java
+++ b/src/test/org/apache/fulcrum/upload/UploadServiceTest.java
@@ -35,20 +35,21 @@ import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Vector;
-import javax.servlet.ReadListener;
-import javax.servlet.ServletInputStream;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
+import jakarta.servlet.ReadListener;
+import jakarta.servlet.ServletInputStream;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpSession;
import org.apache.avalon.framework.component.ComponentException;
-import org.apache.commons.fileupload.FileItem;
-import org.apache.commons.fileupload.FileItemIterator;
+import org.apache.commons.fileupload2.core.FileItem;
+import org.apache.commons.fileupload2.core.FileItemInputIterator;
import org.apache.fulcrum.testcontainer.BaseUnit5Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -107,11 +108,11 @@ public class UploadServiceTest extends BaseUnit5Test
System.out.println( fi.getString() );
assertEquals(15,fi.getSize());
// default is ISO-8859-1
- assertTrue( fi.getString("UTF-8").startsWith( "Überfülle" ), "data
string:'" +fi.getString("UTF-8") +"' not as expected");
+ assertTrue( fi.getString(StandardCharsets.UTF_8).startsWith(
"Überfülle" ), "data string:'" +fi.getString(StandardCharsets.UTF_8) +"' not as
expected");
//reset inputstream
requestFormData( request, testData);
- FileItemIterator fii = uploadService.getItemIterator( request );
+ FileItemInputIterator fii = uploadService.getItemIterator( request );
assertNotNull(fii);
assertTrue( fii.hasNext());
assertNotNull(fii.next());