Skip to content

Commit

Permalink
Cache classes
Browse files Browse the repository at this point in the history
  • Loading branch information
avantra-mis committed Aug 15, 2024
1 parent baf5cd0 commit e629cc3
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions zk/src/main/java/org/zkoss/zk/au/http/AuMultipartUploader.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,38 @@ public class AuMultipartUploader {
private static final String JAVAX_DISK_UPLOAD_CLASS = "org.apache.commons.fileupload2.javax.JavaxServletDiskFileUpload";
private static final String JAKARTA_DISK_UPLOAD_CLASS = "org.apache.commons.fileupload2.jakarta.servlet5.JakartaServletDiskFileUpload";

private static Class<?> getServletFileUploadClass() {
private static Class<?> uploadClass = null;
private static Class<?> diskUploadClass = null;

static {
try {
uploadClass = Class.forName(JAVAX_UPLOAD_CLASS);
} catch (ClassNotFoundException ex0) {
try {
uploadClass = Class.forName(JAKARTA_UPLOAD_CLASS);
} catch (ClassNotFoundException ex1) {
// Ignore
}
}

try {
return Class.forName(JAVAX_UPLOAD_CLASS);
diskUploadClass = Class.forName(JAVAX_DISK_UPLOAD_CLASS);
} catch (ClassNotFoundException ex0) {
try {
return Class.forName(JAKARTA_UPLOAD_CLASS);
diskUploadClass = Class.forName(JAKARTA_DISK_UPLOAD_CLASS);
} catch (ClassNotFoundException ex1) {
throw new RuntimeException("Failed to find " + JAVAX_UPLOAD_CLASS + " or " + JAKARTA_UPLOAD_CLASS);
// Ignore
}
}
}

private static Class<?> getServletFileUploadClass() {
if (uploadClass == null) {
throw new RuntimeException("Failed to find " + JAVAX_UPLOAD_CLASS + " or " + JAKARTA_UPLOAD_CLASS);
}
return uploadClass;
}

public static boolean isMultipartContent(HttpServletRequest request) {
Class<?> clazz = getServletFileUploadClass();
try {
Expand All @@ -108,20 +128,13 @@ public static boolean isMultipartContent(HttpServletRequest request) {
}

private static AbstractFileUpload newServletDiskFileUpload(DiskFileItemFactory factory) {
Class<?> clazz;
try {
clazz = Class.forName(JAVAX_DISK_UPLOAD_CLASS);
} catch (ClassNotFoundException ex0) {
try {
clazz = Class.forName(JAKARTA_DISK_UPLOAD_CLASS);
} catch (ClassNotFoundException ex1) {
throw new RuntimeException("Failed to find " + JAVAX_DISK_UPLOAD_CLASS + " or " + JAKARTA_DISK_UPLOAD_CLASS);
}
if (diskUploadClass == null) {
throw new RuntimeException("Failed to find " + JAVAX_DISK_UPLOAD_CLASS + " or " + JAKARTA_DISK_UPLOAD_CLASS);
}
try {
return (AbstractFileUpload) clazz.getDeclaredConstructor(DiskFileItemFactory.class).newInstance(factory);
return (AbstractFileUpload) diskUploadClass.getDeclaredConstructor(DiskFileItemFactory.class).newInstance(factory);
} catch (Exception ex) {
throw new RuntimeException("Failed to create a new instance of " + clazz.getName(), ex);
throw new RuntimeException("Failed to create a new instance of " + diskUploadClass.getName(), ex);
}
}

Expand Down Expand Up @@ -179,7 +192,7 @@ public static AbstractMap.SimpleImmutableEntry<String, String> splitQueryParamet
}
}
private static Object reconstructPacket(Object data, Map<String, Object> reqData, Desktop desktop,
Map<String, Object> params) throws IOException {
Map<String, Object> params) throws IOException {
if (data instanceof List) {
int i = 0;
List listData = (List) data;
Expand Down Expand Up @@ -256,8 +269,8 @@ private static Map<String, Object> getFileuploadMetaPerComp(Map<String, Object>
try {
Integer compMaxsz = (Integer) comp.getAttribute(Attributes.UPLOAD_MAX_SIZE);
maxsz = compMaxsz != null ? compMaxsz :
desktop.getWebApp().getConfiguration()
.getMaxUploadSize();
desktop.getWebApp().getConfiguration()
.getMaxUploadSize();
params.put("maxSize", maxsz);
} catch (NumberFormatException e) {
throw new UiException("The upload max size must be a number");
Expand Down Expand Up @@ -514,7 +527,7 @@ private static String getBaseName(FileItem fi) {
}

private static final Media processItem(Desktop desktop, FileItem fi, boolean alwaysNative,
org.zkoss.zk.ui.sys.DiskFileItemFactory factory) throws IOException {
org.zkoss.zk.ui.sys.DiskFileItemFactory factory) throws IOException {
String name = getBaseName(fi);
if (name != null) {
//Not sure whether a name might contain ;jsessionid or similar
Expand Down

0 comments on commit e629cc3

Please sign in to comment.