submit(asyncExecutorsFactory.getRequestingExecutor(request), new Runnable() {
@Override
public void run() {
final RequestScope.Instance currentScopeInstance = requestScope.referenceCurrent();
final AsyncConnectorCallback connectorCallback = new AsyncConnectorCallback() {
@Override
public void response(final ClientResponse response) {
submit(asyncExecutorsFactory.getRespondingExecutor(request), currentScopeInstance, new Runnable() {
@Override
public void run() {
final ClientResponse processedResponse;
try {
processedResponse = Stages.process(response, responseProcessingRoot);
} catch (Throwable throwable) {
failure(throwable);
return;
}
try {
callback.completed(processedResponse, requestScope);
} finally {
currentScopeInstance.release();
}
}
});
}
@Override
public void failure(Throwable failure) {
try {
callback.failed(failure instanceof ProcessingException ?
(ProcessingException) failure : new ProcessingException(failure));
} finally {
currentScopeInstance.release();
}
}
};
try {
connector.apply(addUserAgent(Stages.process(request, requestProcessingRoot), connector.getName()), connectorCallback);
} catch (AbortException aborted) {
connectorCallback.response(aborted.getAbortResponse());
} catch (Throwable throwable) {
connectorCallback.failure(throwable);
}
}
});
}