Package org.deftserver.web.http.client

Examples of org.deftserver.web.http.client.AsynchronousHttpClient


import org.deftserver.web.http.client.HttpResponse;

public class AsynchronousHttpClientExample {

  public static void main(String[] args) {
    AsynchronousHttpClient client = new AsynchronousHttpClient();
    client.fetch("http://tt.se/start/", new ResultCallback());
    IOLoop.INSTANCE.start();
  }
View Full Code Here


 
  @Test
  public void connectToUnresolvableAddressUsingAsynchronousHttpClient() throws InterruptedException {
    final String unresolvableAddress = "http://ttasfdqwertyuiop.se./start";
    final CountDownLatch latch = new CountDownLatch(1);
    final AsynchronousHttpClient client = new AsynchronousHttpClient();
    final AsyncCallback runByIOLoop = new AsyncCallback() {

      public void onCallback() {
        client.fetch(unresolvableAddress, new AsyncResult<org.deftserver.web.http.client.HttpResponse>() {

          public void onSuccess(org.deftserver.web.http.client.HttpResponse result) { client.close(); }

          public void onFailure(Throwable caught) {
            if (caught instanceof UnresolvedAddressException) latch.countDown();
            client.close();
          }
        });
      }
    };
    IOLoop.INSTANCE.addCallback(runByIOLoop);
View Full Code Here

  @Test
  public void connectToUnconnectableAddressUsingAsynchronousHttpClient() throws InterruptedException {
    final String unconnectableAddress = "http://localhost:8039/start";
    final CountDownLatch latch = new CountDownLatch(1);
    final AsynchronousHttpClient client = new AsynchronousHttpClient();
    final AsyncCallback runByIOLoop = new AsyncCallback() {

      public void onCallback() {
        client.fetch(unconnectableAddress, new AsyncResult<org.deftserver.web.http.client.HttpResponse>() {

          public void onSuccess(org.deftserver.web.http.client.HttpResponse result) { client.close(); }

          public void onFailure(Throwable caught) {
            if (caught instanceof ConnectException) latch.countDown();
            client.close();
          }
        });
      }
    };
    IOLoop.INSTANCE.addCallback(runByIOLoop);
View Full Code Here

  @Test
  public void multipleAsynchronousHttpClientTest() throws InterruptedException {
    for (int i = 0; i < 100; i++) {
      final CountDownLatch latch = new CountDownLatch(1);
      final String url = "http://localhost:" + PORT + "/";
      final AsynchronousHttpClient http = new AsynchronousHttpClient();
      final String[] result = {"BODY_PLACEHOLDER", "STATUSCODE_PLACEHOLDER"};
      final AsyncResult<org.deftserver.web.http.client.HttpResponse> cb =
        new AsyncResult<org.deftserver.web.http.client.HttpResponse>() {

        public void onSuccess(org.deftserver.web.http.client.HttpResponse response) {
          result[0] = response.getBody();
          result[1] = response.getStatusLine();
          latch.countDown();
        }

        public void onFailure(Throwable ignore) { }
      };
      // make sure that the http.fetch(..) is invoked from the ioloop thread
      IOLoop.INSTANCE.addCallback(new AsyncCallback() { public void onCallback() { http.fetch(url, cb); }});
      latch.await(5, TimeUnit.SECONDS);
      assertEquals(0, latch.getCount());
      assertEquals("hello test", result[0]);
      assertEquals("HTTP/1.1 200 OK", result[1]);
    }
View Full Code Here

 
  @Test
  public void AsynchronousHttpClientConnectionFailedTest() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final String url = "http://localhost:" + (PORT+1) + "/";
    final AsynchronousHttpClient http = new AsynchronousHttpClient();
    final AsyncResult<org.deftserver.web.http.client.HttpResponse> cb =
      new AsyncResult<org.deftserver.web.http.client.HttpResponse>() {

      public void onSuccess(org.deftserver.web.http.client.HttpResponse response) { }

      public void onFailure(Throwable e) { if (e instanceof ConnectException) latch.countDown(); }
    };
    // make sure that the http.fetch(..) is invoked from the ioloop thread
    IOLoop.INSTANCE.addCallback(new AsyncCallback() { public void onCallback() { http.fetch(url, cb); }});
    latch.await(5, TimeUnit.SECONDS);
    assertEquals(0, latch.getCount());
  }
View Full Code Here

 
  @Test
  public void connectToUnresolvableAddressUsingAsynchronousHttpClient() throws InterruptedException {
    final String unresolvableAddress = "http://ttasfdqwertyuiop.se./start";
    final CountDownLatch latch = new CountDownLatch(1);
    final AsynchronousHttpClient client = new AsynchronousHttpClient();
    final AsyncCallback runByIOLoop = new AsyncCallback() {

      public void onCallback() {
        client.fetch(unresolvableAddress, new AsyncResult<org.deftserver.web.http.client.Response>() {

          public void onSuccess(org.deftserver.web.http.client.Response result) { client.close(); }

          public void onFailure(Throwable caught) {
            if (caught instanceof UnresolvedAddressException) latch.countDown();
            client.close();
          }
        });
      }
    };
    IOLoop.INSTANCE.addCallback(runByIOLoop);
View Full Code Here

  @Test
  public void connectToUnconnectableAddressUsingAsynchronousHttpClient() throws InterruptedException {
    final String unconnectableAddress = "http://localhost:8039/start";
    final CountDownLatch latch = new CountDownLatch(1);
    final AsynchronousHttpClient client = new AsynchronousHttpClient();
    final AsyncCallback runByIOLoop = new AsyncCallback() {

      public void onCallback() {
        client.fetch(unconnectableAddress, new AsyncResult<org.deftserver.web.http.client.Response>() {

          public void onSuccess(org.deftserver.web.http.client.Response result) { client.close(); }

          public void onFailure(Throwable caught) {
            if (caught instanceof ConnectException) latch.countDown();
            client.close();
          }
        });
      }
    };
    IOLoop.INSTANCE.addCallback(runByIOLoop);
View Full Code Here

  @Test
  public void multipleAsynchronousHttpClientTest() throws InterruptedException {
    for (int i = 0; i < 100; i++) {
      final CountDownLatch latch = new CountDownLatch(1);
      final String url = "http://localhost:" + PORT + "/";
      final AsynchronousHttpClient http = new AsynchronousHttpClient();
      final String[] result = {"BODY_PLACEHOLDER", "STATUSCODE_PLACEHOLDER"};
      final AsyncResult<org.deftserver.web.http.client.Response> cb =
        new AsyncResult<org.deftserver.web.http.client.Response>() {

        public void onSuccess(org.deftserver.web.http.client.Response response) {
          result[0] = response.getBody();
          result[1] = response.getStatusLine();
          latch.countDown();
        }

        public void onFailure(Throwable ignore) { }
      };
      // make sure that the http.fetch(..) is invoked from the ioloop thread
      IOLoop.INSTANCE.addCallback(new AsyncCallback() { public void onCallback() { http.fetch(url, cb); }});
      latch.await(15, TimeUnit.SECONDS);
      assertEquals(0, latch.getCount());
      assertEquals("hello test", result[0]);
      assertEquals("HTTP/1.1 200 OK", result[1]);
    }
View Full Code Here

 
  @Test
  public void AsynchronousHttpClientConnectionFailedTest() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final String url = "http://localhost:" + (PORT+1) + "/";
    final AsynchronousHttpClient http = new AsynchronousHttpClient();
    final AsyncResult<org.deftserver.web.http.client.Response> cb =
      new AsyncResult<org.deftserver.web.http.client.Response>() {

      public void onSuccess(org.deftserver.web.http.client.Response response) { }

      public void onFailure(Throwable e) { if (e instanceof ConnectException) latch.countDown(); }
    };
    // make sure that the http.fetch(..) is invoked from the ioloop thread
    IOLoop.INSTANCE.addCallback(new AsyncCallback() { public void onCallback() { http.fetch(url, cb); }});
    latch.await(5, TimeUnit.SECONDS);
    assertEquals(0, latch.getCount());
  }
View Full Code Here

  @Test
  public void AsynchronousHttpClientRedirectTest() throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    //final String url = "http://localhost:" + (PORT) + "/moved_perm";
    final String url = "http://localhost:" + PORT + "/moved_perm";
    final AsynchronousHttpClient http = new AsynchronousHttpClient();
    final AsyncResult<org.deftserver.web.http.client.Response> cb =
      new AsyncResult<org.deftserver.web.http.client.Response>() {

      public void onSuccess(org.deftserver.web.http.client.Response response) {
        if (response.getBody().equals(expectedPayload)) {
          latch.countDown();
        }
      }
     
      public void onFailure(Throwable e) { }
   
    };
    // make sure that the http.fetch(..) is invoked from the ioloop thread
    IOLoop.INSTANCE.addCallback(new AsyncCallback() { public void onCallback() { http.fetch(url, cb); }});
    latch.await(5, TimeUnit.SECONDS);
    assertEquals(0, latch.getCount());
  }
View Full Code Here

TOP

Related Classes of org.deftserver.web.http.client.AsynchronousHttpClient

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.