Skip to content

Commit

Permalink
Merge pull request #1040 from apache/fix/WW-5461-input-name
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 f388f6a + 735d1bc commit b04ad6b
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 8 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
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 {

Expand All @@ -45,9 +49,31 @@ public void testEmptyFile() throws Exception {
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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ public UploadedFile[] getFile(String fieldName) {
UploadedFile uploadedFile = StrutsUploadedFile.Builder.create(storeLocation)
.withContentType(fileItem.getContentType())
.withOriginalName(fileItem.getName())
.withInputName(fileItem.getFieldName())
.build();
fileList.add(uploadedFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public UploadedFile[] getFile(String fieldName) {
StrutsUploadedFile.Builder.create(fileInfo.getFile())
.withContentType(fileInfo.contentType)
.withOriginalName(fileInfo.originalName)
.withInputName(fileInfo.getInputName())
.build()
).toArray(UploadedFile[]::new);
}
Expand Down Expand Up @@ -423,7 +424,7 @@ protected void createFileInfoFromItemStream(FileItemStream itemStream, File file
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<>();
Expand All @@ -447,6 +448,7 @@ public static class FileInfo implements Serializable {
private final File file;
private final String contentType;
private final String originalName;
private final String inputName;

/**
* Default constructor.
Expand All @@ -455,10 +457,11 @@ public static class FileInfo implements Serializable {
* @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;
}

/**
Expand All @@ -481,6 +484,14 @@ public String getContentType() {
public String getOriginalName() {
return originalName;
}

/**
* @return file input name
* @since 6.7.0
*/
public String getInputName() {
return inputName;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,12 +37,14 @@ public StrutsUploadedFile(File file) {
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
Expand Down Expand Up @@ -84,18 +87,25 @@ public String getOriginalName() {
return originalName;
}

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

@Override
public String toString() {
return "StrutsUploadedFile{" +
"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 @@ -115,8 +125,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 @@ -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();

}
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 ActionFileUploadInterceptor interceptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public String getOriginalName() {
public String getContentType() {
return null;
}

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

private FileUploadInterceptor interceptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public UploadedFile[] getFile(String fieldName) {
return new UploadedFile[]{StrutsUploadedFile.Builder.create(multi.getFile(fieldName))
.withContentType(multi.getContentType(fieldName))
.withOriginalName(multi.getFileSystemName(fieldName))
.withInputName(fieldName)
.build()
};
}
Expand Down

0 comments on commit b04ad6b

Please sign in to comment.