Java Code Examples for com.ning.http.client.AsyncHttpClientConfig#Builder

The following examples show how to use com.ning.http.client.AsyncHttpClientConfig#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AsyncUtil.java    From httpsig-java with The Unlicense 6 votes vote down vote up
/**
 * Executes and replays a login request until one is found which satisfies the
 * {@link net.adamcin.httpsig.api.Challenge} being returned by the server, or until there are no more keys in the
 * keychain.
 *
 * @since 1.0.4
 *
 * @param client the {@link AsyncHttpClient} to which the {@link Signer} will be attached
 * @param signer the {@link Signer} used for login and subsequent signature authentication
 * @param loginRequest the login {@link Request} to be executed and replayed while rotating the keychain
 * @param responseHandler an {@link AsyncCompletionHandler} of type {@code T}
 * @param calcBefore provide another {@link SignatureCalculator} to call (such as a Content-MD5 generator) prior to
 *                   generating the signature for authentication.
 * @param <T> type parameter for completion handler
 * @return a {@link Future} of type {@code T}
 * @throws IOException if thrown by a login request
 */
public static <T> Future<T> login(final AsyncHttpClient client,
                         final Signer signer,
                         final Request loginRequest,
                         final AsyncCompletionHandler<T> responseHandler,
                         final SignatureCalculator calcBefore) throws IOException {

    final AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder(client.getConfig());
    configBuilder.addResponseFilter(new RotateAndReplayResponseFilter(signer));
    AsyncHttpClient loginClient = new AsyncHttpClient(configBuilder.build());

    enableAuth(loginClient, signer, calcBefore);
    Future<T> response = loginClient.executeRequest(loginClient
                                                            .prepareRequest(loginRequest)
                                                            .setUrl(loginRequest.getUrl()).build(),
                                                    responseHandler);
    enableAuth(client, signer, calcBefore);
    return response;
}
 
Example 2
Source File: MiosUnitConnector.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @param unit
 *            The host to connect to. Give a reachable hostname or IP address, without protocol or port
 */
public MiosUnitConnector(MiosUnit unit, MiosBinding binding) {
    logger.debug("Constructor: unit '{}', binding '{}'", unit, binding);

    this.unit = unit;
    this.binding = binding;

    Builder builder = new AsyncHttpClientConfig.Builder();
    builder.setRequestTimeoutInMs(unit.getTimeout());

    // Use the JDK Provider for now, we're not looking for server-level
    // scalability, and we'd like to lighten the load for folks wanting to
    // run atop RPi units.
    this.client = new AsyncHttpClient(new JDKAsyncHttpProvider(builder.build()));

    pollCall = new LongPoll();
    pollThread = new Thread(pollCall);
}
 
Example 3
Source File: HttpClient.java    From ob1k with Apache License 2.0 6 votes vote down vote up
/**
 * Creates new HttpClient from the configuration set
 *
 * @return new HttpClient instance
 */
public HttpClient build() {

  final AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder().
    setConnectTimeout(connectionTimeout).
    setMaxRequestRetry(retries).
    setRequestTimeout(requestTimeout).
    setCompressionEnforced(compressionEnforced).
    setDisableUrlEncodingForBoundedRequests(disableUrlEncoding).
    setMaxConnectionsPerHost(maxConnectionsPerHost).
    setMaxConnections(maxTotalConnections).
    setAsyncHttpClientProviderConfig(NettyConfigHolder.INSTANCE).
    setFollowRedirect(followRedirect).
    setAcceptAnyCertificate(acceptAnySslCertificate);

  if (readTimeout != null) {
    configBuilder.setReadTimeout(readTimeout);
  }

  return new HttpClient(new AsyncHttpClient(configBuilder.build()), responseMaxSize, marshallingStrategy);
}
 
Example 4
Source File: GrizzlyClientCustomizer.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Override
public AsyncHttpClientConfig.Builder customize(
    Client client, Configuration config, AsyncHttpClientConfig.Builder configBuilder
) {
  if (useProxy && !StringUtils.isEmpty(username)) {
    Realm realm = new Realm.RealmBuilder().setScheme(Realm.AuthScheme.BASIC)
        .setUsePreemptiveAuth(true)
        .setTargetProxy(true)
        .setPrincipal(username)
        .setPassword(password)
        .build();

    configBuilder.setRealm(realm);
  }
  return configBuilder;
}
 
Example 5
Source File: AsyncHttpClientHelper.java    From riposte with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor that gives you maximum control over configuration and behavior.
 *
 * @param builder
 *     The builder that will create the {@link #asyncHttpClient} and execute all the async downstream HTTP
 *     requests.
 * @param performSubSpanAroundDownstreamCalls
 *     Pass in true to have a distributed tracing subspan added to each downstream call to measure the time spent
 *     on the downstream call, false if you do not want subspans performed. The subspans can be used to determine
 *     how much time is spent processing in your app vs. waiting for downstream requests.
 */
public AsyncHttpClientHelper(AsyncHttpClientConfig.Builder builder, boolean performSubSpanAroundDownstreamCalls) {
    this.performSubSpanAroundDownstreamCalls = performSubSpanAroundDownstreamCalls;

    Map<String, String> mdcContextMap = MDC.getCopyOfContextMap();
    Deque<Span> distributedTraceStack = null;

    try {
        // We have to unlink tracing and MDC from the current thread before we setup the async http client library,
        //      otherwise all the internal threads it uses to do its job will be attached to the current thread's
        //      trace/MDC info forever and always.
        distributedTraceStack = Tracer.getInstance().unregisterFromThread();
        MDC.clear();
        AsyncHttpClientConfig cf = builder.build();
        asyncHttpClient = new AsyncHttpClient(cf);
    }
    finally {
        // Reattach the original tracing and MDC before we leave
        if (mdcContextMap == null)
            MDC.clear();
        else
            MDC.setContextMap(mdcContextMap);

        Tracer.getInstance().registerWithThread(distributedTraceStack);
    }
}
 
Example 6
Source File: AsyncHttpClientHelperTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@DataProvider(value = {
    "true",
    "false"
}, splitBy = "\\|")
@Test
public void kitchen_sink_constructor_sets_up_underlying_client_with_expected_config(boolean performSubspan) {
    // given
    int customRequestTimeoutVal = 4242;
    AsyncHttpClientConfig config =
        new AsyncHttpClientConfig.Builder().setRequestTimeout(customRequestTimeoutVal).build();
    AsyncHttpClientConfig.Builder builderMock = mock(AsyncHttpClientConfig.Builder.class);
    doReturn(config).when(builderMock).build();

    // when
    AsyncHttpClientHelper instance = new AsyncHttpClientHelper(builderMock, performSubspan);

    // then
    assertThat(instance.performSubSpanAroundDownstreamCalls).isEqualTo(performSubspan);
    assertThat(instance.asyncHttpClient.getConfig()).isSameAs(config);
    assertThat(instance.asyncHttpClient.getConfig().getRequestTimeout()).isEqualTo(customRequestTimeoutVal);
}
 
Example 7
Source File: ParsecAsyncHttpClient.java    From parsec-libraries with Apache License 2.0 5 votes vote down vote up
/**
 * Private constructor.
 * @param ningClientConfigBuilder Ning client config builder
 * @param cacheExpireAfterWrite cache expire time
 * @param cacheMaximumSize cache maximum size
 */
private ParsecAsyncHttpClient(
    final AsyncHttpClientConfig.Builder ningClientConfigBuilder,
    int cacheRefreshAfterWrite,
    int cacheExpireAfterWrite,
    int cacheMaximumSize,
    boolean enableProfilingFilter,
    boolean recordCacheStats,
    Optional<Long> retryIntervalMillisOpt
) {
    ParsecAsyncHttpResponseLoadingCache.Builder cacheBuilder = new ParsecAsyncHttpResponseLoadingCache.Builder(this)
            .expireAfterWrite(cacheExpireAfterWrite, TimeUnit.SECONDS)
            .maximumSize(cacheMaximumSize);

    if(cacheRefreshAfterWrite > 0)
        cacheBuilder.refreshAfterWrite(cacheRefreshAfterWrite, TimeUnit.SECONDS);

    if (recordCacheStats) {
        cacheBuilder.recordStats();
    }

    responseLoadingCache = cacheBuilder.build();

    if (enableProfilingFilter) {
        //so that there's only one filter.
        ningClientConfigBuilder.removeRequestFilter(PROFILING_FILTER);
        ningClientConfigBuilder.addRequestFilter(PROFILING_FILTER);
        oldFashionProfiling = false;
    }

    this.ningClientConfig = ningClientConfigBuilder.build();

    executorService = (ThreadPoolExecutor) ningClientConfig.executorService();
    client = new AsyncHttpClient(ningClientConfig);

    this.retryIntervalMillisOpt = retryIntervalMillisOpt;
}
 
Example 8
Source File: BaragonServiceModule.java    From Baragon with Apache License 2.0 5 votes vote down vote up
@Provides
@Singleton
@Named(BARAGON_SERVICE_HTTP_CLIENT)
public AsyncHttpClient providesHttpClient(HttpClientConfiguration config) {
  AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();

  builder.setMaxRequestRetry(config.getMaxRequestRetry());
  builder.setRequestTimeout(config.getRequestTimeoutInMs());
  builder.setFollowRedirect(true);
  builder.setConnectTimeout(config.getConnectionTimeoutInMs());
  builder.setUserAgent(config.getUserAgent());

  return new AsyncHttpClient(builder.build());
}
 
Example 9
Source File: SchedulerEventReceiver.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private void openAndReceive(final SchedulerEventListener eventListener, boolean myEventsOnly,
        SchedulerEvent... events) throws IOException {
    Client client = ClientFactory.getDefault().newClient();
    AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
    if (insecure) {
        builder = builder.setAcceptAnyCertificate(true)
                         .setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    }
    AsyncHttpClientConfig httpClientConfig = builder.build();
    AsyncHttpClient httpClient = new AsyncHttpClient(httpClientConfig);

    socket = client.create(client.newOptionsBuilder().runtime(httpClient).reconnect(false).build());
    EventNotificationHandler notificationHandler = new EventNotificationHandler(eventListener);
    socket.on(Event.MESSAGE, notificationHandler);
    socket.on(Event.CLOSE, new Function() {
        public void on(Object t) {
            SchedulerEventReceiver.logger.info("#### Websocket connection is closed ####");
            if (eventListener instanceof DisconnectionAwareSchedulerEventListener) {
                ((DisconnectionAwareSchedulerEventListener) eventListener).notifyDisconnection();
            }
        }
    });
    // initialize the connection
    RequestBuilder requestBuilder = client.newRequestBuilder();
    requestBuilder.method(Request.METHOD.GET);
    requestBuilder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
    // authentication header
    requestBuilder.header("sessionid", sessionId);
    requestBuilder.uri(eventingUrl(restServerUrl));
    requestBuilder.encoder(new EventSubscriptionEncoder());
    requestBuilder.decoder(new EventNotificationDecoder());
    requestBuilder.transport(Request.TRANSPORT.WEBSOCKET);
    socket.open(requestBuilder.build());
    // submit subscription request
    EventSubscription eventSubscription = new EventSubscription(myEventsOnly, asStringArray(events));
    socket.fire(EventCodecUtil.toJsonString(eventSubscription));
}
 
Example 10
Source File: ParsecAsyncHttpClientTest.java    From parsec-libraries with Apache License 2.0 5 votes vote down vote up
@Test
public void testSettingRetryIntervalWillAffectRetryBehavior() throws Exception {
    ThreadPoolExecutor executorService = mock(ThreadPoolExecutor.class);
    AsyncHttpClientConfig clientConfig = mock(AsyncHttpClientConfig.class);
    when(clientConfig.executorService()).thenReturn(executorService);
    AsyncHttpClientConfig.Builder configBuilder = mock(AsyncHttpClientConfig.Builder.class);
    when(configBuilder.build()).thenReturn(clientConfig);

    client = new ParsecAsyncHttpClient.Builder(configBuilder)
        .setRetryIntervalInMilliSeconds(100)
        .build();

    ParsecAsyncHttpRequest request = new ParsecAsyncHttpRequest.Builder()
        .setCriticalGet(true)
        .setUrl(baseUrl + "/sleep/25")
        .addRetryException(TimeoutException.class)
        .setRequestTimeout(10)
        .setMaxRetries(2)
        .build();

    CompletableFuture completableFuture = client.execute(request);
    assertNotNull(completableFuture);
    assertTrue(completableFuture instanceof ParsecCompletableFuture);

    ArgumentCaptor<ParsecHttpRequestRetryCallable> argumentCaptor = ArgumentCaptor.forClass(ParsecHttpRequestRetryCallable.class);
    verify(executorService, times(1)).submit(argumentCaptor.capture());
    ParsecHttpRequestRetryCallable retryCallable = argumentCaptor.getValue();
    assertEquals(100, retryCallable.getRetryDelayer().getRetryIntervalMillis());
}
 
Example 11
Source File: ApplyQueueServiceImpl.java    From qconfig with MIT License 5 votes vote down vote up
private AsyncHttpClient getHttpClient() {
    MapConfig config = MapConfig.get("config.properties");
    config.asMap();
    config.addListener(conf -> {
        REQUESTTIMEOUTMS = Numbers.toInt(conf.get("qtable.validate.timeoutMs"), DEFAULTTIMEOUT);
        logger.info("qtable validate timeout ms is [{}]", REQUESTTIMEOUTMS);
    });

    AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
    builder.setConnectTimeout(CONNECTTIMEOUT);
    builder.setRequestTimeout(REQUESTTIMEOUT);
    return new AsyncHttpClient(builder.build());
}
 
Example 12
Source File: AsyncHttpClientHelperTest.java    From riposte with Apache License 2.0 4 votes vote down vote up
@DataProvider(value = {
    "true   |   true",
    "true   |   false",
    "false  |   true",
    "false  |   false"
}, splitBy = "\\|")
@Test
public void constructor_clears_out_tracing_and_mdc_info_before_building_underlying_client_and_resets_afterward(
    boolean emptyBeforeCall, boolean explode
) {
    AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().build();
    AsyncHttpClientConfig.Builder builderMock = mock(AsyncHttpClientConfig.Builder.class);
    List<Span> traceAtTimeOfBuildCall = new ArrayList<>();
    List<Map<String, String>> mdcAtTimeOfBuildCall = new ArrayList<>();
    RuntimeException explodeEx = new RuntimeException("kaboom");
    doAnswer(invocation -> {
        traceAtTimeOfBuildCall.add(Tracer.getInstance().getCurrentSpan());
        mdcAtTimeOfBuildCall.add(MDC.getCopyOfContextMap());
        if (explode)
            throw explodeEx;
        return config;
    }).when(builderMock).build();

    Span spanBeforeCall = (emptyBeforeCall) ? null : Tracer.getInstance().startRequestWithRootSpan("foo");
    Map<String, String> mdcBeforeCall = MDC.getCopyOfContextMap();
    assertThat(Tracer.getInstance().getCurrentSpan()).isEqualTo(spanBeforeCall);
    if (emptyBeforeCall)
        assertThat(mdcBeforeCall).isNull();
    else
        assertThat(mdcBeforeCall).isNotEmpty();

    // when
    Throwable ex = catchThrowable(() -> new AsyncHttpClientHelper(builderMock, true));

    // then
    verify(builderMock).build();
    assertThat(traceAtTimeOfBuildCall).hasSize(1);
    assertThat(traceAtTimeOfBuildCall.get(0)).isNull();
    assertThat(mdcAtTimeOfBuildCall).hasSize(1);
    assertThat(mdcAtTimeOfBuildCall.get(0)).isNull();

    assertThat(Tracer.getInstance().getCurrentSpan()).isEqualTo(spanBeforeCall);
    assertThat(MDC.getCopyOfContextMap()).isEqualTo(mdcBeforeCall);

    if (explode)
        assertThat(ex).isSameAs(explodeEx);
}
 
Example 13
Source File: SingularityExecutorModule.java    From Singularity with Apache License 2.0 4 votes vote down vote up
@Provides
@Singleton
public LocalDownloadServiceFetcher provideDownloadFetcher(
  SingularityS3Configuration s3Configuration,
  SingularityExecutorConfiguration executorConfiguration,
  ObjectMapper objectMapper
) {
  if (s3Configuration.getLocalDownloadSocket().isPresent()) {
    HttpClient httpClient = new HttpClient(
      new HttpClientTransportOverUnixSockets(
        s3Configuration.getLocalDownloadSocket().get()
      ),
      null
    );
    return new UnixLocalDownloadServiceFetcher(
      httpClient,
      objectMapper,
      executorConfiguration,
      s3Configuration
    );
  } else {
    AsyncHttpClientConfig.Builder configBldr = new AsyncHttpClientConfig.Builder();
    configBldr.setRequestTimeout(
      (int) executorConfiguration.getLocalDownloadServiceTimeoutMillis()
    );
    configBldr.setPooledConnectionIdleTimeout(
      (int) executorConfiguration.getLocalDownloadServiceTimeoutMillis()
    );
    configBldr.addRequestFilter(
      new ThrottleRequestFilter(
        executorConfiguration.getLocalDownloadServiceMaxConnections()
      )
    );
    return new HttpLocalDownloadServiceFetcher(
      new AsyncHttpClient(configBldr.build()),
      objectMapper,
      executorConfiguration,
      s3Configuration
    );
  }
}
 
Example 14
Source File: DenonConnector.java    From openhab1-addons with Eclipse Public License 2.0 4 votes vote down vote up
private AsyncHttpClientConfig createAsyncHttpClientConfig() {
    Builder builder = new AsyncHttpClientConfig.Builder();
    builder.setRequestTimeoutInMs(REQUEST_TIMEOUT_MS);
    builder.setUseRawUrl(true);
    return builder.build();
}
 
Example 15
Source File: XbmcConnector.java    From openhab1-addons with Eclipse Public License 2.0 4 votes vote down vote up
private AsyncHttpClientConfig createAsyncHttpClientConfig() {
    Builder builder = new AsyncHttpClientConfig.Builder();
    builder.setRealm(createRealm());
    builder.setRequestTimeoutInMs(REQUEST_TIMEOUT_MS);
    return builder.build();
}
 
Example 16
Source File: PlexConnector.java    From openhab1-addons with Eclipse Public License 2.0 4 votes vote down vote up
private AsyncHttpClientConfig createAsyncHttpClientConfig() {
    Builder builder = new AsyncHttpClientConfig.Builder();
    builder.setRequestTimeoutInMs(REQUEST_TIMEOUT_MS);
    return builder.build();
}
 
Example 17
Source File: ParsecAsyncHttpClient.java    From parsec-libraries with Apache License 2.0 4 votes vote down vote up
/**
 * Constructor.
 */
public Builder() {
    this(new AsyncHttpClientConfig.Builder());
}
 
Example 18
Source File: SSLFactory.java    From tez with Apache License 2.0 3 votes vote down vote up
/**
 * Set ssl context for {@link com.ning.http.client.AsyncHttpClientConfig.Builder}
 *
 * @param asyncNingBuilder {@link com.ning.http.client.AsyncHttpClientConfig.Builder} instance to
 *                configure.
 * @throws IOException if an IO error occurred.
 */
public void configure(AsyncHttpClientConfig.Builder asyncNingBuilder) throws IOException {
  if (asyncNingBuilder != null) {
    asyncNingBuilder.setSSLContext(context);
    asyncNingBuilder.setHostnameVerifier(getHostnameVerifier());
  }
}
 
Example 19
Source File: ParsecAsyncHttpClient.java    From parsec-libraries with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor.
 * @param configBuilder config builder
 */
public Builder(final AsyncHttpClientConfig.Builder configBuilder) {
    this.configBuilder = configBuilder;
}
 
Example 20
Source File: ParsecAsyncHttpClient.java    From parsec-libraries with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor.
 * @param config config
 */
public Builder(final AsyncHttpClientConfig config) {
    this(new AsyncHttpClientConfig.Builder(config));
}