mirror of
https://github.com/apache/struts.git
synced 2026-08-05 14:47:09 +00:00
WW-5461 Extends UploadedFile with inputName field
This commit is contained in:
+6
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,8 @@
|
||||
<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>Caption: <s:property value="caption" /></li>
|
||||
<li id="input-name">Input name: <s:property value="inputName" /></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -25,10 +25,14 @@ import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlInput;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
public class FileUploadTest {
|
||||
|
||||
@@ -45,9 +49,31 @@ public class FileUploadTest {
|
||||
uploadInput.setValueAttribute(tempFile.getAbsolutePath());
|
||||
final HtmlSubmitInput button = form.getInputByValue("Submit");
|
||||
final HtmlPage resultPage = button.click();
|
||||
|
||||
DomElement errorMessage = resultPage.getFirstByXPath("//span[@class='errorMessage']");
|
||||
Assert.assertNotNull(errorMessage);
|
||||
Assert.assertEquals("File cannot be empty", errorMessage.getVisibleText());
|
||||
assertNotNull(errorMessage);
|
||||
assertEquals("File cannot be empty", errorMessage.getVisibleText());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFileUpload() throws Exception {
|
||||
try (final WebClient webClient = new WebClient()) {
|
||||
final HtmlPage page = webClient.getPage(ParameterUtils.getBaseUrl() + "/fileupload/doUpload.action");
|
||||
final HtmlForm form = page.getFormByName("doUpload");
|
||||
HtmlInput captionInput = form.getInputByName("caption");
|
||||
HtmlFileInput uploadInput = form.getInputByName("upload");
|
||||
captionInput.type("some caption");
|
||||
File tempFile = File.createTempFile("testEmptyFile", ".tmp");
|
||||
Files.write(Paths.get(tempFile.toURI()), "some content".getBytes());
|
||||
tempFile.deleteOnExit();
|
||||
uploadInput.setValueAttribute(tempFile.getAbsolutePath());
|
||||
final HtmlSubmitInput button = form.getInputByValue("Submit");
|
||||
final HtmlPage resultPage = button.click();
|
||||
|
||||
DomElement inputName = resultPage.getElementById("input-name");
|
||||
assertNotNull(inputName);
|
||||
assertEquals("Input name: upload", inputName.getTextContent().trim());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
@@ -246,6 +246,7 @@ public class JakartaMultiPartRequest extends AbstractMultiPartRequest {
|
||||
UploadedFile uploadedFile = StrutsUploadedFile.Builder.create(storeLocation)
|
||||
.withContentType(fileItem.getContentType())
|
||||
.withOriginalName(fileItem.getName())
|
||||
.withInputName(fileItem.getFieldName())
|
||||
.build();
|
||||
fileList.add(uploadedFile);
|
||||
}
|
||||
|
||||
+13
-2
@@ -113,6 +113,7 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
|
||||
StrutsUploadedFile.Builder.create(fileInfo.getFile())
|
||||
.withContentType(fileInfo.contentType)
|
||||
.withOriginalName(fileInfo.originalName)
|
||||
.withInputName(fileInfo.getInputName())
|
||||
.build()
|
||||
).toArray(UploadedFile[]::new);
|
||||
}
|
||||
@@ -423,7 +424,7 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
|
||||
String fileName = itemStream.getName();
|
||||
String fieldName = itemStream.getFieldName();
|
||||
// create internal structure
|
||||
FileInfo fileInfo = new FileInfo(file, itemStream.getContentType(), fileName);
|
||||
FileInfo fileInfo = new FileInfo(file, itemStream.getContentType(), fileName, itemStream.getFieldName());
|
||||
// append or create new entry.
|
||||
if (!fileInfos.containsKey(fieldName)) {
|
||||
List<FileInfo> infos = new ArrayList<>();
|
||||
@@ -447,6 +448,7 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
|
||||
private final File file;
|
||||
private final String contentType;
|
||||
private final String originalName;
|
||||
private final String inputName;
|
||||
|
||||
/**
|
||||
* Default constructor.
|
||||
@@ -455,10 +457,11 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
|
||||
* @param contentType content type
|
||||
* @param originalName original file name
|
||||
*/
|
||||
public FileInfo(File file, String contentType, String originalName) {
|
||||
public FileInfo(File file, String contentType, String originalName, String inputName) {
|
||||
this.file = file;
|
||||
this.contentType = contentType;
|
||||
this.originalName = originalName;
|
||||
this.inputName = inputName;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -481,6 +484,14 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest {
|
||||
public String getOriginalName() {
|
||||
return originalName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return file input name
|
||||
* @since 6.7.0
|
||||
*/
|
||||
public String getInputName() {
|
||||
return inputName;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ public class StrutsUploadedFile implements UploadedFile {
|
||||
private final File file;
|
||||
private final String contentType;
|
||||
private final String originalName;
|
||||
private final String inputName;
|
||||
|
||||
/**
|
||||
* Use builder instead of constructor
|
||||
@@ -36,12 +37,14 @@ public class StrutsUploadedFile implements UploadedFile {
|
||||
this.file = file;
|
||||
this.contentType = null;
|
||||
this.originalName = null;
|
||||
this.inputName = null;
|
||||
}
|
||||
|
||||
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
|
||||
@@ -84,11 +87,17 @@ public class StrutsUploadedFile implements UploadedFile {
|
||||
return originalName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInputName() {
|
||||
return inputName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "StrutsUploadedFile{" +
|
||||
"contentType='" + contentType + '\'' +
|
||||
", originalName='" + originalName + '\'' +
|
||||
", inputName='" + inputName + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@@ -96,6 +105,7 @@ public class StrutsUploadedFile implements UploadedFile {
|
||||
private final File file;
|
||||
private String contentType;
|
||||
private String originalName;
|
||||
private String inputName;
|
||||
|
||||
private Builder(File file) {
|
||||
this.file = file;
|
||||
@@ -115,8 +125,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,4 +41,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();
|
||||
|
||||
}
|
||||
|
||||
@@ -91,6 +91,11 @@ public class ActionFileUploadInterceptorTest extends StrutsInternalTestCase {
|
||||
public String getContentType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInputName() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
private ActionFileUploadInterceptor interceptor;
|
||||
|
||||
@@ -93,6 +93,11 @@ public class FileUploadInterceptorTest extends StrutsInternalTestCase {
|
||||
public String getContentType() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInputName() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
private FileUploadInterceptor interceptor;
|
||||
|
||||
+1
@@ -71,6 +71,7 @@ public class PellMultiPartRequest extends AbstractMultiPartRequest {
|
||||
return new UploadedFile[]{StrutsUploadedFile.Builder.create(multi.getFile(fieldName))
|
||||
.withContentType(multi.getContentType(fieldName))
|
||||
.withOriginalName(multi.getFileSystemName(fieldName))
|
||||
.withInputName(fieldName)
|
||||
.build()
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user