Java Code Examples for org.apache.cxf.jaxrs.utils.ResourceUtils#createClassResourceInfo()

The following examples show how to use org.apache.cxf.jaxrs.utils.ResourceUtils#createClassResourceInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WadlGeneratorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleRootResource() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    wg.setApplicationTitle("My Application");
    wg.setNamespacePrefix("ns");
    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    Message m = mockMessage("http://localhost:8080/baz", "/bookstore/1", WadlGenerator.WADL_QUERY, cri);
    Response r = handleRequest(wg, m);
    checkResponse(r);
    Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
    checkDocs(doc.getDocumentElement(), "My Application", "", "");
    checkGrammars(doc.getDocumentElement(), "thebook", "books", "thebook2s", "thebook2", "thechapter");
    List<Element> els = getWadlResourcesInfo(doc, "http://localhost:8080/baz", 1);
    checkBookStoreInfo(els.get(0),
                       "ns1:thebook",
                       "ns1:thebook2",
                       "ns1:thechapter",
                       "ns1:books");
}
 
Example 2
Source File: WadlGeneratorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleRootResourceNoPrefixIncrement() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    wg.setApplicationTitle("My Application");
    wg.setNamespacePrefix("ns");
    wg.setIncrementNamespacePrefix(false);
    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    Message m = mockMessage("http://localhost:8080/baz", "/bookstore/1", WadlGenerator.WADL_QUERY, cri);
    Response r = handleRequest(wg, m);
    checkResponse(r);
    Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
    checkDocs(doc.getDocumentElement(), "My Application", "", "");
    checkGrammars(doc.getDocumentElement(), "thebook", "books", "thebook2s", "thebook2", "thechapter");
    List<Element> els = getWadlResourcesInfo(doc, "http://localhost:8080/baz", 1);
    checkBookStoreInfo(els.get(0),
                       "ns:thebook",
                       "ns:thebook2",
                       "ns:thechapter",
                       "ns:books");
}
 
Example 3
Source File: WadlGeneratorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testExternalRelativeSchemaJaxbContextPrefixes() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    wg.setExternalLinks(Collections.singletonList("books.xsd"));

    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    Message m = mockMessage("http://localhost:8080/baz", "/bookstore/1", WadlGenerator.WADL_QUERY, cri);
    Response r = handleRequest(wg, m);
    checkResponse(r);
    Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
    checkGrammarsWithLinks(doc.getDocumentElement(),
                           Collections.singletonList("http://localhost:8080/baz/books.xsd"));
    List<Element> els = getWadlResourcesInfo(doc, "http://localhost:8080/baz", 1);
    checkBookStoreInfo(els.get(0), "prefix1:thebook", "prefix1:thebook2", "prefix1:thechapter");
}
 
Example 4
Source File: WadlGeneratorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCustomSchemaAndSchemaPrefixes() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    wg.setSchemaLocations(Collections.singletonList("classpath:/book2.xsd"));
    wg.setUseJaxbContextForQnames(false);

    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    Message m = mockMessage("http://localhost:8080/baz", "/bookstore/1", WadlGenerator.WADL_QUERY, cri);
    Response r = handleRequest(wg, m);
    checkResponse(r);
    Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
    checkGrammars(doc.getDocumentElement(), "book", "book2", "chapter");
    List<Element> els = getWadlResourcesInfo(doc, "http://localhost:8080/baz", 1);
    checkBookStoreInfo(els.get(0), "prefix1:book", "prefix1:book2", "prefix1:chapter");
}
 
Example 5
Source File: WadlGeneratorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleRootResources() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    wg.setDefaultMediaType(WadlGenerator.WADL_TYPE.toString());
    ClassResourceInfo cri1 =
        ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    ClassResourceInfo cri2 =
        ResourceUtils.createClassResourceInfo(Orders.class, Orders.class, true, true);
    List<ClassResourceInfo> cris = new ArrayList<>();
    cris.add(cri1);
    cris.add(cri2);
    Message m = mockMessage("http://localhost:8080/baz", "", WadlGenerator.WADL_QUERY, cris);
    Response r = handleRequest(wg, m);
    assertEquals(WadlGenerator.WADL_TYPE.toString(),
                 r.getMetadata().getFirst(HttpHeaders.CONTENT_TYPE).toString());
    String wadl = r.getEntity().toString();
    Document doc = StaxUtils.read(new StringReader(wadl));
    checkGrammars(doc.getDocumentElement(), "thebook", "books", "thebook2s", "thebook2", "thechapter");
    List<Element> els = getWadlResourcesInfo(doc, "http://localhost:8080/baz", 2);
    checkBookStoreInfo(els.get(0), "prefix1:thebook", "prefix1:thebook2", "prefix1:thechapter");
    Element orderResource = els.get(1);
    assertEquals("/orders", orderResource.getAttribute("path"));
}
 
Example 6
Source File: WadlGeneratorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testRootResourceWithSingleSlash() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookStoreWithSingleSlash.class,
                                              BookStoreWithSingleSlash.class, true, true);
    Message m = mockMessage("http://localhost:8080/baz", "/", WadlGenerator.WADL_QUERY, cri);
    Response r = handleRequest(wg, m);
    checkResponse(r);
    Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
    List<Element> rootEls = getWadlResourcesInfo(doc, "http://localhost:8080/baz", 1);
    assertEquals(1, rootEls.size());
    Element resource = rootEls.get(0);
    assertEquals("/", resource.getAttribute("path"));
    List<Element> resourceEls = DOMUtils.getChildrenWithName(resource,
                                                             WadlGenerator.WADL_NS, "resource");
    assertEquals(1, resourceEls.size());
    assertEquals("book", resourceEls.get(0).getAttribute("path"));

    verifyParameters(resourceEls.get(0), 1, new Param("id", "template", "xs:int"));

    checkGrammars(doc.getDocumentElement(), "thebook", null, "thechapter");
}
 
Example 7
Source File: ClassResourceInfoTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubresourceInheritProduces() {
    ClassResourceInfo c = ResourceUtils.createClassResourceInfo(
                              TestClass2.class, TestClass2.class, true, true);
    assertEquals("test/bar", c.getProduceMime().get(0).toString());
    ClassResourceInfo sub = c.getSubResource(TestClass2.class, TestClass3.class);
    assertNotNull(sub);
    assertEquals("test/bar", sub.getProduceMime().get(0).toString());
    sub = c.getSubResource(TestClass2.class, TestClass2.class);
    assertNotNull(sub);
    assertEquals("test/bar", sub.getProduceMime().get(0).toString());
}
 
Example 8
Source File: JavaDocProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void doTestJavaDocs(String path, String version) throws Exception {
    JavaDocProvider p = new JavaDocProvider(path);
    p.setJavaDocsBuiltByVersion(version);
    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(PetStore.class, PetStore.class, true, true);
    String classDoc = p.getClassDoc(cri);
    assertEquals("The Pet Store", classDoc);

    boolean getStatus1Tested = false;
    boolean getStatus2Tested = false;
    boolean getStatus3Tested = false;
    boolean noDocsTested = false;
    for (OperationResourceInfo ori : cri.getMethodDispatcher().getOperationResourceInfos()) {
        if ("getStatus1Param".equals(ori.getMethodToInvoke().getName())) {
            testGetStatus1JavaDocs(p, ori);
            getStatus1Tested = true;
        } else if ("getStatus2Params".equals(ori.getMethodToInvoke().getName())) {
            testGetStatus2JavaDocs(p, ori);
            getStatus2Tested = true;
        } else if ("getStatus3Params".equals(ori.getMethodToInvoke().getName())) {
            testGetStatus3JavaDocs(p, ori);
            getStatus3Tested = true;
        } else if ("getBaseStatus".equals(ori.getMethodToInvoke().getName())) {
            testOperWithNoJavaDocs(p, ori);
            noDocsTested = true;
        }
    }
    assertTrue(getStatus1Tested);
    assertTrue(getStatus2Tested);
    assertTrue(getStatus3Tested);
    assertTrue(noDocsTested);
}
 
Example 9
Source File: JAXBElementProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleJAXBContext() throws Exception {
    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(JAXBResource.class, JAXBResource.class, true, true);
    JAXBElementProvider<Book> provider = new JAXBElementProvider<>();
    provider.setSingleJaxbContext(true);
    provider.init(Collections.singletonList(cri));
    JAXBContext bookContext = provider.getJAXBContext(Book.class, Book.class);
    assertNotNull(bookContext);
    JAXBContext superBookContext = provider.getJAXBContext(SuperBook.class, SuperBook.class);
    assertSame(bookContext, superBookContext);
    JAXBContext book2Context = provider.getJAXBContext(Book2.class, Book2.class);
    assertSame(bookContext, book2Context);
}
 
Example 10
Source File: WadlGeneratorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwoSchemasSameNs() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    wg.setApplicationTitle("My Application");
    wg.setNamespacePrefix("ns");
    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(TestResource.class, TestResource.class, true, true);
    Message m = mockMessage("http://localhost:8080/baz", "/", WadlGenerator.WADL_QUERY, cri);
    Response r = handleRequest(wg, m);
    checkResponse(r);
    Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
    checkDocs(doc.getDocumentElement(), "My Application", "", "");
    List<Element> grammarEls = DOMUtils.getChildrenWithName(doc.getDocumentElement(),
                                                            WadlGenerator.WADL_NS,
                                                            "grammars");
    assertEquals(1, grammarEls.size());
    List<Element> schemasEls = DOMUtils.getChildrenWithName(grammarEls.get(0),
                                                            Constants.URI_2001_SCHEMA_XSD,
                                                            "schema");
    assertEquals(2, schemasEls.size());
    assertEquals("http://example.com/test", schemasEls.get(0).getAttribute("targetNamespace"));
    assertEquals("http://example.com/test", schemasEls.get(1).getAttribute("targetNamespace"));
    List<Element> reps = DOMUtils.findAllElementsByTagNameNS(doc.getDocumentElement(),
                                   WadlGenerator.WADL_NS, "representation");
    assertEquals(2, reps.size());
    assertEquals("ns1:testCompositeObject", reps.get(0).getAttribute("element"));
    assertEquals("ns1:testCompositeObject", reps.get(1).getAttribute("element"));
}
 
Example 11
Source File: JAXBElementProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtraClassWithoutSingleContext() throws Exception {
    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    JAXBElementProvider<SuperBook> provider = new JAXBElementProvider<>();
    provider.setExtraClass(new Class[]{SuperBook.class});
    provider.init(Collections.singletonList(cri));
    JAXBContext bookContext = provider.getJAXBContext(Book.class, Book.class);

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    bookContext.createMarshaller().marshal(new SuperBook("name", 1L, 2L), os);
    SuperBook book = (SuperBook)bookContext.createUnmarshaller()
                         .unmarshal(new ByteArrayInputStream(os.toByteArray()));
    assertEquals(2L, book.getSuperId());
}
 
Example 12
Source File: JAXBElementProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testExtraClassWithGenericsAndSingleContext() throws Exception {
    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(XmlListResource.class, XmlListResource.class, true, true);
    JAXBElementProvider<?> provider = new JAXBElementProvider<>();
    provider.setSingleJaxbContext(true);
    provider.setExtraClass(new Class[]{XmlObject.class});
    provider.init(Collections.singletonList(cri));
    testXmlList(provider);

}
 
Example 13
Source File: JAXBElementProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericsAndSingleContext() throws Exception {
    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(XmlListResource.class, XmlListResource.class, true, true);
    JAXBElementProvider<?> provider = new JAXBElementProvider<>();
    provider.setSingleJaxbContext(true);
    provider.init(Collections.singletonList(cri));
    testXmlList(provider);
}
 
Example 14
Source File: WadlGeneratorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenericImplementation() throws Exception {
    setUpGenericImplementationTest();

    WadlGenerator wg = new WadlGenerator();
    wg.setApplicationTitle("My Application");
    wg.setNamespacePrefix("ns");
    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(ActualResource.class, ActualResource.class, true, true);
    Message m = mockMessage("http://example.com", "/", WadlGenerator.WADL_QUERY, cri);
    Response r = handleRequest(wg, m);
    checkResponse(r);
    Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
    checkDocs(doc.getDocumentElement(), "My Application", "", "");
    List<Element> grammarEls = DOMUtils.getChildrenWithName(doc.getDocumentElement(),
                                                            WadlGenerator.WADL_NS,
                                                            "grammars");
    assertEquals(1, grammarEls.size());
    List<Element> schemasEls = DOMUtils.getChildrenWithName(grammarEls.get(0),
                                                            Constants.URI_2001_SCHEMA_XSD,
                                                            "schema");
    assertEquals(2, schemasEls.size());
    
    List<Element> importEls = DOMUtils.getChildrenWithName(schemasEls.get(0),
                                                           Constants.URI_2001_SCHEMA_XSD,
                                                           "import");
    int schemaElementsIndex = !importEls.isEmpty() ? 0 : 1;
    int schemaTypesIndex = schemaElementsIndex == 0 ? 1 : 0;
    
    checkGenericImplSchemaWithTypes(schemasEls.get(schemaTypesIndex));
    checkGenericImplSchemaWithElements(schemasEls.get(schemaElementsIndex));

    List<Element> reps = DOMUtils.findAllElementsByTagNameNS(doc.getDocumentElement(),
                                   WadlGenerator.WADL_NS, "representation");
    assertEquals(2, reps.size());
    assertEquals("ns1:actual", reps.get(0).getAttribute("element"));
    assertEquals("ns1:actual", reps.get(1).getAttribute("element"));

}
 
Example 15
Source File: JAXBElementProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetSchemasFromAnnotation() {
    JAXBElementProvider<?> provider = new JAXBElementProvider<>();
    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(JAXBResource.class, JAXBResource.class, true, true);
    provider.init(Collections.singletonList(cri));
    Schema s = provider.getSchema();
    assertNotNull("schema can not be read from classpath", s);
}
 
Example 16
Source File: WadlGeneratorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testWhiteList() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    List<String> whiteList = new ArrayList<>();
    whiteList.add("123.123.123.123");
    wg.setWhiteList(whiteList);
    wg.setExternalLinks(Collections.singletonList("http://books.xsd"));

    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    Message m = mockMessage("http://localhost:8080/baz", "/bookstore/1", WadlGenerator.WADL_QUERY, cri);
    Response response = handleRequest(wg, m);
    assertEquals(response.getStatus(), 404);
}
 
Example 17
Source File: WadlGeneratorJsonTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testWadlInJsonFormat() throws Exception {
    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookChapters.class, BookChapters.class, true, true);
    final Message m = mockMessage("http://localhost:8080/baz", "/bookstore/1", WadlGenerator.WADL_QUERY, cri);
    Map<String, List<String>> headers = new HashMap<>();
    headers.put("Accept", Collections.singletonList("application/json"));
    m.put(Message.PROTOCOL_HEADERS, headers);

    WadlGenerator wg = new WadlGenerator() {
        @Override public void filter(ContainerRequestContext context) {
            super.doFilter(context, m);
        }
    };
    wg.setUseJaxbContextForQnames(false);
    wg.setIgnoreMessageWriters(false);
    wg.setExternalLinks(Collections.singletonList("json.schema"));

    Response r = handleRequest(wg, m);
    assertEquals("application/json",
            r.getMetadata().getFirst("Content-Type").toString());

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    new JSONProvider<Document>().writeTo(
            (Document)r.getEntity(), Document.class, Document.class,
              new Annotation[]{}, MediaType.APPLICATION_JSON_TYPE,
              new MetadataMap<String, Object>(), os);
    String s = os.toString();
    String expected1 =
        "{\"application\":{\"grammars\":{\"include\":{\"@href\":\"http://localhost:8080/baz"
        + "/json.schema\"}},\"resources\":{\"@base\":\"http://localhost:8080/baz\","
        + "\"resource\":{\"@path\":\"/bookstore/{id}\"";
    assertTrue(s.startsWith(expected1));
    String expected2 =
        "\"response\":{\"representation\":[{\"@mediaType\":\"application/xml\"},"
        + "{\"@element\":\"Chapter\",\"@mediaType\":\"application/json\"}]}";
    assertTrue(s.contains(expected2));
}
 
Example 18
Source File: WadlGeneratorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomSchemaWithImportJaxbContextPrefixes() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    wg.setSchemaLocations(Collections.singletonList("classpath:/books.xsd"));

    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    Message m = mockMessage("http://localhost:8080/baz", "/bar", WadlGenerator.WADL_QUERY, cri);
    Response r = handleRequest(wg, m);
    checkResponse(r);
    Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
    List<Element> grammarEls = DOMUtils.getChildrenWithName(doc.getDocumentElement(),
        WadlGenerator.WADL_NS, "grammars");
    assertEquals(1, grammarEls.size());
    List<Element> schemasEls = DOMUtils.getChildrenWithName(grammarEls.get(0),
                                                            Constants.URI_2001_SCHEMA_XSD, "schema");
    assertEquals(1, schemasEls.size());
    assertEquals("http://books", schemasEls.get(0).getAttribute("targetNamespace"));
    List<Element> elementEls = DOMUtils.getChildrenWithName(schemasEls.get(0),
                                                            Constants.URI_2001_SCHEMA_XSD, "element");
    assertEquals(1, elementEls.size());
    assertTrue(checkElement(elementEls, "books", "books"));

    List<Element> complexTypesEls = DOMUtils.getChildrenWithName(schemasEls.get(0),
                                                                 Constants.URI_2001_SCHEMA_XSD, "complexType");
    assertEquals(1, complexTypesEls.size());
    assertTrue(checkComplexType(complexTypesEls, "books"));

    List<Element> importEls = DOMUtils.getChildrenWithName(schemasEls.get(0),
                                                           Constants.URI_2001_SCHEMA_XSD, "import");
    assertEquals(1, importEls.size());
    assertEquals("http://localhost:8080/baz/book1.xsd",
                 importEls.get(0).getAttribute("schemaLocation"));
}
 
Example 19
Source File: WadlGeneratorTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testExternalSchemaJaxbContextPrefixes() throws Exception {
    WadlGenerator wg = new WadlGenerator();
    wg.setExternalLinks(Collections.singletonList("http://books.xsd"));

    ClassResourceInfo cri =
        ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
    Message m = mockMessage("http://localhost:8080/baz", "/bookstore/1", WadlGenerator.WADL_QUERY, cri);
    Response r = handleRequest(wg, m);
    checkResponse(r);
    Document doc = StaxUtils.read(new StringReader(r.getEntity().toString()));
    checkGrammarsWithLinks(doc.getDocumentElement(), Collections.singletonList("http://books.xsd"));
    List<Element> els = getWadlResourcesInfo(doc, "http://localhost:8080/baz", 1);
    checkBookStoreInfo(els.get(0), "prefix1:thebook", "prefix1:thebook2", "prefix1:thechapter");
}
 
Example 20
Source File: JAXRSServiceFactoryBean.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected ClassResourceInfo createResourceInfo(Class<?> cls, boolean isRoot) {
    ClassResourceInfo classResourceInfo =
        ResourceUtils.createClassResourceInfo(cls, cls, isRoot, enableStatic, getBus());
    return addClassResourceInfo(classResourceInfo);
}