Search in sources :

Example 6 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project cxf by apache.

the class JAXRSSoapBookTest method testPostGetBookFastinfosetProxy.

@Test
public void testPostGetBookFastinfosetProxy() throws Exception {
    JAXBElementProvider<Object> p = new JAXBElementProvider<>();
    p.setConsumeMediaTypes(Collections.singletonList("application/fastinfoset"));
    p.setProduceMediaTypes(Collections.singletonList("application/fastinfoset"));
    BookStoreJaxrsJaxws client = JAXRSClientFactory.create("http://localhost:" + PORT + "/test/services/rest4", BookStoreSoapRestFastInfoset2.class, Collections.singletonList(p));
    Book b = new Book("CXF", 1L);
    Book b2 = client.addFastinfoBook(b);
    assertEquals(b2.getName(), b.getName());
    assertEquals(b2.getId(), b.getId());
    checkFiInterceptors(WebClient.getConfig(client));
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) BookStoreJaxrsJaxws(org.apache.cxf.systest.jaxrs.jaxws.BookStoreJaxrsJaxws) Test(org.junit.Test)

Example 7 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project cxf by apache.

the class JAXRSSoapBookTest method testGetBookFastinfoset.

@Test
public void testGetBookFastinfoset() throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress("http://localhost:" + PORT + "/test/services/rest3/bookstore/fastinfoset2");
    bean.getInInterceptors().add(new FIStaxInInterceptor());
    JAXBElementProvider<?> p = new JAXBElementProvider<>();
    p.setConsumeMediaTypes(Collections.singletonList("application/fastinfoset"));
    bean.setProvider(p);
    Map<String, Object> props = new HashMap<>();
    props.put(FIStaxInInterceptor.FI_GET_SUPPORTED, Boolean.TRUE);
    bean.setProperties(props);
    WebClient client = bean.createWebClient();
    Book b = client.accept("application/fastinfoset").get(Book.class);
    assertEquals("CXF2", b.getName());
    assertEquals(2L, b.getId());
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) FIStaxInInterceptor(org.apache.cxf.interceptor.FIStaxInInterceptor) HashMap(java.util.HashMap) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 8 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project cxf by apache.

the class AbstractXmlSecOutInterceptor method getDomDocument.

@SuppressWarnings("unchecked")
private Document getDomDocument(Message m) throws Exception {
    Object body = getRequestBody(m);
    if (body == null) {
        return null;
    }
    if (body instanceof Document) {
        return (Document) body;
    }
    if (body instanceof DOMSource) {
        return (Document) ((DOMSource) body).getNode();
    }
    ProviderFactory pf = ProviderFactory.getInstance(m);
    Object providerObject = pf.createMessageBodyWriter(body.getClass(), body.getClass(), new Annotation[] {}, MediaType.APPLICATION_XML_TYPE, m);
    if (!(providerObject instanceof JAXBElementProvider)) {
        return null;
    }
    JAXBElementProvider<Object> provider = (JAXBElementProvider<Object>) providerObject;
    W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
    m.setContent(XMLStreamWriter.class, writer);
    provider.writeTo(body, body.getClass(), new Annotation[] {}, MediaType.APPLICATION_XML_TYPE, (MultivaluedMap<String, Object>) m.get(Message.PROTOCOL_HEADERS), null);
    return writer.getDocument();
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) DOMSource(javax.xml.transform.dom.DOMSource) ProviderFactory(org.apache.cxf.jaxrs.provider.ProviderFactory) Document(org.w3c.dom.Document)

Example 9 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project cxf by apache.

the class JAXRSClientServerResourceCreatedSpringProviderTest method testPostPetStatusType.

@Test
public void testPostPetStatusType() throws Exception {
    JAXBElementProvider<Object> p = new JAXBElementProvider<>();
    p.setUnmarshallAsJaxbElement(true);
    WebClient wc = WebClient.create("http://localhost:" + PORT + "/webapp/pets/petstore/jaxb/statusType/", Collections.singletonList(p));
    WebClient.getConfig(wc).getInInterceptors().add(new LoggingInInterceptor());
    wc.accept("text/xml");
    PetStore.PetStoreStatusType type = wc.get(PetStore.PetStoreStatusType.class);
    assertEquals(PetStore.CLOSED, type.getStatus());
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) LoggingInInterceptor(org.apache.cxf.ext.logging.LoggingInInterceptor) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 10 with JAXBElementProvider

use of org.apache.cxf.jaxrs.provider.JAXBElementProvider in project cxf by apache.

the class JAXRSClientServerBookTest method testPostGetBookAdapterList.

@Test
public void testPostGetBookAdapterList() throws Exception {
    JAXBElementProvider<?> provider = new JAXBElementProvider<>();
    Map<String, String> outMap = new HashMap<>();
    outMap.put("Books", "CollectionWrapper");
    outMap.put("books", "Book");
    provider.setOutTransformElements(outMap);
    WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/books/adapter-list", Collections.singletonList(provider));
    Collection<? extends Book> books = wc.type("application/xml").accept("application/xml").postAndGetCollection(new Books(new Book("CXF", 123L)), Book.class);
    assertEquals(1, books.size());
    assertEquals(123L, books.iterator().next().getId());
}
Also used : JAXBElementProvider(org.apache.cxf.jaxrs.provider.JAXBElementProvider) HashMap(java.util.HashMap) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Aggregations

JAXBElementProvider (org.apache.cxf.jaxrs.provider.JAXBElementProvider)30 Test (org.junit.Test)9 HashMap (java.util.HashMap)8 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)6 WebClient (org.apache.cxf.jaxrs.client.WebClient)6 ArrayList (java.util.ArrayList)5 Unmarshaller (javax.xml.bind.Unmarshaller)5 XMLStreamException (javax.xml.stream.XMLStreamException)5 InputStream (java.io.InputStream)4 StringReader (java.io.StringReader)4 JAXBException (javax.xml.bind.JAXBException)4 QName (javax.xml.namespace.QName)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)4 IOException (java.io.IOException)3 Response (javax.ws.rs.core.Response)3 RuntimeDelegate (javax.ws.rs.ext.RuntimeDelegate)3 LoggingInInterceptor (org.apache.cxf.ext.logging.LoggingInInterceptor)3 Type (java.lang.reflect.Type)2 TypeVariable (java.lang.reflect.TypeVariable)2