Search in sources :

Example 6 with JAXRSServiceImpl

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

the class JAXRSUtilsTest method testFindTargetResourceClass.

@Test
public void testFindTargetResourceClass() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "*/*";
    // If acceptContentTypes does not specify a specific Mime type, the
    // method is declared with a most specific ProduceMime type is selected.
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books/123/", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/json,application/xml;q=0.9"));
    assertNotNull(ori);
    assertEquals("getBookJSON", ori.getMethodToInvoke().getName());
    // test
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books/123", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/json"));
    assertNotNull(ori);
    assertEquals("getBookJSON", ori.getMethodToInvoke().getName());
    // test
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books/123", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
    assertNotNull(ori);
    assertEquals("getBook", ori.getMethodToInvoke().getName());
    // test
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books", "GET", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
    assertNotNull(ori);
    assertEquals("getBooks", ori.getMethodToInvoke().getName());
    // test find POST
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books", "POST", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
    assertNotNull(ori);
    assertEquals("addBook", ori.getMethodToInvoke().getName());
    // test find PUT
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books", "PUT", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
    assertEquals("updateBook", ori.getMethodToInvoke().getName());
    // test find DELETE
    ori = findTargetResourceClass(resources, createMessage(), "/bookstore/1/books/123", "DELETE", new MetadataMap<String, String>(), contentTypes, getTypes("application/xml"));
    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 7 with JAXRSServiceImpl

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

the class JAXRSUtilsTest method testFindTargetResourceClassWithTemplates.

@Test
public void testFindTargetResourceClassWithTemplates() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreTemplates.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "*/*";
    // If acceptContentTypes does not specify a specific Mime type, the
    // method is declared with a most specific ProduceMime type is selected.
    MetadataMap<String, String> values = new MetadataMap<>();
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), "/1/2/", "GET", values, contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("getBooks", ori.getMethodToInvoke().getName());
    assertEquals("Only id and final match groups should be there", 2, values.size());
    assertEquals("2 {id} values should've been picked up", 2, values.get("id").size());
    assertEquals("FINAL_MATCH_GROUP should've been picked up", 1, values.get(URITemplate.FINAL_MATCH_GROUP).size());
    assertEquals("First {id} is 1", "1", values.getFirst("id"));
    assertEquals("Second id is 2", "2", values.get("id").get(1));
    values = new MetadataMap<>();
    ori = findTargetResourceClass(resources, createMessage(), "/2", "POST", values, contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("updateBookStoreInfo", ori.getMethodToInvoke().getName());
    assertEquals("Only id and final match groups should be there", 2, values.size());
    assertEquals("Only single {id} should've been picked up", 1, values.get("id").size());
    assertEquals("FINAL_MATCH_GROUP should've been picked up", 1, values.get(URITemplate.FINAL_MATCH_GROUP).size());
    assertEquals("Only the first {id} should've been picked up", "2", values.getFirst("id"));
    values = new MetadataMap<>();
    ori = findTargetResourceClass(resources, createMessage(), "/3/4", "PUT", values, contentTypes, getTypes("*/*"));
    assertNotNull(ori);
    assertEquals("updateBook", ori.getMethodToInvoke().getName());
    assertEquals("Only the first {id} should've been picked up", 3, values.size());
    assertEquals("Only the first {id} should've been picked up", 1, values.get("id").size());
    assertEquals("Only the first {id} should've been picked up", 1, values.get("bookId").size());
    assertEquals("Only the first {id} should've been picked up", 1, values.get(URITemplate.FINAL_MATCH_GROUP).size());
    assertEquals("Only the first {id} should've been picked up", "3", values.getFirst("id"));
    assertEquals("Only the first {id} should've been picked up", "4", values.getFirst("bookId"));
}
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 8 with JAXRSServiceImpl

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

the class JAXRSUtilsTest method testSelectBetweenMultipleResourceClasses.

@Test
public void testSelectBetweenMultipleResourceClasses() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.class, org.apache.cxf.jaxrs.resources.BookStore.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    assertEquals(org.apache.cxf.jaxrs.resources.BookStore.class, firstResourceClass(resources, "/bookstore"));
    assertEquals(org.apache.cxf.jaxrs.resources.BookStore.class, firstResourceClass(resources, "/bookstore/"));
    assertEquals(org.apache.cxf.jaxrs.resources.BookStoreNoSubResource.class, firstResourceClass(resources, "/bookstore/bar"));
}
Also used : JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Test(org.junit.Test)

Example 9 with JAXRSServiceImpl

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

the class JAXRSUtilsTest method testSelectBetweenMultipleResourceClasses2.

@Test
public void testSelectBetweenMultipleResourceClasses2() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResourceTemplate1.class, org.apache.cxf.jaxrs.resources.TestResourceTemplate2.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    assertEquals(org.apache.cxf.jaxrs.resources.TestResourceTemplate1.class, firstResourceClass(resources, "/1"));
    assertEquals(org.apache.cxf.jaxrs.resources.TestResourceTemplate1.class, firstResourceClass(resources, "/1/"));
    assertEquals(org.apache.cxf.jaxrs.resources.TestResourceTemplate2.class, firstResourceClass(resources, "/1/foo"));
    assertEquals(org.apache.cxf.jaxrs.resources.TestResourceTemplate2.class, firstResourceClass(resources, "/1/foo/bar"));
}
Also used : JAXRSServiceFactoryBean(org.apache.cxf.jaxrs.JAXRSServiceFactoryBean) JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Test(org.junit.Test)

Example 10 with JAXRSServiceImpl

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

the class JAXRSClientFactoryBean method createWebClient.

/**
 * Creates a WebClient instance
 * @return WebClient instance
 */
public WebClient createWebClient() {
    String serviceAddress = getAddress();
    int queryIndex = serviceAddress != null ? serviceAddress.lastIndexOf('?') : -1;
    if (queryIndex != -1) {
        serviceAddress = serviceAddress.substring(0, queryIndex);
    }
    Service service = new JAXRSServiceImpl(serviceAddress, getServiceName());
    getServiceFactory().setService(service);
    try {
        Endpoint ep = createEndpoint();
        this.getServiceFactory().sendEvent(FactoryBeanListener.Event.PRE_CLIENT_CREATE, ep);
        ClientState actualState = getActualState();
        WebClient client = actualState == null ? new WebClient(getAddress(), getProperties()) : new WebClient(actualState);
        initClient(client, ep, actualState == null);
        notifyLifecycleManager(client);
        this.getServiceFactory().sendEvent(FactoryBeanListener.Event.CLIENT_CREATED, client, ep);
        return client;
    } catch (Exception ex) {
        LOG.severe(ex.getClass().getName() + " : " + ex.getLocalizedMessage());
        throw new RuntimeException(ex);
    }
}
Also used : JAXRSServiceImpl(org.apache.cxf.jaxrs.JAXRSServiceImpl) Endpoint(org.apache.cxf.endpoint.Endpoint) Service(org.apache.cxf.service.Service) Endpoint(org.apache.cxf.endpoint.Endpoint)

Aggregations

JAXRSServiceImpl (org.apache.cxf.jaxrs.JAXRSServiceImpl)12 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)9 JAXRSServiceFactoryBean (org.apache.cxf.jaxrs.JAXRSServiceFactoryBean)6 Test (org.junit.Test)6 Endpoint (org.apache.cxf.endpoint.Endpoint)4 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)4 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)4 EndpointImpl (org.apache.cxf.endpoint.EndpointImpl)2 Exchange (org.apache.cxf.message.Exchange)2 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)2 Message (org.apache.cxf.message.Message)2 MessageImpl (org.apache.cxf.message.MessageImpl)2 BindingInfo (org.apache.cxf.service.model.BindingInfo)2 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)2 ServletDestination (org.apache.cxf.transport.servlet.ServletDestination)2 InternalApplication (org.apache.openejb.server.rest.InternalApplication)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1