Search in sources :

Example 26 with Book

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

the class JSONProviderTest method testWriteBeanNoRootAtJsonLevel.

@Test
public void testWriteBeanNoRootAtJsonLevel() throws Exception {
    JSONProvider<Book> provider = new JSONProvider<Book>();
    provider.setDropRootElement(true);
    provider.setDropElementsInXmlStream(false);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(new Book("cxf", 123), Book.class, Book.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), bos);
    assertTrue(bos.toString().contains("\"name\":\"cxf\""));
    assertTrue(bos.toString().contains("\"id\":123"));
    assertFalse(bos.toString().startsWith("{\"Book\":"));
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 27 with Book

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

the class ProviderFactoryTest method testRegisterMbrMbwProviderAsMbwOnly.

@Test
public void testRegisterMbrMbwProviderAsMbwOnly() {
    ServerProviderFactory pf = ServerProviderFactory.getInstance();
    JAXBElementProvider<Book> customProvider = new JAXBElementProvider<Book>();
    pf.registerUserProvider((Feature) context -> {
        context.register(customProvider, MessageBodyWriter.class);
        return true;
    });
    MessageBodyWriter<Book> writer = pf.createMessageBodyWriter(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
    assertSame(writer, customProvider);
    MessageBodyReader<Book> reader = pf.createMessageBodyReader(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
    assertTrue(reader instanceof JAXBElementProvider);
    assertNotSame(reader, customProvider);
}
Also used : Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) Priorities(javax.ws.rs.Priorities) ExceptionMapper(javax.ws.rs.ext.ExceptionMapper) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) ParamConverter(javax.ws.rs.ext.ParamConverter) ContextResolver(javax.ws.rs.ext.ContextResolver) JAXBContextProvider2(org.apache.cxf.jaxrs.JAXBContextProvider2) MediaType(javax.ws.rs.core.MediaType) Consumes(javax.ws.rs.Consumes) Map(java.util.Map) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider) JAXBContextProvider(org.apache.cxf.jaxrs.JAXBContextProvider) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Collection(java.util.Collection) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Customer(org.apache.cxf.jaxrs.Customer) Priority(javax.annotation.Priority) List(java.util.List) WriterInterceptorContext(javax.ws.rs.ext.WriterInterceptorContext) Response(javax.ws.rs.core.Response) Type(java.lang.reflect.Type) Annotation(java.lang.annotation.Annotation) WebApplicationException(javax.ws.rs.WebApplicationException) Bus(org.apache.cxf.Bus) MessageImpl(org.apache.cxf.message.MessageImpl) Feature(javax.ws.rs.core.Feature) DataHandler(javax.activation.DataHandler) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) ProviderInfo(org.apache.cxf.jaxrs.model.ProviderInfo) WriterInterceptor(javax.ws.rs.ext.WriterInterceptor) Status(javax.ws.rs.core.Response.Status) JAXBContext(javax.xml.bind.JAXBContext) WebApplicationExceptionMapper(org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper) Before(org.junit.Before) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) CustomerParameterHandler(org.apache.cxf.jaxrs.CustomerParameterHandler) Message(org.apache.cxf.message.Message) IOUtils(org.apache.cxf.helpers.IOUtils) IOException(java.io.IOException) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) Book(org.apache.cxf.jaxrs.resources.Book) Exchange(org.apache.cxf.message.Exchange) File(java.io.File) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) AbstractResourceInfo(org.apache.cxf.jaxrs.model.AbstractResourceInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) BusFactory(org.apache.cxf.BusFactory) DataSource(javax.activation.DataSource) Comparator(java.util.Comparator) Assert(org.junit.Assert) Collections(java.util.Collections) InputStream(java.io.InputStream) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) MessageBodyWriter(javax.ws.rs.ext.MessageBodyWriter) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 28 with Book

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

the class ProviderFactoryTest method testGetComplexProvider.

@Test
public void testGetComplexProvider() throws Exception {
    ServerProviderFactory pf = ServerProviderFactory.getInstance();
    pf.registerUserProvider(new ComplexMessageBodyReader());
    Message m = new MessageImpl();
    Exchange ex = new ExchangeImpl();
    m.setExchange(ex);
    m.put(ProviderFactory.IGNORE_TYPE_VARIABLES, true);
    MessageBodyReader<Book> reader = pf.createMessageBodyReader(Book.class, Book.class, null, MediaType.APPLICATION_JSON_TYPE, m);
    assertTrue(ComplexMessageBodyReader.class == reader.getClass());
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 29 with Book

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

the class XPathProviderTest method testReadFrom.

@Test
public void testReadFrom() throws Exception {
    String value = "<Book><name>The Book</name><id>2</id></Book>";
    XPathProvider<Book> provider = new XPathProvider<Book>();
    provider.setExpression("/Book");
    provider.setClassName(Book.class.getName());
    provider.setForceDOM(true);
    Book book = provider.readFrom(Book.class, null, null, null, null, new ByteArrayInputStream(value.getBytes()));
    assertNotNull(book);
    assertEquals(2L, book.getId());
    assertEquals("The Book", book.getName());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Book(org.apache.cxf.jaxrs.resources.Book) Test(org.junit.Test)

Example 30 with Book

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

the class XSLTJaxbProviderTest method testWrite.

@Test
public void testWrite() throws Exception {
    XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>();
    provider.setOutTemplate(TEMPLATE_LOCATION);
    provider.setMessageContext(new MessageContextImpl(createMessage()));
    Book b = new Book();
    b.setId(123L);
    b.setName("TheBook");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(b, Book.class, Book.class, b.getClass().getAnnotations(), MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    Unmarshaller um = provider.getClassContext(Book.class).createUnmarshaller();
    Book b2 = (Book) um.unmarshal(new StringReader(bos.toString()));
    b.setName("TheBook2");
    assertEquals("Transformation is bad", b, b2);
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Unmarshaller(javax.xml.bind.Unmarshaller) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl) 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