Search in sources :

Example 1 with AsyncResponseImpl

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;
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) ResourceContext(javax.ws.rs.container.ResourceContext) Message(org.apache.cxf.message.Message) MessageContentsList(org.apache.cxf.message.MessageContentsList) WebApplicationException(javax.ws.rs.WebApplicationException) ClassLoaderHolder(org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder) Fault(org.apache.cxf.interceptor.Fault) AsyncResponseImpl(org.apache.cxf.jaxrs.impl.AsyncResponseImpl) MediaType(javax.ws.rs.core.MediaType) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List) ResourceContextImpl(org.apache.cxf.jaxrs.impl.ResourceContextImpl) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Method(java.lang.reflect.Method) IOException(java.io.IOException) AsyncResponse(javax.ws.rs.container.AsyncResponse) Response(javax.ws.rs.core.Response) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Application(javax.ws.rs.core.Application)

Example 2 with AsyncResponseImpl

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;
}
Also used : AsyncResponseImpl(org.apache.cxf.jaxrs.impl.AsyncResponseImpl) Observable(rx.Observable)

Example 3 with AsyncResponseImpl

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;
}
Also used : CancellationException(java.util.concurrent.CancellationException) AsyncResponseImpl(org.apache.cxf.jaxrs.impl.AsyncResponseImpl) CompletionStage(java.util.concurrent.CompletionStage)

Example 4 with AsyncResponseImpl

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;
}
Also used : CancellationException(java.util.concurrent.CancellationException) AsyncResponseImpl(org.apache.cxf.jaxrs.impl.AsyncResponseImpl) CompletionStage(java.util.concurrent.CompletionStage)

Example 5 with AsyncResponseImpl

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;
}
Also used : AsyncResponseImpl(org.apache.cxf.jaxrs.impl.AsyncResponseImpl) Flux(reactor.core.publisher.Flux) Message(org.apache.cxf.message.Message) AbstractReactiveInvoker(org.apache.cxf.jaxrs.reactivestreams.server.AbstractReactiveInvoker) Mono(reactor.core.publisher.Mono) Mono(reactor.core.publisher.Mono) Flux(reactor.core.publisher.Flux) AsyncResponseImpl(org.apache.cxf.jaxrs.impl.AsyncResponseImpl)

Aggregations

AsyncResponseImpl (org.apache.cxf.jaxrs.impl.AsyncResponseImpl)10 Message (org.apache.cxf.message.Message)3 InputStream (java.io.InputStream)2 Reader (java.io.Reader)2 CancellationException (java.util.concurrent.CancellationException)2 CompletionStage (java.util.concurrent.CompletionStage)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 AsyncResponse (javax.ws.rs.container.AsyncResponse)2 MediaType (javax.ws.rs.core.MediaType)2 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)2 ReaderInputStream (org.apache.cxf.io.ReaderInputStream)2 MessageContentsList (org.apache.cxf.message.MessageContentsList)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 ResourceContext (javax.ws.rs.container.ResourceContext)1 Application (javax.ws.rs.core.Application)1 Response (javax.ws.rs.core.Response)1 ClassLoaderHolder (org.apache.cxf.common.classloader.ClassLoaderUtils.ClassLoaderHolder)1 Fault (org.apache.cxf.interceptor.Fault)1