Skip to content

Commit

Permalink
Migrate to Apache file upload API 2 w/ Jakarta
Browse files Browse the repository at this point in the history
  • Loading branch information
p3k committed May 25, 2024
1 parent d13c870 commit 7caf52c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ configurations {
dependencies {
implementation 'com.google.code.gson:gson:2.10.1'
implementation 'commons-codec:commons-codec:1.17.0'
implementation 'commons-fileupload:commons-fileupload:1.5'
implementation 'org.apache.commons:commons-fileupload2-core:2.0.0-M2'
implementation 'org.apache.commons:commons-fileupload2-jakarta:2.0.0-M1'
implementation 'commons-logging:commons-logging:1.3.2'
implementation 'commons-net:commons-net:3.10.0'
implementation 'com.sun.mail:javax.mail:1.6.2'
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/helma/servlet/AbstractServletClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,27 @@
import helma.util.*;

import java.io.*;
import java.util.*;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;

import java.security.SecureRandom;
import java.security.NoSuchAlgorithmException;
import java.util.*;

import jakarta.servlet.*;
import jakarta.servlet.http.*;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;

import org.apache.commons.fileupload2.core.DiskFileItemFactory;
import org.apache.commons.fileupload2.core.FileItem;
import org.apache.commons.fileupload2.core.FileUploadException;
import org.apache.commons.fileupload2.core.FileUploadSizeException;
import org.apache.commons.fileupload2.core.ProgressListener;

import org.apache.commons.fileupload2.jakarta.JakartaServletDiskFileUpload;
import org.apache.commons.fileupload2.jakarta.JakartaServletFileUpload;
import org.apache.commons.fileupload2.jakarta.JakartaServletRequestContext;

/**
* This is an abstract Hop servlet adapter. This class communicates with hop applications
Expand Down Expand Up @@ -222,15 +231,15 @@ protected void service (HttpServletRequest request, HttpServletResponse response
List uploads = null;
JakartaServletRequestContext reqcx = new JakartaServletRequestContext(request);

if (ServletFileUpload.isMultipartContent(reqcx)) {
if (JakartaServletFileUpload.isMultipartContent(reqcx)) {
// get session for upload progress monitoring
UploadStatus uploadStatus = getApplication().getUploadStatus(reqtrans);
try {
uploads = parseUploads(reqcx, reqtrans, uploadStatus, encoding);
} catch (Exception upx) {
log("Error in file upload", upx);
String message;
boolean tooLarge = (upx instanceof FileUploadBase.SizeLimitExceededException);
boolean tooLarge = (upx instanceof FileUploadSizeException);
if (tooLarge) {
message = "File upload size exceeds limit of " + uploadLimit + " kB";
} else {
Expand Down Expand Up @@ -657,10 +666,10 @@ private static void putMapEntry(Map map, String name, String value) {

protected List parseUploads(JakartaServletRequestContext reqcx, RequestTrans reqtrans,
final UploadStatus uploadStatus, String encoding)
throws FileUploadException, UnsupportedEncodingException {
throws FileUploadException, UnsupportedCharsetException, IOException {
// handle file upload
DiskFileItemFactory factory = new DiskFileItemFactory();
FileUpload upload = new FileUpload(factory);
DiskFileItemFactory factory = DiskFileItemFactory.builder().get();
JakartaServletFileUpload upload = new JakartaServletFileUpload(factory);
// use upload limit for individual file size, but also set a limit on overall size
upload.setFileSizeMax(uploadLimit * 1024);
upload.setSizeMax(totalUploadLimit * 1024);
Expand All @@ -683,7 +692,7 @@ public void update(long bytesRead, long contentLength, int itemsRead) {
Object value;
// check if this is an ordinary HTML form element or a file upload
if (item.isFormField()) {
value = item.getString(encoding);
value = item.getString(Charset.forName(encoding));
} else {
value = new MimePart(item);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/helma/util/MimePart.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package helma.util;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload2.core.FileItem;

import java.io.*;
import java.util.Date;
Expand Down Expand Up @@ -238,7 +238,7 @@ public String writeToFile(String dir, String fname) {
file = new File(base, filename);

if (fileItem != null) {
fileItem.write(file);
fileItem.write(file.toPath());
// null out fileItem, since calling write() may have moved the temp file
fileItem = null;
} else {
Expand All @@ -249,7 +249,7 @@ public String writeToFile(String dir, String fname) {
// return file name
return filename;
} catch (Exception x) {
System.err.println("Error in MimePart.writeToFile(): " + x);
System.err.println("Error in MimePart.writeToFile(): " + x);
return null;
}
}
Expand Down

0 comments on commit 7caf52c

Please sign in to comment.