Search in sources :

Example 6 with Person

use of common.advanced.Person in project tesb-rt-se by Talend.

the class PersonServiceImpl method addChild.

@Override
public Response addChild(Long parentId, Person child) {
    Person parent = storage.getPerson(parentId);
    if (parent == null) {
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    parent.addChild(child);
    Long childId = storage.addPerson(child);
    UriBuilder locationBuilder = uriInfo.getBaseUriBuilder();
    locationBuilder.path(PersonService.class);
    URI childLocation = locationBuilder.path("{id}").build(childId);
    return Response.status(Response.Status.CREATED).location(childLocation).build();
}
Also used : UriBuilder(javax.ws.rs.core.UriBuilder) Person(common.advanced.Person) URI(java.net.URI)

Example 7 with Person

use of common.advanced.Person in project tesb-rt-se by Talend.

the class PersonServiceProxyClient method useService.

public void useService() {
    System.out.println("Using a simple JAX-RS proxy to get all the persons...");
    // getPersons(a, b): a is zero-based start index, b is number of records
    // to return (-1 for all)
    Response resp = proxy.getPersons(0, -1);
    if (resp.getStatus() == 200) {
        PersonCollection personColl = resp.readEntity(PersonCollection.class);
        List<Person> persons = personColl.getList();
        for (Iterator<Person> it = persons.iterator(); it.hasNext(); ) {
            Person person = it.next();
            System.out.println("ID " + person.getId() + " : " + person.getName() + ", age : " + person.getAge());
        }
    }
    System.out.println("Using PATCH...");
    WebClient.getConfig(proxy).getRequestContext().put("use.async.http.conduit", true);
    String patch = proxy.patch();
    System.out.println("Patch: " + patch);
}
Also used : Response(javax.ws.rs.core.Response) PersonCollection(common.advanced.PersonCollection) Person(common.advanced.Person)

Example 8 with Person

use of common.advanced.Person in project tesb-rt-se by Talend.

the class RESTClient method usePersonService.

/**
 * PersonService provides information about all the persons it knows about, about individual persons and
 * their relatives : - ancestors - parents, grandparents, etc - descendants - children, etc - partners
 * Additionally it can help with adding the information about new children to existing persons and update
 * the age of the current Person
 */
public void usePersonService() throws Exception {
    System.out.println("Using a Web Client...");
    // A single web client will be used to retrieve all the information
    final String personServiceURI = "http://localhost:" + port + "/services/personservice/main";
    WebClient wc = WebClient.create(personServiceURI);
    // Get the list of all persons
    System.out.println("Getting the XML collection of all persons in a type-safe way...");
    wc.accept(MediaType.APPLICATION_XML);
    List<Person> persons = getPersons(wc);
    // Get individual persons using JSON
    System.out.println("Getting individual persons using JSON...");
    wc.reset().accept(MediaType.APPLICATION_JSON);
    for (Person person : persons) {
        // Move forward, for example, given that web client is currently
        // positioned at
        // personServiceURI and a current person id such as 4, wc.path(id)
        // will point
        // the client to "http://localhost:8080/personservice/main/4"
        wc.path(person.getId());
        // Read the stream
        InputStream is = wc.get(InputStream.class);
        System.out.println(IOUtils.toString(is));
        // Move only one path segment back, to
        // "http://localhost:8080/personservice/main"
        // Note that if web client moved few segments forward from the base
        // personServiceURI
        // then wc.back(true) would bring the client back to the baseURI
        wc.back(false);
    }
    // Get Person with id 4 :
    System.out.println("Getting info about Fred...");
    // wc.reset() insures the current path is reset to the base
    // personServiceURI and
    // the headers which may have been set during the previous invocations
    // are also reset.
    wc.reset().accept(MediaType.APPLICATION_XML);
    wc.path("4");
    getPerson(wc);
    System.out.println("Getting info about Fred's mother");
    wc.path("mother");
    getPerson(wc);
    System.out.println("Getting info about Fred's father");
    wc.back(false).path("father");
    getPerson(wc);
    System.out.println("Getting info about Fred's partner");
    wc.back(false).path("partner");
    getPerson(wc);
    // Get info about Fred's ancestors, descendants and children
    wc.reset().accept(MediaType.APPLICATION_XML);
    wc.path("4");
    System.out.println("Getting info about Fred's ancestors");
    wc.path("ancestors");
    getPersons(wc);
    System.out.println("Getting info about Fred's descendants");
    wc.back(false).path("descendants");
    getPersons(wc);
    System.out.println("Getting info about Fred's children");
    wc.back(false).path("children");
    getPersons(wc);
    System.out.println("Fred and Catherine now have a child, adding a child info to PersonService");
    Person child = new Person("Harry", 1);
    wc.reset().accept(MediaType.APPLICATION_XML).type(MediaType.APPLICATION_XML);
    Response response = wc.path("4").path("children").post(child);
    // a newly created (child) Person resource is expected
    if (response.getStatus() != 201) {
        throw new RuntimeException("No child resource has been created");
    }
    // Follow the Location header pointing to a new child resource
    // and get the information, confirm it is Harry
    String location = response.getMetadata().getFirst("Location").toString();
    getPerson(WebClient.create(location));
    System.out.println("Getting an up to date info about Fred's children");
    getPersons(wc);
    System.out.println("Fred has become 40, updating his age");
    // WebClient is currently pointing to personServiceURI + "/4" +
    // "/children"
    wc.back(false);
    // Back to personServiceURI + "/4"
    wc.path("age").type("text/plain");
    // Trying to update the age to the wrong value by mistake
    Response rc = wc.put(20);
    // HTTP Bad Request status is expected
    if (rc.getStatus() != 400) {
        throw new RuntimeException("Fred has become older, not younger");
    }
    rc = wc.put(40);
    // 204 (No Content) is a typical HTTP PUT response
    if (rc.getStatus() != 204) {
        throw new RuntimeException("Impossible to update Fred's age");
    }
    System.out.println("Getting up to date info about Fred");
    // WebClient is currently pointing to personServiceURI + "/4" + "/age"
    wc.back(false);
    // Back to personServiceURI + "/4"
    getPerson(wc);
    // finally, do a basic search:
    wc.reset().path("find").accept(MediaType.APPLICATION_XML);
    wc.query("name", "Fred", "Lorraine");
    printPersonCollection(wc.get(PersonCollection.class));
    System.out.println("Using PATCH...");
    WebClient.getConfig(wc).getRequestContext().put("use.async.http.conduit", true);
    String patch = wc.reset().invoke("PATCH", null, String.class);
    System.out.println("Patch: " + patch);
    wc.close();
}
Also used : Response(javax.ws.rs.core.Response) InputStream(java.io.InputStream) PersonCollection(common.advanced.PersonCollection) WebClient(org.apache.cxf.jaxrs.client.WebClient) Person(common.advanced.Person)

Aggregations

Person (common.advanced.Person)8 PersonCollection (common.advanced.PersonCollection)4 Response (javax.ws.rs.core.Response)2 PersonInfo (common.advanced.PersonInfo)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Tuple (javax.persistence.Tuple)1 SingularAttribute (javax.persistence.metamodel.SingularAttribute)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 WebClient (org.apache.cxf.jaxrs.client.WebClient)1 JPACriteriaQueryVisitor (org.apache.cxf.jaxrs.ext.search.jpa.JPACriteriaQueryVisitor)1