Search in sources :

Example 1 with Customer

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

the class JAXRSUtilsTest method invokeCustomerMethod.

private void invokeCustomerMethod(ClassResourceInfo cri, Customer customer, Server server) throws Exception {
    OperationResourceInfo ori = cri.getMethodDispatcher().getOperationResourceInfo(Customer.class.getMethod("test", new Class[] {}));
    JAXRSInvoker invoker = new JAXRSInvoker();
    Exchange exc = new ExchangeImpl();
    exc.put(Endpoint.class, server.getEndpoint());
    Message inMessage = new MessageImpl();
    exc.setInMessage(inMessage);
    exc.put(OperationResourceInfo.class, ori);
    invoker.invoke(exc, Collections.emptyList(), customer);
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageImpl(org.apache.cxf.message.MessageImpl) JAXRSInvoker(org.apache.cxf.jaxrs.JAXRSInvoker) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 2 with Customer

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

the class JAXRSUtilsTest method testCustomerParameter.

@Test
public void testCustomerParameter() throws Exception {
    Message messageImpl = createMessage();
    ServerProviderFactory.getInstance(messageImpl).registerUserProvider(new CustomerParameterHandler());
    Class<?>[] argType = { Customer.class, Customer[].class, Customer2.class };
    Method m = Customer.class.getMethod("testCustomerParam", argType);
    messageImpl.put(Message.QUERY_STRING, "p1=Fred&p2=Barry&p3=Jack&p4=John");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(3, params.size());
    Customer c = (Customer) params.get(0);
    assertEquals("Fred", c.getName());
    Customer c2 = ((Customer[]) params.get(1))[0];
    assertEquals("Barry", c2.getName());
    Customer2 c3 = (Customer2) params.get(2);
    assertEquals("Jack", c3.getName());
    try {
        messageImpl.put(Message.QUERY_STRING, "p3=noName");
        JAXRSUtils.processParameters(new OperationResourceInfo(m, null), null, messageImpl);
        fail("Customer2 constructor does not accept names starting with lower-case chars");
    } catch (Exception ex) {
    // expected
    }
}
Also used : Message(org.apache.cxf.message.Message) Customer2(org.apache.cxf.jaxrs.Customer2) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Method(java.lang.reflect.Method) WebApplicationException(javax.ws.rs.WebApplicationException) CustomerParameterHandler(org.apache.cxf.jaxrs.CustomerParameterHandler) Test(org.junit.Test)

Example 3 with Customer

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

the class ProviderFactoryTest method testParameterHandlerProviderWithPriority.

@Test
public void testParameterHandlerProviderWithPriority() throws Exception {
    ProviderFactory pf = ServerProviderFactory.getInstance();
    ParamConverterProvider h = new CustomerParameterHandler();
    ParamConverterProvider hp = new PriorityCustomerParameterHandler();
    pf.registerUserProvider(h);
    pf.registerUserProvider(hp);
    ParamConverter<Customer> h2 = pf.createParameterHandler(Customer.class, Customer.class, null, new MessageImpl());
    assertSame(h2, hp);
}
Also used : PriorityCustomerParameterHandler(org.apache.cxf.jaxrs.PriorityCustomerParameterHandler) 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) Test(org.junit.Test)

Example 4 with Customer

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

the class JAXRSUtilsTest method testSingletonContextFields.

@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());
}
Also used : ThreadLocalHttpServletRequest(org.apache.cxf.jaxrs.impl.tl.ThreadLocalHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ThreadLocalProxy(org.apache.cxf.jaxrs.impl.tl.ThreadLocalProxy) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) List(java.util.List) ArrayList(java.util.ArrayList) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) HttpServletResponseFilter(org.apache.cxf.jaxrs.impl.HttpServletResponseFilter) Test(org.junit.Test)

Example 5 with Customer

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

the class JAXRSUtilsTest method testParamAnnotationOnMethod.

@Test
public void testParamAnnotationOnMethod() throws Exception {
    ClassResourceInfo cri = new ClassResourceInfo(Customer.class, true);
    Customer c = new Customer();
    OperationResourceInfo ori = new OperationResourceInfo(Customer.class.getMethods()[0], cri);
    Message m = createMessage();
    MultivaluedMap<String, String> headers = new MetadataMap<>();
    headers.add("AHeader2", "theAHeader2");
    m.put(Message.PROTOCOL_HEADERS, headers);
    m.put(Message.QUERY_STRING, "a_value=aValue&query2=b");
    JAXRSUtils.injectParameters(ori, c, m);
    assertEquals("aValue", c.getQueryParam());
    assertEquals("theAHeader2", c.getAHeader2());
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Message(org.apache.cxf.message.Message) Customer(org.apache.cxf.jaxrs.Customer) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) 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