Skip to content

Commit

Permalink
Merge pull request #904 from apache/merge/master-to-7xx-2024-04-07
Browse files Browse the repository at this point in the history
[2024-04-07] Merge master branch into 7.x.x
  • Loading branch information
lukaszlenart authored Apr 8, 2024
2 parents 4939d3c + e5e6145 commit f59a060
Show file tree
Hide file tree
Showing 39 changed files with 736 additions and 191 deletions.
8 changes: 7 additions & 1 deletion .asf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ notifications:
github:
del_branch_on_merge: true
protected_branches:
master: { }
master:
# contexts are the names of checks that must pass.
contexts:
- build
required_pull_request_reviews:
require_code_owner_reviews: true
required_approving_review_count: 1
autolink_jira:
- WW
dependabot_alerts: true
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
The Apache Struts web framework
-------------------------------

[![Jenkins Build](https://builds.apache.org/buildStatus/icon?job=Struts%2FStruts+Core%2Fmaster)](https://ci-builds.apache.org/job/Struts/job/Struts%20Core/job/master/)
[![Build Status](https://ci-builds.apache.org/buildStatus/icon?job=Struts%2FStruts+Core%2Fmaster)](https://ci-builds.apache.org/job/Struts/job/Struts%20Core/job/master/)
[![Java Build](https://github.com/apache/struts/actions/workflows/maven.yml/badge.svg)](https://github.com/apache/struts/actions/workflows/maven.yml)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.apache.struts/struts2-core/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.apache.struts/struts2-core/)
[![Javadocs](https://javadoc.io/badge/org.apache.struts/struts2-core.svg)](https://javadoc.io/doc/org.apache.struts/struts2-core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.struts2.dispatcher.multipart.UploadedFile;
import org.apache.struts2.interceptor.parameter.StrutsParameter;

import java.io.File;
import java.util.List;

/**
Expand All @@ -34,7 +33,7 @@
public class FileUploadAction extends ActionSupport implements UploadedFilesAware {

private String contentType;
private UploadedFile<File> uploadedFile;
private UploadedFile uploadedFile;
private String fileName;
private String caption;
private String originalName;
Expand Down Expand Up @@ -81,7 +80,7 @@ public long getUploadSize() {
}

@Override
public void withUploadedFiles(List<UploadedFile<File>> uploadedFiles) {
public void withUploadedFiles(List<UploadedFile> uploadedFiles) {
this.uploadedFile = uploadedFiles.get(0);
this.fileName = uploadedFile.getName();
this.contentType = uploadedFile.getContentType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,51 @@
package org.apache.struts2.showcase.fileupload;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.action.UploadedFilesAware;
import org.apache.struts2.dispatcher.multipart.UploadedFile;

import java.io.File;
import java.util.List;

/**
* Showcase action - mutiple file upload using array.
*
* @version $Date$ $Id$
*/
public class MultipleFileUploadUsingArrayAction extends ActionSupport {

private File[] uploads = new File[0];
private String[] uploadFileNames = new String[0];
private String[] uploadContentTypes = new String[0];


public String upload() throws Exception {
System.out.println("\n\n upload2");
System.out.println("files:");
for (File u : uploads) {
System.out.println("*** " + u + "\t" + u.length());
}
System.out.println("filenames:");
for (String n : uploadFileNames) {
System.out.println("*** " + n);
}
System.out.println("content types:");
for (String c : uploadContentTypes) {
System.out.println("*** " + c);
}
System.out.println("\n\n");
return SUCCESS;
}
public class MultipleFileUploadUsingArrayAction extends ActionSupport implements UploadedFilesAware {

public File[] getUpload() {
return this.uploads;
}
private List<UploadedFile> uploadedFiles;

public void setUpload(File[] upload) {
this.uploads = upload;
}
public String upload() throws Exception {
System.out.println("\n\n upload2");
System.out.println("files:");
for (UploadedFile u : uploadedFiles) {
System.out.println("*** " + u + "\t" + u.length());
}
System.out.println("filenames:");
for (String n : getUploadFileNames()) {
System.out.println("*** " + n);
}
System.out.println("content types:");
for (String c : getUploadContentTypes()) {
System.out.println("*** " + c);
}
System.out.println("\n\n");
return SUCCESS;
}

public String[] getUploadFileName() {
return this.uploadFileNames;
}
@Override
public void withUploadedFiles(List<UploadedFile> uploadedFiles) {
this.uploadedFiles = uploadedFiles;
}

public void setUploadFileName(String[] uploadFileName) {
this.uploadFileNames = uploadFileName;
}
private String[] getUploadFileNames() {
return this.uploadedFiles.stream()
.map(UploadedFile::getOriginalName)
.toArray(String[]::new);
}

public String[] getUploadContentType() {
return this.uploadContentTypes;
}
private String[] getUploadContentTypes() {
return this.uploadedFiles.stream()
.map(UploadedFile::getContentType)
.toArray(String[]::new);
}

public void setUploadContentType(String[] uploadContentType) {
this.uploadContentTypes = uploadContentType;
}
}
// END SNIPPET: entire-file
Original file line number Diff line number Diff line change
Expand Up @@ -22,64 +22,56 @@
package org.apache.struts2.showcase.fileupload;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.action.UploadedFilesAware;
import org.apache.struts2.dispatcher.multipart.UploadedFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* Showcase action - multiple file upload using List
*
* @version $Date$ $Id$
*/
public class MultipleFileUploadUsingListAction extends ActionSupport {

private List<File> uploads = new ArrayList<>();
private List<String> uploadFileNames = new ArrayList<>();
private List<String> uploadContentTypes = new ArrayList<>();


public List<File> getUpload() {
return this.uploads;
}

public void setUpload(List<File> uploads) {
this.uploads = uploads;
}
public class MultipleFileUploadUsingListAction extends ActionSupport implements UploadedFilesAware {

public List<String> getUploadFileName() {
return this.uploadFileNames;
}
private List<UploadedFile> uploads = new ArrayList<>();

public void setUploadFileName(List<String> uploadFileNames) {
this.uploadFileNames = uploadFileNames;
}
public List<UploadedFile> getUpload() {
return this.uploads;
}

public List<String> getUploadContentType() {
return this.uploadContentTypes;
}
@Override
public void withUploadedFiles(List<UploadedFile> uploads) {
this.uploads = uploads;
}

public void setUploadContentType(List<String> contentTypes) {
this.uploadContentTypes = contentTypes;
}
private List<String> getUploadFileNames() {
return this.uploads.stream()
.map(UploadedFile::getOriginalName)
.collect(Collectors.toList());
}

public String upload() throws Exception {
private List<String> getUploadContentTypes() {
return this.uploads.stream()
.map(UploadedFile::getContentType)
.collect(Collectors.toList());
}

System.out.println("\n\n upload1");
System.out.println("files:");
for (File u : uploads) {
System.out.println("*** " + u + "\t" + u.length());
}
System.out.println("filenames:");
for (String n : uploadFileNames) {
System.out.println("*** " + n);
}
System.out.println("content types:");
for (String c : uploadContentTypes) {
System.out.println("*** " + c);
}
System.out.println("\n\n");
return SUCCESS;
}
}
// END SNIPPET: entire-file
public String upload() throws Exception {
System.out.println("\n\n upload1");
System.out.println("files:");
for (UploadedFile u : uploads) {
System.out.println("*** " + u + "\t" + u.length());
}
System.out.println("filenames:");
for (String n : getUploadFileNames()) {
System.out.println("*** " + n);
}
System.out.println("content types:");
for (String c : getUploadContentTypes()) {
System.out.println("*** " + c);
}
System.out.println("\n\n");
return SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ private void registerAllowlist() {

@Override
public void destroy() {
providerAllowlist.clearAllowlist(this);
if (providerAllowlist != null) {
providerAllowlist.clearAllowlist(this);
}
}

protected Class<?> allowAndLoadClass(String className) throws ClassNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.apache.struts2.dispatcher.multipart.UploadedFile;

import java.io.File;
import java.util.List;

/**
Expand All @@ -36,6 +35,6 @@ public interface UploadedFilesAware {
*
* @param uploadedFiles a list of {@link UploadedFile}, cannot be null. It can be empty.
*/
void withUploadedFiles(List<UploadedFile<File>> uploadedFiles);
void withUploadedFiles(List<UploadedFile> uploadedFiles);

}
13 changes: 10 additions & 3 deletions core/src/main/java/org/apache/struts2/dispatcher/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -987,10 +987,12 @@ private void applyEncoding(HttpServletResponse response, String encoding) {
public HttpServletRequest wrapRequest(HttpServletRequest request) throws IOException {
// don't wrap more than once
if (request instanceof StrutsRequestWrapper) {
LOG.debug("Request already wrapped with: {}", StrutsRequestWrapper.class.getSimpleName());
return request;
}

if (isMultipartSupportEnabled(request) && isMultipartRequest(request)) {
LOG.debug("Wrapping multipart request with: {}", MultiPartRequestWrapper.class.getSimpleName());
request = new MultiPartRequestWrapper(
getMultiPartRequest(),
request,
Expand All @@ -999,6 +1001,7 @@ public HttpServletRequest wrapRequest(HttpServletRequest request) throws IOExcep
disableRequestAttributeValueStackLookup
);
} else {
LOG.debug("Wrapping request using: {}", StrutsRequestWrapper.class.getSimpleName());
request = new StrutsRequestWrapper(request, disableRequestAttributeValueStackLookup);
}

Expand All @@ -1013,6 +1016,7 @@ public HttpServletRequest wrapRequest(HttpServletRequest request) throws IOExcep
* @since 2.5.11
*/
protected boolean isMultipartSupportEnabled(HttpServletRequest request) {
LOG.debug("Support for multipart request is enabled: {}", multipartSupportEnabled);
return multipartSupportEnabled;
}

Expand All @@ -1027,9 +1031,12 @@ protected boolean isMultipartRequest(HttpServletRequest request) {
String httpMethod = request.getMethod();
String contentType = request.getContentType();

return REQUEST_POST_METHOD.equalsIgnoreCase(httpMethod) &&
contentType != null &&
multipartValidationPattern.matcher(contentType.toLowerCase(Locale.ENGLISH)).matches();
boolean isPostRequest = REQUEST_POST_METHOD.equalsIgnoreCase(httpMethod);
boolean isProperContentType = contentType != null && multipartValidationPattern.matcher(contentType.toLowerCase(Locale.ENGLISH)).matches();

LOG.debug("Validating if this is a proper Multipart request. Request is POST: {} and ContentType matches pattern ({}): {}",
isPostRequest, multipartValidationPattern, isProperContentType);
return isPostRequest && isProperContentType;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
* Abstract class with some helper methods, it should be used
* when starting development of another implementation of {@link MultiPartRequest}
*/
public abstract class AbstractMultiPartRequest<T> implements MultiPartRequest {
public abstract class AbstractMultiPartRequest implements MultiPartRequest {

protected static final String STRUTS_MESSAGES_UPLOAD_ERROR_PARAMETER_TOO_LONG_KEY = "struts.messages.upload.error.parameter.too.long";

Expand Down Expand Up @@ -100,7 +100,7 @@ public abstract class AbstractMultiPartRequest<T> implements MultiPartRequest {
/**
* Map between file fields and file data.
*/
protected Map<String, List<UploadedFile<T>>> uploadedFiles = new HashMap<>();
protected Map<String, List<UploadedFile>> uploadedFiles = new HashMap<>();

/**
* Map between non-file fields and values.
Expand Down Expand Up @@ -326,8 +326,7 @@ public String[] getContentType(String fieldName) {
/* (non-Javadoc)
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequest#getFile(java.lang.String)
*/
@SuppressWarnings("unchecked")
public UploadedFile<T>[] getFile(String fieldName) {
public UploadedFile[] getFile(String fieldName) {
return uploadedFiles.getOrDefault(fieldName, Collections.emptyList())
.toArray(UploadedFile[]::new);
}
Expand Down Expand Up @@ -383,8 +382,8 @@ public String[] getParameterValues(String name) {
public void cleanUp() {
try {
LOG.debug("Performing File Upload temporary storage cleanup.");
for (List<UploadedFile<T>> uploadedFileList : uploadedFiles.values()) {
for (UploadedFile<T> uploadedFile : uploadedFileList) {
for (List<UploadedFile> uploadedFileList : uploadedFiles.values()) {
for (UploadedFile uploadedFile : uploadedFileList) {
if (uploadedFile.isFile()) {
LOG.debug("Deleting file: {}", uploadedFile.getName());
if (!uploadedFile.delete()) {
Expand Down
Loading

0 comments on commit f59a060

Please sign in to comment.