Java Code Examples for org.apache.cxf.jaxrs.client.WebClient#postAndGetCollection()

The following examples show how to use org.apache.cxf.jaxrs.client.WebClient#postAndGetCollection() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JAXRSMultipartTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddGetJaxbBooksWebClient() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/books/jaxbonly";
    WebClient client = WebClient.create(address);

    client.type("multipart/mixed;type=application/xml").accept("multipart/mixed");

    Book b = new Book("jaxb", 1L);
    Book b2 = new Book("jaxb2", 2L);
    List<Book> books = new ArrayList<>();
    books.add(b);
    books.add(b2);
    Collection<? extends Book> coll = client.postAndGetCollection(books, Book.class);
    List<Book> result = new ArrayList<>(coll);
    Book jaxb = result.get(0);
    assertEquals("jaxb", jaxb.getName());
    assertEquals(1L, jaxb.getId());
    Book jaxb2 = result.get(1);
    assertEquals("jaxb2", jaxb2.getName());
    assertEquals(2L, jaxb2.getId());
}
 
Example 2
Source File: JAXRSClientServerResourceJacksonSpringProviderTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEchoGenericSuperBookCollectionWebClient() throws Exception {

    String endpointAddress =
        "http://localhost:" + PORT + "/webapp/custombus/genericstore/books/superbooks";
    WebClient wc = WebClient.create(endpointAddress,
                                    Collections.singletonList(new JacksonJsonProvider()));
    WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(100000000L);
    wc.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON);
    Collection<? extends SuperBook> books =
        wc.postAndGetCollection(Collections.singletonList(new SuperBook("Super", 124L, true)),
                                SuperBook.class,
                                SuperBook.class);
    SuperBook book = books.iterator().next();
    assertEquals(124L, book.getId());
    assertTrue(book.isSuperBook());
}
 
Example 3
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testPostCollectionOfBooksWebClient() throws Exception {

    String endpointAddress =
        "http://localhost:" + PORT + "/bookstore/collections";
    WebClient wc = WebClient.create(endpointAddress);
    wc.accept("application/xml").type("application/xml");
    Book b1 = new Book("CXF in Action", 123L);
    Book b2 = new Book("CXF Rocks", 124L);
    List<Book> books = new ArrayList<>();
    books.add(b1);
    books.add(b2);
    List<Book> books2 = new ArrayList<>(wc.postAndGetCollection(books, Book.class, Book.class));
    assertNotNull(books2);
    assertNotSame(books, books2);
    assertEquals(2, books2.size());
    Book b11 = books.get(0);
    assertEquals(123L, b11.getId());
    assertEquals("CXF in Action", b11.getName());
    Book b22 = books.get(1);
    assertEquals(124L, b22.getId());
    assertEquals("CXF Rocks", b22.getName());
    assertEquals(200, wc.getResponse().getStatus());
}
 
Example 4
Source File: JAXRSClientServerBookTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetJAXBElementXmlRootBookCollectionWebClient() throws Exception {
    WebClient store = WebClient.create("http://localhost:" + PORT
                                       + "/bookstore/jaxbelementxmlrootcollections");
    Book b1 = new Book("CXF in Action", 123L);
    Book b2 = new Book("CXF Rocks", 124L);
    List<Book> books = new ArrayList<>();
    books.add(b1);
    books.add(b2);
    store.type("application/xml").accept("application/xml");
    List<Book> books2 = new ArrayList<>(store.postAndGetCollection(books, Book.class, Book.class));
    assertNotNull(books2);
    assertNotSame(books, books2);
    assertEquals(2, books2.size());
    Book b11 = books2.get(0);
    assertEquals(123L, b11.getId());
    assertEquals("CXF in Action", b11.getName());
    Book b22 = books2.get(1);
    assertEquals(124L, b22.getId());
    assertEquals("CXF Rocks", b22.getName());
}
 
Example 5
Source File: JAXRSMultipartTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Map<String, String> doTestAddBookJaxbJsonImageWebClient(String multipartType) throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/books/jaxbjsonimage";
    WebClient client = WebClient.create(address);
    WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor());
    client.type(multipartType).accept(multipartType);

    Book jaxb = new Book("jaxb", 1L);
    Book json = new Book("json", 2L);
    InputStream is1 =
        getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
    Map<String, Object> objects = new LinkedHashMap<>();
    objects.put(MediaType.APPLICATION_XML, jaxb);
    objects.put(MediaType.APPLICATION_JSON, json);
    objects.put(MediaType.APPLICATION_OCTET_STREAM, is1);
    Collection<? extends Attachment> coll = client.postAndGetCollection(objects, Attachment.class);
    List<Attachment> result = new ArrayList<>(coll);
    Book jaxb2 = readBookFromInputStream(result.get(0).getDataHandler().getInputStream());
    assertEquals("jaxb", jaxb2.getName());
    assertEquals(1L, jaxb2.getId());
    Book json2 = readJSONBookFromInputStream(result.get(1).getDataHandler().getInputStream());
    assertEquals("json", json2.getName());
    assertEquals(2L, json2.getId());
    InputStream is2 = result.get(2).getDataHandler().getInputStream();
    byte[] image1 = IOUtils.readBytesFromStream(
        getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));
    byte[] image2 = IOUtils.readBytesFromStream(is2);
    assertArrayEquals(image1, image2);

    String ctString =
        client.getResponse().getMetadata().getFirst("Content-Type").toString();
    MediaType mt = MediaType.valueOf(ctString);
    return mt.getParameters();
}
 
Example 6
Source File: JAXRSMultipartTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddBookJaxbJsonImageAttachments() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/books/jaxbimagejson";
    WebClient client = WebClient.create(address);
    client.type("multipart/mixed").accept("multipart/mixed");

    Book jaxb = new Book("jaxb", 1L);
    Book json = new Book("json", 2L);
    InputStream is1 =
        getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
    List<Attachment> objects = new ArrayList<>();
    objects.add(new Attachment("<theroot>", MediaType.APPLICATION_XML, jaxb));
    objects.add(new Attachment("thejson", MediaType.APPLICATION_JSON, json));
    objects.add(new Attachment("theimage", MediaType.APPLICATION_OCTET_STREAM, is1));
    Collection<? extends Attachment> coll = client.postAndGetCollection(objects, Attachment.class);
    List<Attachment> result = new ArrayList<>(coll);
    Book jaxb2 = readBookFromInputStream(result.get(0).getDataHandler().getInputStream());
    assertEquals("jaxb", jaxb2.getName());
    assertEquals(1L, jaxb2.getId());
    Book json2 = readJSONBookFromInputStream(result.get(1).getDataHandler().getInputStream());
    assertEquals("json", json2.getName());
    assertEquals(2L, json2.getId());
    InputStream is2 = result.get(2).getDataHandler().getInputStream();
    byte[] image1 = IOUtils.readBytesFromStream(
        getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));
    byte[] image2 = IOUtils.readBytesFromStream(is2);
    assertArrayEquals(image1, image2);
}
 
Example 7
Source File: JAXRSClientServerResourceJacksonSpringProviderTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testEchoGenericSuperBookCollectionWebClientXml() throws Exception {

    String endpointAddress =
        "http://localhost:" + PORT + "/webapp/custombus/genericstore/books/superbooks";
    WebClient wc = WebClient.create(endpointAddress);
    wc.accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML);
    Collection<? extends SuperBook> books =
        wc.postAndGetCollection(Collections.singletonList(new SuperBook("Super", 124L, true)),
                                SuperBook.class,
                                SuperBook.class);
    SuperBook book = books.iterator().next();
    assertEquals(124L, book.getId());
    assertTrue(book.isSuperBook());
}
 
Example 8
Source File: JAXRSMultipartTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddBookJaxbJsonImageWebClientRelated2() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/books/jaxbimagejson";
    WebClient client = WebClient.create(address);
    WebClient.getConfig(client).getOutInterceptors().add(new LoggingOutInterceptor());
    WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor());
    client.type("multipart/mixed").accept("multipart/mixed");

    Book jaxb = new Book("jaxb", 1L);
    Book json = new Book("json", 2L);
    InputStream is1 =
        getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
    Map<String, Object> objects = new LinkedHashMap<>();

    MultivaluedMap<String, String> headers = new MetadataMap<>();
    headers.putSingle("Content-Type", "application/xml");
    headers.putSingle("Content-ID", "theroot");
    headers.putSingle("Content-Transfer-Encoding", "customxml");
    Attachment attJaxb = new Attachment(headers, jaxb);

    headers = new MetadataMap<>();
    headers.putSingle("Content-Type", "application/json");
    headers.putSingle("Content-ID", "thejson");
    headers.putSingle("Content-Transfer-Encoding", "customjson");
    Attachment attJson = new Attachment(headers, json);

    headers = new MetadataMap<>();
    headers.putSingle("Content-Type", "application/octet-stream");
    headers.putSingle("Content-ID", "theimage");
    headers.putSingle("Content-Transfer-Encoding", "customstream");
    Attachment attIs = new Attachment(headers, is1);

    objects.put(MediaType.APPLICATION_XML, attJaxb);
    objects.put(MediaType.APPLICATION_JSON, attJson);
    objects.put(MediaType.APPLICATION_OCTET_STREAM, attIs);

    Collection<? extends Attachment> coll = client.postAndGetCollection(objects, Attachment.class);
    List<Attachment> result = new ArrayList<>(coll);
    Book jaxb2 = readBookFromInputStream(result.get(0).getDataHandler().getInputStream());
    assertEquals("jaxb", jaxb2.getName());
    assertEquals(1L, jaxb2.getId());
    Book json2 = readJSONBookFromInputStream(result.get(1).getDataHandler().getInputStream());
    assertEquals("json", json2.getName());
    assertEquals(2L, json2.getId());
    InputStream is2 = result.get(2).getDataHandler().getInputStream();
    byte[] image1 = IOUtils.readBytesFromStream(
        getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));
    byte[] image2 = IOUtils.readBytesFromStream(is2);
    assertArrayEquals(image1, image2);
}
 
Example 9
Source File: JAXRSMultipartTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddBookJsonImageStream() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/books/jsonimagestream";
    WebClient client = WebClient.create(address);
    WebClient.getConfig(client).getOutInterceptors().add(new LoggingOutInterceptor());
    WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor());
    client.type("multipart/mixed").accept("multipart/mixed");

    Book json = new Book("json", 1L);
    InputStream is1 =
        getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg");
    Map<String, Object> objects = new LinkedHashMap<>();

    MultivaluedMap<String, String> headers = new MetadataMap<>();

    headers = new MetadataMap<>();
    headers.putSingle("Content-Type", "application/json");
    headers.putSingle("Content-ID", "thejson");
    headers.putSingle("Content-Transfer-Encoding", "customjson");
    Attachment attJson = new Attachment(headers, json);

    headers = new MetadataMap<>();
    headers.putSingle("Content-Type", "application/octet-stream");
    headers.putSingle("Content-ID", "theimage");
    headers.putSingle("Content-Transfer-Encoding", "customstream");
    Attachment attIs = new Attachment(headers, is1);

    objects.put(MediaType.APPLICATION_JSON, attJson);
    objects.put(MediaType.APPLICATION_OCTET_STREAM, attIs);

    Collection<? extends Attachment> coll = client.postAndGetCollection(objects, Attachment.class);
    List<Attachment> result = new ArrayList<>(coll);
    assertEquals(2, result.size());
    Book json2 = readJSONBookFromInputStream(result.get(0).getDataHandler().getInputStream());
    assertEquals("json", json2.getName());
    assertEquals(1L, json2.getId());
    InputStream is2 = result.get(1).getDataHandler().getInputStream();
    byte[] image1 = IOUtils.readBytesFromStream(
        getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));
    byte[] image2 = IOUtils.readBytesFromStream(is2);
    assertArrayEquals(image1, image2);
}