WW-5461 Extends UploadedFile with inputName field

This commit is contained in:
Lukasz Lenart
2024-09-03 07:40:17 +02:00
parent b69e44122d
commit b237fb6f88
9 changed files with 82 additions and 28 deletions
@@ -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;
@@ -58,6 +59,10 @@ public class FileUploadAction extends ActionSupport implements UploadedFilesAwar
return originalName;
}
public String getInputName() {
return inputName;
}
public Object getUploadedFile() {
return uploadedFile.getContent();
}
@@ -85,5 +90,6 @@ public class FileUploadAction extends ActionSupport implements UploadedFilesAwar
this.fileName = uploadedFile.getName();
this.contentType = uploadedFile.getContentType();
this.originalName = uploadedFile.getOriginalName();
this.inputName = uploadedFile.getInputName();
}
}
@@ -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>
@@ -59,7 +59,8 @@ public class FileUploadTest {
"ContentType: text/plain",
"Original FileName: " + tempFile.getName(),
"Caption: some caption",
"Size: 12"
"Size: 12",
"Input name: upload"
);
}
}
@@ -118,6 +118,7 @@ public class JakartaMultiPartRequest extends AbstractMultiPartRequest {
.create(item.getPath().toFile())
.withOriginalName(item.getName())
.withContentType(item.getContentType())
.withInputName(item.getFieldName())
.build();
values.add(uploadedFile);
}
@@ -251,6 +251,7 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
.create(file)
.withOriginalName(fileName)
.withContentType(fileItemInput.getContentType())
.withInputName(fileItemInput.getFieldName())
.build();
if (uploadedFiles.containsKey(fieldName)) {
@@ -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
@@ -72,18 +74,25 @@ public class StrutsUploadedFile implements UploadedFile {
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;
@@ -103,8 +112,13 @@ public class StrutsUploadedFile implements UploadedFile {
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);
}
}
}
@@ -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();
}
@@ -102,6 +102,8 @@ abstract class AbstractMultiPartRequestTest {
.isEqualTo("test1.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file1");
assertThat(file.getContent()).asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
.content()
@@ -114,6 +116,8 @@ abstract class AbstractMultiPartRequestTest {
.isEqualTo("test2.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file2");
assertThat(file.getContent())
.asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
@@ -152,6 +156,8 @@ abstract class AbstractMultiPartRequestTest {
.isEqualTo("test1.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file1");
assertThat(file.getContent()).asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
.content()
@@ -164,6 +170,8 @@ abstract class AbstractMultiPartRequestTest {
.isEqualTo("test2.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file1");
assertThat(file.getContent())
.asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
@@ -202,6 +210,8 @@ abstract class AbstractMultiPartRequestTest {
.isEqualTo("test1.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file1");
assertThat(file.getContent())
.asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
@@ -213,6 +223,8 @@ abstract class AbstractMultiPartRequestTest {
.isTrue();
assertThat(file.getOriginalName())
.isEqualTo("test2.csv");
assertThat(file.getInputName())
.isEqualTo("file2");
assertThat(file.getContent())
.asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
@@ -249,6 +261,8 @@ abstract class AbstractMultiPartRequestTest {
.isEqualTo("test1.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file1");
assertThat(file.getContent()).asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
.content()
@@ -261,6 +275,8 @@ abstract class AbstractMultiPartRequestTest {
.isEqualTo("test2.csv");
assertThat(file.getContentType())
.isEqualTo("text/csv");
assertThat(file.getInputName())
.isEqualTo("file2");
assertThat(file.getContent())
.asInstanceOf(InstanceOfAssertFactories.FILE)
.exists()
@@ -91,6 +91,11 @@ public class ActionFileUploadInterceptorTest extends StrutsInternalTestCase {
public String getContentType() {
return null;
}
@Override
public String getInputName() {
return null;
}
};
private MockHttpServletRequest request;