Skip to content

Commit 9613325

Browse files
committed
WW-5461 Extends UploadedFile with inputName field
1 parent b69e441 commit 9613325

File tree

8 files changed

+72
-28
lines changed

8 files changed

+72
-28
lines changed

apps/showcase/src/main/java/org/apache/struts2/showcase/fileupload/FileUploadAction.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class FileUploadAction extends ActionSupport implements UploadedFilesAwar
3737
private String fileName;
3838
private String caption;
3939
private String originalName;
40+
private String inputName;
4041

4142
public String input() throws Exception {
4243
return SUCCESS;
@@ -58,6 +59,10 @@ public String getOriginalName() {
5859
return originalName;
5960
}
6061

62+
public String getInputName() {
63+
return inputName;
64+
}
65+
6166
public Object getUploadedFile() {
6267
return uploadedFile.getContent();
6368
}
@@ -85,5 +90,6 @@ public void withUploadedFiles(List<UploadedFile> uploadedFiles) {
8590
this.fileName = uploadedFile.getName();
8691
this.contentType = uploadedFile.getContentType();
8792
this.originalName = uploadedFile.getOriginalName();
93+
this.inputName = uploadedFile.getInputName();
8894
}
8995
}

apps/showcase/src/main/webapp/WEB-INF/fileupload/upload-success.jsp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,52 @@
11
<!--
22
/*
33
* Licensed to the Apache Software Foundation (ASF) under one
4-
* or more contributor license agreements. See the NOTICE file
4+
* or more contributor license agreements. See the NOTICE file
55
* distributed with this work for additional information
6-
* regarding copyright ownership. The ASF licenses this file
6+
* regarding copyright ownership. The ASF licenses this file
77
* to you under the Apache License, Version 2.0 (the
88
* "License"); you may not use this file except in compliance
9-
* with the License. You may obtain a copy of the License at
9+
* with the License. You may obtain a copy of the License at
1010
*
11-
* http://www.apache.org/licenses/LICENSE-2.0
11+
* http://www.apache.org/licenses/LICENSE-2.0
1212
*
1313
* Unless required by applicable law or agreed to in writing,
1414
* software distributed under the License is distributed on an
1515
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16-
* KIND, either express or implied. See the License for the
16+
* KIND, either express or implied. See the License for the
1717
* specific language governing permissions and limitations
1818
* under the License.
1919
*/
2020
-->
2121
<%@ page
22-
language="java"
23-
contentType="text/html; charset=UTF-8"
24-
pageEncoding="UTF-8"%>
22+
language="java"
23+
contentType="text/html; charset=UTF-8"
24+
pageEncoding="UTF-8" %>
2525
<%@ taglib prefix="s" uri="/struts-tags" %>
2626
<html>
2727
<head>
28-
<title>Struts2 Showcase - Fileupload sample</title>
28+
<title>Struts2 Showcase - Fileupload sample</title>
2929
</head>
3030

3131
<body>
3232
<div class="page-header">
33-
<h1>Fileupload sample</h1>
33+
<h1>Fileupload sample</h1>
3434
</div>
3535

3636
<div class="container-fluid">
37-
<div class="row">
38-
<div class="col-md-12">
39-
<ul>
40-
<li>ContentType: <s:property value="contentType" /></li>
41-
<li>FileName: <s:property value="fileName" /></li>
42-
<li>Original FileName: <s:property value="originalName" /></li>
43-
<li>File: <s:property value="uploadedFile" /></li>
44-
<li>Size: <s:property value="uploadSize" /></li>
45-
<li>Caption: <s:property value="caption" /></li>
46-
</ul>
47-
</div>
48-
</div>
37+
<div class="row">
38+
<div class="col-md-12">
39+
<ul>
40+
<li>ContentType: <s:property value="contentType"/></li>
41+
<li>FileName: <s:property value="fileName"/></li>
42+
<li>Original FileName: <s:property value="originalName"/></li>
43+
<li>File: <s:property value="uploadedFile"/></li>
44+
<li>Caption: <s:property value="caption"/></li>
45+
<li>Size: <s:property value="uploadSize"/></li>
46+
<li>Input name: <s:property value="inputName"/></li>
47+
</ul>
48+
</div>
49+
</div>
4950
</div>
5051

5152
</body>

apps/showcase/src/test/java/it/org/apache/struts2/showcase/FileUploadTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@
2424
import org.htmlunit.html.HtmlInput;
2525
import org.htmlunit.html.HtmlPage;
2626
import org.htmlunit.html.HtmlSubmitInput;
27+
import org.htmlunit.html.DomElement;
2728
import org.junit.Test;
2829

2930
import java.io.File;
3031
import java.io.FileWriter;
3132
import java.security.SecureRandom;
3233

3334
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
36+
import static org.junit.Assert.assertNotNull;
37+
38+
import java.nio.file.Files;
39+
import java.nio.file.Paths;
3440

3541
public class FileUploadTest {
3642

@@ -59,7 +65,8 @@ public void testSimpleFileUpload() throws Exception {
5965
"ContentType: text/plain",
6066
"Original FileName: " + tempFile.getName(),
6167
"Caption: some caption",
62-
"Size: 12"
68+
"Size: 12",
69+
"Input name: upload"
6370
);
6471
}
6572
}

core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaMultiPartRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ protected void processFileField(DiskFileItem item) {
118118
.create(item.getPath().toFile())
119119
.withOriginalName(item.getName())
120120
.withContentType(item.getContentType())
121+
.withInputName(item.getFieldName())
121122
.build();
122123
values.add(uploadedFile);
123124
}

core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ protected void createUploadedFile(FileItemInput fileItemInput, File file) {
251251
.create(file)
252252
.withOriginalName(fileName)
253253
.withContentType(fileItemInput.getContentType())
254+
.withInputName(fileItemInput.getFieldName())
254255
.build();
255256

256257
if (uploadedFiles.containsKey(fieldName)) {

core/src/main/java/org/apache/struts2/dispatcher/multipart/StrutsUploadedFile.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ public class StrutsUploadedFile implements UploadedFile {
2525
private final File file;
2626
private final String contentType;
2727
private final String originalName;
28+
private final String inputName;
2829

29-
private StrutsUploadedFile(File file, String contentType, String originalName) {
30+
private StrutsUploadedFile(File file, String contentType, String originalName, String inputName) {
3031
this.file = file;
3132
this.contentType = contentType;
3233
this.originalName = originalName;
34+
this.inputName = inputName;
3335
}
3436

3537
@Override
@@ -72,18 +74,25 @@ public String getOriginalName() {
7274
return originalName;
7375
}
7476

77+
@Override
78+
public String getInputName() {
79+
return inputName;
80+
}
81+
7582
@Override
7683
public String toString() {
7784
return "StrutsUploadedFile{" +
78-
"contentType='" + contentType + '\'' +
79-
", originalName='" + originalName + '\'' +
80-
'}';
85+
"contentType='" + contentType + '\'' +
86+
", originalName='" + originalName + '\'' +
87+
", inputName='" + inputName + '\'' +
88+
'}';
8189
}
8290

8391
public static class Builder {
8492
private final File file;
8593
private String contentType;
8694
private String originalName;
95+
private String inputName;
8796

8897
private Builder(File file) {
8998
this.file = file;
@@ -103,8 +112,13 @@ public Builder withOriginalName(String originalName) {
103112
return this;
104113
}
105114

115+
public Builder withInputName(String inputName) {
116+
this.inputName = inputName;
117+
return this;
118+
}
119+
106120
public UploadedFile build() {
107-
return new StrutsUploadedFile(this.file, this.contentType, this.originalName);
121+
return new StrutsUploadedFile(this.file, this.contentType, this.originalName, this.inputName);
108122
}
109123
}
110124
}

core/src/main/java/org/apache/struts2/dispatcher/multipart/UploadedFile.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,13 @@ public interface UploadedFile extends Serializable {
6565
*/
6666
String getContentType();
6767

68+
/**
69+
* Represents a name of the input file, eg.:
70+
* "myFile" in case of <input type="file" name="myFile">
71+
*
72+
* @return name of the input file field
73+
* @since 6.7.0
74+
*/
75+
String getInputName();
76+
6877
}

core/src/test/java/org/apache/struts2/interceptor/ActionFileUploadInterceptorTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public String getOriginalName() {
9191
public String getContentType() {
9292
return null;
9393
}
94+
95+
@Override
96+
public String getInputName() {
97+
return null;
98+
}
9499
};
95100

96101
private MockHttpServletRequest request;

0 commit comments

Comments
 (0)