Java Code Examples for org.apache.cxf.jaxrs.model.ClassResourceInfo#setResourceProvider()

The following examples show how to use org.apache.cxf.jaxrs.model.ClassResourceInfo#setResourceProvider() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JAXRSUtilsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@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());

}
 
Example 2
Source File: JAXRSUtilsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testContextAnnotationOnMethod() throws Exception {

    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    Customer c = new Customer();
    cri.setResourceProvider(new SingletonResourceProvider(c));
    InjectionUtils.injectContextProxies(cri, cri.getResourceProvider().getInstance(null));

    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethods()[0],
                                                          cri);
    Message message = createMessage();
    InjectionUtils.injectContextMethods(c, ori.getClassResourceInfo(), message);
    assertNotNull(c.getUriInfo());
    assertSame(ThreadLocalUriInfo.class, c.getUriInfo().getClass());
    assertSame(UriInfoImpl.class,
               ((ThreadLocalProxy<UriInfo>)c.getUriInfo()).get().getClass());
    assertSame(ThreadLocalServletConfig.class, c.getSuperServletConfig().getClass());
    assertSame(ThreadLocalHttpServletRequest.class, c.getHttpServletRequest().getClass());
}
 
Example 3
Source File: JAXRSUtilsTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 4
Source File: ClassResourceInfoProxyHelper.java    From HotswapAgent with GNU General Public License v2.0 6 votes vote down vote up
public static void reloadClassResourceInfo(ClassResourceInfo classResourceInfoProxy) {
    try {
        DISABLE_PROXY_GENERATION.set(true);
        CriProxyMethodHandler criMethodHandler = (CriProxyMethodHandler) ((ProxyObject)classResourceInfoProxy).getHandler();
        ClassResourceInfo newClassResourceInfo = (ClassResourceInfo) ReflectionHelper.invoke(null, ResourceUtils.class, "createClassResourceInfo",
                criMethodHandler.generatorTypes, criMethodHandler.generatorParams);
        ClassResourceInfo oldClassResourceInfo = criMethodHandler.delegate;
        ResourceProvider resourceProvider = oldClassResourceInfo.getResourceProvider();
        updateResourceProvider(resourceProvider);
        newClassResourceInfo.setResourceProvider(resourceProvider);
        criMethodHandler.delegate = newClassResourceInfo;
    } catch (Exception e) {
        LOGGER.error("reloadClassResourceInfo() exception {}", e.getMessage());
    } finally {
        DISABLE_PROXY_GENERATION.remove();
    }
}
 
Example 5
Source File: JAXRSServerFactoryBean.java    From cxf with Apache License 2.0 5 votes vote down vote up
protected void updateClassResourceProviders(Endpoint ep) {
    for (ClassResourceInfo cri : serviceFactory.getClassResourceInfo()) {
        if (cri.getResourceProvider() == null) {
            ResourceProvider rp = resourceProviders.get(cri.getResourceClass());
            if (rp != null) {
                cri.setResourceProvider(rp);
            } else {
                setDefaultResourceProvider(cri);
            }
        }
        if (cri.getResourceProvider() instanceof SingletonResourceProvider) {
            ((SingletonResourceProvider)cri.getResourceProvider()).init(ep);
        }
    }
}
 
Example 6
Source File: JAXRSUtilsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testSingletonHttpResourceFields() throws Exception {

    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    Customer c = new Customer();
    cri.setResourceProvider(new SingletonResourceProvider(c));

    Message m = createMessage();
    ServletContext servletContextMock = EasyMock.createNiceMock(ServletContext.class);
    m.put(AbstractHTTPDestination.HTTP_CONTEXT, servletContextMock);
    HttpServletRequest httpRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    m.put(AbstractHTTPDestination.HTTP_REQUEST, httpRequest);
    HttpServletResponse httpResponse = EasyMock.createMock(HttpServletResponse.class);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, httpResponse);
    InjectionUtils.injectContextProxies(cri, cri.getResourceProvider().getInstance(null));
    InjectionUtils.injectContextFields(c, cri, m);
    assertSame(servletContextMock,
               ((ThreadLocalProxy<ServletContext>)c.getServletContextResource()).get());
    HttpServletRequest currentReq =
        ((ThreadLocalProxy<HttpServletRequest>)c.getServletRequestResource()).get();
    assertSame(httpRequest,
               ((HttpServletRequestFilter)currentReq).getRequest());
    HttpServletResponseFilter filter = (
        HttpServletResponseFilter)((ThreadLocalProxy<HttpServletResponse>)c.getServletResponseResource())
            .get();
    assertSame(httpResponse, filter.getResponse());
}
 
Example 7
Source File: JAXRSServerFactoryBean.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected void setDefaultResourceProvider(ClassResourceInfo cri) {
    cri.setResourceProvider(new PerRequestResourceProvider(cri.getResourceClass()));
}
 
Example 8
Source File: JAXRSUtilsTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testSingletonContextFields() throws Exception {

    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    Customer c = new Customer();
    cri.setResourceProvider(new SingletonResourceProvider(c));

    Message m = createMessage();
    m.put(Message.PROTOCOL_HEADERS, new HashMap<String, List<String>>());
    ServletContext servletContextMock = EasyMock.createNiceMock(ServletContext.class);
    m.put(AbstractHTTPDestination.HTTP_CONTEXT, servletContextMock);
    HttpServletRequest httpRequest = EasyMock.createNiceMock(HttpServletRequest.class);
    m.put(AbstractHTTPDestination.HTTP_REQUEST, httpRequest);
    HttpServletResponse httpResponse = EasyMock.createMock(HttpServletResponse.class);
    m.put(AbstractHTTPDestination.HTTP_RESPONSE, httpResponse);

    InjectionUtils.injectContextProxies(cri, cri.getResourceProvider().getInstance(null));
    InjectionUtils.injectContextFields(c, cri, m);
    InjectionUtils.injectContextMethods(c, cri, m);
    assertSame(ThreadLocalUriInfo.class, c.getUriInfo2().getClass());
    assertSame(UriInfoImpl.class,
               ((ThreadLocalProxy<UriInfo>)c.getUriInfo2()).get().getClass());
    assertSame(HttpHeadersImpl.class,
               ((ThreadLocalProxy<HttpHeaders>)c.getHeaders()).get().getClass());
    assertSame(RequestImpl.class,
               ((ThreadLocalProxy<Request>)c.getRequest()).get().getClass());
    assertSame(ResourceInfoImpl.class,
               ((ThreadLocalProxy<ResourceInfo>)c.getResourceInfo()).get().getClass());
    assertSame(SecurityContextImpl.class,
               ((ThreadLocalProxy<SecurityContext>)c.getSecurityContext()).get().getClass());
    assertSame(ProvidersImpl.class,
               ((ThreadLocalProxy<Providers>)c.getBodyWorkers()).get().getClass());

    assertSame(servletContextMock,
               ((ThreadLocalProxy<ServletContext>)c.getThreadLocalServletContext()).get());
    assertSame(servletContextMock,
               ((ThreadLocalProxy<ServletContext>)c.getServletContext()).get());
    assertSame(servletContextMock,
               ((ThreadLocalProxy<ServletContext>)c.getSuperServletContext()).get());
    HttpServletRequest currentReq =
        ((ThreadLocalProxy<HttpServletRequest>)c.getServletRequest()).get();
    assertSame(httpRequest,
               ((HttpServletRequestFilter)currentReq).getRequest());
    HttpServletResponseFilter filter = (
        HttpServletResponseFilter)((ThreadLocalProxy<HttpServletResponse>)c.getServletResponse()).get();
    assertSame(httpResponse, filter.getResponse());
}
 
Example 9
Source File: JAXRSUtilsTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@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());
}