use of org.apache.struts2.dispatcher.multipart.MultiPartRequest in project struts by apache.
the class StrutsSpringPortletMockObjectsTest method testMockMultipartActionRequest.
public void testMockMultipartActionRequest() {
final String TEST_FILENAME = "TEST_TEMPFILE";
final String TEST_FILENAME2 = "TEST_TEMPFILE2";
final String TEST_CONTENT_TYPE = "text/html";
final String TEST_FAKECONTENT = "Some fake content for the test.";
final byte[] TEST_FAKECONTENT_ASBYTES = TEST_FAKECONTENT.getBytes(StandardCharsets.UTF_8);
// Call each constructor in sequence, do some basic checks
MockMultipartActionRequest mockMultipartActionRequest = new MockMultipartActionRequest();
assertFalse("Initial multipartrequest has file names ?", mockMultipartActionRequest.getFileNames().hasNext());
assertTrue("Initial multipartrequest has nonempty map ?", mockMultipartActionRequest.getFileMap().isEmpty());
assertTrue("Initial multipartrequest has nonempty multivalue map ?", mockMultipartActionRequest.getMultiFileMap().isEmpty());
final MultipartFile multipartFile = new TestMockMultipartFile(TEST_FILENAME, TEST_FILENAME, TEST_CONTENT_TYPE, TEST_FAKECONTENT_ASBYTES);
final MultipartFile multipartFile2 = new TestMockMultipartFile(TEST_FILENAME2, TEST_FILENAME2, TEST_CONTENT_TYPE, TEST_FAKECONTENT_ASBYTES);
mockMultipartActionRequest.addFile(multipartFile);
assertTrue("Multipartrequest does not have file names ?", mockMultipartActionRequest.getFileNames().hasNext());
assertFalse("Multipartrequest does not have nonempty map ?", mockMultipartActionRequest.getFileMap().isEmpty());
assertFalse("Multipartrequest does not have nonempty multivalue map ?", mockMultipartActionRequest.getMultiFileMap().isEmpty());
mockMultipartActionRequest.addFile(multipartFile2);
assertTrue("Multipartrequest does not have file names ?", mockMultipartActionRequest.getFileNames().hasNext());
assertFalse("Multipartrequest does not have nonempty map ?", mockMultipartActionRequest.getFileMap().isEmpty());
assertFalse("Multipartrequest does not have nonempty multivalue map ?", mockMultipartActionRequest.getMultiFileMap().isEmpty());
assertEquals("Retrieved file 1 not as expected ?", multipartFile, mockMultipartActionRequest.getFile(TEST_FILENAME));
assertEquals("Retrieved file 1 content-type not as expected ?", TEST_CONTENT_TYPE, mockMultipartActionRequest.getMultipartContentType(TEST_FILENAME));
assertEquals("Retrieved file 2 not as expected ?", multipartFile2, mockMultipartActionRequest.getFile(TEST_FILENAME2));
assertEquals("Retrieved file 2 content-type not as expected ?", TEST_CONTENT_TYPE, mockMultipartActionRequest.getMultipartContentType(TEST_FILENAME2));
final Map<String, MultipartFile> fileMap = mockMultipartActionRequest.getFileMap();
assertNotNull("File map is null ?", fileMap);
assertTrue("File 1 name not in map ?", fileMap.containsKey(TEST_FILENAME));
assertTrue("File 2 name not in map ?", fileMap.containsKey(TEST_FILENAME2));
assertTrue("File 1 not in map ?", fileMap.containsValue(multipartFile));
assertTrue("File 2 not in map ?", fileMap.containsValue(multipartFile2));
MultiValueMap<String, MultipartFile> fileMultiValueMap = mockMultipartActionRequest.getMultiFileMap();
assertNotNull("MultiValue file map is null ?", fileMultiValueMap);
MultipartFile retrievedFile = fileMultiValueMap.getFirst(TEST_FILENAME);
assertNotNull("File 1 1st value is null ?", retrievedFile);
assertEquals("File 1 not in multivalue map ?", multipartFile, retrievedFile);
retrievedFile = fileMultiValueMap.getFirst(TEST_FILENAME2);
assertNotNull("File 2 1st value is null ?", retrievedFile);
assertEquals("File 2 not in multivalue map ?", multipartFile2, retrievedFile);
}
use of org.apache.struts2.dispatcher.multipart.MultiPartRequest in project struts by apache.
the class Dispatcher method wrapRequest.
/**
* <p>
* Wrap and return the given request or return the original request object.
* </p>
*
* <p>
* This method transparently handles multipart data as a wrapped class around the given request.
* Override this method to handle multipart requests in a special way or to handle other types of requests.
* Note, {@link org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper} is
* flexible - look first to that object before overriding this method to handle multipart data.
* </p>
*
* @param request the HttpServletRequest object.
* @return a wrapped request or original request.
* @throws java.io.IOException on any error.
* @see org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper
* @since 2.3.17
*/
public HttpServletRequest wrapRequest(HttpServletRequest request) throws IOException {
// don't wrap more than once
if (request instanceof StrutsRequestWrapper) {
return request;
}
if (isMultipartSupportEnabled(request) && isMultipartRequest(request)) {
MultiPartRequest multiPartRequest = getMultiPartRequest();
LocaleProviderFactory localeProviderFactory = getContainer().getInstance(LocaleProviderFactory.class);
request = new MultiPartRequestWrapper(multiPartRequest, request, getSaveDir(), localeProviderFactory.createLocaleProvider(), disableRequestAttributeValueStackLookup);
} else {
request = new StrutsRequestWrapper(request, disableRequestAttributeValueStackLookup);
}
return request;
}
use of org.apache.struts2.dispatcher.multipart.MultiPartRequest in project struts by apache.
the class Dispatcher method getMultiPartRequest.
/**
* On each request it must return a new instance as implementation could be not thread safe
* and thus ensure of resource clean up
*
* @return a multi part request object
*/
protected MultiPartRequest getMultiPartRequest() {
MultiPartRequest mpr = null;
// check for alternate implementations of MultiPartRequest
Set<String> multiNames = getContainer().getInstanceNames(MultiPartRequest.class);
for (String multiName : multiNames) {
if (multiName.equals(multipartHandlerName)) {
mpr = getContainer().getInstance(MultiPartRequest.class, multiName);
}
}
if (mpr == null) {
mpr = getContainer().getInstance(MultiPartRequest.class);
}
return mpr;
}
Aggregations