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();
}
}
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();
}
}
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() + "]");
}
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));
}
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]);
}
}
Aggregations