Extracts one base class

This commit is contained in:
Lukasz Lenart
2016-11-22 20:02:22 +01:00
parent de51e79718
commit 4de8309d0a
4 changed files with 118 additions and 156 deletions
@@ -0,0 +1,112 @@
package org.apache.struts2.dispatcher.multipart;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.dispatcher.LocalizedMessage;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Abstract class with some helper methods, it should be used
* when starting development of another implementation of {@link MultiPartRequest}
*/
public abstract class AbstractMultiPartRequest implements MultiPartRequest {
private static final Logger LOG = LogManager.getLogger(AbstractMultiPartRequest.class);
/**
* Defines the internal buffer size used during streaming operations.
*/
public static final int BUFFER_SIZE = 10240;
/**
* Internal list of raised errors to be passed to the the Struts2 framework.
*/
protected List<LocalizedMessage> errors = new ArrayList<>();
/**
* Specifies the maximum size of the entire request.
*/
protected int maxSize;
protected boolean maxSizeProvided;
/**
* Specifies the buffer size to use during streaming.
*/
protected int bufferSize = BUFFER_SIZE;
protected String defaultEncoding;
/**
* Localization to be used regarding errors.
*/
protected Locale defaultLocale = Locale.ENGLISH;
/**
* @param bufferSize Sets the buffer size to be used.
*/
@Inject(value = StrutsConstants.STRUTS_MULTIPART_BUFFERSIZE, required = false)
public void setBufferSize(String bufferSize) {
this.bufferSize = Integer.parseInt(bufferSize);
}
@Inject(StrutsConstants.STRUTS_I18N_ENCODING)
public void setDefaultEncoding(String enc) {
this.defaultEncoding = enc;
}
/**
* @param maxSize Injects the Struts multiple part maximum size.
*/
@Inject(StrutsConstants.STRUTS_MULTIPART_MAXSIZE)
public void setMaxSize(String maxSize) {
this.maxSizeProvided = true;
this.maxSize = Integer.parseInt(maxSize);
}
/**
* @param provider Injects the Struts locale provider.
*/
@Inject
public void setLocaleProvider(LocaleProvider provider) {
defaultLocale = provider.getLocale();
}
/**
* @param request Inspect the servlet request and set the locale if one wasn't provided by
* the Struts2 framework.
*/
protected void setLocale(HttpServletRequest request) {
if (defaultLocale == null) {
defaultLocale = request.getLocale();
}
}
/**
* Build error message.
*
* @param e the Throwable/Exception
* @param args arguments
* @return error message
*/
protected LocalizedMessage buildErrorMessage(Throwable e, Object[] args) {
String errorKey = "struts.messages.upload.error." + e.getClass().getSimpleName();
LOG.debug("Preparing error message for key: [{}]", errorKey);
return new LocalizedMessage(this.getClass(), errorKey, e.getMessage(), args);
}
/* (non-Javadoc)
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getErrors()
*/
public List<LocalizedMessage> getErrors() {
return errors;
}
}
@@ -21,8 +21,6 @@
package org.apache.struts2.dispatcher.multipart;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.LocalizedTextUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadBase;
@@ -33,7 +31,6 @@ import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.dispatcher.LocalizedMessage;
import javax.servlet.http.HttpServletRequest;
@@ -46,7 +43,7 @@ import java.util.*;
/**
* Multipart form data request adapter for Jakarta Commons Fileupload package.
*/
public class JakartaMultiPartRequest implements MultiPartRequest {
public class JakartaMultiPartRequest extends AbstractMultiPartRequest {
static final Logger LOG = LogManager.getLogger(JakartaMultiPartRequest.class);
@@ -56,22 +53,6 @@ public class JakartaMultiPartRequest implements MultiPartRequest {
// maps parameter name -> List of param values
protected Map<String, List<String>> params = new HashMap<>();
// any errors while processing this request
protected List<LocalizedMessage> errors = new ArrayList<>();
protected long maxSize;
private Locale defaultLocale = Locale.ENGLISH;
@Inject(StrutsConstants.STRUTS_MULTIPART_MAXSIZE)
public void setMaxSize(String maxSize) {
this.maxSize = Long.parseLong(maxSize);
}
@Inject
public void setLocaleProvider(LocaleProvider provider) {
defaultLocale = provider.getLocale();
}
/**
* Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
* multipart classes (see class description).
@@ -86,15 +67,14 @@ public class JakartaMultiPartRequest implements MultiPartRequest {
processUpload(request, saveDir);
} catch (FileUploadException e) {
LOG.warn("Request exceeded size limit!", e);
LocalizedMessage errorMessage = null;
LocalizedMessage errorMessage;
if(e instanceof FileUploadBase.SizeLimitExceededException) {
FileUploadBase.SizeLimitExceededException ex = (FileUploadBase.SizeLimitExceededException) e;
errorMessage = buildErrorMessage(e, new Object[]{ex.getPermittedSize(), ex.getActualSize()});
} else {
errorMessage = buildErrorMessage(e, new Object[]{});
}
if (!errors.contains(errorMessage)) {
errors.add(errorMessage);
}
@@ -107,18 +87,6 @@ public class JakartaMultiPartRequest implements MultiPartRequest {
}
}
protected void setLocale(HttpServletRequest request) {
if (defaultLocale == null) {
defaultLocale = request.getLocale();
}
}
protected LocalizedMessage buildErrorMessage(Throwable e, Object[] args) {
String errorKey = "struts.messages.upload.error." + e.getClass().getSimpleName();
LOG.debug("Preparing error message for key: [{}]", errorKey);
return new LocalizedMessage(this.getClass(), errorKey, e.getMessage(), args);
}
protected void processUpload(HttpServletRequest request, String saveDir) throws FileUploadException, UnsupportedEncodingException {
for (FileItem item : parseRequest(request, saveDir)) {
LOG.debug("Found file item: [{}]", item.getFieldName());
@@ -313,13 +281,6 @@ public class JakartaMultiPartRequest implements MultiPartRequest {
return null;
}
/* (non-Javadoc)
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getErrors()
*/
public List<LocalizedMessage> getErrors() {
return errors;
}
/**
* Returns the canonical name of the given file.
*
@@ -1,8 +1,5 @@
package org.apache.struts2.dispatcher.multipart;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.LocalizedTextUtil;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadBase;
@@ -11,7 +8,6 @@ import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.dispatcher.LocalizedMessage;
import javax.servlet.http.HttpServletRequest;
@@ -27,15 +23,10 @@ import java.util.*;
* @author Chris Cranford
* @since 2.3.18
*/
public class JakartaStreamMultiPartRequest implements MultiPartRequest {
public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
static final Logger LOG = LogManager.getLogger(JakartaStreamMultiPartRequest.class);
/**
* Defines the internal buffer size used during streaming operations.
*/
private static final int BUFFER_SIZE = 10240;
/**
* Map between file fields and file data.
*/
@@ -46,55 +37,6 @@ public class JakartaStreamMultiPartRequest implements MultiPartRequest {
*/
private Map<String, List<String>> parameters = new HashMap<>();
/**
* Internal list of raised errors to be passed to the the Struts2 framework.
*/
private List<LocalizedMessage> errors = new ArrayList<>();
/**
* Internal list of non-critical messages to be passed to the Struts2 framework.
*/
private List<String> messages = new ArrayList<>();
/**
* Specifies the maximum size of the entire request.
*/
private Long maxSize;
/**
* Specifies the buffer size to use during streaming.
*/
private int bufferSize = BUFFER_SIZE;
/**
* Localization to be used regarding errors.
*/
private Locale defaultLocale = Locale.ENGLISH;
/**
* @param maxSize Injects the Struts multiple part maximum size.
*/
@Inject(StrutsConstants.STRUTS_MULTIPART_MAXSIZE)
public void setMaxSize(String maxSize) {
this.maxSize = Long.parseLong(maxSize);
}
/**
* @param bufferSize Sets the buffer size to be used.
*/
@Inject(value = StrutsConstants.STRUTS_MULTIPART_BUFFERSIZE, required = false)
public void setBufferSize(String bufferSize) {
this.bufferSize = Integer.parseInt(bufferSize);
}
/**
* @param provider Injects the Struts locale provider.
*/
@Inject
public void setLocaleProvider(LocaleProvider provider) {
defaultLocale = provider.getLocale();
}
/* (non-Javadoc)
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#cleanUp()
*/
@@ -128,13 +70,6 @@ public class JakartaStreamMultiPartRequest implements MultiPartRequest {
return types.toArray(new String[types.size()]);
}
/* (non-Javadoc)
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getErrors()
*/
public List<LocalizedMessage> getErrors() {
return errors;
}
/* (non-Javadoc)
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFile(java.lang.String)
*/
@@ -238,16 +173,6 @@ public class JakartaStreamMultiPartRequest implements MultiPartRequest {
}
}
/**
* @param request Inspect the servlet request and set the locale if one wasn't provided by
* the Struts2 framework.
*/
protected void setLocale(HttpServletRequest request) {
if (defaultLocale == null) {
defaultLocale = request.getLocale();
}
}
/**
* Processes the upload.
*
@@ -492,19 +417,6 @@ public class JakartaStreamMultiPartRequest implements MultiPartRequest {
return fileName;
}
/**
* Build error message.
*
* @param e the Throwable/Exception
* @param args arguments
* @return error message
*/
private LocalizedMessage buildErrorMessage(Throwable e, Object[] args) {
String errorKey = "struts.message.upload.error." + e.getClass().getSimpleName();
LOG.debug("Preparing error message for key: [{}]", errorKey);
return new LocalizedMessage(this.getClass(), errorKey, e.getMessage(), args);
}
/**
* Internal data structure used to store a reference to information needed
* to later pass post processing data to the <code>FileUploadInterceptor</code>.
@@ -21,20 +21,15 @@
package org.apache.struts2.dispatcher.multipart;
import com.opensymphony.xwork2.inject.Inject;
import com.opensymphony.xwork2.util.LocalizedTextUtil;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import http.utils.multipartrequest.ServletMultipartRequest;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.dispatcher.LocalizedMessage;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
@@ -44,26 +39,12 @@ import java.util.Locale;
* Multipart form data request adapter for Jason Pell's multipart utils package.
*
*/
public class PellMultiPartRequest implements MultiPartRequest {
public class PellMultiPartRequest extends AbstractMultiPartRequest {
private static final Logger LOG = LogManager.getLogger(PellMultiPartRequest.class);
private ServletMultipartRequest multi;
private String defaultEncoding;
private boolean maxSizeProvided;
private int maxSize;
@Inject(StrutsConstants.STRUTS_I18N_ENCODING)
public void setDefaultEncoding(String enc) {
this.defaultEncoding = enc;
}
@Inject(StrutsConstants.STRUTS_MULTIPART_MAXSIZE)
public void setMaxSize(String maxSize) {
this.maxSizeProvided = true;
this.maxSize = Integer.parseInt(maxSize);
}
/**
* Creates a new request wrapper to handle multi-part data using methods adapted from Jason Pell's
* multipart classes (see class description).
@@ -132,10 +113,6 @@ public class PellMultiPartRequest implements MultiPartRequest {
return values.toArray(new String[values.size()]);
}
public List<LocalizedMessage> getErrors() {
return Collections.emptyList();
}
/**
* Sets the encoding for the uploaded params. This needs to be set if you are using character sets other than
* ASCII.