Search in sources :

Example 11 with Customer

use of org.apache.cxf.jaxrs.Customer in project cxf by apache.

the class ProviderFactoryTest method testCustomProviderSortingParamConverterProvider.

@Test
public void testCustomProviderSortingParamConverterProvider() {
    ParamConverterProvider h = new CustomerParameterHandler();
    ParamConverterProvider hp = new PriorityCustomerParameterHandler();
    ProviderFactory pf = ServerProviderFactory.getInstance();
    pf.setUserProviders(Arrays.asList(h, hp));
    Comparator<ProviderInfo<ParamConverterProvider>> comp = new Comparator<ProviderInfo<ParamConverterProvider>>() {

        @Override
        public int compare(ProviderInfo<ParamConverterProvider> o1, ProviderInfo<ParamConverterProvider> o2) {
            ParamConverterProvider provider1 = o1.getProvider();
            ParamConverterProvider provider2 = o2.getProvider();
            return provider1.getClass().getName().compareTo(provider2.getClass().getName());
        }
    };
    pf.setProviderComparator(comp);
    ParamConverter<Customer> h2 = pf.createParameterHandler(Customer.class, Customer.class, null, new MessageImpl());
    assertSame(h2, h);
}
Also used : PriorityCustomerParameterHandler(org.apache.cxf.jaxrs.PriorityCustomerParameterHandler) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) Customer(org.apache.cxf.jaxrs.Customer) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) MessageImpl(org.apache.cxf.message.MessageImpl) PriorityCustomerParameterHandler(org.apache.cxf.jaxrs.PriorityCustomerParameterHandler) CustomerParameterHandler(org.apache.cxf.jaxrs.CustomerParameterHandler) Comparator(java.util.Comparator) Test(org.junit.Test)

Example 12 with Customer

use of org.apache.cxf.jaxrs.Customer in project cxf by apache.

the class JAXRSUtilsTest method testInjectApplicationInSingleton.

@Test
public void testInjectApplicationInSingleton() throws Exception {
    CustomerApplication app = new CustomerApplication();
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    Customer customer = new Customer();
    sf.setServiceBeanObjects(customer);
    sf.setApplication(app);
    sf.setStart(false);
    Server server = sf.create();
    assertSame(app, customer.getApplication1());
    assertSame(app, customer.getApplication2());
    @SuppressWarnings("unchecked") ThreadLocalProxy<UriInfo> proxy = (ThreadLocalProxy<UriInfo>) app.getUriInfo();
    assertNotNull(proxy);
    invokeCustomerMethod(sf.getServiceFactory().getClassResourceInfo().get(0), customer, server);
    assertSame(app, customer.getApplication2());
    assertTrue(proxy.get() instanceof UriInfo);
}
Also used : ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) Server(org.apache.cxf.endpoint.Server) CustomerApplication(org.apache.cxf.jaxrs.CustomerApplication) Customer(org.apache.cxf.jaxrs.Customer) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) UriInfo(javax.ws.rs.core.UriInfo) ThreadLocalUriInfo(org.apache.cxf.jaxrs.impl.tl.ThreadLocalUriInfo) Test(org.junit.Test)

Example 13 with Customer

use of org.apache.cxf.jaxrs.Customer 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 14 with Customer

use of org.apache.cxf.jaxrs.Customer in project cxf by apache.

the class PerRequestResourceProviderTest method testGetInstance.

@Test
public void testGetInstance() {
    PerRequestResourceProvider rp = new PerRequestResourceProvider(Customer.class);
    Message message = createMessage();
    message.put(Message.QUERY_STRING, "a=aValue");
    Customer c = (Customer) rp.getInstance(message);
    assertNotNull(c.getUriInfo());
    assertEquals("aValue", c.getQueryParam());
    assertTrue(c.isPostConstuctCalled());
    rp.releaseInstance(message, c);
    assertTrue(c.isPreDestroyCalled());
}
Also used : Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) Test(org.junit.Test)

Example 15 with Customer

use of org.apache.cxf.jaxrs.Customer in project cxf by apache.

the class JAXRSUtilsTest method testInjectCustomContext.

@Test
public void testInjectCustomContext() throws Exception {
    final CustomerContext contextImpl = new CustomerContext() {

        public String get() {
            return "customerContext";
        }
    };
    JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
    Customer customer = new Customer();
    sf.setServiceBeanObjects(customer);
    sf.setProvider(new ContextProvider<CustomerContext>() {

        public CustomerContext createContext(Message message) {
            return contextImpl;
        }
    });
    sf.setStart(false);
    Server s = sf.create();
    assertTrue(customer.getCustomerContext() instanceof ThreadLocalProxy<?>);
    invokeCustomerMethod(sf.getServiceFactory().getClassResourceInfo().get(0), customer, s);
    CustomerContext context = customer.getCustomerContext();
    assertEquals("customerContext", context.get());
}
Also used : Message(org.apache.cxf.message.Message) Server(org.apache.cxf.endpoint.Server) Customer(org.apache.cxf.jaxrs.Customer) CustomerContext(org.apache.cxf.jaxrs.Customer.CustomerContext) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) Test(org.junit.Test)

Aggregations

Customer (org.apache.cxf.jaxrs.Customer)18 Test (org.junit.Test)17 Message (org.apache.cxf.message.Message)13 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)11 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ThreadLocalProxy (org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy)5 CustomerParameterHandler (org.apache.cxf.jaxrs.CustomerParameterHandler)4 MessageImpl (org.apache.cxf.message.MessageImpl)4 ServletContext (javax.servlet.ServletContext)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 ParamConverterProvider (javax.ws.rs.ext.ParamConverterProvider)3 Server (org.apache.cxf.endpoint.Server)3 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)3 PriorityCustomerParameterHandler (org.apache.cxf.jaxrs.PriorityCustomerParameterHandler)3 HttpServletResponseFilter (org.apache.cxf.jaxrs.impl.HttpServletResponseFilter)3 ThreadLocalHttpServletRequest (org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest)3 PerRequestResourceProvider (org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider)3 SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)3 ArrayList (java.util.ArrayList)2