Search in sources :

Example 21 with HttpOutputMessage

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

use of org.springframework.http.HttpOutputMessage in project xm-ms-entity by xm-online.

the class MultipartMixedConverter 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 : this.partConverters) {
        if (messageConverter.canWrite(partType, partContentType)) {
            HttpOutputMessage multipartMessage = new MultipartMixedConverter.MultipartHttpOutputMessage(os);
            multipartMessage.getHeaders().setContentDispositionFormData(name, null);
            if (!partHeaders.isEmpty()) {
                multipartMessage.getHeaders().putAll(partHeaders);
            }
            ((HttpMessageConverter<Object>) messageConverter).write(partBody, partContentType, multipartMessage);
            return;
        }
    }
    throw new HttpMessageNotWritableException("Could not write request: no suitable HttpMessageConverter found for request type [" + partType.getName() + "]");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) HttpOutputMessage(org.springframework.http.HttpOutputMessage) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) 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) MediaType(org.springframework.http.MediaType)

Example 23 with HttpOutputMessage

use of org.springframework.http.HttpOutputMessage in project ontrack by nemerosa.

the class ResourceHttpMessageConverterIT method branch_disable_granted_for_automation.

@Test
public void branch_disable_granted_for_automation() throws Exception {
    // Objects
    Project p = Project.of(new NameDescription("P", "Projet créé")).withId(ID.of(1)).withSignature(SIGNATURE);
    Branch b = Branch.of(p, new NameDescription("B", "Branch")).withId(ID.of(1)).withSignature(SIGNATURE);
    // Message
    HttpOutputMessage message = mock(HttpOutputMessage.class);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    when(message.getBody()).thenReturn(output);
    // Serialization
    asGlobalRole("AUTOMATION").execute(() -> {
        try {
            converter.writeInternal(b, message);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    });
    // Content
    String json = new String(output.toByteArray(), "UTF-8");
    // Parsing
    JsonNode node = ObjectMapperFactory.create().readTree(json);
    // Disable link
    assertEquals("urn:test:net.nemerosa.ontrack.boot.ui.BranchController#disableBranch:1", node.path("_disable").asText());
}
Also used : Project(net.nemerosa.ontrack.model.structure.Project) NameDescription(net.nemerosa.ontrack.model.structure.NameDescription) Branch(net.nemerosa.ontrack.model.structure.Branch) HttpOutputMessage(org.springframework.http.HttpOutputMessage) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 24 with HttpOutputMessage

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

the class HttpEntityMethodProcessorMockTests method shouldHandleResponseHeaderAndBody.

@Test
public void shouldHandleResponseHeaderAndBody() throws Exception {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("header", "headerValue");
    ResponseEntity<String> returnValue = new ResponseEntity<>("body", responseHeaders, HttpStatus.ACCEPTED);
    initStringMessageConversion(TEXT_PLAIN);
    processor.handleReturnValue(returnValue, returnTypeResponseEntity, mavContainer, webRequest);
    ArgumentCaptor<HttpOutputMessage> outputMessage = ArgumentCaptor.forClass(HttpOutputMessage.class);
    verify(stringHttpMessageConverter).write(eq("body"), eq(TEXT_PLAIN), outputMessage.capture());
    assertThat(mavContainer.isRequestHandled()).isTrue();
    assertThat(outputMessage.getValue().getHeaders().get("header").get(0)).isEqualTo("headerValue");
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) HttpOutputMessage(org.springframework.http.HttpOutputMessage) Test(org.junit.jupiter.api.Test)

Example 25 with HttpOutputMessage

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

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