List<MediaType> availableContentTypes =
computeAvailableContentTypes(message, response);
Method invoked = operation == null ? null : operation.getMethodToInvoke();
MessageBodyWriter writer = null;
MediaType responseType = null;
for (MediaType type : availableContentTypes) {
writer = ProviderFactory.getInstance()
.createMessageBodyWriter(targetType,
invoked != null ? invoked.getGenericReturnType() : null,
invoked != null ? invoked.getAnnotations() : new Annotation[]{},
type,
exchange.getInMessage());
if (writer != null) {
responseType = type;
break;
}
}
OutputStream out = message.getContent(OutputStream.class);
if (writer == null) {
message.put(Message.RESPONSE_CODE, 406);
writeResponseErrorMessage(out,
"NO_MSG_WRITER",
invoked != null ? invoked.getReturnType().getSimpleName() : "");
return;
}
try {
responseType = checkFinalContentType(responseType);
LOG.fine("Response content type is: " + responseType.toString());
message.put(Message.CONTENT_TYPE, responseType.toString());
LOG.fine("Response EntityProvider is: " + writer.getClass().getName());
writer.writeTo(responseObj, targetType, invoked.getGenericReturnType(),
invoked != null ? invoked.getAnnotations() : new Annotation[]{},
responseType,
response.getMetadata(),
out);