Search in sources :

Example 21 with ResourceProvider

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

the class ResourceContextImpl method getResource.

@Override
public <T> T getResource(Class<T> cls) {
    final ResourceProvider rp;
    Object propValue = m.getContextualProperty(CONTEXT_PROVIDER_PROP);
    if (propValue instanceof ResourceContextProvider) {
        rp = ((ResourceContextProvider) propValue).getResourceProvider(cls);
    } else {
        rp = new PerRequestResourceProvider(cls);
    }
    T resource = cls.cast(rp.getInstance(m));
    return doInitResource(cls, resource);
}
Also used : PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) ResourceContextProvider(org.apache.cxf.jaxrs.ext.ResourceContextProvider) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider)

Example 22 with ResourceProvider

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

the class JAXRSInInterceptor method setExchangeProperties.

private void setExchangeProperties(Message message, Exchange exchange, OperationResourceInfo ori, MultivaluedMap<String, String> values, int numberOfResources) {
    final ClassResourceInfo cri = ori.getClassResourceInfo();
    exchange.put(OperationResourceInfo.class, ori);
    exchange.put(JAXRSUtils.ROOT_RESOURCE_CLASS, cri);
    message.put(RESOURCE_METHOD, ori.getMethodToInvoke());
    message.put(URITemplate.TEMPLATE_PARAMETERS, values);
    message.put(URITemplate.URI_TEMPLATE, JAXRSUtils.getUriTemplate(message, cri, ori));
    String plainOperationName = ori.getMethodToInvoke().getName();
    if (numberOfResources > 1) {
        plainOperationName = cri.getServiceClass().getSimpleName() + "#" + plainOperationName;
    }
    exchange.put(RESOURCE_OPERATION_NAME, plainOperationName);
    if (ori.isOneway() || PropertyUtils.isTrue(HttpUtils.getProtocolHeader(message, Message.ONE_WAY_REQUEST, null))) {
        exchange.setOneWay(true);
    }
    ResourceProvider rp = cri.getResourceProvider();
    if (rp instanceof SingletonResourceProvider) {
        // cri.isSingleton is not guaranteed to indicate we have a 'pure' singleton
        exchange.put(Message.SERVICE_OBJECT, rp.getInstance(message));
    }
}
Also used : SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)

Example 23 with ResourceProvider

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

the class CXFNonSpringJaxrsServlet method init.

@Override
public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);
    if (getApplication() != null) {
        createServerFromApplication(servletConfig);
        return;
    }
    String applicationClass = servletConfig.getInitParameter(JAXRS_APPLICATION_PARAM);
    if (applicationClass != null) {
        createServerFromApplication(applicationClass, servletConfig);
        return;
    }
    String splitChar = getParameterSplitChar(servletConfig);
    JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
    bean.setBus(getBus());
    String address = servletConfig.getInitParameter(SERVICE_ADDRESS_PARAM);
    if (address == null) {
        address = "/";
    }
    bean.setAddress(address);
    bean.setStaticSubresourceResolution(getStaticSubResolutionValue(servletConfig));
    String modelRef = servletConfig.getInitParameter(USER_MODEL_PARAM);
    if (modelRef != null) {
        bean.setModelRef(modelRef.trim());
    }
    setDocLocation(bean, servletConfig);
    setSchemasLocations(bean, servletConfig);
    setAllInterceptors(bean, servletConfig, splitChar);
    setInvoker(bean, servletConfig);
    Map<Class<?>, Map<String, List<String>>> resourceClasses = getServiceClasses(servletConfig, modelRef != null, splitChar);
    Map<Class<?>, ResourceProvider> resourceProviders = getResourceProviders(servletConfig, resourceClasses);
    List<?> providers = getProviders(servletConfig, splitChar);
    bean.setResourceClasses(new ArrayList<Class<?>>(resourceClasses.keySet()));
    bean.setProviders(providers);
    for (Map.Entry<Class<?>, ResourceProvider> entry : resourceProviders.entrySet()) {
        bean.setResourceProvider(entry.getKey(), entry.getValue());
    }
    setExtensions(bean, servletConfig);
    List<? extends Feature> features = getFeatures(servletConfig, splitChar);
    bean.getFeatures().addAll(features);
    bean.create();
}
Also used : PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with ResourceProvider

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

the class CdiResourceProviderTest method assertNotSingleton.

private void assertNotSingleton() {
    final ResourceProvider provider = new PerRequestResourceProvider(() -> new Lifecycle(beanManager, bean), Object.class);
    assertFalse(new JAXRSCdiResourceExtension().isCxfSingleton(beanManager, bean));
    assertFalse(provider.isSingleton());
    final MessageImpl message1 = new MessageImpl();
    final MessageImpl message2 = new MessageImpl();
    final Object instance = provider.getInstance(message1);
    assertNotNull(instance);
    assertNotEquals(provider.getInstance(message1), provider.getInstance(message2));
    // ensure we can close the lifecycle
    final Lifecycle lifecycle1 = message1.get(Lifecycle.class);
    assertNotNull(lifecycle1);
    assertNotNull(message2.get(Lifecycle.class));
}
Also used : ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) MessageImpl(org.apache.cxf.message.MessageImpl)

Example 25 with ResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project carbon-apimgt by wso2.

the class ValidationInInterceptor method handleMessage.

@MethodStats
public void handleMessage(Message message) {
    final OperationResourceInfo operationResource = message.getExchange().get(OperationResourceInfo.class);
    if (operationResource == null) {
        log.info("OperationResourceInfo is not available, skipping validation");
        return;
    }
    final ClassResourceInfo classResource = operationResource.getClassResourceInfo();
    if (classResource == null) {
        log.info("ClassResourceInfo is not available, skipping validation");
        return;
    }
    final ResourceProvider resourceProvider = classResource.getResourceProvider();
    if (resourceProvider == null) {
        log.info("ResourceProvider is not available, skipping validation");
        return;
    }
    final List<Object> arguments = MessageContentsList.getContentsList(message);
    final Method method = operationResource.getAnnotatedMethod();
    final Object instance = resourceProvider.getInstance(message);
    if (method != null && arguments != null) {
        // validate the parameters(arguments) over the invoked method
        validate(method, arguments.toArray(), instance);
        // validate the fields of each argument
        for (Object arg : arguments) {
            if (arg != null)
                validate(arg);
        }
    }
}
Also used : ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Method(java.lang.reflect.Method) MethodStats(org.wso2.carbon.apimgt.rest.api.util.MethodStats)

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