Search in sources :

Example 6 with HttpInputMessage

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

the class HttpTunnelPayloadTests method getWithMissingHeader.

@Test
public void getWithMissingHeader() throws Exception {
    MockHttpServletRequest servletRequest = new MockHttpServletRequest();
    servletRequest.setContent("hello".getBytes());
    HttpInputMessage request = new ServletServerHttpRequest(servletRequest);
    this.thrown.expect(IllegalStateException.class);
    this.thrown.expectMessage("Missing sequence header");
    HttpTunnelPayload.get(request);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.Test)

Example 7 with HttpInputMessage

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

the class DefaultOAuth2ExceptionRenderer method handleHttpEntityResponse.

public void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest) throws Exception {
    if (responseEntity == null) {
        return;
    }
    HttpInputMessage inputMessage = createHttpInputMessage(webRequest);
    HttpOutputMessage outputMessage = createHttpOutputMessage(webRequest);
    if (responseEntity instanceof ResponseEntity && outputMessage instanceof ServerHttpResponse) {
        ((ServerHttpResponse) outputMessage).setStatusCode(((ResponseEntity<?>) responseEntity).getStatusCode());
    }
    HttpHeaders entityHeaders = responseEntity.getHeaders();
    if (!entityHeaders.isEmpty()) {
        outputMessage.getHeaders().putAll(entityHeaders);
    }
    Object body = responseEntity.getBody();
    if (body != null) {
        writeWithMessageConverters(body, inputMessage, outputMessage);
    } else {
        // flush headers
        outputMessage.getBody();
    }
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpOutputMessage(org.springframework.http.HttpOutputMessage) ServerHttpResponse(org.springframework.http.server.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.ServletServerHttpResponse)

Example 8 with HttpInputMessage

use of org.springframework.http.HttpInputMessage in project fastjson by alibaba.

the class FastJsonHttpMessageConverterTest method test_1.

public void test_1() throws Exception {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    Assert.assertNotNull(converter.getFastJsonConfig());
    converter.setFastJsonConfig(new FastJsonConfig());
    converter.canRead(VO.class, MediaType.APPLICATION_JSON_UTF8);
    converter.canWrite(VO.class, MediaType.APPLICATION_JSON_UTF8);
    converter.canRead(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
    converter.canWrite(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
    HttpInputMessage input = new HttpInputMessage() {

        public HttpHeaders getHeaders() {
            // TODO Auto-generated method stub
            return null;
        }

        public InputStream getBody() throws IOException {
            return new ByteArrayInputStream("{\"id\":123}".getBytes(Charset.forName("UTF-8")));
        }
    };
    VO vo = (VO) converter.read(VO.class, VO.class, input);
    Assert.assertEquals(123, vo.getId());
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    HttpOutputMessage out = new HttpOutputMessage() {

        public HttpHeaders getHeaders() {
            return new HttpHeaders();
        }

        public OutputStream getBody() throws IOException {
            return byteOut;
        }
    };
    converter.write(vo, VO.class, MediaType.TEXT_PLAIN, out);
    byte[] bytes = byteOut.toByteArray();
    Assert.assertEquals("{\"id\":123}", new String(bytes, "UTF-8"));
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
    converter.write(vo, VO.class, null, out);
    converter.write(vo, VO.class, MediaType.ALL, out);
    HttpOutputMessage out2 = new HttpOutputMessage() {

        public HttpHeaders getHeaders() {
            return new HttpHeaders() {

                private static final long serialVersionUID = 1L;

                @Override
                public MediaType getContentType() {
                    return MediaType.APPLICATION_JSON;
                }

                @Override
                public long getContentLength() {
                    return 1;
                }
            };
        }

        public OutputStream getBody() throws IOException {
            return byteOut;
        }
    };
    converter.write(vo, VO.class, MediaType.ALL, out2);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpHeaders(org.springframework.http.HttpHeaders) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpOutputMessage(org.springframework.http.HttpOutputMessage) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 9 with HttpInputMessage

use of org.springframework.http.HttpInputMessage in project fastjson by alibaba.

the class FastJsonpHttpMessageConverter4Test method test_1.

public void test_1() throws Exception {
    FastJsonpHttpMessageConverter4 converter = new FastJsonpHttpMessageConverter4();
    Assert.assertNotNull(converter.getFastJsonConfig());
    converter.setFastJsonConfig(new FastJsonConfig());
    converter.canRead(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
    converter.canWrite(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
    Method method1 = FastJsonpHttpMessageConverter4.class.getDeclaredMethod("supports", Class.class);
    method1.setAccessible(true);
    method1.invoke(converter, int.class);
    HttpInputMessage input = new HttpInputMessage() {

        public HttpHeaders getHeaders() {
            return null;
        }

        public InputStream getBody() throws IOException {
            return new ByteArrayInputStream("{\"id\":123}".getBytes(Charset.forName("UTF-8")));
        }
    };
    VO vo = (VO) converter.read(VO.class, VO.class, input);
    Assert.assertEquals(123, vo.getId());
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    HttpOutputMessage out = new HttpOutputMessage() {

        public HttpHeaders getHeaders() {
            return new HttpHeaders();
        }

        public OutputStream getBody() throws IOException {
            return byteOut;
        }
    };
    converter.write(vo, VO.class, MediaType.TEXT_PLAIN, out);
    byte[] bytes = byteOut.toByteArray();
    Assert.assertEquals("{\"id\":123}", new String(bytes, "UTF-8"));
    Method method2 = FastJsonpHttpMessageConverter4.class.getDeclaredMethod("readInternal", Class.class, HttpInputMessage.class);
    method2.setAccessible(true);
    method2.invoke(converter, VO.class, input);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpHeaders(org.springframework.http.HttpHeaders) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpOutputMessage(org.springframework.http.HttpOutputMessage) FastJsonpHttpMessageConverter4(com.alibaba.fastjson.support.spring.FastJsonpHttpMessageConverter4) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 10 with HttpInputMessage

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

the class RequestPartMethodArgumentResolver method resolveArgument.

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest request, WebDataBinderFactory binderFactory) throws Exception {
    HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
    RequestPart requestPart = parameter.getParameterAnnotation(RequestPart.class);
    boolean isRequired = ((requestPart == null || requestPart.required()) && !parameter.isOptional());
    String name = getPartName(parameter, requestPart);
    parameter = parameter.nestedIfOptional();
    Object arg = null;
    Object mpArg = MultipartResolutionDelegate.resolveMultipartArgument(name, parameter, servletRequest);
    if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
        arg = mpArg;
    } else {
        try {
            HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(servletRequest, name);
            arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType());
            WebDataBinder binder = binderFactory.createBinder(request, arg, name);
            if (arg != null) {
                validateIfApplicable(binder, parameter);
                if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
                    throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
                }
            }
            mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
        } catch (MissingServletRequestPartException ex) {
            if (isRequired) {
                throw ex;
            }
        } catch (MultipartException ex) {
            if (isRequired) {
                throw ex;
            }
        }
    }
    if (arg == null && isRequired) {
        if (!MultipartResolutionDelegate.isMultipartRequest(servletRequest)) {
            throw new MultipartException("Current request is not a multipart request");
        } else {
            throw new MissingServletRequestPartException(name);
        }
    }
    return adaptArgumentIfNecessary(arg, parameter);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpInputMessage(org.springframework.http.HttpInputMessage) WebDataBinder(org.springframework.web.bind.WebDataBinder) MissingServletRequestPartException(org.springframework.web.multipart.support.MissingServletRequestPartException) RequestPart(org.springframework.web.bind.annotation.RequestPart) MultipartException(org.springframework.web.multipart.MultipartException) RequestPartServletServerHttpRequest(org.springframework.web.multipart.support.RequestPartServletServerHttpRequest) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException)

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