Search in sources :

Example 1 with PerRequestResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider 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<Class<?>>();
    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.setFeatures(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 2 with PerRequestResourceProvider

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

the class JAXRSUtilsTest method testPerRequestContextFields.

@Test
public void testPerRequestContextFields() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    cri.setResourceProvider(new PerRequestResourceProvider(Customer.class));
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("postConstruct", new Class[] {}), cri);
    Customer c = new Customer();
    Message m = createMessage();
    m.put(Message.PROTOCOL_HEADERS, new HashMap<String, List<String>>());
    HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, response);
    InjectionUtils.injectContextFields(c, ori.getClassResourceInfo(), m);
    assertSame(UriInfoImpl.class, c.getUriInfo2().getClass());
    assertSame(HttpHeadersImpl.class, c.getHeaders().getClass());
    assertSame(RequestImpl.class, c.getRequest().getClass());
    assertSame(SecurityContextImpl.class, c.getSecurityContext().getClass());
    assertSame(ProvidersImpl.class, c.getBodyWorkers().getClass());
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) HttpServletResponse(javax.servlet.http.HttpServletResponse) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) List(java.util.List) ArrayList(java.util.ArrayList) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) Test(org.junit.Test)

Example 3 with PerRequestResourceProvider

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

the class JAXRSUtilsTest method testContextResolverFields.

@Test
public void testContextResolverFields() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    cri.setResourceProvider(new PerRequestResourceProvider(Customer.class));
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("postConstruct", new Class[] {}), cri);
    Message m = createMessage();
    HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, response);
    Customer c = new Customer();
    ContextResolver<JAXBContext> cr = new JAXBContextProvider();
    ProviderFactory.getInstance(m).registerUserProvider(cr);
    m.put(Message.BASE_PATH, "/");
    InjectionUtils.injectContextFields(c, ori.getClassResourceInfo(), m);
    assertSame(cr.getClass(), c.getContextResolver().getClass());
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) HttpServletResponse(javax.servlet.http.HttpServletResponse) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) JAXBContext(javax.xml.bind.JAXBContext) JAXBContextProvider(org.apache.cxf.jaxrs.JAXBContextProvider) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) Test(org.junit.Test)

Example 4 with PerRequestResourceProvider

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

the class JAXRSUtilsTest method testServletResourceFields.

@Test
public void testServletResourceFields() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    cri.setResourceProvider(new PerRequestResourceProvider(Customer.class));
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethod("postConstruct", new Class[] {}), cri);
    Customer c = new Customer();
    // Creating mocks for the servlet request, response and context
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
    ServletContext context = EasyMock.createMock(ServletContext.class);
    EasyMock.replay(request);
    EasyMock.replay(response);
    EasyMock.replay(context);
    Message m = createMessage();
    m.put(AbstractHTTPDestination.HTTP_REQUEST, request);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, response);
    m.put(AbstractHTTPDestination.HTTP_CONTEXT, context);
    InjectionUtils.injectContextFields(c, ori.getClassResourceInfo(), m);
    assertSame(request.getClass(), ((HttpServletRequestFilter) c.getServletRequestResource()).getRequest().getClass());
    HttpServletResponseFilter filter = (HttpServletResponseFilter) c.getServletResponseResource();
    assertSame(response.getClass(), filter.getResponse().getClass());
    assertSame(context.getClass(), c.getServletContextResource().getClass());
    assertNotNull(c.getServletRequest());
    assertNotNull(c.getServletResponse());
    assertNotNull(c.getServletContext());
    assertNotNull(c.getServletRequestResource());
    assertNotNull(c.getServletResponseResource());
    assertNotNull(c.getServletContextResource());
    assertSame(request.getClass(), ((HttpServletRequestFilter) c.getServletRequestResource()).getRequest().getClass());
    filter = (HttpServletResponseFilter) c.getServletResponse();
    assertSame(response.getClass(), filter.getResponse().getClass());
    assertSame(context.getClass(), c.getServletContext().getClass());
}
Also used : ThreadLocalHttpServletRequest(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletContext(javax.servlet.ServletContext) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) HttpServletResponseFilter(org.apache.cxf.jaxrs.impl.HttpServletResponseFilter) HttpServletRequestFilter(org.apache.cxf.jaxrs.impl.HttpServletRequestFilter) Test(org.junit.Test)

Example 5 with PerRequestResourceProvider

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

the class ResourceContextImpl method getResource.

@Override
public <T> T getResource(Class<T> cls) {
    ResourceProvider rp = null;
    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)

Aggregations

PerRequestResourceProvider (org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Customer (org.apache.cxf.jaxrs.Customer)3 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)3 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)3 Message (org.apache.cxf.message.Message)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 ResourceProvider (org.apache.cxf.jaxrs.lifecycle.ResourceProvider)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ApplicationPath (javax.ws.rs.ApplicationPath)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 JAXBContext (javax.xml.bind.JAXBContext)1 Feature (org.apache.cxf.feature.Feature)1 JAXBContextProvider (org.apache.cxf.jaxrs.JAXBContextProvider)1 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)1