Search in sources :

Example 6 with Book

use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.

the class JAXBElementProviderTest method doTestWriteJAXBCollection.

private void doTestWriteJAXBCollection(String mName) throws Exception {
    JAXBElementProvider<List<?>> provider = new JAXBElementProvider<List<?>>();
    List<JAXBElement<Book>> books = new ArrayList<JAXBElement<Book>>();
    books.add(new JAXBElement<Book>(new QName("Books"), Book.class, null, new Book("CXF in Action", 123L)));
    books.add(new JAXBElement<Book>(new QName("Books"), Book.class, null, new Book("CXF Rocks", 124L)));
    Method m = CollectionsResource.class.getMethod(mName, new Class[0]);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(books, m.getReturnType(), m.getGenericReturnType(), new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    doReadUnqualifiedCollection(bos.toString(), "setBooks", List.class);
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) ParamJAXBElement(org.apache.cxf.jaxrs.fortest.jaxb.jaxbelement.ParamJAXBElement) JAXBElement(javax.xml.bind.JAXBElement) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) List(java.util.List) ArrayList(java.util.ArrayList)

Example 7 with Book

use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.

the class JAXBElementProviderTest method testReadFromISO.

@Test
public void testReadFromISO() throws Exception {
    String eWithAcute = "\u00E9";
    String nameStringUTF16 = "F" + eWithAcute + "lix";
    String bookStringUTF16 = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" + "<Book><name>" + nameStringUTF16 + "</name></Book>";
    byte[] iso88591bytes = bookStringUTF16.getBytes("ISO-8859-1");
    JAXBElementProvider<Book> p = new JAXBElementProvider<Book>();
    Book book = p.readFrom(Book.class, null, new Annotation[] {}, MediaType.valueOf(MediaType.APPLICATION_XML), null, new ByteArrayInputStream(iso88591bytes));
    assertEquals(book.getName(), nameStringUTF16);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) Test(org.junit.Test)

Example 8 with Book

use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.

the class JAXBElementProviderTest method doReadUnqualifiedCollection.

@SuppressWarnings("unchecked")
private <T> void doReadUnqualifiedCollection(String data, String mName, Class<T> type) throws Exception {
    JAXBElementProvider<T> provider = new JAXBElementProvider<T>();
    Method m = CollectionsResource.class.getMethod(mName, new Class[] { type });
    ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
    Object o = provider.readFrom(type, m.getGenericParameterTypes()[0], new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), is);
    assertNotNull(o);
    Book b1 = null;
    Book b2 = null;
    if (type.isArray()) {
        assertEquals(2, ((Book[]) o).length);
        b1 = ((Book[]) o)[0];
        b2 = ((Book[]) o)[1];
    } else if (type == Set.class) {
        Set<Book> set = CastUtils.cast((Set<?>) o);
        List<Book> books = new ArrayList<>(new TreeSet<Book>(set));
        b1 = books.get(0);
        b2 = books.get(1);
    } else {
        List<Book> books = (List<Book>) o;
        b1 = books.get(0);
        b2 = books.get(1);
    }
    assertEquals(123, b1.getId());
    assertEquals("CXF in Action", b1.getName());
    assertEquals(124, b2.getId());
    assertEquals("CXF Rocks", b2.getName());
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Method(java.lang.reflect.Method) GET(javax.ws.rs.GET) ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) TreeSet(java.util.TreeSet) List(java.util.List) ArrayList(java.util.ArrayList)

Example 9 with Book

use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.

the class JAXBElementProviderTest method testWriteWithoutXmlRootElementDerived.

@Test
public void testWriteWithoutXmlRootElementDerived() throws Exception {
    JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider = new JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book>();
    provider.setJaxbElementClassMap(Collections.singletonMap(org.apache.cxf.jaxrs.fortest.jaxb.Book.class.getName(), "Book"));
    org.apache.cxf.jaxrs.fortest.jaxb.Book b = new org.apache.cxf.jaxrs.fortest.jaxb.SuperBook("CXF in Action", 123L, 124L);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(b, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, org.apache.cxf.jaxrs.fortest.jaxb.Book.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    readSuperBook2(bos.toString(), false);
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Test(org.junit.Test)

Example 10 with Book

use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.

the class JAXBElementProviderTest method testReadMalformedXML.

@Test
public void testReadMalformedXML() throws Exception {
    JAXBElementProvider<Book> provider = new JAXBElementProvider<Book>();
    try {
        provider.readFrom(Book.class, Book.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream("<Book>".getBytes()));
        fail("404 is expected");
    } catch (WebApplicationException ex) {
        assertEquals(400, ex.getResponse().getStatus());
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) Test(org.junit.Test)

Aggregations

Book (org.apache.cxf.jaxrs.resources.Book)42 SuperBook (org.apache.cxf.jaxrs.resources.SuperBook)35 Test (org.junit.Test)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 MessageImpl (org.apache.cxf.message.MessageImpl)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Method (java.lang.reflect.Method)5 MediaType (javax.ws.rs.core.MediaType)5 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)5 Exchange (org.apache.cxf.message.Exchange)5 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)5 Message (org.apache.cxf.message.Message)5 OutputStream (java.io.OutputStream)4 StringReader (java.io.StringReader)4 Annotation (java.lang.annotation.Annotation)4 Response (javax.ws.rs.core.Response)4 Endpoint (org.apache.cxf.endpoint.Endpoint)4 InputStream (java.io.InputStream)3