Package com.sun.jersey.api.client

Examples of com.sun.jersey.api.client.Client


        return links;
    }

    @Test
    public void testCreateCancelPurge() throws Exception {
        Client c = new Client();
        String url = "http://localhost:" + getJettyPort() + "/shop";

        WebResource wr = c.resource(url);
        ClientResponse response = wr.head();
        Map<String, Link> shoppingLinks = processLinkHeaders(response);

        Link customers = shoppingLinks.get("customers");
        System.out.println("** Create a customer through this URL: " + customers.getHref());

        Customer customer = new Customer();
        customer.setFirstName("Bill");
        customer.setLastName("Burke");
        customer.setStreet("10 Somewhere Street");
        customer.setCity("Westford");
        customer.setState("MA");
        customer.setZip("01711");
        customer.setCountry("USA");

        wr = c.resource(customers.getHref());
        response = wr.type(MediaType.APPLICATION_XML).post(ClientResponse.class, customer);
        Assert.assertEquals(201, response.getStatus());

        Link orders = shoppingLinks.get("orders");

        Order order = new Order();
        order.setTotal("$199.99");
        order.setCustomer(customer);
        order.setDate(new Date().toString());
        LineItem item = new LineItem();
        item.setCost("$199.99");
        item.setProduct("iPhone");
        order.setLineItems(new ArrayList<LineItem>());
        order.getLineItems().add(item);

        System.out.println();
        System.out.println("** Create an order through this URL: " + orders.getHref());
        wr = c.resource(orders.getHref());
        response = wr.type(MediaType.APPLICATION_XML).post(ClientResponse.class, order);
        Assert.assertEquals(201, response.getStatus());
        String createdOrderUrl = (String) response.getHeaders().getFirst("Location");

        System.out.println();
        System.out.println("** New list of orders");
        wr = c.resource(orders.getHref());
        response = wr.get(ClientResponse.class);
        System.out.println(response.getEntity(String.class));
        Map<String, Link> ordersLinks = processLinkHeaders(response);

        wr = c.resource(createdOrderUrl);
        response = wr.head();
        Map<String, Link> orderLinks = processLinkHeaders(response);

        Link cancel = orderLinks.get("cancel");
        if (cancel != null) {
            System.out.println("** Canceling the order at URL: " + cancel.getHref());
            wr = c.resource(cancel.getHref());
            response = wr.post(ClientResponse.class);
            Assert.assertEquals(204, response.getStatus());
        }

        System.out.println();
        System.out.println("** New list of orders after cancel: ");
        wr = c.resource(orders.getHref());
        response = wr.get(ClientResponse.class);
        System.out.println(response.getEntity(String.class));

        System.out.println();
        Link purge = ordersLinks.get("purge");
        System.out.println("** Purge cancelled orders at URL: " + purge.getHref());
        wr = c.resource(purge.getHref());
        response = wr.post(ClientResponse.class);
        Assert.assertEquals(204, response.getStatus());

        System.out.println();
        System.out.println("** New list of orders after purge: ");
        wr = c.resource(orders.getHref());
        response = wr.get(ClientResponse.class);
        System.out.println(response.getEntity(String.class));
    }
View Full Code Here


  }

  @Test
  public void testRegistration() throws Exception {
    RegistrationResponse response;
    Client client = createTestClient();
    WebResource webResource = client.resource(base_url + "test/register");
    response = webResource.type(MediaType.APPLICATION_JSON)
        .post(RegistrationResponse.class, createDummyJSONRegister());
    Assert.assertEquals(RegistrationStatus.OK, response.getResponseStatus());
  }
View Full Code Here

  }

  @Test
  public void testHeartbeat() throws Exception {
    HeartBeatResponse response;
    Client client = createTestClient();
    WebResource webResource = client.resource(base_url + "test/heartbeat");
    response = webResource.type(MediaType.APPLICATION_JSON)
        .post(HeartBeatResponse.class, createDummyHeartBeat());
    assertEquals(response.getResponseId(), 0L);
  }
View Full Code Here

    assertEquals(response.getResponseId(), 0L);
  }

  @Test
  public void testHeadURL() throws Exception {
    Client client = createTestClient();
    WebResource webResource = client.resource(base_url);
    ClientResponse response = webResource.type(MediaType.APPLICATION_JSON)
                                         .head();
    assertEquals(200, response.getStatus());
  }
View Full Code Here

   *
   * @param uri     the uri
   * @param filters optional filters (e.g. for authentication)
   */
  public CouchDatabase( @Nonnull URI uri, @Nullable ClientFilter... filters ) {
    client = new Client();
    if ( filters != null ) {
      for ( ClientFilter filter : filters ) {
        client.addFilter( filter );
      }
    }
View Full Code Here

* @author Paul.Sandoz@Sun.Com
*/
public class FilterTest extends TestCase {

    public void testAddRemoveOneFilter() {
        Client client = Client.create();
        ClientFilter filter = new ClientFilter() {

            @Override
            public ClientResponse handle(ClientRequest cr)
                    throws ClientHandlerException {
                return getNext().handle(cr);
            }
        };

        client.addFilter(filter);
        assertTrue(client.isFilterPreset(filter));
        client.removeFilter(filter);
        assertFalse(client.isFilterPreset(filter));
    }
View Full Code Here

        client.removeFilter(filter);
        assertFalse(client.isFilterPreset(filter));
    }

    public void testAddRemoveMultipleFilters() {
        Client client = Client.create();
        ClientFilter filterOne = new ClientFilter() {

            @Override
            public ClientResponse handle(ClientRequest cr)
                    throws ClientHandlerException {
                return getNext().handle(cr);
            }
        };
        ClientFilter filterTwo = new ClientFilter() {

            @Override
            public ClientResponse handle(ClientRequest cr)
                    throws ClientHandlerException {
                return getNext().handle(cr);
            }
        };

        client.addFilter(filterOne);
        assertTrue(client.isFilterPreset(filterOne));
        client.addFilter(filterTwo);
        assertTrue(client.isFilterPreset(filterTwo));

        client.removeFilter(filterOne);
        assertFalse(client.isFilterPreset(filterOne));
        client.removeFilter(filterTwo);
        assertFalse(client.isFilterPreset(filterTwo));

        client.addFilter(filterOne);
        assertTrue(client.isFilterPreset(filterOne));
        client.addFilter(filterTwo);
        assertTrue(client.isFilterPreset(filterTwo));

        client.removeFilter(filterTwo);
        assertFalse(client.isFilterPreset(filterTwo));
        client.removeFilter(filterOne);
        assertFalse(client.isFilterPreset(filterOne));
    }
View Full Code Here

    public void testInjected() throws IOException {
        ClientConfig cc = new DefaultClientConfig();
        cc.getClasses().add(MyProvider.class);
        MyIoCComponentProviderFactory pf = new MyIoCComponentProviderFactory();
        Client c = Client.create(cc, pf);
        assertEquals(MyProvider.class, pf.componentClass);
    }
View Full Code Here

    public void testNoInjected() throws IOException {
        ClientConfig cc = new DefaultClientConfig();
        cc.getClasses().add(MyProviderNoInject.class);
        MyIoCComponentProviderFactory pf = new MyIoCComponentProviderFactory();
        Client c = Client.create(cc, pf);
        assertEquals(MyProviderNoInject.class, pf.componentClass);
    }
View Full Code Here

    public void testWebApplicationException() {
        WebApplicationException wae = new WebApplicationException();
    }

    public void testClient() throws IOException {
        Client c = Client.create();

        int port = getEnvVariable("JERSEY_HTTP_PORT", 9998);

        HttpServer server = null;
        try {
            server = HttpServer.create(new InetSocketAddress(port), 0);

            HttpHandler handler = new HttpHandler() {
                public void handle(HttpExchange e) throws IOException {
                    InputStream in = e.getRequestBody();
                    ByteArrayOutputStream _out = new ByteArrayOutputStream();
                    byte[] buf = new byte[2048];
                    int read = 0;
                    while ((read = in.read(buf)) != -1) {
                        _out.write(buf, 0, read);
                    }

                    e.sendResponseHeaders(200, 0);

                    OutputStream out = e.getResponseBody();
                    in = new ByteArrayInputStream(_out.toByteArray());
                    while ((read = in.read(buf)) != -1) {
                        out.write(buf, 0, read);
                    }
                    out.flush();
                    e.close();
                }
            };
            server.createContext("/", handler);
            server.start();
           
            URI u = UriBuilder.fromUri("http://localhost/").port(port).build();
            WebResource r = c.resource(u);

            String s = r.type("text/plain").post(String.class, "CONTENT");
            assertEquals("CONTENT", s);
        } finally {
            if (server != null)
View Full Code Here

TOP

Related Classes of com.sun.jersey.api.client.Client

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.