Skip to content

Commit 4a0bd5c

Browse files
authored
Fixes #4541 - Add ability to set file upload size treshold (#4551)
1 parent 51b8072 commit 4a0bd5c

File tree

6 files changed

+71
-8
lines changed

6 files changed

+71
-8
lines changed

extension/fileupload/src/main/java/cloud/piranha/extension/fileupload/FileUploadExtension.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
import static java.lang.System.Logger.Level.TRACE;
3333

3434
/**
35-
* The WebApplicationExtension that is responsible for setting up the Apache
36-
* multi-part manager.
35+
* The File Upload extension that configures the web application so it can
36+
* support file uploads.
3737
*
3838
* @author Manfred Riem (mriem@manorrock.com)
3939
*/
@@ -52,7 +52,7 @@ public FileUploadExtension() {
5252

5353
@Override
5454
public void configure(WebApplication webApplication) {
55-
LOGGER.log(TRACE, "Configuring Apache Commons FileUpload extension");
55+
LOGGER.log(TRACE, "Configuring File Upload extension");
5656
webApplication.addInitializer(FileUploadMultiPartInitializer.class.getName());
5757
}
5858
}

extension/fileupload/src/main/java/cloud/piranha/extension/fileupload/FileUploadMultiPart.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.apache.commons.fileupload2.core.FileItem;
3737

3838
/**
39-
* The Part for the ApacheMultiPartManager.
39+
* The Part for the FileUploadMultiPartManager.
4040
*
4141
* <p>
4242
* This class implements the Servlet Part API and delegates to an Apache Commons

extension/fileupload/src/main/java/cloud/piranha/extension/fileupload/FileUploadMultiPartInitializer.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
import java.util.Set;
3737

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

60+
/**
61+
* Constructor.
62+
*/
63+
public FileUploadMultiPartInitializer() {
64+
}
65+
5966
@Override
6067
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
61-
LOGGER.log(TRACE, "Setting ApacheMultiPartManager");
6268
WebApplication webApplication = (WebApplication) servletContext;
69+
LOGGER.log(TRACE, "Setting the MultiPartManager");
6370
webApplication.getManager().setMultiPartManager(new FileUploadMultiPartManager());
71+
LOGGER.log(TRACE, "Adding the listener used to cleanup temporary files");
6472
webApplication.addListener("org.apache.commons.fileupload2.jakarta.servlet6.JakartaFileCleaner");
6573
}
6674
}

extension/fileupload/src/main/java/cloud/piranha/extension/fileupload/FileUploadMultiPartManager.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
*/
6060
public class FileUploadMultiPartManager implements MultiPartManager {
6161

62+
/**
63+
* Stores the constant for the file size treshold.
64+
*/
65+
private static final String FILE_SIZE_THRESHOLD_NAME
66+
= "cloud.piranha.extension.fileupload.fileSizeTreshold";
67+
6268
/**
6369
* Stores the logger.
6470
*/
@@ -76,7 +82,7 @@ public Collection<Part> getParts(WebApplication webApplication, WebApplicationRe
7682
if (LOGGER.isLoggable(TRACE)) {
7783
LOGGER.log(TRACE, "Getting parts for request: {0}", request);
7884
}
79-
85+
8086
if (!JakartaServletFileUpload.isMultipartContent(request)) {
8187
if (LOGGER.isLoggable(TRACE)) {
8288
LOGGER.log(TRACE, "Request: {0} is not a multipart/form-date request");
@@ -147,6 +153,13 @@ private synchronized JakartaServletFileUpload setupFileUpload(WebApplication web
147153
outputDirectory = location;
148154
}
149155
int sizeThreshold = 10240;
156+
if (webApplication.getInitParameter(FILE_SIZE_THRESHOLD_NAME) != null) {
157+
try {
158+
sizeThreshold = Integer.parseInt(webApplication.getInitParameter("cloud.piranha.extension.fileupload.fileSizeTreshold"));
159+
} catch (NumberFormatException nfe) {
160+
// ignore and let defaults apply.
161+
}
162+
}
150163
if (multipartConfig.getFileSizeThreshold() != 0) {
151164
sizeThreshold = multipartConfig.getFileSizeThreshold();
152165
}

extension/fileupload/src/test/java/cloud/piranha/extension/fileupload/FileUploadMultiPartManagerTest.java renamed to extension/fileupload/src/test/java/cloud/piranha/extension/fileupload/tests/FileUploadMultiPartManagerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2626
* POSSIBILITY OF SUCH DAMAGE.
2727
*/
28-
package cloud.piranha.extension.fileupload;
28+
package cloud.piranha.extension.fileupload.tests;
2929

3030
import cloud.piranha.core.impl.DefaultWebApplication;
3131
import cloud.piranha.core.impl.DefaultWebApplicationRequest;
32+
import cloud.piranha.extension.fileupload.FileUploadMultiPartManager;
3233
import jakarta.servlet.MultipartConfigElement;
3334
import java.io.File;
3435
import static org.junit.jupiter.api.Assertions.assertNull;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2002-2025 Manorrock.com. All Rights Reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* 1. Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
* 2. Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* 3. Neither the name of the copyright holder nor the names of its
13+
* contributors may be used to endorse or promote products derived from
14+
* this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
/**
30+
* This module delivers the tests for the File Upload extension.
31+
*
32+
* @author Manfred Riem (mriem@manorrock.com)
33+
*/
34+
module cloud.piranha.extension.fileupload.tests {
35+
36+
exports cloud.piranha.extension.fileupload.tests;
37+
opens cloud.piranha.extension.fileupload.tests;
38+
requires cloud.piranha.core.impl;
39+
requires cloud.piranha.extension.fileupload;
40+
requires org.junit.jupiter.api;
41+
}

0 commit comments

Comments
 (0)