private boolean errorFlow = false;
public void handleResponse(MessageContext context) throws Throwable {
MediaType responseMediaType = null;
Object result = context.getResponseEntity();
if (result == null) {
return;
}
if (result instanceof Response) {
Response response = (Response)result;
Object first = response.getMetadata().getFirst(HttpHeaders.CONTENT_TYPE);
if (first != null) {
if (first instanceof MediaType) {
responseMediaType = (MediaType)first;
} else {
responseMediaType = MediaType.valueOf(first.toString());
}
}
}
if (responseMediaType == null) {
Set<MediaType> producedMime = null;
SearchResult searchResult = context.getAttribute(SearchResult.class);
if (searchResult != null && searchResult.isFound()) {
MethodMetadata methodMetadata = searchResult.getMethod().getMetadata();
producedMime = methodMetadata.getProduces();
}
if (producedMime == null || producedMime.isEmpty()) {
producedMime =
context.getAttribute(ProvidersRegistry.class)
.getMessageBodyWriterMediaTypes(result.getClass());
}
if (producedMime.isEmpty()) {
producedMime.add(MediaType.WILDCARD_TYPE);
}
List<MediaType> acceptableMediaTypes =
context.getHttpHeaders().getAcceptableMediaTypes();
// collect all candidates
List<CandidateMediaType> candidates = new LinkedList<CandidateMediaType>();
for (MediaType acceptableMediaType : acceptableMediaTypes) {
for (MediaType mediaType : producedMime) {
if (mediaType.isCompatible(acceptableMediaType)) {
MediaType candidateMediaType = null;
if (MediaTypeUtils.compareTo(mediaType, acceptableMediaType) > 0) {
candidateMediaType = mediaType;
} else {
candidateMediaType = acceptableMediaType;
}