use of org.springframework.web.multipart.MultipartFile in project qpp-conversion-tool by CMSgov.
the class QrdaControllerV1Test method testExceptionalInputStreamSupplier.
@Test
void testExceptionalInputStreamSupplier() {
Assertions.assertThrows(UncheckedIOException.class, () -> {
MultipartFile spy = spy(multipartFile);
try {
doThrow(new IOException()).when(spy).getInputStream();
InputStream in = objectUnderTest.inputStream(spy);
in.close();
} catch (IOException ex) {
Assertions.fail("wrong exception");
}
});
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class RequestPartServletServerHttpRequest method getBody.
@Override
public InputStream getBody() throws IOException {
// Prefer Servlet Part resolution to cover file as well as parameter streams
boolean servletParts = (this.multipartRequest instanceof StandardMultipartHttpServletRequest);
if (servletParts) {
Part part = retrieveServletPart();
if (part != null) {
return part.getInputStream();
}
}
// Spring-style distinction between MultipartFile and String parameters
MultipartFile file = this.multipartRequest.getFile(this.requestPartName);
if (file != null) {
return file.getInputStream();
}
String paramValue = this.multipartRequest.getParameter(this.requestPartName);
if (paramValue != null) {
return new ByteArrayInputStream(paramValue.getBytes(determineCharset()));
}
// Fallback: Servlet Part resolution even if not indicated
if (!servletParts) {
Part part = retrieveServletPart();
if (part != null) {
return part.getInputStream();
}
}
throw new IllegalStateException("No body available for request part '" + this.requestPartName + "'");
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class MultipartResolutionDelegate method resolveMultipartArgument.
@Nullable
public static Object resolveMultipartArgument(String name, MethodParameter parameter, HttpServletRequest request) throws Exception {
MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
boolean isMultipart = (multipartRequest != null || isMultipartContent(request));
if (MultipartFile.class == parameter.getNestedParameterType()) {
if (!isMultipart) {
return null;
}
if (multipartRequest == null) {
multipartRequest = new StandardMultipartHttpServletRequest(request);
}
return multipartRequest.getFile(name);
} else if (isMultipartFileCollection(parameter)) {
if (!isMultipart) {
return null;
}
if (multipartRequest == null) {
multipartRequest = new StandardMultipartHttpServletRequest(request);
}
List<MultipartFile> files = multipartRequest.getFiles(name);
return (!files.isEmpty() ? files : null);
} else if (isMultipartFileArray(parameter)) {
if (!isMultipart) {
return null;
}
if (multipartRequest == null) {
multipartRequest = new StandardMultipartHttpServletRequest(request);
}
List<MultipartFile> files = multipartRequest.getFiles(name);
return (!files.isEmpty() ? files.toArray(new MultipartFile[0]) : null);
} else if (Part.class == parameter.getNestedParameterType()) {
if (!isMultipart) {
return null;
}
return request.getPart(name);
} else if (isPartCollection(parameter)) {
if (!isMultipart) {
return null;
}
List<Part> parts = resolvePartList(request, name);
return (!parts.isEmpty() ? parts : null);
} else if (isPartArray(parameter)) {
if (!isMultipart) {
return null;
}
List<Part> parts = resolvePartList(request, name);
return (!parts.isEmpty() ? parts.toArray(new Part[0]) : null);
} else {
return UNRESOLVABLE;
}
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class StandardMultipartHttpServletRequest method parseRequest.
private void parseRequest(HttpServletRequest request) {
try {
Collection<Part> parts = request.getParts();
this.multipartParameterNames = new LinkedHashSet<>(parts.size());
MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<>(parts.size());
for (Part part : parts) {
String headerValue = part.getHeader(HttpHeaders.CONTENT_DISPOSITION);
ContentDisposition disposition = ContentDisposition.parse(headerValue);
String filename = disposition.getFilename();
if (filename != null) {
if (filename.startsWith("=?") && filename.endsWith("?=")) {
filename = MimeDelegate.decode(filename);
}
files.add(part.getName(), new StandardMultipartFile(part, filename));
} else {
this.multipartParameterNames.add(part.getName());
}
}
setMultipartFiles(files);
} catch (Throwable ex) {
handleParseFailure(ex);
}
}
use of org.springframework.web.multipart.MultipartFile in project spring-framework by spring-projects.
the class RequestPartMethodArgumentResolverTests method resolveOptionalMultipartFileList.
@Test
public void resolveOptionalMultipartFileList() throws Exception {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
MultipartFile expected = new MockMultipartFile("requestPart", "Hello World".getBytes());
request.addFile(expected);
request.addFile(new MockMultipartFile("otherPart", "", "text/plain", "Hello World".getBytes()));
webRequest = new ServletWebRequest(request);
Object actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
boolean condition1 = actualValue instanceof Optional;
assertThat(condition1).isTrue();
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected));
actualValue = resolver.resolveArgument(optionalMultipartFileList, null, webRequest, null);
assertThat(actualValue instanceof Optional).isTrue();
assertThat(((Optional<?>) actualValue).get()).as("Invalid result").isEqualTo(Collections.singletonList(expected));
}
Aggregations