Created
November 9, 2016 14:03
-
-
Save johnpoth/dff548845914286988901d884101c5b6 to your computer and use it in GitHub Desktop.
InvocationCallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class CxfInvocationCallback implements InvocationCallback<Response> { | |
private final Exchange exchange; | |
private final CxfRsBinding binding; | |
private final Class<?> responseClass; | |
private final AsyncCallback callback; | |
private final Type type; | |
private CxfInvocationCallback(Exchange exchange, CxfRsBinding binding, Class<?> responseClass, AsyncCallback callback, Type type) { | |
this.exchange = exchange; | |
this.binding=binding; | |
this.responseClass=responseClass; | |
this.callback=callback; | |
this.type=type; | |
} | |
@Override | |
public void completed(Response response) { | |
try { | |
if (shouldHandleError(response)) { | |
handleError(response); | |
return; | |
} | |
if (exchange.getPattern().isOutCapable()) { | |
LOG.trace("Response body = {}", response); | |
exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders()); | |
exchange.getOut().getHeaders().putAll(binding.bindResponseHeadersToCamelHeaders(response, exchange)); | |
//check if necessary if | |
if(type !=null && !type.equals(Void.TYPE)){ | |
GenericType genericType = new GenericType(type); | |
exchange.getOut().setBody(binding.bindResponseToCamelBody(response.readEntity(genericType), exchange)); | |
} | |
else if(responseClass!=null && !responseClass.equals(Void.TYPE)) { | |
exchange.getOut().setBody(binding.bindResponseToCamelBody(response.readEntity(responseClass), exchange)); | |
}else{ | |
exchange.getOut().setBody(binding.bindResponseToCamelBody(response, exchange)); | |
} | |
exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, response.getStatus()); | |
}else{ | |
response.close(); | |
} | |
}catch(Exception e){ | |
failed(e); | |
}finally { | |
callback.done(false); | |
} | |
} | |
private boolean shouldHandleError(Response response) { | |
//Throw exception on a response > 207 | |
//http://en.wikipedia.org/wiki/List_of_HTTP_status_codes | |
if (throwException) { | |
Integer respCode = response.getStatus(); | |
if (respCode > 207) { | |
return true; | |
} | |
} | |
return false; | |
} | |
private void handleError(Response response){ | |
exchange.setException(populateCxfRsProducerException(exchange, response, response.getStatus())); | |
} | |
@Override | |
public void failed(Throwable throwable) { | |
LOG.error("Error during request ",throwable); | |
if(throwable.getClass().isInstance(WebApplicationException.class)){ | |
final WebApplicationException cast = WebApplicationException.class.cast(throwable); | |
final Response response = cast.getResponse(); | |
if (shouldHandleError(response)) { | |
handleError(response); | |
return; | |
} | |
}else if(throwable.getClass().isInstance(ResponseProcessingException.class)){ | |
final ResponseProcessingException cast = ResponseProcessingException.class.cast(throwable); | |
final Response response = cast.getResponse(); | |
if (shouldHandleError(response)) { | |
handleError(response); | |
return; | |
} | |
}else{ | |
exchange.setException(throwable); | |
} | |
callback.done(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment