mirror of
https://github.com/apache/struts.git
synced 2026-08-07 07:37:20 +00:00
WW-5388 Extracts method to read charset
This commit is contained in:
@@ -147,7 +147,7 @@ public class Dispatcher {
|
||||
private String defaultLocale;
|
||||
|
||||
/**
|
||||
* Store state of StrutsConstants.STRUTS_MULTIPART_SAVEDIR setting.
|
||||
* Store state of {@link StrutsConstants#STRUTS_MULTIPART_SAVE_DIR} setting.
|
||||
*/
|
||||
private String multipartSaveDir;
|
||||
|
||||
@@ -323,7 +323,7 @@ public class Dispatcher {
|
||||
/**
|
||||
* @deprecated since 6.4.0, no replacement.
|
||||
*/
|
||||
@Deprecated(since = "6.4.9", forRemoval = true)
|
||||
@Deprecated(since = "6.4.0", forRemoval = true)
|
||||
public void setMultipartHandler(String val) {
|
||||
// no-op
|
||||
}
|
||||
@@ -868,11 +868,12 @@ public class Dispatcher {
|
||||
* @return the path to save uploaded files to
|
||||
*/
|
||||
protected String getSaveDir() {
|
||||
String saveDir = multipartSaveDir.trim();
|
||||
String saveDir = Objects.toString(multipartSaveDir, "").trim();
|
||||
|
||||
if (saveDir.equals("")) {
|
||||
File tempdir = (File) servletContext.getAttribute("jakarta.servlet.context.tempdir");
|
||||
LOG.info("Unable to find 'struts.multipart.saveDir' property setting. Defaulting to jakarta.servlet.context.tempdir");
|
||||
if (saveDir.isEmpty()) {
|
||||
File tempdir = (File) servletContext.getAttribute(ServletContext.TEMPDIR);
|
||||
LOG.info("Unable to find: {} property setting. Defaulting to: {}",
|
||||
StrutsConstants.STRUTS_MULTIPART_SAVE_DIR, ServletContext.TEMPDIR);
|
||||
|
||||
if (tempdir != null) {
|
||||
saveDir = tempdir.toString();
|
||||
@@ -885,9 +886,9 @@ public class Dispatcher {
|
||||
if (!multipartSaveDir.mkdirs()) {
|
||||
String logMessage;
|
||||
try {
|
||||
logMessage = "Could not find create multipart save directory '" + multipartSaveDir.getCanonicalPath() + "'.";
|
||||
logMessage = "Could not create multipart save directory '" + multipartSaveDir.getCanonicalPath() + "'.";
|
||||
} catch (IOException e) {
|
||||
logMessage = "Could not find create multipart save directory '" + multipartSaveDir.toString() + "'.";
|
||||
logMessage = "Could not create multipart save directory '" + multipartSaveDir + "'.";
|
||||
}
|
||||
if (devMode) {
|
||||
LOG.error(logMessage);
|
||||
|
||||
+13
@@ -26,6 +26,7 @@ import org.apache.commons.fileupload2.core.FileUploadException;
|
||||
import org.apache.commons.fileupload2.core.FileUploadFileCountLimitException;
|
||||
import org.apache.commons.fileupload2.core.FileUploadSizeException;
|
||||
import org.apache.commons.fileupload2.jakarta.servlet6.JakartaServletDiskFileUpload;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.struts2.StrutsConstants;
|
||||
@@ -167,6 +168,18 @@ public abstract class AbstractMultiPartRequest<T> implements MultiPartRequest {
|
||||
*/
|
||||
protected abstract void processUpload(HttpServletRequest request, String saveDir) throws IOException;
|
||||
|
||||
/**
|
||||
* @param request multipart request
|
||||
* @return character encoding from request or {@link #defaultEncoding}
|
||||
*/
|
||||
protected Charset readCharsetEncoding(HttpServletRequest request) {
|
||||
String charsetStr = StringUtils.isBlank(request.getCharacterEncoding())
|
||||
? defaultEncoding
|
||||
: request.getCharacterEncoding();
|
||||
|
||||
return Charset.forName(charsetStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an instance of {@link JakartaServletDiskFileUpload} used by the parser to extract uploaded files
|
||||
*
|
||||
|
||||
+21
-29
@@ -42,12 +42,10 @@ public class JakartaMultiPartRequest extends AbstractMultiPartRequest<File> {
|
||||
|
||||
@Override
|
||||
protected void processUpload(HttpServletRequest request, String saveDir) throws IOException {
|
||||
String charset = StringUtils.isBlank(request.getCharacterEncoding())
|
||||
? defaultEncoding
|
||||
: request.getCharacterEncoding();
|
||||
Charset charset = readCharsetEncoding(request);
|
||||
|
||||
JakartaServletDiskFileUpload servletFileUpload =
|
||||
prepareServletFileUpload(Charset.forName(charset), Path.of(saveDir));
|
||||
prepareServletFileUpload(charset, Path.of(saveDir));
|
||||
|
||||
for (DiskFileItem item : servletFileUpload.parseRequest(request)) {
|
||||
LOG.debug(() -> "Processing a form field: " + sanitizeNewlines(item.getFieldName()));
|
||||
@@ -76,33 +74,27 @@ public class JakartaMultiPartRequest extends AbstractMultiPartRequest<File> {
|
||||
return new JakartaServletDiskFileUpload(factory);
|
||||
}
|
||||
|
||||
protected void processNormalFormField(DiskFileItem item, Charset charset) throws IOException {
|
||||
LOG.debug("Item: {} is a normal form field", item.getName());
|
||||
|
||||
protected void processNormalFormField(DiskFileItem item, String charset) throws IOException {
|
||||
try {
|
||||
LOG.debug("Item: {} is a normal form field", item.getName());
|
||||
Charset encoding = StringUtils.isBlank(charset) ? Charset.forName(defaultEncoding) : Charset.forName(charset);
|
||||
|
||||
List<String> values;
|
||||
String fieldName = item.getFieldName();
|
||||
if (parameters.get(fieldName) != null) {
|
||||
values = parameters.get(fieldName);
|
||||
} else {
|
||||
values = new ArrayList<>();
|
||||
}
|
||||
|
||||
String fieldValue = item.getString(encoding);
|
||||
if (exceedsMaxStringLength(fieldName, fieldValue)) {
|
||||
return;
|
||||
}
|
||||
if (item.getSize() == 0) {
|
||||
values.add(StringUtils.EMPTY);
|
||||
} else {
|
||||
values.add(fieldValue);
|
||||
}
|
||||
parameters.put(fieldName, values);
|
||||
} finally {
|
||||
item.delete();
|
||||
List<String> values;
|
||||
String fieldName = item.getFieldName();
|
||||
if (parameters.get(fieldName) != null) {
|
||||
values = parameters.get(fieldName);
|
||||
} else {
|
||||
values = new ArrayList<>();
|
||||
}
|
||||
|
||||
String fieldValue = item.getString(charset);
|
||||
if (exceedsMaxStringLength(fieldName, fieldValue)) {
|
||||
return;
|
||||
}
|
||||
if (item.getSize() == 0) {
|
||||
values.add(StringUtils.EMPTY);
|
||||
} else {
|
||||
values.add(fieldValue);
|
||||
}
|
||||
parameters.put(fieldName, values);
|
||||
}
|
||||
|
||||
protected void processFileField(DiskFileItem item) {
|
||||
|
||||
+3
-6
@@ -24,7 +24,6 @@ import org.apache.commons.fileupload2.core.FileItemInput;
|
||||
import org.apache.commons.fileupload2.core.FileUploadFileCountLimitException;
|
||||
import org.apache.commons.fileupload2.core.FileUploadSizeException;
|
||||
import org.apache.commons.fileupload2.jakarta.servlet6.JakartaServletDiskFileUpload;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.struts2.dispatcher.LocalizedMessage;
|
||||
@@ -63,13 +62,11 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest<File
|
||||
*/
|
||||
@Override
|
||||
protected void processUpload(HttpServletRequest request, String saveDir) throws IOException {
|
||||
String charset = StringUtils.isBlank(request.getCharacterEncoding())
|
||||
? defaultEncoding
|
||||
: request.getCharacterEncoding();
|
||||
|
||||
Charset charset = readCharsetEncoding(request);
|
||||
Path location = Path.of(saveDir);
|
||||
|
||||
JakartaServletDiskFileUpload servletFileUpload =
|
||||
prepareServletFileUpload(Charset.forName(charset), location);
|
||||
prepareServletFileUpload(charset, location);
|
||||
|
||||
LOG.debug("Using Jakarta Stream API to process request");
|
||||
servletFileUpload.getItemIterator(request).forEachRemaining(item -> {
|
||||
|
||||
@@ -29,6 +29,6 @@
|
||||
<Root level="info">
|
||||
<AppenderRef ref="STDOUT"/>
|
||||
</Root>
|
||||
<!--<Logger name="org.apache.struts2.dispatcher.multipart" level="debug"/>-->
|
||||
<Logger name="org.apache.struts2.dispatcher.multipart" level="debug"/>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
Reference in New Issue
Block a user