Search in sources :

Example 11 with ResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.

the class CdiResourceProviderTest method assertSingleton.

private void assertSingleton() {
    final ResourceProvider provider = new SingletonResourceProvider(new Lifecycle(beanManager, bean), Object.class);
    assertTrue(new JAXRSCdiResourceExtension().isCxfSingleton(beanManager, bean));
    assertTrue(provider.isSingleton());
    final Object instance = provider.getInstance(new MessageImpl());
    assertNotNull(instance);
    assertEquals(instance, provider.getInstance(new MessageImpl()));
}
Also used : ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) MessageImpl(org.apache.cxf.message.MessageImpl)

Example 12 with ResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.

the class JAXRSInvoker method getResourceProvider.

private ResourceProvider getResourceProvider(Exchange exchange) {
    Object provider = exchange.remove(JAXRSUtils.ROOT_PROVIDER);
    if (provider == null) {
        OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
        ClassResourceInfo cri = ori.getClassResourceInfo();
        return cri.getResourceProvider();
    }
    return (ResourceProvider) provider;
}
Also used : ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Example 13 with ResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.

the class JAXRSServerFactoryBean method setResourceProviders.

/**
 * Sets the list of providers managing the life-cycle of the resource classes
 *
 * @param rps resource providers
 */
public void setResourceProviders(List<ResourceProvider> rps) {
    for (ResourceProvider rp : rps) {
        Class<?> c = rp.getResourceClass();
        setServiceClass(c);
        resourceProviders.put(c, rp);
    }
}
Also used : PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider)

Example 14 with ResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.

the class ResourceUtils method createApplication.

@SuppressWarnings("unchecked")
public static JAXRSServerFactoryBean createApplication(Application app, boolean ignoreAppPath, boolean staticSubresourceResolution, boolean useSingletonResourceProvider, Bus bus) {
    Set<Object> singletons = app.getSingletons();
    verifySingletons(singletons);
    List<Class<?>> resourceClasses = new ArrayList<>();
    List<Object> providers = new ArrayList<>();
    List<Feature> features = new ArrayList<>();
    Map<Class<?>, ResourceProvider> map = new HashMap<>();
    // or singleton provider classes
    for (Class<?> cls : app.getClasses()) {
        if (isValidApplicationClass(cls, singletons)) {
            if (isValidProvider(cls)) {
                providers.add(createProviderInstance(cls));
            } else if (Feature.class.isAssignableFrom(cls)) {
                features.add(createFeatureInstance((Class<? extends Feature>) cls));
            } else {
                resourceClasses.add(cls);
                if (useSingletonResourceProvider) {
                    map.put(cls, new SingletonResourceProvider(createProviderInstance(cls)));
                } else {
                    map.put(cls, new PerRequestResourceProvider(cls));
                }
            }
        }
    }
    // we can get either a provider or resource class here
    for (Object o : singletons) {
        if (isValidProvider(o.getClass())) {
            providers.add(o);
        } else if (o instanceof Feature) {
            features.add((Feature) o);
        } else {
            resourceClasses.add(o.getClass());
            map.put(o.getClass(), new SingletonResourceProvider(o));
        }
    }
    JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
    if (bus != null) {
        bean.setBus(bus);
    }
    String address = "/";
    if (!ignoreAppPath) {
        ApplicationPath appPath = locateApplicationPath(app.getClass());
        if (appPath != null) {
            address = appPath.value();
        }
    }
    if (!address.startsWith("/")) {
        address = "/" + address;
    }
    bean.setAddress(address);
    bean.setStaticSubresourceResolution(staticSubresourceResolution);
    bean.setResourceClasses(resourceClasses);
    bean.setProviders(providers);
    bean.getFeatures().addAll(features);
    for (Map.Entry<Class<?>, ResourceProvider> entry : map.entrySet()) {
        bean.setResourceProvider(entry.getKey(), entry.getValue());
    }
    Map<String, Object> appProps = app.getProperties();
    if (appProps != null) {
        bean.getProperties(true).putAll(appProps);
    }
    bean.setApplication(app);
    return bean;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ApplicationPath(javax.ws.rs.ApplicationPath) Feature(org.apache.cxf.feature.Feature) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ElementClass(org.apache.cxf.jaxrs.ext.xml.ElementClass) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 15 with ResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project cxf by apache.

the class ValidationUtils method getResourceInstance.

public static Object getResourceInstance(Message message) {
    final OperationResourceInfo ori = message.getExchange().get(OperationResourceInfo.class);
    if (ori == null) {
        return null;
    }
    if (!ori.getClassResourceInfo().isRoot()) {
        return message.getExchange().get("org.apache.cxf.service.object.last");
    }
    final ResourceProvider resourceProvider = ori.getClassResourceInfo().getResourceProvider();
    if (!resourceProvider.isSingleton()) {
        String error = "Service object is not a singleton, use a custom invoker to validate";
        LOG.warning(error);
        return null;
    }
    return resourceProvider.getInstance(message);
}
Also used : ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Aggregations

ResourceProvider (org.apache.cxf.jaxrs.lifecycle.ResourceProvider)25 SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)14 PerRequestResourceProvider (org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider)8 ArrayList (java.util.ArrayList)7 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)6 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)5 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 DetectorResource (org.apache.tika.server.resource.DetectorResource)4 MetadataResource (org.apache.tika.server.resource.MetadataResource)4 RecursiveMetadataResource (org.apache.tika.server.resource.RecursiveMetadataResource)3 TikaResource (org.apache.tika.server.resource.TikaResource)3 UnpackerResource (org.apache.tika.server.resource.UnpackerResource)3 ApplicationPath (javax.ws.rs.ApplicationPath)2 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 Feature (org.apache.cxf.feature.Feature)2 ResourceContextProvider (org.apache.cxf.jaxrs.ext.ResourceContextProvider)2 ElementClass (org.apache.cxf.jaxrs.ext.xml.ElementClass)2 MessageImpl (org.apache.cxf.message.MessageImpl)2