Uses the new class in implementation of MultiPartRequest

This commit is contained in:
Lukasz Lenart
2016-11-21 11:05:33 +01:00
parent fe05ca2095
commit 45edbcbe45
7 changed files with 148 additions and 26 deletions
@@ -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<FileItem> items = files.get(fieldName);
if (items == null) {
return null;
}
List<File> fileList = new ArrayList<>(items.size());
List<UploadedFile> 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)
@@ -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<FileInfo> infos = fileInfos.get(fieldName);
if (infos == null) {
return null;
}
List<File> files = new ArrayList<>(infos.size());
List<UploadedFile> 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)
@@ -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<File> {
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;
}
}
@@ -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<String> getFileParameterNames();
Enumeration<String> 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 <tt>null</tt> 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 <tt>null</tt> if no files
* Returns a {@link UploadedFile} object for the filename specified or <tt>null</tt> 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 <tt>null</tt> 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<String> getParameterNames();
Enumeration<String> 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<LocalizedMessage> getErrors();
List<LocalizedMessage> getErrors();
/**
* Cleans up all uploaded file, should be called at the end of request
*/
public void cleanUp();
void cleanUp();
}
@@ -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;
}
@@ -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});
@@ -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<File> {
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;
}
}