use of org.apache.cxf.jaxrs.impl.AsyncResponseImpl in project cxf by apache.
the class JAXRSInvoker method invoke.
@SuppressWarnings("unchecked")
public Object invoke(Exchange exchange, Object request, Object resourceObject) {
final OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
final ClassResourceInfo cri = ori.getClassResourceInfo();
final Message inMessage = exchange.getInMessage();
final ServerProviderFactory providerFactory = ServerProviderFactory.getInstance(inMessage);
if (cri.isRoot()) {
cri.injectContexts(resourceObject, ori, inMessage);
ProviderInfo<Application> appProvider = providerFactory.getApplicationProvider();
if (appProvider != null) {
InjectionUtils.injectContexts(appProvider.getProvider(), appProvider, inMessage);
}
}
Method methodToInvoke = getMethodToInvoke(cri, ori, resourceObject);
List<Object> params = null;
if (request instanceof List) {
params = CastUtils.cast((List<?>) request);
} else if (request != null) {
params = new MessageContentsList(request);
}
Object result = null;
ClassLoaderHolder contextLoader = null;
AsyncResponseImpl asyncResponse = null;
try {
if (setServiceLoaderAsContextLoader(inMessage)) {
contextLoader = ClassLoaderUtils.setThreadContextClassloader(resourceObject.getClass().getClassLoader());
}
if (!ori.isSubResourceLocator()) {
asyncResponse = (AsyncResponseImpl) inMessage.get(AsyncResponse.class);
}
result = invoke(exchange, resourceObject, methodToInvoke, params);
if (asyncResponse == null && !ori.isSubResourceLocator()) {
asyncResponse = checkFutureResponse(inMessage, checkResultObject(result));
}
if (asyncResponse != null) {
if (!asyncResponse.suspendContinuationIfNeeded()) {
result = handleAsyncResponse(exchange, asyncResponse);
} else {
providerFactory.clearThreadLocalProxies();
}
}
} catch (Fault ex) {
Object faultResponse;
if (asyncResponse != null) {
faultResponse = handleAsyncFault(exchange, asyncResponse, ex.getCause() == null ? ex : ex.getCause());
} else {
faultResponse = handleFault(ex, inMessage, cri, methodToInvoke);
}
return faultResponse;
} finally {
exchange.put(LAST_SERVICE_OBJECT, resourceObject);
if (contextLoader != null) {
contextLoader.reset();
}
}
ClassResourceInfo subCri = null;
if (ori.isSubResourceLocator()) {
try {
MultivaluedMap<String, String> values = getTemplateValues(inMessage);
String subResourcePath = values.getFirst(URITemplate.FINAL_MATCH_GROUP);
String httpMethod = (String) inMessage.get(Message.HTTP_REQUEST_METHOD);
String contentType = (String) inMessage.get(Message.CONTENT_TYPE);
if (contentType == null) {
contentType = "*/*";
}
List<MediaType> acceptContentType = (List<MediaType>) exchange.get(Message.ACCEPT_CONTENT_TYPE);
result = checkSubResultObject(result, subResourcePath);
Class<?> subResponseType = null;
if (result.getClass() == Class.class) {
ResourceContext rc = new ResourceContextImpl(inMessage, ori);
result = rc.getResource((Class<?>) result);
subResponseType = InjectionUtils.getActualType(methodToInvoke.getGenericReturnType());
} else {
subResponseType = methodToInvoke.getReturnType();
}
subCri = cri.getSubResource(subResponseType, ClassHelper.getRealClass(exchange.getBus(), result), result);
if (subCri == null) {
org.apache.cxf.common.i18n.Message errorM = new org.apache.cxf.common.i18n.Message("NO_SUBRESOURCE_FOUND", BUNDLE, subResourcePath);
LOG.severe(errorM.toString());
throw ExceptionUtils.toNotFoundException(null, null);
}
OperationResourceInfo subOri = JAXRSUtils.findTargetMethod(Collections.singletonMap(subCri, values), inMessage, httpMethod, values, contentType, acceptContentType);
exchange.put(OperationResourceInfo.class, subOri);
inMessage.put(URITemplate.TEMPLATE_PARAMETERS, values);
if (!subOri.isSubResourceLocator() && JAXRSUtils.runContainerRequestFilters(providerFactory, inMessage, false, subOri.getNameBindings())) {
return new MessageContentsList(exchange.get(Response.class));
}
// work out request parameters for the sub-resource class. Here we
// presume InputStream has not been consumed yet by the root resource class.
List<Object> newParams = JAXRSUtils.processParameters(subOri, values, inMessage);
inMessage.setContent(List.class, newParams);
return this.invoke(exchange, newParams, result);
} catch (IOException ex) {
Response resp = JAXRSUtils.convertFaultToResponse(ex, inMessage);
if (resp == null) {
resp = JAXRSUtils.convertFaultToResponse(ex, inMessage);
}
return new MessageContentsList(resp);
} catch (WebApplicationException ex) {
Response excResponse;
if (JAXRSUtils.noResourceMethodForOptions(ex.getResponse(), (String) inMessage.get(Message.HTTP_REQUEST_METHOD))) {
excResponse = JAXRSUtils.createResponse(Collections.singletonList(subCri), null, null, 200, true);
} else {
excResponse = JAXRSUtils.convertFaultToResponse(ex, inMessage);
}
return new MessageContentsList(excResponse);
}
}
setResponseContentTypeIfNeeded(inMessage, result);
return result;
}
use of org.apache.cxf.jaxrs.impl.AsyncResponseImpl in project cxf by apache.
the class ObservableInvoker method checkFutureResponse.
protected AsyncResponseImpl checkFutureResponse(Message inMessage, Object result) {
if (result instanceof Observable) {
final Observable<?> obs = (Observable<?>) result;
final AsyncResponseImpl asyncResponse = new AsyncResponseImpl(inMessage);
obs.subscribe(v -> asyncResponse.resume(v), t -> handleThrowable(asyncResponse, t));
return asyncResponse;
}
return null;
}
use of org.apache.cxf.jaxrs.impl.AsyncResponseImpl in project tomee by apache.
the class JAXRSInvoker method checkFutureResponse.
protected AsyncResponseImpl checkFutureResponse(Message inMessage, Object result) {
if (result instanceof CompletionStage) {
final CompletionStage<?> stage = (CompletionStage<?>) result;
final AsyncResponseImpl asyncResponse = new AsyncResponseImpl(inMessage);
stage.whenComplete((v, t) -> {
if (t instanceof CancellationException) {
asyncResponse.cancel();
} else {
asyncResponse.resume(v != null ? v : t);
}
});
return asyncResponse;
}
return null;
}
use of org.apache.cxf.jaxrs.impl.AsyncResponseImpl in project cxf by apache.
the class JAXRSInvoker method checkFutureResponse.
protected AsyncResponseImpl checkFutureResponse(Message inMessage, Object result) {
if (result instanceof CompletionStage) {
final CompletionStage<?> stage = (CompletionStage<?>) result;
final AsyncResponseImpl asyncResponse = new AsyncResponseImpl(inMessage);
stage.whenComplete((v, t) -> {
if (t instanceof CancellationException) {
asyncResponse.cancel();
} else {
asyncResponse.resume(v != null ? v : t);
}
});
return asyncResponse;
}
return null;
}
use of org.apache.cxf.jaxrs.impl.AsyncResponseImpl in project cxf by apache.
the class ReactorInvoker method checkFutureResponse.
@Override
protected AsyncResponseImpl checkFutureResponse(Message inMessage, Object result) {
if (result instanceof Flux) {
final Flux<?> flux = (Flux<?>) result;
final AsyncResponseImpl asyncResponse = new AsyncResponseImpl(inMessage);
if (!isStreamingSubscriberUsed(flux, asyncResponse, inMessage)) {
flux.doOnNext(asyncResponse::resume).doOnError(t -> handleThrowable(asyncResponse, t)).subscribe();
}
return asyncResponse;
} else if (result instanceof Mono) {
final Mono<?> mono = (Mono<?>) result;
final AsyncResponseImpl asyncResponse = new AsyncResponseImpl(inMessage);
mono.doOnNext(asyncResponse::resume).doOnError(t -> handleThrowable(asyncResponse, t)).subscribe();
return asyncResponse;
}
return null;
}
Aggregations