Search in sources :

Example 6 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project spring-framework by spring-projects.

the class HttpEntityMethodProcessorTests method handleReturnValueCharSequence.

// SPR-13423
@Test
public void handleReturnValueCharSequence() throws Exception {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(new StringHttpMessageConverter());
    Method method = getClass().getDeclaredMethod("handle");
    MethodParameter returnType = new MethodParameter(method, -1);
    ResponseEntity<StringBuilder> returnValue = ResponseEntity.ok(new StringBuilder("Foo"));
    HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
    processor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
    assertEquals("text/plain;charset=ISO-8859-1", servletResponse.getHeader("Content-Type"));
    assertEquals("Foo", servletResponse.getContentAsString());
}
Also used : ArrayList(java.util.ArrayList) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.Test)

Example 7 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorTests method handleReturnValueString.

@Test
public void handleReturnValueString() throws Exception {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(new StringHttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    processor.handleReturnValue("Foo", returnTypeString, container, request);
    assertEquals("text/plain;charset=ISO-8859-1", servletResponse.getHeader("Content-Type"));
    assertEquals("Foo", servletResponse.getContentAsString());
}
Also used : ArrayList(java.util.ArrayList) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.Test)

Example 8 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorTests method handleReturnValueCharSequence.

// SPR-13423
@Test
public void handleReturnValueCharSequence() throws Exception {
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new ByteArrayHttpMessageConverter());
    converters.add(new StringHttpMessageConverter());
    Method method = ResponseBodyController.class.getMethod("handleWithCharSequence");
    MethodParameter returnType = new MethodParameter(method, -1);
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    processor.handleReturnValue(new StringBuilder("Foo"), returnType, container, request);
    assertEquals("text/plain;charset=ISO-8859-1", servletResponse.getHeader("Content-Type"));
    assertEquals("Foo", servletResponse.getContentAsString());
}
Also used : ArrayList(java.util.ArrayList) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.Test)

Example 9 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project spring-framework by spring-projects.

the class RequestPartIntegrationTests method standardMultipartResolverWithEncodedFileName.

// SPR-13319
@Test
public void standardMultipartResolverWithEncodedFileName() throws Exception {
    byte[] boundary = MimeTypeUtils.generateMultipartBoundary();
    String boundaryText = new String(boundary, "US-ASCII");
    Map<String, String> params = Collections.singletonMap("boundary", boundaryText);
    String content = "--" + boundaryText + "\n" + "Content-Disposition: form-data; name=\"file\"; filename*=\"utf-8''%C3%A9l%C3%A8ve.txt\"\n" + "Content-Type: text/plain\n" + "Content-Length: 7\n" + "\n" + "content\n" + "--" + boundaryText + "--";
    RequestEntity<byte[]> requestEntity = RequestEntity.post(new URI(baseUrl + "/standard-resolver/spr13319")).contentType(new MediaType(MediaType.MULTIPART_FORM_DATA, params)).body(content.getBytes(StandardCharsets.US_ASCII));
    ByteArrayHttpMessageConverter converter = new ByteArrayHttpMessageConverter();
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.MULTIPART_FORM_DATA));
    this.restTemplate.setMessageConverters(Collections.singletonList(converter));
    ResponseEntity<Void> responseEntity = restTemplate.exchange(requestEntity, Void.class);
    assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
}
Also used : MediaType(org.springframework.http.MediaType) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) URI(java.net.URI) Test(org.junit.Test)

Example 10 with ByteArrayHttpMessageConverter

use of org.springframework.http.converter.ByteArrayHttpMessageConverter in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method unsupportedRequestBody.

@Test
public void unsupportedRequestBody() throws ServletException, IOException {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
            adapterDef.getPropertyValues().add("messageConverters", new ByteArrayHttpMessageConverter());
            wac.registerBeanDefinition("handlerAdapter", adapterDef);
        }
    }, RequestResponseBodyController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
    String requestBody = "Hello World";
    request.setContent(requestBody.getBytes("UTF-8"));
    request.addHeader("Content-Type", "application/pdf");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(415, response.getStatus());
    assertNotNull("No Accept response header set", response.getHeader("Accept"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

ByteArrayHttpMessageConverter (org.springframework.http.converter.ByteArrayHttpMessageConverter)11 MappingJackson2HttpMessageConverter (org.springframework.http.converter.json.MappingJackson2HttpMessageConverter)7 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)6 ResourceHttpMessageConverter (org.springframework.http.converter.ResourceHttpMessageConverter)6 StringHttpMessageConverter (org.springframework.http.converter.StringHttpMessageConverter)6 AllEncompassingFormHttpMessageConverter (org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter)5 MappingJackson2XmlHttpMessageConverter (org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter)4 RestTemplate (org.springframework.web.client.RestTemplate)4 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 Method (java.lang.reflect.Method)2 MethodParameter (org.springframework.core.MethodParameter)2 HttpEntity (org.springframework.http.HttpEntity)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HandlerMethod (org.springframework.web.method.HandlerMethod)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 URI (java.net.URI)1