Skip to content

Commit

Permalink
Merge branch 'current' into issue-4544
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Jan 30, 2025
2 parents 1750c5b + e6eb513 commit 635aa0d
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 15 deletions.
3 changes: 0 additions & 3 deletions .mvn/maven.config

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@
* </p>
*
* <ol>
* <li>Sets the MultiPartManager to an instance of FileUploadMultiPartManager.</li>
* <li>Adds the JakartaFileCleaner listener that cleans up the temporary files.</li>
* <li>Sets the MultiPartManager to an instance of
* FileUploadMultiPartManager.</li>
* <li>Adds the JakartaFileCleaner listener that cleans up the temporary
* files.</li>
* </ol>
*
* @author Manfred Riem ([email protected])
Expand All @@ -62,13 +64,17 @@ public class FileUploadMultiPartInitializer implements ServletContainerInitializ
*/
public FileUploadMultiPartInitializer() {
}

@Override
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {
WebApplication webApplication = (WebApplication) servletContext;
LOGGER.log(TRACE, "Setting the MultiPartManager");
if (LOGGER.isLoggable(TRACE)) {
LOGGER.log(TRACE, "Setting the MultiPartManager");
}
webApplication.getManager().setMultiPartManager(new FileUploadMultiPartManager());
LOGGER.log(TRACE, "Adding the listener used to cleanup temporary files");
if (LOGGER.isLoggable(TRACE)) {
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 @@ -65,6 +65,12 @@ public class FileUploadMultiPartManager implements MultiPartManager {
private static final String FILE_SIZE_THRESHOLD_NAME
= "cloud.piranha.extension.fileupload.fileSizeTreshold";

/**
* Stores the constant for the output directory.
*/
private static final String OUTPUT_DIRECTORY_NAME
= "cloud.piranha.extension.fileupload.outputDirectory";

/**
* Stores the logger.
*/
Expand Down Expand Up @@ -142,27 +148,47 @@ public Part getPart(WebApplication webApplication, WebApplicationRequest request
private synchronized JakartaServletFileUpload setupFileUpload(WebApplication webApplication, MultipartConfigElement multipartConfig) {
JakartaServletFileUpload upload = (JakartaServletFileUpload) webApplication.getAttribute(FileUploadMultiPartManager.class.getName());
if (upload == null) {
File outputDirectory;
if (multipartConfig.getLocation() == null || multipartConfig.getLocation().isEmpty()) {
outputDirectory = (File) webApplication.getAttribute(TEMPDIR);
} else {
/*
* Default to TEMPDIR.
*/
File outputDirectory = (File) webApplication.getAttribute(TEMPDIR);
/*
* If the multipart config has a location use it. If it is relative
* use the TEMPDIR as the parent directory.
*/
if (multipartConfig.getLocation() != null && !multipartConfig.getLocation().isEmpty()) {
File location = new File(multipartConfig.getLocation());
if (!location.isAbsolute()) {
location = ((File) webApplication.getAttribute(TEMPDIR)).toPath().resolve(location.toPath()).toFile();
}
outputDirectory = location;
}
/*
* If OUTPUT_DIRECTORY_NAME is set use it.
*/
if (webApplication.getInitParameter(OUTPUT_DIRECTORY_NAME) != null) {
outputDirectory = new File(webApplication.getInitParameter(OUTPUT_DIRECTORY_NAME));
}
/*
* Default to 10240 (10 KB).
*/
int sizeThreshold = 10240;
/*
* If the multipart config has a size > 0 use it.
*/
if (multipartConfig.getFileSizeThreshold() != 0) {
sizeThreshold = multipartConfig.getFileSizeThreshold();
}
/*
* If FILE_SIZE_THRESHOLD_NAME is set use it.
*/
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();
}
DiskFileItemFactory factory = DiskFileItemFactory
.builder()
.setBufferSize(sizeThreshold)
Expand Down
22 changes: 22 additions & 0 deletions extension/fileupload/src/site/markdown/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,25 @@ file upload. This extension is available by default for the following runtimes:
1. Piranha Server
1. Piranha Servlet
1. Piranha Web Profile

## Configuration parameters

The following configuration parameters are available:

1. `cloud.piranha.extension.fileupload.outputDirectory` - the directory where
the file upload will store temporary files. The default is the location as

1. `cloud.piranha.extension.fileupload.fileSizeTreshold` - the file size
threshold (in bytes) before the runtime will create a temporary file on the
filesystem to store the upload. The default is 10240 (10 KB).

## Setting the configuration parameters using web.xml

You can set the configuration parameter in the web.xml as illustrated below:

```xml
<context-param>
<param-name>cloud.piranha.extension.fileupload.fileSizeTreshold</param-name>
<param-value>1048576</param-value>
</context-param>
```
6 changes: 6 additions & 0 deletions extension/tempdir/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement>
<site>
<id>default</id>
<url>file:///tmp/piranha/extension/tempdir/</url>
</site>
</distributionManagement>
</project>
10 changes: 10 additions & 0 deletions extension/tempdir/src/site/markdown/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Piranha TEMPDIR Extension

The TEMPDIR extension delivers the ability for a Piranha runtime to support
the Servlet temporary directory. This extension is available by default for the
following runtimes:

1. Piranha Server
1. Piranha Servlet
1. Piranha Web Profile

9 changes: 9 additions & 0 deletions extension/tempdir/src/site/site.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="ISO-8859-1"?>

<project>
<body>
<menu name="Overview">
<item name="Introduction" href="index.html"/>
</menu>
</body>
</project>
1 change: 1 addition & 0 deletions src/site/site.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
</menu>
<menu name="Extensions">
<item name="File Upload" href="extension/file-upload/index.html"/>
<item name="Temp Dir" href="extesnion/tempdir/index.html"/>
</menu>
</body>
<custom>
Expand Down

0 comments on commit 635aa0d

Please sign in to comment.