Search in sources :

Example 11 with HttpInputMessage

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

the class RequestResponseBodyAdviceChainTests method requestBodyAdvice.

@SuppressWarnings("unchecked")
@Test
public void requestBodyAdvice() throws IOException {
    RequestBodyAdvice requestAdvice = Mockito.mock(RequestBodyAdvice.class);
    ResponseBodyAdvice<String> responseAdvice = Mockito.mock(ResponseBodyAdvice.class);
    List<Object> advice = Arrays.asList(requestAdvice, responseAdvice);
    RequestResponseBodyAdviceChain chain = new RequestResponseBodyAdviceChain(advice);
    HttpInputMessage wrapped = new ServletServerHttpRequest(new MockHttpServletRequest());
    given(requestAdvice.supports(this.paramType, String.class, this.converterType)).willReturn(true);
    given(requestAdvice.beforeBodyRead(eq(this.request), eq(this.paramType), eq(String.class), eq(this.converterType))).willReturn(wrapped);
    assertSame(wrapped, chain.beforeBodyRead(this.request, this.paramType, String.class, this.converterType));
    String modified = "body++";
    given(requestAdvice.afterBodyRead(eq(this.body), eq(this.request), eq(this.paramType), eq(String.class), eq(this.converterType))).willReturn(modified);
    assertEquals(modified, chain.afterBodyRead(this.body, this.request, this.paramType, String.class, this.converterType));
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 12 with HttpInputMessage

use of org.springframework.http.HttpInputMessage in project spring-security-oauth by spring-projects.

the class OAuth2ErrorHandlerTests method testHandleMessageConversionExceptions.

@Test
public void testHandleMessageConversionExceptions() throws Exception {
    HttpMessageConverter<?> extractor = new HttpMessageConverter() {

        @Override
        public boolean canRead(Class clazz, MediaType mediaType) {
            return true;
        }

        @Override
        public boolean canWrite(Class clazz, MediaType mediaType) {
            return false;
        }

        @Override
        public List<MediaType> getSupportedMediaTypes() {
            return null;
        }

        @Override
        public Object read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
            throw new HttpMessageConversionException("error");
        }

        @Override
        public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        }
    };
    ArrayList<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
    messageConverters.add(extractor);
    handler.setMessageConverters(messageConverters);
    HttpHeaders headers = new HttpHeaders();
    final String appSpecificBodyContent = "This user is not authorized";
    InputStream appSpecificErrorBody = new ByteArrayInputStream(appSpecificBodyContent.getBytes("UTF-8"));
    ClientHttpResponse response = new TestClientHttpResponse(headers, 401, appSpecificErrorBody);
    expected.expect(HttpClientErrorException.class);
    handler.handleError(response);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpHeaders(org.springframework.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpOutputMessage(org.springframework.http.HttpOutputMessage) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) MediaType(org.springframework.http.MediaType) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) Test(org.junit.Test)

Example 13 with HttpInputMessage

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

the class HttpPutFormContentFilter method doFilterInternal.

@Override
protected void doFilterInternal(final HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    if (("PUT".equals(request.getMethod()) || "PATCH".equals(request.getMethod())) && isFormContentType(request)) {
        HttpInputMessage inputMessage = new ServletServerHttpRequest(request) {

            @Override
            public InputStream getBody() throws IOException {
                return request.getInputStream();
            }
        };
        MultiValueMap<String, String> formParameters = formConverter.read(null, inputMessage);
        HttpServletRequest wrapper = new HttpPutFormContentRequestWrapper(request, formParameters);
        filterChain.doFilter(wrapper, response);
    } else {
        filterChain.doFilter(request, response);
    }
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest)

Example 14 with HttpInputMessage

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

the class ContentRequestMatchers method formData.

/**
	 * Parse the body as form data and compare to the given {@code MultiValueMap}.
	 * @since 4.3
	 */
public RequestMatcher formData(final MultiValueMap<String, String> expectedContent) {
    return new RequestMatcher() {

        @Override
        public void match(final ClientHttpRequest request) throws IOException, AssertionError {
            HttpInputMessage inputMessage = new HttpInputMessage() {

                @Override
                public InputStream getBody() throws IOException {
                    MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
                    return new ByteArrayInputStream(mockRequest.getBodyAsBytes());
                }

                @Override
                public HttpHeaders getHeaders() {
                    return request.getHeaders();
                }
            };
            FormHttpMessageConverter converter = new FormHttpMessageConverter();
            assertEquals("Request content", expectedContent, converter.read(null, inputMessage));
        }
    };
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) FormHttpMessageConverter(org.springframework.http.converter.FormHttpMessageConverter) RequestMatcher(org.springframework.test.web.client.RequestMatcher) ByteArrayInputStream(java.io.ByteArrayInputStream) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest)

Example 15 with HttpInputMessage

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

the class MockHttpServletRequestBuilder method parseFormData.

private MultiValueMap<String, String> parseFormData(final MediaType mediaType) {
    HttpInputMessage message = new HttpInputMessage() {

        @Override
        public InputStream getBody() throws IOException {
            return new ByteArrayInputStream(content);
        }

        @Override
        public HttpHeaders getHeaders() {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(mediaType);
            return headers;
        }
    };
    try {
        return new FormHttpMessageConverter().read(null, message);
    } catch (IOException ex) {
        throw new IllegalStateException("Failed to parse form data in request body", ex);
    }
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) FormHttpMessageConverter(org.springframework.http.converter.FormHttpMessageConverter) HttpHeaders(org.springframework.http.HttpHeaders) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException)

Aggregations

HttpInputMessage (org.springframework.http.HttpInputMessage)17 ByteArrayInputStream (java.io.ByteArrayInputStream)9 HttpHeaders (org.springframework.http.HttpHeaders)8 HttpOutputMessage (org.springframework.http.HttpOutputMessage)8 Test (org.junit.Test)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)5 FastJsonConfig (com.alibaba.fastjson.support.config.FastJsonConfig)4 Method (java.lang.reflect.Method)4 MediaType (org.springframework.http.MediaType)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 FastJsonHttpMessageConverter (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)2 FastJsonpHttpMessageConverter4 (com.alibaba.fastjson.support.spring.FastJsonpHttpMessageConverter4)2 IOException (java.io.IOException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 FormHttpMessageConverter (org.springframework.http.converter.FormHttpMessageConverter)2 FastJsonHttpMessageConverter4 (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4)1 MappingFastJsonValue (com.alibaba.fastjson.support.spring.MappingFastJsonValue)1 Message (com.google.protobuf.Message)1 InputStream (java.io.InputStream)1