Search in sources :

Example 6 with LinkedMultiValueMap

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);
    }
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) NotMultipartRequestException(cn.taketoday.web.bind.NotMultipartRequestException) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MaxUploadSizeExceededException(cn.taketoday.web.multipart.MaxUploadSizeExceededException) ContentDisposition(cn.taketoday.http.ContentDisposition) Part(jakarta.servlet.http.Part) MultipartException(cn.taketoday.web.bind.MultipartException)

Example 7 with LinkedMultiValueMap

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);
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MultiValueMap(cn.taketoday.core.MultiValueMap) Test(org.junit.jupiter.api.Test)

Example 8 with LinkedMultiValueMap

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();
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) Test(org.junit.jupiter.api.Test)

Example 9 with LinkedMultiValueMap

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();
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) MockClientHttpRequest(cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest) Test(org.junit.jupiter.api.Test)

Example 10 with LinkedMultiValueMap

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);
    }
}
Also used : LinkedMultiValueMap(cn.taketoday.core.LinkedMultiValueMap) IOException(java.io.IOException) ServletException(jakarta.servlet.ServletException) NotMultipartRequestException(cn.taketoday.web.bind.NotMultipartRequestException) ServletPartMultipartFile(cn.taketoday.web.multipart.ServletPartMultipartFile) MultipartFile(cn.taketoday.web.multipart.MultipartFile) MaxUploadSizeExceededException(cn.taketoday.web.multipart.MaxUploadSizeExceededException) ServletPartMultipartFile(cn.taketoday.web.multipart.ServletPartMultipartFile) ContentDisposition(cn.taketoday.http.ContentDisposition) Part(jakarta.servlet.http.Part) MultipartException(cn.taketoday.web.bind.MultipartException)

Aggregations

LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)14 Test (org.junit.jupiter.api.Test)12 MultiValueMap (cn.taketoday.core.MultiValueMap)6 MockClientHttpRequest (cn.taketoday.web.testfixture.http.client.reactive.MockClientHttpRequest)6 ServletException (jakarta.servlet.ServletException)4 IOException (java.io.IOException)4 ByteArrayResource (cn.taketoday.core.io.ByteArrayResource)2 ContentDisposition (cn.taketoday.http.ContentDisposition)2 HttpEntity (cn.taketoday.http.HttpEntity)2 HttpHeaders (cn.taketoday.http.HttpHeaders)2 ResponseCookie (cn.taketoday.http.ResponseCookie)2 ClientHttpRequest (cn.taketoday.http.client.reactive.ClientHttpRequest)2 MultipartException (cn.taketoday.web.bind.MultipartException)2 NotMultipartRequestException (cn.taketoday.web.bind.NotMultipartRequestException)2 RestTemplate (cn.taketoday.web.client.RestTemplate)2 MaxUploadSizeExceededException (cn.taketoday.web.multipart.MaxUploadSizeExceededException)2 MultipartFile (cn.taketoday.web.multipart.MultipartFile)2 MultipartConfigElement (jakarta.servlet.MultipartConfigElement)2 ServletContext (jakarta.servlet.ServletContext)2 Dynamic (jakarta.servlet.ServletRegistration.Dynamic)2