public static void main(final String[] args) throws Exception {

    System.out.println("-------------------------------------------------------------------");
    System.out.println("java.version: " + System.getProperty("java.version"));
    System.out.println("java.vendor.url: " + System.getProperty("java.vendor.url"));
    System.out.println("java.vendor: " + System.getProperty("java.vendor"));
    System.out.println("java.home: " + System.getProperty("java.home"));
    System.out.println("os.arch: " + System.getProperty("os.arch"));
    System.out.println("os.name: " + System.getProperty("os.name"));
    System.out.println("os.version: " + System.getProperty("os.version"));
    System.out.println("-------------------------------------------------------------------");

    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    httpclient.start();
    try {
      long start = System.currentTimeMillis();
      for (int i = 0; i < 10; i++) {
        Future<Boolean> future =
            httpclient.execute(
                HttpAsyncMethods.createGet("http://www.google.com/"),
                new MyResponseConsumer(),
                null);
        Boolean result = future.get();
        System.out.println("result: " + result);
        if (!result) {
          System.out.println("Request failed");
        }
      }
      long end = System.currentTimeMillis();
      System.out.println("Total time (sec):" + ((double) (end - start)) / 1000);
    } finally {
      httpclient.close();
    }
    System.out.println("Done");
  }
 private Future<HttpResponse> checkForUpdates() {
   CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
   httpclient.start();
   HttpHead request = new HttpHead("http://broken-by.me/download/pdf-metadata-editor/");
   Future<HttpResponse> future = httpclient.execute(request, null);
   return future;
 }
Esempio n. 3
0
  public Object get(
      String url, AbstractAsyncResponseConsumer myConsumer, Map<String, String> headerMap)
      throws IOException {
    URI uri = URI.create(url);
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    Object obj = null;
    try {
      httpclient.start();
      HttpGet httpget = new HttpGet(uri);
      HttpHost httphost = URIUtils.extractHost(uri);
      if (headerMap != null) {
        for (String key : headerMap.keySet()) {
          httpget.addHeader(key, headerMap.get(key));
        }
      }
      BasicAsyncRequestProducer requset = new BasicAsyncRequestProducer(httphost, httpget);

      Future<Object> future = httpclient.execute(requset, myConsumer, null);
      obj = future.get();
    } catch (InterruptedException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    } catch (ExecutionException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    } finally {
      httpclient.close();
      return obj;
    }
  }
Esempio n. 4
0
  public static void refresh() {
    // Load timeouts
    Object connectionTimeout = Options.getOption(Option.CONNECTION_TIMEOUT);
    if (connectionTimeout == null) connectionTimeout = CONNECTION_TIMEOUT;
    Object socketTimeout = Options.getOption(Option.SOCKET_TIMEOUT);
    if (socketTimeout == null) socketTimeout = SOCKET_TIMEOUT;

    // Create common default configuration
    RequestConfig clientConfig =
        RequestConfig.custom()
            .setConnectTimeout(((Long) connectionTimeout).intValue())
            .setSocketTimeout(((Long) socketTimeout).intValue())
            .build();

    // Create clients
    setOption(
        Option.HTTPCLIENT,
        HttpClientBuilder.create().setDefaultRequestConfig(clientConfig).build());

    CloseableHttpAsyncClient asyncClient =
        HttpAsyncClientBuilder.create().setDefaultRequestConfig(clientConfig).build();
    asyncClient.start();

    setOption(Option.ASYNCHTTPCLIENT, asyncClient);
  }
Esempio n. 5
0
  public Object post(String url, AbstractAsyncResponseConsumer myConsumer, String json)
      throws IOException {
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    Object obj = null;
    try {
      httpclient.start();

      Future<Object> future =
          httpclient.execute(
              HttpAsyncMethods.createPost(url, json, ContentType.APPLICATION_JSON),
              myConsumer,
              null);
      obj = future.get();
    } catch (InterruptedException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    } catch (ExecutionException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    } finally {
      httpclient.close();
      return obj;
    }
  }
Esempio n. 6
0
 static CloseableHttpAsyncClient buildDefaultHttpClient() {
   // TODO: Increase timeout??
   RequestConfig requestConfig = RequestConfig.custom().build();
   CloseableHttpAsyncClient httpClient =
       HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig).build();
   httpClient.start();
   return httpClient;
 }
  public static CloseableHttpAsyncClient getHttpAsyncClient() {
    if (httpAsyncClient == null) {
      synchronized (HttpClientManager.class) {
        if (httpAsyncClient == null) {
          CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
          client.start();
          httpAsyncClient = client;
        }
      }
    }

    return httpAsyncClient;
  }
Esempio n. 8
0
  static {
    RequestConfig clientConfig =
        RequestConfig.custom()
            .setConnectTimeout(CONNECTION_TIMEOUT)
            .setSocketTimeout(SOCKET_TIMEOUT)
            .build();

    setOption(
        Option.HTTPCLIENT,
        HttpClientBuilder.create().setDefaultRequestConfig(clientConfig).build());

    CloseableHttpAsyncClient asyncClient =
        HttpAsyncClientBuilder.create().setDefaultRequestConfig(clientConfig).build();
    asyncClient.start();

    setOption(Option.ASYNCHTTPCLIENT, asyncClient);
  }
  @SuppressWarnings({"unchecked", "rawtypes"})
  public void testTooLargeResponse() throws Exception {
    ContentTooLongException tooLong = new ContentTooLongException("too long!");
    CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class);
    when(httpClient.<HttpResponse>execute(
            any(HttpAsyncRequestProducer.class),
            any(HttpAsyncResponseConsumer.class),
            any(FutureCallback.class)))
        .then(
            new Answer<Future<HttpResponse>>() {
              @Override
              public Future<HttpResponse> answer(InvocationOnMock invocationOnMock)
                  throws Throwable {
                HeapBufferedAsyncResponseConsumer consumer =
                    (HeapBufferedAsyncResponseConsumer) invocationOnMock.getArguments()[1];
                FutureCallback callback = (FutureCallback) invocationOnMock.getArguments()[2];

                assertEquals(
                    new ByteSizeValue(200, ByteSizeUnit.MB).bytesAsInt(),
                    consumer.getBufferLimit());
                callback.failed(tooLong);
                return null;
              }
            });
    RemoteScrollableHitSource source = sourceWithMockedClient(true, httpClient);

    AtomicBoolean called = new AtomicBoolean();
    Consumer<Response> checkResponse = r -> called.set(true);
    Throwable e =
        expectThrows(
            RuntimeException.class,
            () -> source.doStartNextScroll(FAKE_SCROLL_ID, timeValueMillis(0), checkResponse));
    // Unwrap the some artifacts from the test
    while (e.getMessage().equals("failed")) {
      e = e.getCause();
    }
    // This next exception is what the user sees
    assertEquals(
        "Remote responded with a chunk that was too large. Use a smaller batch size.",
        e.getMessage());
    // And that exception is reported as being caused by the underlying exception returned by the
    // client
    assertSame(tooLong, e.getCause());
    assertFalse(called.get());
  }
Esempio n. 10
0
  /**
   * Close the asynchronous client and its event loop. Use this method to close all the threads and
   * allow an application to exit.
   */
  public static void shutdown() throws IOException {
    // Closing the sync client
    CloseableHttpClient syncClient = (CloseableHttpClient) Options.getOption(Option.HTTPCLIENT);
    syncClient.close();

    SyncIdleConnectionMonitorThread syncIdleConnectionMonitorThread =
        (SyncIdleConnectionMonitorThread) Options.getOption(Option.SYNC_MONITOR);
    syncIdleConnectionMonitorThread.shutdown();

    // Closing the async client (if running)
    CloseableHttpAsyncClient asyncClient =
        (CloseableHttpAsyncClient) Options.getOption(Option.ASYNCHTTPCLIENT);
    if (asyncClient.isRunning()) {
      asyncClient.close();
      AsyncIdleConnectionMonitorThread asyncIdleConnectionMonitorThread =
          (AsyncIdleConnectionMonitorThread) Options.getOption(Option.ASYNC_MONITOR);
      asyncIdleConnectionMonitorThread.shutdown();
    }
  }
 public static void main(final String[] args) throws Exception {
   CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
   try {
     httpclient.start();
     Future<Boolean> future =
         httpclient.execute(
             HttpAsyncMethods.createGet("http://www.sina.com.cn/"),
             new MyResponseConsumer(),
             null);
     Boolean result = future.get();
     if (result != null && result.booleanValue()) {
       System.out.println("Request successfully executed");
     } else {
       System.out.println("Request failed");
     }
     System.out.println("Shutting down");
   } finally {
     httpclient.close();
   }
   System.out.println("Done");
 }
  private void hecPost(String currentMessage) throws Exception {

    HttpPost post = new HttpPost(uri);
    post.addHeader("Authorization", "Splunk " + config.getToken());

    StringEntity requestEntity =
        new StringEntity(currentMessage, ContentType.create("application/json", "UTF-8"));

    post.setEntity(requestEntity);
    Future<HttpResponse> future = httpClient.execute(post, null);
    HttpResponse response = future.get();
    // System.out.println(response.getStatusLine());
    // System.out.println(EntityUtils.toString(response.getEntity()));

  }
  @Override
  public void init(Object obj) {
    config = (HECTransportConfig) obj;

    this.batchBuffer = Collections.synchronizedList(new LinkedList<String>());
    this.lastEventReceivedTime = System.currentTimeMillis();

    try {

      Registry<SchemeIOSessionStrategy> sslSessionStrategy =
          RegistryBuilder.<SchemeIOSessionStrategy>create()
              .register("http", NoopIOSessionStrategy.INSTANCE)
              .register("https", new SSLIOSessionStrategy(getSSLContext(), HOSTNAME_VERIFIER))
              .build();

      ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
      PoolingNHttpClientConnectionManager cm =
          new PoolingNHttpClientConnectionManager(ioReactor, sslSessionStrategy);
      cm.setMaxTotal(config.getPoolsize());

      HttpHost splunk = new HttpHost(config.getHost(), config.getPort());
      cm.setMaxPerRoute(new HttpRoute(splunk), config.getPoolsize());

      httpClient = HttpAsyncClients.custom().setConnectionManager(cm).build();

      uri =
          new URIBuilder()
              .setScheme(config.isHttps() ? "https" : "http")
              .setHost(config.getHost())
              .setPort(config.getPort())
              .setPath("/services/collector")
              .build();

      httpClient.start();

      if (config.isBatchMode()) {
        new BatchBufferActivityCheckerThread().start();
      }

    } catch (Exception e) {
      logger.error("Error initialising HEC Transport: " + ModularInput.getStackTrace(e));
    }
  }
Esempio n. 14
0
  protected ListenableFuture<HttpResponse> asyncExecuteHttp(HttpUriRequest request) {
    final SettableFuture<HttpResponse> future = SettableFuture.create();

    httpClient.execute(
        request,
        new FutureCallback<HttpResponse>() {
          public void completed(HttpResponse result) {
            future.set(result);
          }

          public void failed(Exception ex) {
            future.setException(ex);
          }

          public void cancelled() {
            future.setException(new InterruptedException());
          }
        });

    return future;
  }
Esempio n. 15
0
  /** public for unit test, not to be used otherwise */
  public void execute(
      HttpUriRequest httpUriRequest,
      ContentType contentType,
      FutureCallback<HttpResponse> callback) {

    // add accept header when its not a form or multipart
    final String contentTypeString = contentType.toString();
    if (!ContentType.APPLICATION_FORM_URLENCODED.getMimeType().equals(contentType.getMimeType())
        && !contentType.getMimeType().startsWith(MULTIPART_MIME_TYPE)) {
      // otherwise accept what is being sent
      httpUriRequest.addHeader(HttpHeaders.ACCEPT, contentTypeString);
    }
    // is something being sent?
    if (httpUriRequest instanceof HttpEntityEnclosingRequestBase
        && httpUriRequest.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) {
      httpUriRequest.addHeader(HttpHeaders.CONTENT_TYPE, contentTypeString);
    }

    // set user specified custom headers
    if (httpHeaders != null && !httpHeaders.isEmpty()) {
      for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
        httpUriRequest.setHeader(entry.getKey(), entry.getValue());
      }
    }

    // add client protocol version if not specified
    if (!httpUriRequest.containsHeader(ODataHttpHeaders.DATASERVICEVERSION)) {
      httpUriRequest.addHeader(ODataHttpHeaders.DATASERVICEVERSION, ODataServiceVersion.V20);
    }
    if (!httpUriRequest.containsHeader(MAX_DATA_SERVICE_VERSION)) {
      httpUriRequest.addHeader(MAX_DATA_SERVICE_VERSION, ODataServiceVersion.V30);
    }

    // execute request
    client.execute(httpUriRequest, callback);
  }
  /**
   * Creates a hit source that doesn't make the remote request and instead returns data from some
   * files. Also requests are always returned synchronously rather than asynchronously.
   */
  @SuppressWarnings("unchecked")
  private RemoteScrollableHitSource sourceWithMockedRemoteCall(
      boolean mockRemoteVersion, String... paths) throws Exception {
    URL[] resources = new URL[paths.length];
    for (int i = 0; i < paths.length; i++) {
      resources[i] =
          Thread.currentThread()
              .getContextClassLoader()
              .getResource("responses/" + paths[i].replace("fail:", ""));
      if (resources[i] == null) {
        throw new IllegalArgumentException("Couldn't find [" + paths[i] + "]");
      }
    }

    CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class);
    when(httpClient.<HttpResponse>execute(
            any(HttpAsyncRequestProducer.class),
            any(HttpAsyncResponseConsumer.class),
            any(FutureCallback.class)))
        .thenAnswer(
            new Answer<Future<HttpResponse>>() {

              int responseCount = 0;

              @Override
              public Future<HttpResponse> answer(InvocationOnMock invocationOnMock)
                  throws Throwable {
                // Throw away the current thread context to simulate running async httpclient's
                // thread pool
                threadPool.getThreadContext().stashContext();
                HttpAsyncRequestProducer requestProducer =
                    (HttpAsyncRequestProducer) invocationOnMock.getArguments()[0];
                FutureCallback<HttpResponse> futureCallback =
                    (FutureCallback<HttpResponse>) invocationOnMock.getArguments()[2];
                HttpEntityEnclosingRequest request =
                    (HttpEntityEnclosingRequest) requestProducer.generateRequest();
                URL resource = resources[responseCount];
                String path = paths[responseCount++];
                ProtocolVersion protocolVersion = new ProtocolVersion("http", 1, 1);
                if (path.startsWith("fail:")) {
                  String body =
                      Streams.copyToString(
                          new InputStreamReader(
                              request.getEntity().getContent(), StandardCharsets.UTF_8));
                  if (path.equals("fail:rejection.json")) {
                    StatusLine statusLine =
                        new BasicStatusLine(
                            protocolVersion, RestStatus.TOO_MANY_REQUESTS.getStatus(), "");
                    BasicHttpResponse httpResponse = new BasicHttpResponse(statusLine);
                    futureCallback.completed(httpResponse);
                  } else {
                    futureCallback.failed(new RuntimeException(body));
                  }
                } else {
                  StatusLine statusLine = new BasicStatusLine(protocolVersion, 200, "");
                  HttpResponse httpResponse = new BasicHttpResponse(statusLine);
                  httpResponse.setEntity(
                      new InputStreamEntity(
                          resource.openStream(),
                          randomBoolean() ? ContentType.APPLICATION_JSON : null));
                  futureCallback.completed(httpResponse);
                }
                return null;
              }
            });
    return sourceWithMockedClient(mockRemoteVersion, httpClient);
  }