Search in sources :

Example 21 with Part

use of javax.servlet.http.Part 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 disposition = part.getHeader(CONTENT_DISPOSITION);
            String filename = extractFilename(disposition);
            if (filename == null) {
                filename = extractFilenameWithCharset(disposition);
            }
            if (filename != null) {
                files.add(part.getName(), new StandardMultipartFile(part, filename));
            } else {
                this.multipartParameterNames.add(part.getName());
            }
        }
        setMultipartFiles(files);
    } catch (Throwable ex) {
        handleParseFailure(ex);
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Part(javax.servlet.http.Part)

Example 22 with Part

use of javax.servlet.http.Part in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolvePartNotAnnot.

@Test
public void resolvePartNotAnnot() throws Exception {
    MockPart expected = new MockPart("part", "Hello World".getBytes());
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof Part);
    assertEquals("Invalid result", expected, result);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.method.MvcAnnotationPredicates.requestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) MockPart(org.springframework.mock.web.test.MockPart) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 23 with Part

use of javax.servlet.http.Part in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method resolvePart.

@Test
public void resolvePart() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockPart expected = new MockPart("pfile", "Hello World".getBytes());
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.addPart(expected);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof Part);
    assertEquals("Invalid result", expected, result);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.method.MvcAnnotationPredicates.requestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) MockPart(org.springframework.mock.web.test.MockPart) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Example 24 with Part

use of javax.servlet.http.Part in project spring-framework by spring-projects.

the class RequestParamMethodArgumentResolverTests method supportsParameter.

@Test
public void supportsParameter() {
    resolver = new RequestParamMethodArgumentResolver(null, true);
    MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().name("name")).arg(Map.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(List.class, Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Part[].class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().noName()).arg(Map.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent().arg(MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(List.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(Part.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestPart()).arg(MultipartFile.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam()).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annot(requestParam().notRequired()).arg(String.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, Integer.class);
    assertTrue(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestParam.class).arg(Optional.class, MultipartFile.class);
    assertTrue(resolver.supportsParameter(param));
    resolver = new RequestParamMethodArgumentResolver(null, false);
    param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class);
    assertFalse(resolver.supportsParameter(param));
    param = this.testMethod.annotPresent(RequestPart.class).arg(MultipartFile.class);
    assertFalse(resolver.supportsParameter(param));
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MultipartFile(org.springframework.web.multipart.MultipartFile) Optional(java.util.Optional) RequestPart(org.springframework.web.bind.annotation.RequestPart) MvcAnnotationPredicates.requestPart(org.springframework.web.method.MvcAnnotationPredicates.requestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) List(java.util.List) MethodParameter(org.springframework.core.MethodParameter) Map(java.util.Map) Test(org.junit.Test)

Example 25 with Part

use of javax.servlet.http.Part in project spring-framework by spring-projects.

the class RequestPartMethodArgumentResolverTests method resolvePartArgument.

@Test
public void resolvePartArgument() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    MockPart expected = new MockPart("part", "Hello World".getBytes());
    request.addPart(expected);
    request.addPart(new MockPart("otherPart", "Hello World".getBytes()));
    webRequest = new ServletWebRequest(request);
    Object result = resolver.resolveArgument(paramPart, null, webRequest, null);
    assertTrue(result instanceof Part);
    assertEquals("Invalid result", expected, result);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RequestPart(org.springframework.web.bind.annotation.RequestPart) MockPart(org.springframework.mock.web.test.MockPart) Part(javax.servlet.http.Part) MockPart(org.springframework.mock.web.test.MockPart) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Test(org.junit.Test)

Aggregations

Part (javax.servlet.http.Part)68 Test (org.junit.Test)42 ByteArrayInputStream (java.io.ByteArrayInputStream)27 MultipartConfigElement (javax.servlet.MultipartConfigElement)27 MultiPart (org.eclipse.jetty.util.MultiPartInputStreamParser.MultiPart)24 IOException (java.io.IOException)19 InputStream (java.io.InputStream)12 ArrayList (java.util.ArrayList)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 ServletException (javax.servlet.ServletException)10 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 AbstractHttpClientServerTest (org.eclipse.jetty.client.AbstractHttpClientServerTest)7 File (java.io.File)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)5 MockPart (org.springframework.mock.web.test.MockPart)5 RequestPart (org.springframework.web.bind.annotation.RequestPart)5 PrintWriter (java.io.PrintWriter)4