Skip to content

Commit

Permalink
Merge pull request #1041 from apache/fix/WW-5461-input-name-cherry-pick
Browse files Browse the repository at this point in the history
WW-5461 Extends UploadedFile with inputName field
  • Loading branch information
lukaszlenart authored Sep 6, 2024
2 parents b69e441 + b237fb6 commit 74c01b3
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class FileUploadAction extends ActionSupport implements UploadedFilesAwar
private String fileName;
private String caption;
private String originalName;
private String inputName;

public String input() throws Exception {
return SUCCESS;
Expand All @@ -58,6 +59,10 @@ public String getOriginalName() {
return originalName;
}

public String getInputName() {
return inputName;
}

public Object getUploadedFile() {
return uploadedFile.getContent();
}
Expand Down Expand Up @@ -85,5 +90,6 @@ public void withUploadedFiles(List<UploadedFile> uploadedFiles) {
this.fileName = uploadedFile.getName();
this.contentType = uploadedFile.getContentType();
this.originalName = uploadedFile.getOriginalName();
this.inputName = uploadedFile.getInputName();
}
}
45 changes: 23 additions & 22 deletions apps/showcase/src/main/webapp/WEB-INF/fileupload/upload-success.jsp
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
<!--
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* 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
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* 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
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
-->
<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts2 Showcase - Fileupload sample</title>
<title>Struts2 Showcase - Fileupload sample</title>
</head>

<body>
<div class="page-header">
<h1>Fileupload sample</h1>
<h1>Fileupload sample</h1>
</div>

<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<ul>
<li>ContentType: <s:property value="contentType" /></li>
<li>FileName: <s:property value="fileName" /></li>
<li>Original FileName: <s:property value="originalName" /></li>
<li>File: <s:property value="uploadedFile" /></li>
<li>Size: <s:property value="uploadSize" /></li>
<li>Caption: <s:property value="caption" /></li>
</ul>
</div>
</div>
<div class="row">
<div class="col-md-12">
<ul>
<li>ContentType: <s:property value="contentType"/></li>
<li>FileName: <s:property value="fileName"/></li>
<li>Original FileName: <s:property value="originalName"/></li>
<li>File: <s:property value="uploadedFile"/></li>
<li>Caption: <s:property value="caption"/></li>
<li>Size: <s:property value="uploadSize"/></li>
<li>Input name: <s:property value="inputName"/></li>
</ul>
</div>
</div>
</div>

</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public void testSimpleFileUpload() throws Exception {
"ContentType: text/plain",
"Original FileName: " + tempFile.getName(),
"Caption: some caption",
"Size: 12"
"Size: 12",
"Input name: upload"
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ protected void processFileField(DiskFileItem item) {
.create(item.getPath().toFile())
.withOriginalName(item.getName())
.withContentType(item.getContentType())
.withInputName(item.getFieldName())
.build();
values.add(uploadedFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ protected void createUploadedFile(FileItemInput fileItemInput, File file) {
.create(file)
.withOriginalName(fileName)
.withContentType(fileItemInput.getContentType())
.withInputName(fileItemInput.getFieldName())
.build();

if (uploadedFiles.containsKey(fieldName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public class StrutsUploadedFile implements UploadedFile {
private final File file;
private final String contentType;
private final String originalName;
private final String inputName;

private StrutsUploadedFile(File file, String contentType, String originalName) {
private StrutsUploadedFile(File file, String contentType, String originalName, String inputName) {
this.file = file;
this.contentType = contentType;
this.originalName = originalName;
this.inputName = inputName;
}

@Override
Expand Down Expand Up @@ -72,18 +74,25 @@ public String getOriginalName() {
return originalName;
}

@Override
public String getInputName() {
return inputName;
}

@Override
public String toString() {
return "StrutsUploadedFile{" +
"contentType='" + contentType + '\'' +
", originalName='" + originalName + '\'' +
'}';
"contentType='" + contentType + '\'' +
", originalName='" + originalName + '\'' +
", inputName='" + inputName + '\'' +
'}';
}

public static class Builder {
private final File file;
private String contentType;
private String originalName;
private String inputName;

private Builder(File file) {
this.file = file;
Expand All @@ -103,8 +112,13 @@ public Builder withOriginalName(String originalName) {
return this;
}

public Builder withInputName(String inputName) {
this.inputName = inputName;
return this;
}

public UploadedFile build() {
return new StrutsUploadedFile(this.file, this.contentType, this.originalName);
return new StrutsUploadedFile(this.file, this.contentType, this.originalName, this.inputName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@ public interface UploadedFile extends Serializable {
*/
String getContentType();

/**
* Represents a name of the input file, eg.:
* "myFile" in case of <input type="file" name="myFile">
*
* @return name of the input file field
* @since 6.7.0
*/
String getInputName();

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public void uploadedFilesToDisk() throws IOException {
.isEqualTo("test1.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file1");
assertThat(file.getContent()).asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
.content()
Expand All @@ -114,6 +116,8 @@ public void uploadedFilesToDisk() throws IOException {
.isEqualTo("test2.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file2");
assertThat(file.getContent())
.asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
Expand Down Expand Up @@ -152,6 +156,8 @@ public void uploadedMultipleFilesToDisk() throws IOException {
.isEqualTo("test1.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file1");
assertThat(file.getContent()).asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
.content()
Expand All @@ -164,6 +170,8 @@ public void uploadedMultipleFilesToDisk() throws IOException {
.isEqualTo("test2.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file1");
assertThat(file.getContent())
.asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
Expand Down Expand Up @@ -202,6 +210,8 @@ public void uploadedFilesWithLargeBuffer() throws IOException {
.isEqualTo("test1.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file1");
assertThat(file.getContent())
.asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
Expand All @@ -213,6 +223,8 @@ public void uploadedFilesWithLargeBuffer() throws IOException {
.isTrue();
assertThat(file.getOriginalName())
.isEqualTo("test2.csv");
assertThat(file.getInputName())
.isEqualTo("file2");
assertThat(file.getContent())
.asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
Expand Down Expand Up @@ -249,6 +261,8 @@ public void cleanUp() throws IOException {
.isEqualTo("test1.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file1");
assertThat(file.getContent()).asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
.content()
Expand All @@ -261,6 +275,8 @@ public void cleanUp() throws IOException {
.isEqualTo("test2.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file2");
assertThat(file.getContent())
.asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public String getOriginalName() {
public String getContentType() {
return null;
}

@Override
public String getInputName() {
return null;
}
};

private MockHttpServletRequest request;
Expand Down

0 comments on commit 74c01b3

Please sign in to comment.