Search in sources :

Example 11 with HttpOutputMessage

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

the class AbstractGenericHttpMessageConverter method write.

/**
	 * This implementation sets the default headers by calling {@link #addDefaultHeaders},
	 * and then calls {@link #writeInternal}.
	 */
public final void write(final T t, final Type type, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    final HttpHeaders headers = outputMessage.getHeaders();
    addDefaultHeaders(headers, t, contentType);
    if (outputMessage instanceof StreamingHttpOutputMessage) {
        StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
        streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {

            @Override
            public void writeTo(final OutputStream outputStream) throws IOException {
                writeInternal(t, type, new HttpOutputMessage() {

                    @Override
                    public OutputStream getBody() throws IOException {
                        return outputStream;
                    }

                    @Override
                    public HttpHeaders getHeaders() {
                        return headers;
                    }
                });
            }
        });
    } else {
        writeInternal(t, type, outputMessage);
        outputMessage.getBody().flush();
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) HttpOutputMessage(org.springframework.http.HttpOutputMessage) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 12 with HttpOutputMessage

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

the class AbstractHttpMessageConverter method write.

/**
	 * This implementation sets the default headers by calling {@link #addDefaultHeaders},
	 * and then calls {@link #writeInternal}.
	 */
@Override
public final void write(final T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    final HttpHeaders headers = outputMessage.getHeaders();
    addDefaultHeaders(headers, t, contentType);
    if (outputMessage instanceof StreamingHttpOutputMessage) {
        StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) outputMessage;
        streamingOutputMessage.setBody(new StreamingHttpOutputMessage.Body() {

            @Override
            public void writeTo(final OutputStream outputStream) throws IOException {
                writeInternal(t, new HttpOutputMessage() {

                    @Override
                    public OutputStream getBody() throws IOException {
                        return outputStream;
                    }

                    @Override
                    public HttpHeaders getHeaders() {
                        return headers;
                    }
                });
            }
        });
    } else {
        writeInternal(t, outputMessage);
        outputMessage.getBody().flush();
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) HttpOutputMessage(org.springframework.http.HttpOutputMessage) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Example 13 with HttpOutputMessage

use of org.springframework.http.HttpOutputMessage in project spring-framework 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 : this.partConverters) {
        if (messageConverter.canWrite(partType, partContentType)) {
            HttpOutputMessage multipartMessage = new MultipartHttpOutputMessage(os);
            multipartMessage.getHeaders().setContentDispositionFormData(name, getFilename(partBody));
            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) StreamingHttpOutputMessage(org.springframework.http.StreamingHttpOutputMessage) HttpOutputMessage(org.springframework.http.HttpOutputMessage) MediaType(org.springframework.http.MediaType)

Example 14 with HttpOutputMessage

use of org.springframework.http.HttpOutputMessage in project java-chassis by ServiceComb.

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 e) {
    // TODO Auto-generated catch block
    } catch (IOException e) {
    // TODO Auto-generated catch block
    }
    try {
        HttpOutputMessage httpOutputMessage = Mockito.mock(CseClientHttpRequest.class);
        lCseHttpMessageConverter.write(null, null, httpOutputMessage);
    } catch (HttpMessageNotWritableException | IOException e) {
    // TODO Auto-generated catch block
    }
    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 15 with HttpOutputMessage

use of org.springframework.http.HttpOutputMessage in project geode by apache.

the class SerializableObjectHttpMessageConverterJUnitTest method testWriteInternal.

@Test
public void testWriteInternal() throws IOException {
    final String expectedOutputMessageBody = "Expected media of the HTTP output message body!";
    final byte[] expectedOutputMessageBodyBytes = IOUtils.serializeObject(expectedOutputMessageBody);
    final ByteArrayOutputStream out = new ByteArrayOutputStream(expectedOutputMessageBodyBytes.length);
    final HttpHeaders headers = new HttpHeaders();
    final HttpOutputMessage mockOutputMessage = mockContext.mock(HttpOutputMessage.class, "HttpOutputMessage");
    mockContext.checking(new Expectations() {

        {
            oneOf(mockOutputMessage).getHeaders();
            will(returnValue(headers));
            oneOf(mockOutputMessage).getBody();
            will(returnValue(out));
        }
    });
    final SerializableObjectHttpMessageConverter converter = new SerializableObjectHttpMessageConverter();
    converter.writeInternal(expectedOutputMessageBody, mockOutputMessage);
    final byte[] actualOutputMessageBodyBytes = out.toByteArray();
    assertEquals(expectedOutputMessageBodyBytes.length, headers.getContentLength());
    assertEquals(expectedOutputMessageBodyBytes.length, actualOutputMessageBodyBytes.length);
    for (int index = 0; index < actualOutputMessageBodyBytes.length; index++) {
        assertEquals(expectedOutputMessageBodyBytes[index], actualOutputMessageBodyBytes[index]);
    }
}
Also used : Expectations(org.jmock.Expectations) HttpHeaders(org.springframework.http.HttpHeaders) HttpOutputMessage(org.springframework.http.HttpOutputMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Aggregations

HttpOutputMessage (org.springframework.http.HttpOutputMessage)16 HttpHeaders (org.springframework.http.HttpHeaders)14 HttpInputMessage (org.springframework.http.HttpInputMessage)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Test (org.junit.Test)6 FastJsonConfig (com.alibaba.fastjson.support.config.FastJsonConfig)4 Method (java.lang.reflect.Method)4 MediaType (org.springframework.http.MediaType)4 IOException (java.io.IOException)3 StreamingHttpOutputMessage (org.springframework.http.StreamingHttpOutputMessage)3 FastJsonHttpMessageConverter (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter)2 FastJsonpHttpMessageConverter4 (com.alibaba.fastjson.support.spring.FastJsonpHttpMessageConverter4)2 OutputStream (java.io.OutputStream)2 UnitTest (org.apache.geode.test.junit.categories.UnitTest)2 Expectations (org.jmock.Expectations)2 ResponseEntity (org.springframework.http.ResponseEntity)2 ServletServerHttpResponse (org.springframework.http.server.ServletServerHttpResponse)2 FastJsonHttpMessageConverter4 (com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4)1 MappingFastJsonValue (com.alibaba.fastjson.support.spring.MappingFastJsonValue)1