Search in sources :

Example 11 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSInvoker method getResourceProvider.

private ResourceProvider getResourceProvider(Exchange exchange) {
    Object provider = exchange.remove(JAXRSUtils.ROOT_PROVIDER);
    if (provider == null) {
        OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
        ClassResourceInfo cri = ori.getClassResourceInfo();
        return cri.getResourceProvider();
    }
    return (ResourceProvider) provider;
}
Also used : ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Example 12 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo 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)

Example 13 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method testQueryParamAsListWithDefaultValue.

@SuppressWarnings("unchecked")
@Test
public void testQueryParamAsListWithDefaultValue() throws Exception {
    Class<?>[] argType = { List.class, List.class, List.class, Integer[].class, List.class, List.class, List.class, List.class, List.class };
    Method m = Customer.class.getMethod("testQueryAsList", argType);
    Message messageImpl = createMessage();
    ProviderFactory.getInstance(messageImpl).registerUserProvider(new MyTypeParamConverterProvider());
    messageImpl.put(Message.QUERY_STRING, "query2=query2Value&query2=query2Value2&query3=1&query3=2&query4=");
    List<Object> params = JAXRSUtils.processParameters(new OperationResourceInfo(m, new ClassResourceInfo(Customer.class)), null, messageImpl);
    assertEquals(9, params.size());
    List<String> queryList = (List<String>) params.get(0);
    assertNotNull(queryList);
    assertEquals(1, queryList.size());
    assertEquals("default", queryList.get(0));
    List<String> queryList2 = (List<String>) params.get(1);
    assertNotNull(queryList2);
    assertEquals(2, queryList2.size());
    assertEquals("query2Value", queryList2.get(0));
    assertEquals("query2Value2", queryList2.get(1));
    List<Integer> queryList3 = (List<Integer>) params.get(2);
    assertNotNull(queryList3);
    assertEquals(2, queryList3.size());
    assertEquals(Integer.valueOf(1), queryList3.get(0));
    assertEquals(Integer.valueOf(2), queryList3.get(1));
    Integer[] queryList3Array = (Integer[]) params.get(3);
    assertNotNull(queryList3Array);
    assertEquals(2, queryList3Array.length);
    assertEquals(Integer.valueOf(1), queryList3Array[0]);
    assertEquals(Integer.valueOf(2), queryList3Array[1]);
    List<String> queryList4 = (List<String>) params.get(4);
    assertNotNull(queryList4);
    assertEquals(1, queryList4.size());
    assertEquals("", queryList4.get(0));
    List<String> queryList5 = (List<String>) params.get(5);
    assertNotNull(queryList5);
    assertEquals(0, queryList5.size());
    List<MyType<Integer>> queryList6 = (List<MyType<Integer>>) params.get(6);
    assertNotNull(queryList6);
    assertEquals(2, queryList6.size());
    assertEquals(Integer.valueOf(1), queryList6.get(0).get());
    assertEquals(Integer.valueOf(2), queryList6.get(1).get());
    List<Long> queryList7 = (List<Long>) params.get(7);
    assertNotNull(queryList7);
    assertEquals(2, queryList7.size());
    assertEquals(1L, queryList7.get(0).longValue());
    assertEquals(2L, queryList7.get(1).longValue());
    List<Double> queryList8 = (List<Double>) params.get(8);
    assertNotNull(queryList8);
    assertEquals(2, queryList8.size());
    assertEquals(1., queryList8.get(0), 0.);
    assertEquals(2., queryList8.get(1), 0.);
}
Also used : Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Method(java.lang.reflect.Method) MyType(org.apache.cxf.jaxrs.Customer.MyType) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 14 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo in project cxf by apache.

the class JAXRSUtilsTest method testFindTargetResourceClassWithSubResource.

@Test
public void testFindTargetResourceClassWithSubResource() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStore.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "*/*";
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), "/bookstore/books/sub/123", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("getBook", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/books/123/true/chapter/1", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("getNewBook", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/books", "POST", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("addBook", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/books", "PUT", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("updateBook", ori.getMethodToInvoke().getName());
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/books/123", "DELETE", new MetadataMap<String, String>(), contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("deleteBook", ori.getMethodToInvoke().getName());
}
Also used : JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) Test(org.junit.Test)

Example 15 with OperationResourceInfo

use of org.apache.cxf.jaxrs.model.OperationResourceInfo 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)

Aggregations

OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)130 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)105 Message (org.apache.cxf.message.Message)72 Test (org.junit.Test)70 Method (java.lang.reflect.Method)53 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)40 Customer (org.apache.cxf.jaxrs.Customer)22 ArrayList (java.util.ArrayList)13 List (java.util.List)13 Endpoint (org.apache.cxf.endpoint.Endpoint)13 MessageImpl (org.apache.cxf.message.MessageImpl)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)11 Response (javax.ws.rs.core.Response)11 URITemplate (org.apache.cxf.jaxrs.model.URITemplate)11 Exchange (org.apache.cxf.message.Exchange)11 MediaType (javax.ws.rs.core.MediaType)10 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)10 MethodDispatcher (org.apache.cxf.jaxrs.model.MethodDispatcher)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7