Search in sources :

Example 36 with Book

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

the class JAXBElementProviderTest method testReadChineeseChars.

@Test
public void testReadChineeseChars() throws Exception {
    String nameStringUTF16 = "中文";
    String bookStringUTF16 = "<Book><name>" + nameStringUTF16 + "</name></Book>";
    JAXBElementProvider<Book> p = new JAXBElementProvider<Book>();
    Book book = p.readFrom(Book.class, null, new Annotation[] {}, MediaType.valueOf(MediaType.APPLICATION_XML + ";charset=UTF-8"), null, new ByteArrayInputStream(bookStringUTF16.getBytes(StandardCharsets.UTF_8)));
    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 37 with Book

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

the class JAXBElementProviderTest method testWriteDerivedType2.

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

Example 38 with Book

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

the class JAXBElementProviderTest method doWriteUnqualifiedCollection.

public <T> void doWriteUnqualifiedCollection(boolean setName, String mName, String setterName, Class<T> type) throws Exception {
    JAXBElementProvider<T> provider = new JAXBElementProvider<T>();
    if (setName) {
        provider.setCollectionWrapperName("Books");
    }
    List<Book> books = new ArrayList<>();
    books.add(new Book("CXF in Action", 123L));
    books.add(new Book("CXF Rocks", 124L));
    @SuppressWarnings("unchecked") T o = (T) (type.isArray() ? books.toArray() : type == Set.class ? new HashSet<>(books) : books);
    Method m = CollectionsResource.class.getMethod(mName, new Class[0]);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(o, m.getReturnType(), m.getGenericReturnType(), new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    doReadUnqualifiedCollection(bos.toString(), setterName, type);
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GET(javax.ws.rs.GET) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) HashSet(java.util.HashSet)

Example 39 with Book

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

the class JAXBElementProviderTest method doTestObjectFactoryExtraClass.

private void doTestObjectFactoryExtraClass(JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider) throws Exception {
    org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3 b = new org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3("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);
    JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book> provider2 = new JAXBElementProvider<org.apache.cxf.jaxrs.fortest.jaxb.Book>();
    provider2.setExtraClass(new Class[] { org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3.class });
    ByteArrayInputStream is = new ByteArrayInputStream(bos.toByteArray());
    org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3 book = (org.apache.cxf.jaxrs.fortest.jaxb.index.SuperBook3) provider2.readFrom(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, String>(), is);
    assertEquals(124L, book.getSuperId());
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book)

Example 40 with Book

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

the class JAXBElementProviderTest method testWriteDerivedType.

@Test
public void testWriteDerivedType() throws Exception {
    JAXBElementProvider<Book> provider = new JAXBElementProvider<Book>();
    provider.setJaxbElementClassNames(Collections.singletonList(Book.class.getName()));
    Book b = new SuperBook("CXF in Action", 123L, 124L);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(b, Book.class, Book.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    readSuperBook(bos.toString(), true);
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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