Search in sources :

Example 1 with Book

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

the class ProviderFactoryTest method testSchemaLocations.

@Test
public void testSchemaLocations() {
    ProviderFactory pf = ServerProviderFactory.getInstance();
    MessageBodyReader<Book> jaxbReader = pf.createMessageBodyReader(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
    assertTrue(jaxbReader instanceof JAXBElementProvider);
    pf.setSchemaLocations(Collections.singletonList("classpath:/test.xsd"));
    MessageBodyReader<Book> customJaxbReader = pf.createMessageBodyReader(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
    assertTrue(jaxbReader instanceof JAXBElementProvider);
    assertSame(jaxbReader, customJaxbReader);
    assertNotNull(((JAXBElementProvider<Book>) customJaxbReader).getSchema());
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 2 with Book

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

the class ProviderFactoryTest method testRegisterMbrMbwProviderAsMbrOnly.

@Test
public void testRegisterMbrMbwProviderAsMbrOnly() {
    ServerProviderFactory pf = ServerProviderFactory.getInstance();
    JAXBElementProvider<Book> customProvider = new JAXBElementProvider<Book>();
    pf.registerUserProvider((Feature) context -> {
        context.register(customProvider, MessageBodyReader.class);
        return true;
    });
    MessageBodyReader<Book> reader = pf.createMessageBodyReader(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
    assertSame(reader, customProvider);
    MessageBodyWriter<Book> writer = pf.createMessageBodyWriter(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
    assertTrue(writer instanceof JAXBElementProvider);
    assertNotSame(writer, 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) MessageBodyReader(javax.ws.rs.ext.MessageBodyReader) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 3 with Book

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

the class ProviderFactoryTest method testCustomJaxbProvider.

@Test
public void testCustomJaxbProvider() {
    ProviderFactory pf = ServerProviderFactory.getInstance();
    JAXBElementProvider<Book> provider = new JAXBElementProvider<Book>();
    pf.registerUserProvider(provider);
    MessageBodyReader<Book> customJaxbReader = pf.createMessageBodyReader(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
    assertSame(customJaxbReader, provider);
    MessageBodyWriter<Book> customJaxbWriter = pf.createMessageBodyWriter(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
    assertSame(customJaxbWriter, provider);
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 4 with Book

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

the class XSLTJaxbProviderTest method testReadFromStreamReader.

@Test
public void testReadFromStreamReader() throws Exception {
    XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>() {

        @Override
        protected XMLStreamReader getStreamReader(InputStream is, Class<?> type, MediaType mt) {
            return StaxUtils.createXMLStreamReader(is);
        }
    };
    provider.setInTemplate(TEMPLATE_LOCATION);
    Book b = new Book();
    b.setId(123L);
    b.setName("TheBook");
    Book b2 = provider.readFrom(Book.class, Book.class, b.getClass().getAnnotations(), MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream(BOOK_XML.getBytes()));
    b.setName("TheBook2");
    assertEquals("Transformation is bad", b, b2);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MediaType(javax.ws.rs.core.MediaType) Test(org.junit.Test)

Example 5 with Book

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

the class XSLTJaxbProviderTest method testWriteWithAnnotation.

@Test
public void testWriteWithAnnotation() throws Exception {
    XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>();
    provider.setMessageContext(new MessageContextImpl(createMessage()));
    Book b = new Book();
    b.setId(123L);
    b.setName("TheBook");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Annotation[] anns = Root.class.getMethod("getBook").getAnnotations();
    assertTrue(provider.isWriteable(Book.class, Book.class, anns, MediaType.TEXT_XML_TYPE));
    provider.writeTo(b, Book.class, Book.class, anns, 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 : ByteArrayOutputStream(java.io.ByteArrayOutputStream) Annotation(java.lang.annotation.Annotation) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) StringReader(java.io.StringReader) 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