Skip to content

Commit

Permalink
Fixes #4541 - Add ability to set file upload size treshold (#4551)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Jan 22, 2025
1 parent 51b8072 commit 4a0bd5c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import static java.lang.System.Logger.Level.TRACE;

/**
* The WebApplicationExtension that is responsible for setting up the Apache
* multi-part manager.
* The File Upload extension that configures the web application so it can
* support file uploads.
*
* @author Manfred Riem ([email protected])
*/
Expand All @@ -52,7 +52,7 @@ public FileUploadExtension() {

@Override
public void configure(WebApplication webApplication) {
LOGGER.log(TRACE, "Configuring Apache Commons FileUpload extension");
LOGGER.log(TRACE, "Configuring File Upload extension");
webApplication.addInitializer(FileUploadMultiPartInitializer.class.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.apache.commons.fileupload2.core.FileItem;

/**
* The Part for the ApacheMultiPartManager.
* The Part for the FileUploadMultiPartManager.
*
* <p>
* This class implements the Servlet Part API and delegates to an Apache Commons
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
import java.util.Set;

/**
* The ServletContainerInitializer for the ApacheMultiPartManager.
* The ServletContainerInitializer that is used to configure the web application
* to support file upload:
*
* <p>
* The ServletContainerInitializer performs the following steps:
Expand All @@ -56,11 +57,18 @@ public class FileUploadMultiPartInitializer implements ServletContainerInitializ
*/
private static final Logger LOGGER = System.getLogger(FileUploadMultiPartInitializer.class.getName());

/**
* Constructor.
*/
public FileUploadMultiPartInitializer() {
}

@Override
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
LOGGER.log(TRACE, "Setting ApacheMultiPartManager");
WebApplication webApplication = (WebApplication) servletContext;
LOGGER.log(TRACE, "Setting the MultiPartManager");
webApplication.getManager().setMultiPartManager(new FileUploadMultiPartManager());
LOGGER.log(TRACE, "Adding the listener used to cleanup temporary files");
webApplication.addListener("org.apache.commons.fileupload2.jakarta.servlet6.JakartaFileCleaner");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
*/
public class FileUploadMultiPartManager implements MultiPartManager {

/**
* Stores the constant for the file size treshold.
*/
private static final String FILE_SIZE_THRESHOLD_NAME
= "cloud.piranha.extension.fileupload.fileSizeTreshold";

/**
* Stores the logger.
*/
Expand All @@ -76,7 +82,7 @@ public Collection<Part> getParts(WebApplication webApplication, WebApplicationRe
if (LOGGER.isLoggable(TRACE)) {
LOGGER.log(TRACE, "Getting parts for request: {0}", request);
}

if (!JakartaServletFileUpload.isMultipartContent(request)) {
if (LOGGER.isLoggable(TRACE)) {
LOGGER.log(TRACE, "Request: {0} is not a multipart/form-date request");
Expand Down Expand Up @@ -147,6 +153,13 @@ private synchronized JakartaServletFileUpload setupFileUpload(WebApplication web
outputDirectory = location;
}
int sizeThreshold = 10240;
if (webApplication.getInitParameter(FILE_SIZE_THRESHOLD_NAME) != null) {
try {
sizeThreshold = Integer.parseInt(webApplication.getInitParameter("cloud.piranha.extension.fileupload.fileSizeTreshold"));
} catch (NumberFormatException nfe) {
// ignore and let defaults apply.
}
}
if (multipartConfig.getFileSizeThreshold() != 0) {
sizeThreshold = multipartConfig.getFileSizeThreshold();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.extension.fileupload;
package cloud.piranha.extension.fileupload.tests;

import cloud.piranha.core.impl.DefaultWebApplication;
import cloud.piranha.core.impl.DefaultWebApplicationRequest;
import cloud.piranha.extension.fileupload.FileUploadMultiPartManager;
import jakarta.servlet.MultipartConfigElement;
import java.io.File;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down
41 changes: 41 additions & 0 deletions extension/fileupload/src/test/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2002-2025 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/**
* This module delivers the tests for the File Upload extension.
*
* @author Manfred Riem ([email protected])
*/
module cloud.piranha.extension.fileupload.tests {

exports cloud.piranha.extension.fileupload.tests;
opens cloud.piranha.extension.fileupload.tests;
requires cloud.piranha.core.impl;
requires cloud.piranha.extension.fileupload;
requires org.junit.jupiter.api;
}

0 comments on commit 4a0bd5c

Please sign in to comment.