Search in sources :

Example 91 with OperationResourceInfo

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

the class JAXRSServiceFactoryBeanTest method testSubResources.

@Test
public void testSubResources() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setEnableStaticResolution(true);
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.BookStore.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    assertEquals(1, resources.size());
    // Verify root ClassResourceInfo: BookStore
    ClassResourceInfo rootCri = resources.get(0);
    assertNotNull(rootCri.getURITemplate());
    URITemplate template = rootCri.getURITemplate();
    MultivaluedMap<String, String> values = new MetadataMap<>();
    assertTrue(template.match("/bookstore/books/123", values));
    assertTrue(rootCri.hasSubResources());
    MethodDispatcher md = rootCri.getMethodDispatcher();
    assertEquals(7, md.getOperationResourceInfos().size());
    for (OperationResourceInfo ori : md.getOperationResourceInfos()) {
        if ("getDescription".equals(ori.getMethodToInvoke().getName())) {
            assertEquals("GET", ori.getHttpMethod());
            assertEquals("/path", ori.getURITemplate().getValue());
            assertEquals("text/bar", ori.getProduceTypes().get(0).toString());
            assertEquals("text/foo", ori.getConsumeTypes().get(0).toString());
            assertFalse(ori.isSubResourceLocator());
        } else if ("getAuthor".equals(ori.getMethodToInvoke().getName())) {
            assertEquals("GET", ori.getHttpMethod());
            assertEquals("/path2", ori.getURITemplate().getValue());
            assertEquals("text/bar2", ori.getProduceTypes().get(0).toString());
            assertEquals("text/foo2", ori.getConsumeTypes().get(0).toString());
            assertFalse(ori.isSubResourceLocator());
        } else if ("getBook".equals(ori.getMethodToInvoke().getName())) {
            assertNull(ori.getHttpMethod());
            assertNotNull(ori.getURITemplate());
            assertTrue(ori.isSubResourceLocator());
        } else if ("getNewBook".equals(ori.getMethodToInvoke().getName())) {
            assertNull(ori.getHttpMethod());
            assertNotNull(ori.getURITemplate());
            assertTrue(ori.isSubResourceLocator());
        } else if ("addBook".equals(ori.getMethodToInvoke().getName())) {
            assertEquals("POST", ori.getHttpMethod());
            assertNotNull(ori.getURITemplate());
            assertFalse(ori.isSubResourceLocator());
        } else if ("updateBook".equals(ori.getMethodToInvoke().getName())) {
            assertEquals("PUT", ori.getHttpMethod());
            assertNotNull(ori.getURITemplate());
            assertFalse(ori.isSubResourceLocator());
        } else if ("deleteBook".equals(ori.getMethodToInvoke().getName())) {
            assertEquals("DELETE", ori.getHttpMethod());
            assertNotNull(ori.getURITemplate());
            assertFalse(ori.isSubResourceLocator());
        } else {
            fail("unexpected OperationResourceInfo" + ori.getMethodToInvoke().getName());
        }
    }
    // Verify sub-resource ClassResourceInfo: Book
    assertEquals(1, rootCri.getSubResources().size());
    ClassResourceInfo subCri = rootCri.getSubResources().iterator().next();
    assertNull(subCri.getURITemplate());
    assertEquals(org.apache.cxf.jaxrs.resources.Book.class, subCri.getResourceClass());
    MethodDispatcher subMd = subCri.getMethodDispatcher();
    assertEquals(2, subMd.getOperationResourceInfos().size());
    // getChapter method
    OperationResourceInfo subOri = subMd.getOperationResourceInfos().iterator().next();
    assertEquals("GET", subOri.getHttpMethod());
    assertNotNull(subOri.getURITemplate());
    // getState method
    OperationResourceInfo subOri2 = subMd.getOperationResourceInfos().iterator().next();
    assertEquals("GET", subOri2.getHttpMethod());
    assertNotNull(subOri2.getURITemplate());
}
Also used : ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) URITemplate(org.apache.cxf.jaxrs.model.URITemplate) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MethodDispatcher(org.apache.cxf.jaxrs.model.MethodDispatcher) Test(org.junit.Test)

Example 92 with OperationResourceInfo

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

the class SelectMethodCandidatesTest method testSelectUsingQualityFactors.

@Test
public void testSelectUsingQualityFactors() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResource.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "*/*";
    String acceptContentTypes = "application/xml;q=0.5,application/json";
    MetadataMap<String, String> values = new MetadataMap<>();
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), "/1/2/3/d/resource1", "GET", values, contentTypes, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("jsonResource needs to be selected", "jsonResource", ori.getMethodToInvoke().getName());
}
Also used : 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 93 with OperationResourceInfo

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

the class SelectMethodCandidatesTest method testFindFromAbstractGenericImpl4.

@Test
public void testFindFromAbstractGenericImpl4() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(GenericEntityImpl4.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "text/xml";
    String acceptContentTypes = "text/xml";
    Message m = new MessageImpl();
    m.put(Message.CONTENT_TYPE, "text/xml");
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(m);
    m.setExchange(ex);
    Endpoint e = mockEndpoint();
    ex.put(Endpoint.class, e);
    MetadataMap<String, String> values = new MetadataMap<>();
    OperationResourceInfo ori = findTargetResourceClass(resources, m, "/books", "POST", values, contentTypes, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", "postEntity", ori.getMethodToInvoke().getName());
    String value = "<Books><Book><name>The Book</name><id>2</id></Book></Books>";
    m.setContent(InputStream.class, new ByteArrayInputStream(value.getBytes()));
    List<Object> params = JAXRSUtils.processParameters(ori, values, m);
    assertEquals(1, params.size());
    List<?> books = (List<?>) params.get(0);
    assertEquals(1, books.size());
    Book book = (Book) books.get(0);
    assertNotNull(book);
    assertEquals(2L, book.getId());
    assertEquals("The Book", book.getName());
}
Also used : Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Exchange(org.apache.cxf.message.Exchange) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Endpoint(org.apache.cxf.endpoint.Endpoint) ByteArrayInputStream(java.io.ByteArrayInputStream) Book(org.apache.cxf.jaxrs.resources.Book) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 94 with OperationResourceInfo

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

the class SelectMethodCandidatesTest method doTestFindTargetSubResource.

public void doTestFindTargetSubResource(String path, String method, boolean setKeepSubProp) throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(org.apache.cxf.jaxrs.resources.TestResource.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "*/*";
    String acceptContentTypes = "text/xml,*/*";
    MetadataMap<String, String> values = new MetadataMap<>();
    OperationResourceInfo ori = findTargetResourceClass(resources, createMessage(), path, "GET", values, contentTypes, sortMediaTypes(acceptContentTypes), setKeepSubProp);
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", method, ori.getMethodToInvoke().getName());
}
Also used : MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo)

Example 95 with OperationResourceInfo

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

the class SelectMethodCandidatesTest method testFindFromAbstractGenericClass3.

@Test
public void testFindFromAbstractGenericClass3() throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(BookEntity.class);
    sf.create();
    List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
    String contentTypes = "text/xml";
    String acceptContentTypes = "text/xml";
    Message m = new MessageImpl();
    m.put(Message.CONTENT_TYPE, "text/xml");
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(m);
    m.setExchange(ex);
    Endpoint e = mockEndpoint();
    ex.put(Endpoint.class, e);
    MetadataMap<String, String> values = new MetadataMap<>();
    OperationResourceInfo ori = findTargetResourceClass(resources, m, "/books", "PUT", values, contentTypes, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", "putEntity", ori.getMethodToInvoke().getName());
    String value = "<Chapter><title>The Book</title><id>2</id></Chapter>";
    m.setContent(InputStream.class, new ByteArrayInputStream(value.getBytes()));
    List<Object> params = JAXRSUtils.processParameters(ori, values, m);
    assertEquals(1, params.size());
    Chapter c = (Chapter) params.get(0);
    assertNotNull(c);
    assertEquals(2L, c.getId());
    assertEquals("The Book", c.getTitle());
}
Also used : Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Chapter(org.apache.cxf.jaxrs.resources.Chapter) Exchange(org.apache.cxf.message.Exchange) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Endpoint(org.apache.cxf.endpoint.Endpoint) ByteArrayInputStream(java.io.ByteArrayInputStream) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Aggregations

OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)129 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)104 Message (org.apache.cxf.message.Message)72 Test (org.junit.Test)70 Method (java.lang.reflect.Method)52 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)40 Customer (org.apache.cxf.jaxrs.Customer)22 Endpoint (org.apache.cxf.endpoint.Endpoint)13 MessageImpl (org.apache.cxf.message.MessageImpl)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ArrayList (java.util.ArrayList)12 List (java.util.List)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 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 MethodDispatcher (org.apache.cxf.jaxrs.model.MethodDispatcher)7