Message message,
Response theResponse,
OperationResourceInfo ori,
boolean firstTry) {
ResponseImpl response = (ResponseImpl)JAXRSUtils.copyResponseIfNeeded(theResponse);
final Exchange exchange = message.getExchange();
boolean headResponse = response.getStatus() == 200 && firstTry
&& ori != null && HttpMethod.HEAD.equals(ori.getHttpMethod());
Object entity = response.getActualEntity();
if (headResponse && entity != null) {
LOG.info(new org.apache.cxf.common.i18n.Message("HEAD_WITHOUT_ENTITY", BUNDLE).toString());
entity = null;
}
Method invoked = ori == null ? null : ori.getAnnotatedMethod() != null
? ori.getAnnotatedMethod() : ori.getMethodToInvoke();
Annotation[] annotations = null;
Annotation[] staticAnns = invoked != null ? invoked.getAnnotations() : new Annotation[]{};
Annotation[] responseAnns = response.getEntityAnnotations();
if (responseAnns != null) {
annotations = new Annotation[staticAnns.length + responseAnns.length];
System.arraycopy(staticAnns, 0, annotations, 0, staticAnns.length);
System.arraycopy(responseAnns, 0, annotations, staticAnns.length, responseAnns.length);
} else {
annotations = staticAnns;
}
response.setStatus(getActualStatus(response.getStatus(), entity));
response.setEntity(entity, annotations);
// Prepare the headers
MultivaluedMap<String, Object> responseHeaders = response.getMetadata();
Map<String, List<Object>> userHeaders =
(Map<String, List<Object>>)message.get(Message.PROTOCOL_HEADERS);
if (firstTry && userHeaders != null) {
responseHeaders.putAll(userHeaders);
}
if (entity != null) {
String initialResponseContentType = (String)message.get(Message.CONTENT_TYPE);
if (initialResponseContentType != null && !responseHeaders.containsKey(HttpHeaders.CONTENT_TYPE)) {
responseHeaders.putSingle(HttpHeaders.CONTENT_TYPE, initialResponseContentType);
}
} else {
message.remove(Message.CONTENT_TYPE);
}
message.put(Message.PROTOCOL_HEADERS, responseHeaders);
setResponseDate(responseHeaders, firstTry);
// Run the filters
try {
JAXRSUtils.runContainerResponseFilters(providerFactory, response, message, ori, invoked);
} catch (IOException ex) {
handleWriteException(providerFactory, message, ex, firstTry);
return;
} catch (Throwable ex) {
handleWriteException(providerFactory, message, ex, firstTry);
return;
}
// Write the entity
entity = InjectionUtils.getEntity(response.getActualEntity());
setResponseStatus(message, getActualStatus(response.getStatus(), entity));
if (entity == null) {
if (!headResponse) {
responseHeaders.putSingle(HttpHeaders.CONTENT_LENGTH, "0");
responseHeaders.remove(HttpHeaders.CONTENT_TYPE);
message.remove(Message.CONTENT_TYPE);
}
HttpUtils.convertHeaderValuesToString(responseHeaders, true);
return;
}
Object ignoreWritersProp = exchange.get(JAXRSUtils.IGNORE_MESSAGE_WRITERS);
boolean ignoreWriters =
ignoreWritersProp == null ? false : Boolean.valueOf(ignoreWritersProp.toString());
if (ignoreWriters) {
writeResponseToStream(message.getContent(OutputStream.class), entity);
return;
}
MediaType responseMediaType =
getResponseMediaType(responseHeaders.getFirst(HttpHeaders.CONTENT_TYPE));
Class<?> serviceCls = invoked != null ? ori.getClassResourceInfo().getServiceClass() : null;
Class<?> targetType = InjectionUtils.getRawResponseClass(entity);
Type genericType = InjectionUtils.getGenericResponseType(invoked, serviceCls,
response.getActualEntity(), targetType, exchange);
targetType = InjectionUtils.updateParamClassToTypeIfNeeded(targetType, genericType);
annotations = response.getEntityAnnotations();
List<WriterInterceptor> writers = providerFactory
.createMessageBodyWriterInterceptor(targetType, genericType, annotations, responseMediaType, message,
ori == null ? null : ori.getNameBindings());