use of cn.taketoday.core.LinkedMultiValueMap in project today-framework by TAKETODAY.
the class ServletMultipartRequest method parseRequest.
private MultiValueMap<String, MultipartFile> parseRequest(HttpServletRequest request) {
try {
Collection<Part> parts = request.getParts();
LinkedMultiValueMap<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();
files.add(part.getName(), new ServletPartMultipartFile(part, filename));
}
return files;
} catch (IOException e) {
throw new MultipartException("MultipartFile parsing failed.", e);
} catch (ServletException e) {
throw new NotMultipartRequestException("This is not a multipart request", e);
} catch (Throwable ex) {
String msg = ex.getMessage();
if (msg != null && msg.contains("size") && msg.contains("exceed")) {
throw new MaxUploadSizeExceededException(-1, ex);
}
throw new MultipartException("Failed to parse multipart servlet request", ex);
}
}
use of cn.taketoday.core.LinkedMultiValueMap in project today-framework by TAKETODAY.
the class RequestHeaderMapMethodArgumentResolverTests method resolveMultiValueMapArgument.
@Test
public void resolveMultiValueMapArgument() throws Throwable {
String name = "foo";
String value1 = "bar";
String value2 = "baz";
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader(name, value1);
request.addHeader(name, value2);
MultiValueMap<String, String> expected = new LinkedMultiValueMap<>(1);
expected.add(name, value1);
expected.add(name, value2);
ServletRequestContext webRequest = new ServletRequestContext(null, request, new MockHttpServletResponse());
Object result = resolver.resolveArgument(webRequest, paramMultiValueMap);
assertThat(result instanceof MultiValueMap).isTrue();
assertThat(expected).as("Invalid result").isEqualTo(result);
}
use of cn.taketoday.core.LinkedMultiValueMap in project today-framework by TAKETODAY.
the class BodyInsertersTests method fromMultipartDataWithMultipleValues.
// SPR-16350
@Test
public void fromMultipartDataWithMultipleValues() {
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.put("name", Arrays.asList("value1", "value2"));
BodyInserters.FormInserter<Object> inserter = BodyInserters.fromMultipartData(map);
MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("https://example.com"));
Mono<Void> result = inserter.insert(request, this.context);
StepVerifier.create(result).expectComplete().verify();
StepVerifier.create(DataBufferUtils.join(request.getBody())).consumeNextWith(dataBuffer -> {
byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
dataBuffer.read(resultBytes);
DataBufferUtils.release(dataBuffer);
String content = new String(resultBytes, StandardCharsets.UTF_8);
assertThat(content).contains("Content-Disposition: form-data; name=\"name\"\r\n" + "Content-Type: text/plain;charset=UTF-8\r\n" + "Content-Length: 6\r\n" + "\r\n" + "value1");
assertThat(content).contains("Content-Disposition: form-data; name=\"name\"\r\n" + "Content-Type: text/plain;charset=UTF-8\r\n" + "Content-Length: 6\r\n" + "\r\n" + "value2");
}).expectComplete().verify();
}
use of cn.taketoday.core.LinkedMultiValueMap in project today-framework by TAKETODAY.
the class BodyInsertersTests method fromMultipartData.
@Test
public void fromMultipartData() {
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
map.set("name 3", "value 3");
BodyInserters.FormInserter<Object> inserter = BodyInserters.fromMultipartData("name 1", "value 1").withPublisher("name 2", Flux.just("foo", "bar", "baz"), String.class).with(map);
MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("https://example.com"));
Mono<Void> result = inserter.insert(request, this.context);
StepVerifier.create(result).expectComplete().verify();
}
use of cn.taketoday.core.LinkedMultiValueMap in project today-infrastructure by TAKETODAY.
the class ServletMultipartRequest method parseRequest.
private MultiValueMap<String, MultipartFile> parseRequest(HttpServletRequest request) {
try {
Collection<Part> parts = request.getParts();
LinkedMultiValueMap<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 ServletPartMultipartFile(part, filename));
}
}
return files;
} catch (IOException e) {
throw new MultipartException("MultipartFile parsing failed.", e);
} catch (ServletException e) {
throw new NotMultipartRequestException("This is not a multipart request", e);
} catch (Throwable ex) {
String msg = ex.getMessage();
if (msg != null && msg.contains("size") && msg.contains("exceed")) {
throw new MaxUploadSizeExceededException(-1, ex);
}
throw new MultipartException("Failed to parse multipart servlet request", ex);
}
}
Aggregations