Search in sources :

Example 1 with Booking

use of org.apache.karaf.examples.rest.api.Booking in project karaf by apache.

the class ListAllBookingCommand method execute.

@Override
public Object execute() throws Exception {
    List providers = new ArrayList();
    providers.add(new JacksonJsonProvider());
    WebClient webClient = WebClient.create(restLocation, providers);
    List<Attachment> atts = new LinkedList<>();
    Booking b1 = new Booking();
    b1.setCustomer("the one who must not be named");
    b1.setFlight("101");
    b1.setId(43L);
    atts.add(new Attachment("root", "application/json", b1));
    atts.add(new Attachment("image", "application/octet-stream", new ByteArrayInputStream("UGhvdG8=".getBytes(StandardCharsets.UTF_8))));
    Collection<? extends Attachment> results = webClient.type("multipart/mixed").accept("multipart/mixed").postAndGetCollection(atts, Attachment.class);
    for (Attachment a : results) {
        System.out.println(a.getContentId() + ": " + a.getContentType() + ": " + a.getDataHandler().getContent());
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) Booking(org.apache.karaf.examples.rest.api.Booking) WebClient(org.apache.cxf.jaxrs.client.WebClient) LinkedList(java.util.LinkedList)

Example 2 with Booking

use of org.apache.karaf.examples.rest.api.Booking in project karaf by apache.

the class AddBookingCommand method execute.

@Override
public Object execute() throws Exception {
    Booking booking = new Booking();
    booking.setId(id);
    booking.setFlight(flight);
    booking.setCustomer(customer);
    Client client = ClientBuilder.newClient();
    client.register(new JacksonJsonProvider());
    WebTarget target = client.target(restLocation);
    target.request().post(Entity.json(booking));
    return null;
}
Also used : JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) Booking(org.apache.karaf.examples.rest.api.Booking) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Example 3 with Booking

use of org.apache.karaf.examples.rest.api.Booking in project karaf by apache.

the class BookingServiceRest method traceMultipart.

@Path("/all")
@Consumes("multipart/mixed")
@Produces("multipart/mixed")
@POST
public MultipartBody traceMultipart(List<Attachment> atts) {
    final List<Attachment> results = new ArrayList<>();
    final Attachment att1 = new Attachment("text", MediaType.TEXT_HTML, "Hello World!");
    results.add(att1);
    Booking b1 = new Booking();
    b1.setCustomer("me");
    b1.setFlight("far far away");
    b1.setId(42L);
    final Attachment att2 = new Attachment("json", MediaType.APPLICATION_JSON, b1);
    results.add(att2);
    final Attachment att3 = new Attachment("stream", MediaType.APPLICATION_OCTET_STREAM, new ByteArrayInputStream("important information".getBytes()));
    results.add(att3);
    results.addAll(atts);
    return new MultipartBody(results, true);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) MultipartBody(org.apache.cxf.jaxrs.ext.multipart.MultipartBody) ArrayList(java.util.ArrayList) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) Booking(org.apache.karaf.examples.rest.api.Booking)

Example 4 with Booking

use of org.apache.karaf.examples.rest.api.Booking in project karaf by apache.

the class ListBookingCommand method execute.

@Override
public Object execute() throws Exception {
    Client client = ClientBuilder.newClient();
    client.register(new JacksonJsonProvider());
    WebTarget target = client.target(restLocation);
    List<Booking> bookings = (List<Booking>) target.request(MediaType.APPLICATION_JSON).get(new GenericType<List<Booking>>() {
    });
    for (Booking booking : bookings) {
        System.out.println(booking.getId() + " " + booking.getCustomer() + " " + booking.getFlight());
    }
    return null;
}
Also used : GenericType(javax.ws.rs.core.GenericType) JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) Booking(org.apache.karaf.examples.rest.api.Booking) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Example 5 with Booking

use of org.apache.karaf.examples.rest.api.Booking in project karaf by apache.

the class AddBookingCommand method execute.

@Override
public Object execute() throws Exception {
    Booking booking = new Booking();
    booking.setId(id);
    booking.setFlight(flight);
    booking.setCustomer(customer);
    List providers = new ArrayList();
    providers.add(new JacksonJsonProvider());
    WebClient webClient = WebClient.create(restLocation, providers);
    webClient.header("Content-Type", MediaType.APPLICATION_JSON).post(booking);
    return null;
}
Also used : JacksonJsonProvider(com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider) ArrayList(java.util.ArrayList) Booking(org.apache.karaf.examples.rest.api.Booking) List(java.util.List) ArrayList(java.util.ArrayList) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Aggregations

Booking (org.apache.karaf.examples.rest.api.Booking)6 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 WebClient (org.apache.cxf.jaxrs.client.WebClient)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Client (javax.ws.rs.client.Client)2 WebTarget (javax.ws.rs.client.WebTarget)2 Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)2 LinkedList (java.util.LinkedList)1 GenericType (javax.ws.rs.core.GenericType)1 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)1