Search in sources :

Example 1 with ResourceContextImpl

use of org.apache.cxf.jaxrs.impl.ResourceContextImpl in project tomee by apache.

the class JAXRSUtils method createContextValue.

public static <T> T createContextValue(Message m, Type genericType, Class<T> clazz) {
    Message contextMessage = getContextMessage(m);
    Object o = null;
    if (UriInfo.class.isAssignableFrom(clazz)) {
        o = createUriInfo(contextMessage);
    } else if (HttpHeaders.class.isAssignableFrom(clazz) || ProtocolHeaders.class.isAssignableFrom(clazz)) {
        o = createHttpHeaders(contextMessage, clazz);
    } else if (SecurityContext.class.isAssignableFrom(clazz)) {
        SecurityContext customContext = contextMessage.get(SecurityContext.class);
        o = customContext == null ? new SecurityContextImpl(contextMessage) : customContext;
    } else if (MessageContext.class.isAssignableFrom(clazz)) {
        o = new MessageContextImpl(m);
    } else if (ResourceInfo.class.isAssignableFrom(clazz)) {
        o = new ResourceInfoImpl(contextMessage);
    } else if (ResourceContext.class.isAssignableFrom(clazz)) {
        OperationResourceInfo operationResourceInfo = contextMessage.getExchange().get(OperationResourceInfo.class);
        if (operationResourceInfo != null) {
            o = new ResourceContextImpl(contextMessage, operationResourceInfo);
        }
    } else if (Request.class.isAssignableFrom(clazz)) {
        o = new RequestImpl(contextMessage);
    } else if (Providers.class.isAssignableFrom(clazz)) {
        o = new ProvidersImpl(contextMessage);
    } else if (ContextResolver.class.isAssignableFrom(clazz)) {
        o = createContextResolver(genericType, contextMessage);
    } else if (Configuration.class.isAssignableFrom(clazz)) {
        o = ProviderFactory.getInstance(contextMessage).getConfiguration(contextMessage);
    } else if (Application.class.isAssignableFrom(clazz)) {
        ProviderInfo<?> providerInfo = (ProviderInfo<?>) contextMessage.getExchange().getEndpoint().get(Application.class.getName());
        o = providerInfo == null ? null : providerInfo.getProvider();
    } else if (contextMessage != null) {
        ContextProvider<?> provider = ProviderFactory.getInstance(contextMessage).createContextProvider(clazz, contextMessage);
        if (provider != null) {
            o = provider.createContext(contextMessage);
        }
    }
    if (o == null && contextMessage != null && !MessageUtils.isRequestor(contextMessage)) {
        o = HttpUtils.createServletResourceValue(contextMessage, clazz);
    }
    return clazz.cast(o);
}
Also used : SecurityContextImpl(org.apache.cxf.jaxrs.impl.SecurityContextImpl) ResourceContext(javax.ws.rs.container.ResourceContext) Message(org.apache.cxf.message.Message) Configuration(javax.ws.rs.core.Configuration) ResourceInfoImpl(org.apache.cxf.jaxrs.impl.ResourceInfoImpl) ContextProvider(org.apache.cxf.jaxrs.ext.ContextProvider) Providers(javax.ws.rs.ext.Providers) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) SecurityContext(javax.ws.rs.core.SecurityContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) ResourceContextImpl(org.apache.cxf.jaxrs.impl.ResourceContextImpl) RequestImpl(org.apache.cxf.jaxrs.impl.RequestImpl) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Example 2 with ResourceContextImpl

use of org.apache.cxf.jaxrs.impl.ResourceContextImpl 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);
    cri.injectContexts(resourceObject, ori, inMessage);
    if (cri.isRoot()) {
        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);
            final Class<?> subResponseType;
            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);
            inMessage.put(URITemplate.URI_TEMPLATE, JAXRSUtils.getUriTemplate(inMessage, subCri, ori, subOri));
            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 3 with ResourceContextImpl

use of org.apache.cxf.jaxrs.impl.ResourceContextImpl in project cxf by apache.

the class JAXRSUtils method createContextValue.

public static <T> T createContextValue(Message m, Type genericType, Class<T> clazz) {
    Message contextMessage = getContextMessage(m);
    Object o = null;
    if (UriInfo.class.isAssignableFrom(clazz)) {
        o = createUriInfo(contextMessage);
    } else if (HttpHeaders.class.isAssignableFrom(clazz) || ProtocolHeaders.class.isAssignableFrom(clazz)) {
        o = createHttpHeaders(contextMessage, clazz);
    } else if (SecurityContext.class.isAssignableFrom(clazz)) {
        SecurityContext customContext = contextMessage.get(SecurityContext.class);
        o = customContext == null ? new SecurityContextImpl(contextMessage) : customContext;
    } else if (MessageContext.class.isAssignableFrom(clazz)) {
        o = new MessageContextImpl(m);
    } else if (ResourceInfo.class.isAssignableFrom(clazz)) {
        o = new ResourceInfoImpl(contextMessage);
    } else if (ResourceContext.class.isAssignableFrom(clazz)) {
        final OperationResourceInfo ori = contextMessage.getExchange().get(OperationResourceInfo.class);
        if (ori != null) {
            o = new ResourceContextImpl(contextMessage, ori);
        }
    } else if (Request.class.isAssignableFrom(clazz)) {
        o = new RequestImpl(contextMessage);
    } else if (Providers.class.isAssignableFrom(clazz)) {
        o = new ProvidersImpl(contextMessage);
    } else if (ContextResolver.class.isAssignableFrom(clazz)) {
        o = createContextResolver(genericType, contextMessage);
    } else if (Configuration.class.isAssignableFrom(clazz)) {
        o = ProviderFactory.getInstance(contextMessage).getConfiguration(contextMessage);
    } else if (Application.class.isAssignableFrom(clazz)) {
        ProviderInfo<?> providerInfo = (ProviderInfo<?>) contextMessage.getExchange().getEndpoint().get(Application.class.getName());
        o = providerInfo == null ? null : providerInfo.getProvider();
    } else if (contextMessage != null) {
        ContextProvider<?> provider = ProviderFactory.getInstance(contextMessage).createContextProvider(clazz, contextMessage);
        if (provider != null) {
            o = provider.createContext(contextMessage);
        }
    }
    if (o == null && contextMessage != null && !MessageUtils.isRequestor(contextMessage)) {
        o = HttpUtils.createServletResourceValue(contextMessage, clazz);
    }
    return clazz.cast(o);
}
Also used : SecurityContextImpl(org.apache.cxf.jaxrs.impl.SecurityContextImpl) ResourceContext(javax.ws.rs.container.ResourceContext) Message(org.apache.cxf.message.Message) Configuration(javax.ws.rs.core.Configuration) ResourceInfoImpl(org.apache.cxf.jaxrs.impl.ResourceInfoImpl) ContextProvider(org.apache.cxf.jaxrs.ext.ContextProvider) Providers(javax.ws.rs.ext.Providers) ProvidersImpl(org.apache.cxf.jaxrs.impl.ProvidersImpl) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) SecurityContext(javax.ws.rs.core.SecurityContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) ResourceContextImpl(org.apache.cxf.jaxrs.impl.ResourceContextImpl) RequestImpl(org.apache.cxf.jaxrs.impl.RequestImpl) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl)

Aggregations

ResourceContext (javax.ws.rs.container.ResourceContext)3 ResourceContextImpl (org.apache.cxf.jaxrs.impl.ResourceContextImpl)3 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)3 Message (org.apache.cxf.message.Message)3 Configuration (javax.ws.rs.core.Configuration)2 SecurityContext (javax.ws.rs.core.SecurityContext)2 Providers (javax.ws.rs.ext.Providers)2 ContextProvider (org.apache.cxf.jaxrs.ext.ContextProvider)2 MessageContext (org.apache.cxf.jaxrs.ext.MessageContext)2 MessageContextImpl (org.apache.cxf.jaxrs.ext.MessageContextImpl)2 ProvidersImpl (org.apache.cxf.jaxrs.impl.ProvidersImpl)2 RequestImpl (org.apache.cxf.jaxrs.impl.RequestImpl)2 ResourceInfoImpl (org.apache.cxf.jaxrs.impl.ResourceInfoImpl)2 SecurityContextImpl (org.apache.cxf.jaxrs.impl.SecurityContextImpl)2 ProviderInfo (org.apache.cxf.jaxrs.model.ProviderInfo)2 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 AsyncResponse (javax.ws.rs.container.AsyncResponse)1