From 45edbcbe45d303240af998687387d12a9e15733e Mon Sep 17 00:00:00 2001 From: Lukasz Lenart Date: Mon, 21 Nov 2016 11:05:33 +0100 Subject: [PATCH] Uses the new class in implementation of MultiPartRequest --- .../multipart/JakartaMultiPartRequest.java | 8 +-- .../JakartaStreamMultiPartRequest.java | 8 +-- .../multipart/JakartaUploadedFile.java | 61 +++++++++++++++++++ .../multipart/MultiPartRequest.java | 26 ++++---- .../multipart/MultiPartRequestWrapper.java | 2 +- .../multipart/PellMultiPartRequest.java | 8 +-- .../multipart/PellUploadedFile.java | 61 +++++++++++++++++++ 7 files changed, 148 insertions(+), 26 deletions(-) create mode 100644 core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaUploadedFile.java create mode 100644 plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellUploadedFile.java diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java index 32890c62c..900733c09 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java @@ -223,14 +223,14 @@ public class JakartaMultiPartRequest implements MultiPartRequest { /* (non-Javadoc) * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFile(java.lang.String) */ - public File[] getFile(String fieldName) { + public UploadedFile[] getFile(String fieldName) { List items = files.get(fieldName); if (items == null) { return null; } - List fileList = new ArrayList<>(items.size()); + List fileList = new ArrayList<>(items.size()); for (FileItem fileItem : items) { File storeLocation = ((DiskFileItem) fileItem).getStoreLocation(); if (fileItem.isInMemory() && storeLocation != null && !storeLocation.exists()) { @@ -240,10 +240,10 @@ public class JakartaMultiPartRequest implements MultiPartRequest { LOG.error("Cannot write uploaded empty file to disk: {}", storeLocation.getAbsolutePath(), e); } } - fileList.add(storeLocation); + fileList.add(new JakartaUploadedFile(storeLocation)); } - return fileList.toArray(new File[fileList.size()]); + return fileList.toArray(new UploadedFile[fileList.size()]); } /* (non-Javadoc) diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java index b1aa563db..a6e15b874 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java @@ -147,18 +147,18 @@ public class JakartaStreamMultiPartRequest implements MultiPartRequest { /* (non-Javadoc) * @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFile(java.lang.String) */ - public File[] getFile(String fieldName) { + public UploadedFile[] getFile(String fieldName) { List infos = fileInfos.get(fieldName); if (infos == null) { return null; } - List files = new ArrayList<>(infos.size()); + List files = new ArrayList<>(infos.size()); for (FileInfo fileInfo : infos) { - files.add(fileInfo.getFile()); + files.add(new JakartaUploadedFile(fileInfo.getFile())); } - return files.toArray(new File[files.size()]); + return files.toArray(new UploadedFile[files.size()]); } /* (non-Javadoc) diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaUploadedFile.java b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaUploadedFile.java new file mode 100644 index 000000000..5461b9d21 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaUploadedFile.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2.dispatcher.multipart; + +import java.io.File; + +public class JakartaUploadedFile implements UploadedFile { + + private File file; + + public JakartaUploadedFile(File file) { + this.file = file; + } + + @Override + public Long length() { + return file.length(); + } + + @Override + public String getName() { + return file.getName(); + } + + @Override + public boolean isFile() { + return file.isFile(); + } + + @Override + public boolean delete() { + return file.delete(); + } + + @Override + public String getAbsolutePath() { + return file.getAbsolutePath(); + } + + @Override + public File getContent() { + return file; + } +} diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java b/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java index a230a60df..22ffe8efa 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequest.java @@ -36,14 +36,14 @@ import java.util.List; */ public interface MultiPartRequest { - public void parse(HttpServletRequest request, String saveDir) throws IOException; + void parse(HttpServletRequest request, String saveDir) throws IOException; /** * Returns an enumeration of the parameter names for uploaded files * * @return an enumeration of the parameter names for uploaded files */ - public Enumeration getFileParameterNames(); + Enumeration getFileParameterNames(); /** * Returns the content type(s) of the file(s) associated with the specified field name @@ -54,16 +54,16 @@ public interface MultiPartRequest { * @return an array of content encoding for the specified input field name or null if * no content type was specified. */ - public String[] getContentType(String fieldName); + String[] getContentType(String fieldName); /** - * Returns a {@link java.io.File} object for the filename specified or null if no files + * Returns a {@link UploadedFile} object for the filename specified or null if no files * are associated with the given field name. * * @param fieldName input field name - * @return a File[] object for files associated with the specified input field name + * @return a UploadedFile[] object for files associated with the specified input field name */ - public File[] getFile(String fieldName); + UploadedFile[] getFile(String fieldName); /** * Returns a String[] of file names for files associated with the specified input field name @@ -71,7 +71,7 @@ public interface MultiPartRequest { * @param fieldName input field name * @return a String[] of file names for files associated with the specified input field name */ - public String[] getFileNames(String fieldName); + String[] getFileNames(String fieldName); /** * Returns the file system name(s) of files associated with the given field name or @@ -80,7 +80,7 @@ public interface MultiPartRequest { * @param fieldName input field name * @return the file system name(s) of files associated with the given field name */ - public String[] getFilesystemName(String fieldName); + String[] getFilesystemName(String fieldName); /** * Returns the specified request parameter. @@ -88,14 +88,14 @@ public interface MultiPartRequest { * @param name the name of the parameter to get * @return the parameter or null if it was not found. */ - public String getParameter(String name); + String getParameter(String name); /** * Returns an enumeration of String parameter names. * * @return an enumeration of String parameter names. */ - public Enumeration getParameterNames(); + Enumeration getParameterNames(); /** * Returns a list of all parameter values associated with a parameter name. If there is only @@ -104,7 +104,7 @@ public interface MultiPartRequest { * @param name the name of the parameter. * @return an array of all values associated with the parameter name. */ - public String[] getParameterValues(String name); + String[] getParameterValues(String name); /** * Returns a list of error messages that may have occurred while processing the request. @@ -115,11 +115,11 @@ public interface MultiPartRequest { * * @return a list of Strings that represent various errors during parsing */ - public List getErrors(); + List getErrors(); /** * Cleans up all uploaded file, should be called at the end of request */ - public void cleanUp(); + void cleanUp(); } diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java b/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java index 05798e3e9..4f5562114 100644 --- a/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java +++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/MultiPartRequestWrapper.java @@ -143,7 +143,7 @@ public class MultiPartRequestWrapper extends StrutsRequestWrapper { * @param fieldName input field name * @return a File[] object for files associated with the specified input field name */ - public File[] getFiles(String fieldName) { + public UploadedFile[] getFiles(String fieldName) { if (multi == null) { return null; } diff --git a/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java b/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java index 4a5f7c0f1..1acfe4654 100644 --- a/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java +++ b/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellMultiPartRequest.java @@ -92,8 +92,8 @@ public class PellMultiPartRequest implements MultiPartRequest { return new String[]{multi.getContentType(fieldName)}; } - public File[] getFile(String fieldName) { - return new File[]{multi.getFile(fieldName)}; + public UploadedFile[] getFile(String fieldName) { + return new UploadedFile[]{ new PellUploadedFile(multi.getFile(fieldName)) }; } public String[] getFileNames(String fieldName) { @@ -174,8 +174,8 @@ public class PellMultiPartRequest implements MultiPartRequest { Enumeration fileParameterNames = multi.getFileParameterNames(); while (fileParameterNames != null && fileParameterNames.hasMoreElements()) { String inputValue = (String) fileParameterNames.nextElement(); - File[] files = getFile(inputValue); - for (File currentFile : files) { + UploadedFile[] files = getFile(inputValue); + for (UploadedFile currentFile : files) { if (LOG.isInfoEnabled()) { String msg = LocalizedTextUtil.findText(this.getClass(), "struts.messages.removing.file", Locale.ENGLISH, "no.message.found", new Object[]{inputValue, currentFile}); diff --git a/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellUploadedFile.java b/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellUploadedFile.java new file mode 100644 index 000000000..a1d9e4a91 --- /dev/null +++ b/plugins/pell-multipart/src/main/java/org/apache/struts2/dispatcher/multipart/PellUploadedFile.java @@ -0,0 +1,61 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2.dispatcher.multipart; + +import java.io.File; + +public class PellUploadedFile implements UploadedFile { + + private File file; + + public PellUploadedFile(File file) { + this.file = file; + } + + @Override + public Long length() { + return file.length(); + } + + @Override + public String getName() { + return file.getName(); + } + + @Override + public boolean isFile() { + return file.isFile(); + } + + @Override + public boolean delete() { + return file.delete(); + } + + @Override + public String getAbsolutePath() { + return file.getAbsolutePath(); + } + + @Override + public File getContent() { + return file; + } +}