(TypeVariable)genericType);
}
Annotation[] annotations = invoked != null ? invoked.getAnnotations() : new Annotation[]{};
MessageBodyWriter writer = null;
MediaType responseType = null;
for (MediaType type : availableContentTypes) {
writer = ProviderFactory.getInstance(message)
.createMessageBodyWriter(targetType, genericType, annotations, type, message);
if (writer != null) {
responseType = type;
break;
}
}
OutputStream outOriginal = message.getContent(OutputStream.class);
if (writer == null) {
message.put(Message.CONTENT_TYPE, "text/plain");
message.put(Message.RESPONSE_CODE, 500);
writeResponseErrorMessage(outOriginal, "NO_MSG_WRITER", targetType.getSimpleName());
return;
}
boolean enabled = checkBufferingMode(message, writer, firstTry);
Object entity = getEntity(responseObj);
try {
responseType = checkFinalContentType(responseType);
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Response content type is: " + responseType.toString());
}
message.put(Message.CONTENT_TYPE, responseType.toString());
long size = writer.getSize(entity, targetType, genericType, annotations, responseType);
if (size > 0) {
LOG.fine("Setting ContentLength to " + size + " as requested by "
+ writer.getClass().getName());
responseHeaders.putSingle(HttpHeaders.CONTENT_LENGTH, Long.toString(size));
}
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("Response EntityProvider is: " + writer.getClass().getName());
}
try {
writer.writeTo(entity, targetType, genericType,
annotations,
responseType,
responseHeaders,
message.getContent(OutputStream.class));