Exemple #1
0
 protected static void setInterceptors(HttpClientBuilder cb) {
   for (HttpRequestInterceptor hrq : reqintercepts) {
     cb.addInterceptorLast(hrq);
   }
   for (HttpResponseInterceptor hrs : rspintercepts) {
     cb.addInterceptorLast(hrs);
   }
   // Add debug interceptors
   for (HttpRequestInterceptor hrq : dbgreq) {
     cb.addInterceptorFirst(hrq);
   }
   for (HttpResponseInterceptor hrs : dbgrsp) {
     cb.addInterceptorFirst(hrs);
   }
   // Hack: add Content-Encoding suppressor
   cb.addInterceptorFirst(CEKILL);
 }
  @Override
  @Guarded(by = STARTED)
  public HttpClientBuilder prepare(final @Nullable Customizer customizer) {
    final HttpClientPlan plan = new HttpClientPlan();

    // attach connection manager early, so customizer has chance to replace it if needed
    plan.getClient().setConnectionManager(sharedConnectionManager);

    // apply defaults
    defaultsCustomizer.customize(plan);

    // apply globals
    new ConfigurationCustomizer(getConfigurationInternal()).customize(plan);

    // apply instance customization
    if (customizer != null) {
      customizer.customize(plan);
    }

    // apply plan to builder
    HttpClientBuilder builder = plan.getClient();
    builder.setDefaultConnectionConfig(plan.getConnection().build());
    builder.setDefaultSocketConfig(plan.getSocket().build());
    builder.setDefaultRequestConfig(plan.getRequest().build());
    builder.setDefaultCredentialsProvider(plan.getCredentials());

    builder.addInterceptorFirst(
        (HttpRequest request, HttpContext context) -> {
          // add custom http-context attributes
          for (Entry<String, Object> entry : plan.getAttributes().entrySet()) {
            // only set context attribute if not already set, to allow per request overrides
            if (context.getAttribute(entry.getKey()) == null) {
              context.setAttribute(entry.getKey(), entry.getValue());
            }
          }

          // add custom http-request headers
          for (Entry<String, String> entry : plan.getHeaders().entrySet()) {
            request.addHeader(entry.getKey(), entry.getValue());
          }
        });
    builder.addInterceptorLast(
        (HttpRequest httpRequest, HttpContext httpContext) -> {
          if (outboundLog.isDebugEnabled()) {
            httpContext.setAttribute(CTX_REQ_STOPWATCH, Stopwatch.createStarted());
            httpContext.setAttribute(CTX_REQ_URI, getRequestURI(httpContext));
            outboundLog.debug(
                "{} > {}", httpContext.getAttribute(CTX_REQ_URI), httpRequest.getRequestLine());
          }
        });
    builder.addInterceptorLast(
        (HttpResponse httpResponse, HttpContext httpContext) -> {
          Stopwatch stopwatch = (Stopwatch) httpContext.getAttribute(CTX_REQ_STOPWATCH);
          if (stopwatch != null) {
            outboundLog.debug(
                "{} < {} @ {}",
                httpContext.getAttribute(CTX_REQ_URI),
                httpResponse.getStatusLine(),
                stopwatch);
          }
        });

    return builder;
  }