Search in sources :

Example 11 with Part

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

the class HtmlUnitRequestBuilderTests method buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNullFileToUploadAsParameter.

// gh-26799
@Test
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNullFileToUploadAsParameter() throws Exception {
    webRequest.setRequestParameters(Collections.singletonList(new KeyDataPair("key", null, null, null, (Charset) null)));
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getParts()).hasSize(1);
    Part part = actualRequest.getPart("key");
    assertSoftly(softly -> {
        softly.assertThat(part).as("part").isNotNull();
        softly.assertThat(part.getName()).as("name").isEqualTo("key");
        softly.assertThat(part.getSize()).as("size").isEqualTo(0);
        try {
            softly.assertThat(part.getInputStream()).as("input stream").isEmpty();
        } catch (IOException ex) {
            softly.fail("failed to get InputStream", ex);
        }
        softly.assertThat(part.getSubmittedFileName()).as("filename").isEqualTo("");
        softly.assertThat(part.getContentType()).as("content-type").isEqualTo("application/octet-stream");
    });
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Part(jakarta.servlet.http.Part) IOException(java.io.IOException) KeyDataPair(com.gargoylesoftware.htmlunit.util.KeyDataPair) Test(org.junit.jupiter.api.Test)

Example 12 with Part

use of jakarta.servlet.http.Part in project tomcat by apache.

the class Request method recycle.

/**
 * Release all object references, and initialize instance variables, in
 * preparation for reuse of this object.
 */
public void recycle() {
    internalDispatcherType = null;
    requestDispatcherPath = null;
    authType = null;
    inputBuffer.recycle();
    usingInputStream = false;
    usingReader = false;
    userPrincipal = null;
    subject = null;
    parametersParsed = false;
    if (parts != null) {
        for (Part part : parts) {
            try {
                part.delete();
            } catch (IOException ignored) {
            // ApplicationPart.delete() never throws an IOEx
            }
        }
        parts = null;
    }
    partsParseException = null;
    locales.clear();
    localesParsed = false;
    secure = false;
    remoteAddr = null;
    peerAddr = null;
    remoteHost = null;
    remotePort = -1;
    localPort = -1;
    localAddr = null;
    localName = null;
    attributes.clear();
    sslAttributesParsed = false;
    notes.clear();
    recycleSessionInfo();
    recycleCookieInfo(false);
    if (getDiscardFacades()) {
        parameterMap = new ParameterMap<>();
    } else {
        parameterMap.setLocked(false);
        parameterMap.clear();
    }
    mappingData.recycle();
    applicationMapping.recycle();
    applicationRequest = null;
    if (getDiscardFacades()) {
        if (facade != null) {
            facade.clear();
            facade = null;
        }
        if (inputStream != null) {
            inputStream.clear();
            inputStream = null;
        }
        if (reader != null) {
            reader.clear();
            reader = null;
        }
    }
    asyncSupported = null;
    if (asyncContext != null) {
        asyncContext.recycle();
    }
    asyncContext = null;
}
Also used : ApplicationPart(org.apache.catalina.core.ApplicationPart) Part(jakarta.servlet.http.Part) IOException(java.io.IOException)

Example 13 with Part

use of jakarta.servlet.http.Part in project tomcat by apache.

the class HTMLManagerServlet method upload.

protected String upload(HttpServletRequest request, StringManager smClient) {
    String message = "";
    try {
        while (true) {
            Part warPart = request.getPart("deployWar");
            if (warPart == null) {
                message = smClient.getString("htmlManagerServlet.deployUploadNoFile");
                break;
            }
            String filename = warPart.getSubmittedFileName();
            if (!filename.toLowerCase(Locale.ENGLISH).endsWith(".war")) {
                message = smClient.getString("htmlManagerServlet.deployUploadNotWar", filename);
                break;
            }
            // Get the filename if uploaded name includes a path
            if (filename.lastIndexOf('\\') >= 0) {
                filename = filename.substring(filename.lastIndexOf('\\') + 1);
            }
            if (filename.lastIndexOf('/') >= 0) {
                filename = filename.substring(filename.lastIndexOf('/') + 1);
            }
            // Identify the appBase of the owning Host of this Context
            // (if any)
            File file = new File(host.getAppBaseFile(), filename);
            if (file.exists()) {
                message = smClient.getString("htmlManagerServlet.deployUploadWarExists", filename);
                break;
            }
            ContextName cn = new ContextName(filename, true);
            String name = cn.getName();
            if ((host.findChild(name) != null) && !isDeployed(name)) {
                message = smClient.getString("htmlManagerServlet.deployUploadInServerXml", filename);
                break;
            }
            if (tryAddServiced(name)) {
                try {
                    warPart.write(file.getAbsolutePath());
                } finally {
                    removeServiced(name);
                }
                // Perform new deployment
                check(name);
            } else {
                message = smClient.getString("managerServlet.inService", name);
            }
            break;
        }
    } catch (Exception e) {
        message = smClient.getString("htmlManagerServlet.deployUploadFail", e.getMessage());
        log(message, e);
    }
    return message;
}
Also used : Part(jakarta.servlet.http.Part) File(java.io.File) ServletException(jakarta.servlet.ServletException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ContextName(org.apache.catalina.util.ContextName)

Example 14 with Part

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

the class HtmlUnitRequestBuilderTests method buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileToUploadAsParameter.

// gh-24926
@Test
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileToUploadAsParameter() throws Exception {
    webRequest.setRequestParameters(Collections.singletonList(new KeyDataPair("key", new ClassPathResource("org/springframework/test/web/htmlunit/test.txt").getFile(), "test.txt", MimeType.TEXT_PLAIN, StandardCharsets.UTF_8)));
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getParts()).hasSize(1);
    Part part = actualRequest.getPart("key");
    assertThat(part).isNotNull();
    assertThat(part.getName()).isEqualTo("key");
    assertThat(IOUtils.toString(part.getInputStream(), StandardCharsets.UTF_8)).isEqualTo("test file");
    assertThat(part.getSubmittedFileName()).isEqualTo("test.txt");
    assertThat(part.getContentType()).isEqualTo(MimeType.TEXT_PLAIN);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Part(jakarta.servlet.http.Part) KeyDataPair(com.gargoylesoftware.htmlunit.util.KeyDataPair) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test)

Example 15 with Part

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

the class RequestParamMapMethodArgumentResolverTests method resolveMultiValueMapOfPart.

@Test
@SuppressWarnings("unchecked")
public void resolveMultiValueMapOfPart() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setContentType("multipart/form-data");
    Part expected1 = new MockPart("mfilelist", "Hello World 1".getBytes());
    Part expected2 = new MockPart("mfilelist", "Hello World 2".getBytes());
    Part expected3 = new MockPart("other", "Hello World 3".getBytes());
    request.addPart(expected1);
    request.addPart(expected2);
    request.addPart(expected3);
    webRequest = new ServletWebRequest(request);
    MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(MultiValueMap.class, String.class, Part.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    boolean condition = result instanceof MultiValueMap;
    assertThat(condition).isTrue();
    MultiValueMap<String, Part> resultMap = (MultiValueMap<String, Part>) result;
    assertThat(resultMap.size()).isEqualTo(2);
    assertThat(resultMap.get("mfilelist").size()).isEqualTo(2);
    assertThat(resultMap.get("mfilelist").get(0)).isEqualTo(expected1);
    assertThat(resultMap.get("mfilelist").get(1)).isEqualTo(expected2);
    assertThat(resultMap.get("other").size()).isEqualTo(1);
    assertThat(resultMap.get("other").get(0)).isEqualTo(expected3);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockPart(org.springframework.web.testfixture.servlet.MockPart) Part(jakarta.servlet.http.Part) MockPart(org.springframework.web.testfixture.servlet.MockPart) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.jupiter.api.Test)

Aggregations

Part (jakarta.servlet.http.Part)19 Test (org.junit.jupiter.api.Test)13 MockPart (org.springframework.web.testfixture.servlet.MockPart)10 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)9 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)8 MethodParameter (org.springframework.core.MethodParameter)7 RequestPart (org.springframework.web.bind.annotation.RequestPart)7 MvcAnnotationPredicates.requestPart (org.springframework.web.testfixture.method.MvcAnnotationPredicates.requestPart)5 IOException (java.io.IOException)4 RequestParam (org.springframework.web.bind.annotation.RequestParam)4 MultipartFile (org.springframework.web.multipart.MultipartFile)4 KeyDataPair (com.gargoylesoftware.htmlunit.util.KeyDataPair)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 MultiValueMap (org.springframework.util.MultiValueMap)2 ServletException (jakarta.servlet.ServletException)1