Search in sources :

Example 21 with ProviderInfo

use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.

the class ClientResponseFilterInterceptor method handleMessage.

public void handleMessage(Message inMessage) throws Fault {
    ClientProviderFactory pf = ClientProviderFactory.getInstance(inMessage);
    if (pf == null) {
        return;
    }
    List<ProviderInfo<ClientResponseFilter>> filters = pf.getClientResponseFilters();
    if (!filters.isEmpty()) {
        final ClientRequestContext reqContext = new ClientRequestContextImpl(inMessage.getExchange().getOutMessage(), true);
        final ResponseImpl response = (ResponseImpl) getResponse(inMessage);
        final ClientResponseContext respContext = new ClientResponseContextImpl(response, inMessage);
        for (ProviderInfo<ClientResponseFilter> filter : filters) {
            InjectionUtils.injectContexts(filter.getProvider(), filter, inMessage);
            try {
                filter.getProvider().filter(reqContext, respContext);
            } catch (RuntimeException | IOException ex) {
                // would be stuck waiting for the IN chain completion.
                if (!inMessage.getExchange().isOneWay()) {
                    synchronized (inMessage.getExchange()) {
                        inMessage.getExchange().put("IN_CHAIN_COMPLETE", Boolean.TRUE);
                    }
                }
                // a response (4.5.2 Client Runtime).
                throw new ResponseProcessingException(response, ex);
            }
        }
    }
}
Also used : ClientRequestContext(javax.ws.rs.client.ClientRequestContext) IOException(java.io.IOException) ClientProviderFactory(org.apache.cxf.jaxrs.client.ClientProviderFactory) ClientResponseContext(javax.ws.rs.client.ClientResponseContext) ResponseImpl(org.apache.cxf.jaxrs.impl.ResponseImpl) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ClientResponseFilter(javax.ws.rs.client.ClientResponseFilter) ResponseProcessingException(javax.ws.rs.client.ResponseProcessingException)

Example 22 with ProviderInfo

use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.

the class ServerProviderFactory method setProviders.

@SuppressWarnings("unchecked")
@Override
protected void setProviders(boolean custom, boolean busGlobal, Object... providers) {
    List<Object> allProviders = new LinkedList<>();
    for (Object p : providers) {
        if (p instanceof Feature) {
            FeatureContext featureContext = createServerFeatureContext();
            Feature feature = (Feature) p;
            injectApplicationIntoFeature(feature);
            feature.configure(featureContext);
            Configuration cfg = featureContext.getConfiguration();
            for (Object featureProvider : cfg.getInstances()) {
                Map<Class<?>, Integer> contracts = cfg.getContracts(featureProvider.getClass());
                if (contracts != null && !contracts.isEmpty()) {
                    Class<?> providerCls = ClassHelper.getRealClass(getBus(), featureProvider);
                    allProviders.add(new FilterProviderInfo<Object>(featureProvider.getClass(), providerCls, featureProvider, getBus(), getFilterNameBindings(getBus(), featureProvider), false, contracts));
                } else {
                    allProviders.add(featureProvider);
                }
            }
        } else {
            allProviders.add(p);
        }
    }
    List<ProviderInfo<ContainerRequestFilter>> postMatchRequestFilters = new LinkedList<>();
    List<ProviderInfo<ContainerResponseFilter>> postMatchResponseFilters = new LinkedList<>();
    List<ProviderInfo<? extends Object>> theProviders = prepareProviders(custom, busGlobal, allProviders.toArray(), application);
    super.setCommonProviders(theProviders, RuntimeType.SERVER);
    for (ProviderInfo<? extends Object> provider : theProviders) {
        Class<?> providerCls = ClassHelper.getRealClass(getBus(), provider.getProvider());
        // Check if provider is constrained to server
        if (!constrainedTo(providerCls, RuntimeType.SERVER)) {
            continue;
        }
        if (filterContractSupported(provider, providerCls, ContainerRequestFilter.class)) {
            addContainerRequestFilter(postMatchRequestFilters, (ProviderInfo<ContainerRequestFilter>) provider);
        }
        if (filterContractSupported(provider, providerCls, ContainerResponseFilter.class)) {
            postMatchResponseFilters.add((ProviderInfo<ContainerResponseFilter>) provider);
        }
        if (DynamicFeature.class.isAssignableFrom(providerCls)) {
            // TODO: review the possibility of DynamicFeatures needing to have Contexts injected
            Object feature = provider.getProvider();
            dynamicFeatures.add((DynamicFeature) feature);
        }
        if (filterContractSupported(provider, providerCls, ExceptionMapper.class)) {
            addProviderToList(exceptionMappers, provider);
        }
    }
    Collections.sort(preMatchContainerRequestFilters, new BindingPriorityComparator(ContainerRequestFilter.class, true));
    mapInterceptorFilters(postMatchContainerRequestFilters, postMatchRequestFilters, ContainerRequestFilter.class, true);
    mapInterceptorFilters(containerResponseFilters, postMatchResponseFilters, ContainerResponseFilter.class, false);
    injectContextProxies(exceptionMappers, postMatchContainerRequestFilters.values(), preMatchContainerRequestFilters, containerResponseFilters.values());
}
Also used : Configuration(javax.ws.rs.core.Configuration) DynamicFeature(javax.ws.rs.container.DynamicFeature) Feature(javax.ws.rs.core.Feature) LinkedList(java.util.LinkedList) FeatureContext(javax.ws.rs.core.FeatureContext) ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) FilterProviderInfo(org.apache.cxf.jaxrs.model.FilterProviderInfo) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter)

Example 23 with ProviderInfo

use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.

the class PerRequestResourceProvider method createInstance.

protected Object createInstance(Message m) {
    ProviderInfo<?> application = (ProviderInfo<?>) m.getExchange().getEndpoint().get(Application.class.getName());
    Map<Class<?>, Object> mapValues = CastUtils.cast(application == null ? null : Collections.singletonMap(Application.class, application.getProvider()));
    Object[] values = ResourceUtils.createConstructorArguments(c, m, true, mapValues, params, anns, genericTypes);
    try {
        Object instance = values.length > 0 ? c.newInstance(values) : c.newInstance(new Object[] {});
        InjectionUtils.invokeLifeCycleMethod(instance, postConstructMethod);
        return instance;
    } catch (InstantiationException ex) {
        String msg = "Resource class " + c.getDeclaringClass().getName() + " can not be instantiated";
        throw ExceptionUtils.toInternalServerErrorException(null, serverError(msg));
    } catch (IllegalAccessException ex) {
        String msg = "Resource class " + c.getDeclaringClass().getName() + " can not be instantiated" + " due to IllegalAccessException";
        throw ExceptionUtils.toInternalServerErrorException(null, serverError(msg));
    } catch (InvocationTargetException ex) {
        Response r = JAXRSUtils.convertFaultToResponse(ex.getCause(), m);
        if (r != null) {
            m.getExchange().put(Response.class, r);
            throw new WebApplicationException();
        }
        String msg = "Resource class " + c.getDeclaringClass().getName() + " can not be instantiated" + " due to InvocationTargetException";
        throw ExceptionUtils.toInternalServerErrorException(null, serverError(msg));
    }
}
Also used : Response(javax.ws.rs.core.Response) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) WebApplicationException(javax.ws.rs.WebApplicationException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 24 with ProviderInfo

use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.

the class JAXRSUtils method runContainerResponseFilters.

public static void runContainerResponseFilters(ServerProviderFactory pf, ResponseImpl r, Message m, OperationResourceInfo ori, Method invoked) throws IOException, Throwable {
    List<ProviderInfo<ContainerResponseFilter>> containerFilters = pf.getContainerResponseFilters(ori == null ? null : ori.getNameBindings());
    if (!containerFilters.isEmpty()) {
        ContainerRequestContext requestContext = new ContainerRequestContextImpl(m.getExchange().getInMessage(), false, true);
        ContainerResponseContext responseContext = new ContainerResponseContextImpl(r, m, ori == null ? null : ori.getClassResourceInfo().getServiceClass(), invoked);
        for (ProviderInfo<ContainerResponseFilter> filter : containerFilters) {
            InjectionUtils.injectContexts(filter.getProvider(), filter, m);
            filter.getProvider().filter(requestContext, responseContext);
        }
    }
}
Also used : ContainerResponseFilter(javax.ws.rs.container.ContainerResponseFilter) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) ContainerRequestContextImpl(org.apache.cxf.jaxrs.impl.ContainerRequestContextImpl) ContainerResponseContext(javax.ws.rs.container.ContainerResponseContext) ContainerResponseContextImpl(org.apache.cxf.jaxrs.impl.ContainerResponseContextImpl)

Example 25 with ProviderInfo

use of org.apache.cxf.jaxrs.model.ProviderInfo in project cxf by apache.

the class ProviderFactory method createProviderFromConstructor.

public static ProviderInfo<? extends Object> createProviderFromConstructor(Constructor<?> c, Map<Class<?>, Object> values, Bus theBus, boolean checkContexts, boolean custom) {
    Map<Class<?>, Map<Class<?>, ThreadLocalProxy<?>>> proxiesMap = CastUtils.cast((Map<?, ?>) theBus.getProperty(AbstractResourceInfo.CONSTRUCTOR_PROXY_MAP));
    Map<Class<?>, ThreadLocalProxy<?>> existingProxies = null;
    if (proxiesMap != null) {
        existingProxies = proxiesMap.get(c.getDeclaringClass());
    }
    Class<?>[] paramTypes = c.getParameterTypes();
    Object[] cArgs = ResourceUtils.createConstructorArguments(c, null, false, values);
    if (existingProxies != null && existingProxies.size() <= paramTypes.length) {
        for (int i = 0; i < paramTypes.length; i++) {
            if (cArgs[i] instanceof ThreadLocalProxy) {
                cArgs[i] = existingProxies.get(paramTypes[i]);
            }
        }
    }
    final Object instance;
    try {
        instance = c.newInstance(cArgs);
    } catch (Throwable ex) {
        throw new RuntimeException("Resource or provider class " + c.getDeclaringClass().getName() + " can not be instantiated", ex);
    }
    Map<Class<?>, ThreadLocalProxy<?>> proxies = new LinkedHashMap<>();
    for (int i = 0; i < paramTypes.length; i++) {
        if (cArgs[i] instanceof ThreadLocalProxy) {
            @SuppressWarnings("unchecked") ThreadLocalProxy<Object> proxy = (ThreadLocalProxy<Object>) cArgs[i];
            proxies.put(paramTypes[i], proxy);
        }
    }
    boolean isApplication = Application.class.isAssignableFrom(c.getDeclaringClass());
    if (isApplication) {
        return new ApplicationInfo((Application) instance, proxies, theBus);
    }
    return new ProviderInfo<Object>(instance, proxies, theBus, checkContexts, custom);
}
Also used : ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) ApplicationInfo(org.apache.cxf.jaxrs.model.ApplicationInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) LinkedHashMap(java.util.LinkedHashMap) FilterProviderInfo(org.apache.cxf.jaxrs.model.FilterProviderInfo) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) TreeMap(java.util.TreeMap)

Aggregations

ProviderInfo (org.apache.cxf.jaxrs.model.ProviderInfo)26 Endpoint (org.apache.cxf.endpoint.Endpoint)9 FilterProviderInfo (org.apache.cxf.jaxrs.model.FilterProviderInfo)7 Response (javax.ws.rs.core.Response)6 Message (org.apache.cxf.message.Message)5 Test (org.junit.Test)5 IOException (java.io.IOException)4 Comparator (java.util.Comparator)4 Map (java.util.Map)4 WriterInterceptor (javax.ws.rs.ext.WriterInterceptor)4 MessageImpl (org.apache.cxf.message.MessageImpl)4 List (java.util.List)3 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)3 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)3 MessageBodyReader (javax.ws.rs.ext.MessageBodyReader)3 ReaderInterceptor (javax.ws.rs.ext.ReaderInterceptor)3 Bus (org.apache.cxf.Bus)3 ContainerRequestContextImpl (org.apache.cxf.jaxrs.impl.ContainerRequestContextImpl)3 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)3 File (java.io.File)2