Search in sources :

Example 1 with HttpOutputMessage

use of org.springframework.http.HttpOutputMessage 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 2 with HttpOutputMessage

use of org.springframework.http.HttpOutputMessage in project incubator-servicecomb-java-chassis by apache.

the class TestCseHttpMessageConverter method testAll.

@Test
public void testAll() {
    MockUtil.getInstance().mockReflectionUtils();
    MockUtil.getInstance().mockCseClientHttpRequest();
    CseHttpMessageConverter lCseHttpMessageConverter = new CseHttpMessageConverter();
    lCseHttpMessageConverter.canWrite(null, null);
    lCseHttpMessageConverter.getSupportedMediaTypes();
    try {
        lCseHttpMessageConverter.read(this.getClass(), null);
    } catch (HttpMessageNotReadableException | IOException ignored) {
    }
    try {
        HttpOutputMessage httpOutputMessage = Mockito.mock(CseClientHttpRequest.class);
        lCseHttpMessageConverter.write(null, null, httpOutputMessage);
    } catch (HttpMessageNotWritableException | IOException ignored) {
    }
    Assert.assertEquals(true, lCseHttpMessageConverter.canRead(null, null));
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpOutputMessage(org.springframework.http.HttpOutputMessage) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with HttpOutputMessage

use of org.springframework.http.HttpOutputMessage in project spring-data-document-examples by spring-projects.

the class FormHttpMessageConverter method writePart.

@SuppressWarnings("unchecked")
private void writePart(String name, HttpEntity partEntity, OutputStream os) throws IOException {
    Object partBody = partEntity.getBody();
    Class<?> partType = partBody.getClass();
    HttpHeaders partHeaders = partEntity.getHeaders();
    MediaType partContentType = partHeaders.getContentType();
    for (HttpMessageConverter messageConverter : partConverters) {
        if (messageConverter.canWrite(partType, partContentType)) {
            HttpOutputMessage multipartOutputMessage = new MultipartHttpOutputMessage(os);
            multipartOutputMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody));
            if (!partHeaders.isEmpty()) {
                multipartOutputMessage.getHeaders().putAll(partHeaders);
            }
            messageConverter.write(partBody, partContentType, multipartOutputMessage);
            return;
        }
    }
    throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter found for request type [" + partType.getName() + "]");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpOutputMessage(org.springframework.http.HttpOutputMessage) MediaType(org.springframework.http.MediaType)

Example 4 with HttpOutputMessage

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

the class DateFormatPriorityTest method test_priority_01.

// Annotation + FastJsonConfig (Annotation优先)
public void test_priority_01() throws Exception {
    // FastJsonConfig
    FastJsonConfig config = new FastJsonConfig();
    config.setDateFormat("yyyy-MM.dd");
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    converter.setFastJsonConfig(config);
    converter.canRead(VO.class, MediaType.APPLICATION_JSON_UTF8);
    converter.canWrite(VO.class, MediaType.APPLICATION_JSON_UTF8);
    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    HttpOutputMessage out = new HttpOutputMessage() {

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

                private static final long serialVersionUID = 1L;

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

        public OutputStream getBody() throws IOException {
            return byteOut;
        }
    };
    VO2 vo = new VO2();
    vo.setDate(calendar.getTime());
    converter.write(vo, VO.class, MediaType.APPLICATION_JSON_UTF8, out);
    byte[] bytes = byteOut.toByteArray();
    String jsonString = new String(bytes, "UTF-8");
    assertEquals("{\"date\":\"1995.10-26\"}", jsonString);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) HttpOutputMessage(org.springframework.http.HttpOutputMessage) FastJsonHttpMessageConverter(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with HttpOutputMessage

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

the class FastJsonHttpMessageConverter4Test method test_1.

public void test_1() throws Exception {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
    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 = FastJsonHttpMessageConverter4.class.getDeclaredMethod("supports", Class.class);
    method1.setAccessible(true);
    method1.invoke(converter, int.class);
    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"));
    Method method2 = FastJsonHttpMessageConverter4.class.getDeclaredMethod("readInternal", Class.class, HttpInputMessage.class);
    method2.setAccessible(true);
    method2.invoke(converter, VO.class, input);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) FastJsonHttpMessageConverter4(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4) HttpHeaders(org.springframework.http.HttpHeaders) FastJsonConfig(com.alibaba.fastjson.support.config.FastJsonConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpOutputMessage(org.springframework.http.HttpOutputMessage) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

HttpOutputMessage (org.springframework.http.HttpOutputMessage)25 HttpHeaders (org.springframework.http.HttpHeaders)18 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 Test (org.junit.Test)9 HttpInputMessage (org.springframework.http.HttpInputMessage)8 FastJsonConfig (com.alibaba.fastjson.support.config.FastJsonConfig)7 IOException (java.io.IOException)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 MediaType (org.springframework.http.MediaType)6 FastJsonHttpMessageConverter (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)5 Method (java.lang.reflect.Method)4 StreamingHttpOutputMessage (org.springframework.http.StreamingHttpOutputMessage)4 HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Branch (net.nemerosa.ontrack.model.structure.Branch)3 NameDescription (net.nemerosa.ontrack.model.structure.NameDescription)3 Project (net.nemerosa.ontrack.model.structure.Project)3 HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)3 FastJsonpHttpMessageConverter4 (com.alibaba.fastjson.support.spring.FastJsonpHttpMessageConverter4)2 InputStream (java.io.InputStream)2