/**
* This implementation delegates to {@link #getDefaultContentType(Object)} if a content
* type was not provided, calls {@link #getContentLength}, and sets the corresponding headers
* on the output message. It then calls {@link #writeInternal}.
*/
@Override
public
final
void
write(
final
T t, MediaType contentType, HttpOutputMessage outputMessage)
throws
IOException, HttpMessageNotWritableException {
final
HttpHeaders headers = outputMessage.getHeaders();
if
(headers.getContentType() ==
null
) {
MediaType contentTypeToUse = contentType;
if
(contentType ==
null
|| contentType.isWildcardType() || contentType.isWildcardSubtype()) {
contentTypeToUse = getDefaultContentType(t);
}
if
(contentTypeToUse !=
null
) {
headers.setContentType(contentTypeToUse);
}
}
if
(headers.getContentLength() == -
1
) {
Long contentLength = getContentLength(t, headers.getContentType());
if
(contentLength !=
null
) {
headers.setContentLength(contentLength);
}
}
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();
}
}