org.apache.http.HttpResponseInterceptor Java Examples

The following examples show how to use org.apache.http.HttpResponseInterceptor. 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: HTTPSession.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static synchronized void setGlobalCompression(String compressors) {
  if (globalsettings.get(Prop.COMPRESSION) != null)
    removeGlobalCompression();
  String compresslist = checkCompressors(compressors);
  if (HTTPUtil.nullify(compresslist) == null)
    throw new IllegalArgumentException("Bad compressors: " + compressors);
  globalsettings.put(Prop.COMPRESSION, compresslist);
  HttpResponseInterceptor hrsi;
  if (compresslist.contains("gzip")) {
    hrsi = new GZIPResponseInterceptor();
    rspintercepts.add(hrsi);
  }
  if (compresslist.contains("deflate")) {
    hrsi = new DeflateResponseInterceptor();
    rspintercepts.add(hrsi);
  }
}
 
Example #2
Source File: HTTPSession.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static synchronized void removeGlobalCompression() {
  if (globalsettings.remove(Prop.COMPRESSION) != null) {
    for (int i = rspintercepts.size() - 1; i >= 0; i--) { // walk backwards
      HttpResponseInterceptor hrsi = rspintercepts.get(i);
      if (hrsi instanceof GZIPResponseInterceptor || hrsi instanceof DeflateResponseInterceptor)
        rspintercepts.remove(i);
    }
  }
}
 
Example #3
Source File: HTTPSession.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static HTTPUtil.InterceptResponse debugResponseInterceptor() {
  if (!TESTING)
    throw new UnsupportedOperationException();
  for (HttpResponseInterceptor hri : rspintercepts) {
    if (hri instanceof HTTPUtil.InterceptResponse)
      return ((HTTPUtil.InterceptResponse) hri);
  }
  return null;
}
 
Example #4
Source File: HttpClientConfigTests.java    From vividus with Apache License 2.0 5 votes vote down vote up
@Test
void testGetAndSetLastResponseInterceptor()
{
    HttpResponseInterceptor interceptor = mock(HttpResponseInterceptor.class);
    config.setLastResponseInterceptor(interceptor);
    assertEquals(interceptor, config.getLastResponseInterceptor());
}
 
Example #5
Source File: SofaTracerHttpClientBuilder.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
public static HttpAsyncClientBuilder asyncClientBuilder(HttpAsyncClientBuilder httpAsyncClientBuilder,
                                                        String currentApp, String targetApp) {
    SofaTracerAsyncHttpInterceptor interceptor = new SofaTracerAsyncHttpInterceptor(
        getHttpClientTracer(), currentApp, targetApp);
    return httpAsyncClientBuilder.addInterceptorFirst((HttpRequestInterceptor) interceptor)
        .addInterceptorFirst((HttpResponseInterceptor) interceptor);
}
 
Example #6
Source File: SSLCertificateLoader.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private HttpResponseInterceptor getHttpResponseInterceptor() {
	return new HttpResponseInterceptor() {
		@Override
		public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
			ManagedHttpClientConnection routedConnection = (ManagedHttpClientConnection)context.getAttribute(HttpCoreContext.HTTP_CONNECTION);
            SSLSession sslSession = routedConnection.getSSLSession();
            if (sslSession != null) {
                Certificate[] certificates = sslSession.getPeerCertificates();
                context.setAttribute(PEER_CERTIFICATES, certificates);
            }
		}
	};
}
 
Example #7
Source File: WingtipsApacheHttpClientInterceptorTest.java    From wingtips with Apache License 2.0 5 votes vote down vote up
public HttpClientBuilderInterceptors(HttpClientBuilder builder) {
    this.firstRequestInterceptors =
        (List<HttpRequestInterceptor>) Whitebox.getInternalState(builder, "requestFirst");
    this.lastRequestInterceptors =
        (List<HttpRequestInterceptor>) Whitebox.getInternalState(builder, "requestLast");
    this.firstResponseInterceptors =
        (List<HttpResponseInterceptor>) Whitebox.getInternalState(builder, "responseFirst");
    this.lastResponseInterceptors =
        (List<HttpResponseInterceptor>) Whitebox.getInternalState(builder, "responseLast");
}
 
Example #8
Source File: WingtipsApacheHttpClientInterceptorTest.java    From wingtips with Apache License 2.0 5 votes vote down vote up
@DataProvider(value = {
    "true",
    "false"
})
@Test
public void addTracingInterceptors_double_arg_works_as_expected(boolean subspanOptionOn) {
    // given
    HttpClientBuilder builder = HttpClientBuilder.create();

    // when
    addTracingInterceptors(builder, subspanOptionOn);

    // then
    HttpClientBuilderInterceptors builderInterceptors = new HttpClientBuilderInterceptors(builder);

    assertThat(builderInterceptors.firstRequestInterceptors).hasSize(1);
    assertThat(builderInterceptors.lastResponseInterceptors).hasSize(1);

    HttpRequestInterceptor requestInterceptor = builderInterceptors.firstRequestInterceptors.get(0);
    HttpResponseInterceptor responseInterceptor = builderInterceptors.lastResponseInterceptors.get(0);
    assertThat(requestInterceptor).isInstanceOf(WingtipsApacheHttpClientInterceptor.class);
    assertThat(responseInterceptor).isInstanceOf(WingtipsApacheHttpClientInterceptor.class);

    assertThat(((WingtipsApacheHttpClientInterceptor) requestInterceptor).surroundCallsWithSubspan)
        .isEqualTo(subspanOptionOn);
    assertThat(((WingtipsApacheHttpClientInterceptor) responseInterceptor).surroundCallsWithSubspan)
        .isEqualTo(subspanOptionOn);

    assertThat(builderInterceptors.lastRequestInterceptors).isNullOrEmpty();
    assertThat(builderInterceptors.firstResponseInterceptors).isNullOrEmpty();

    if (subspanOptionOn) {
        assertThat(builderInterceptors.firstRequestInterceptors).containsExactly(DEFAULT_REQUEST_IMPL);
        assertThat(builderInterceptors.lastResponseInterceptors).containsExactly(DEFAULT_RESPONSE_IMPL);
    }
}
 
Example #9
Source File: HttpClientWrapper.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * 
 * @return HttpProcessor with WebManager specific clients
 */
protected BasicHttpProcessor createHttpProcessor() {
	BasicHttpProcessor httpproc = new BasicHttpProcessor();

	/**
	 * In this section, add interceptors
	 */

	PerformanceMonitoringInterceptor performanceMonitoringInterceptor = new PerformanceMonitoringInterceptor();

	performanceMonitoringInterceptor
			.setMetricsCollector(this.metricsCollector);

	httpproc.addInterceptor((HttpRequestInterceptor) performanceMonitoringInterceptor);

	httpproc.addInterceptor((HttpResponseInterceptor) performanceMonitoringInterceptor);

	return httpproc;
}
 
Example #10
Source File: Twitter.java    From streams with Apache License 2.0 4 votes vote down vote up
private Twitter(TwitterConfiguration configuration) throws InstantiationException {
  this.configuration = configuration;
  this.rootUrl = TwitterProviderUtil.baseUrl(configuration);
  this.oauthInterceptor = new TwitterOAuthRequestInterceptor(configuration.getOauth());
  this.httpclient = HttpClientBuilder.create()
    .setDefaultRequestConfig(
      RequestConfig.custom()
        .setConnectionRequestTimeout(5000)
        .setConnectTimeout(5000)
        .setSocketTimeout(5000)
        .setCookieSpec("easy")
        .build()
    )
    .setMaxConnPerRoute(20)
    .setMaxConnTotal(100)
    .addInterceptorFirst(oauthInterceptor)
    .addInterceptorLast((HttpRequestInterceptor) (httpRequest, httpContext) -> LOGGER.debug(httpRequest.getRequestLine().getUri()))
    .addInterceptorLast((HttpResponseInterceptor) (httpResponse, httpContext) -> LOGGER.debug(httpResponse.getStatusLine().toString()))
    .build();
  this.restClientBuilder = RestClient.create()
    .httpClient(httpclient, true)
    .parser(
      JsonParser.DEFAULT.builder()
        .ignoreUnknownBeanProperties(true)
        .pojoSwaps(TwitterJodaDateSwap.class)
        .build())
    .serializer(
      JsonSerializer.DEFAULT.builder()
        .pojoSwaps(TwitterJodaDateSwap.class)
        .trimEmptyCollections(true)
        .trimEmptyMaps(true)
        .build())
    .rootUrl(rootUrl)
    .retryable(
      configuration.getRetryMax().intValue(),
      configuration.getRetrySleepMs().intValue(),
      new TwitterRetryHandler());
  if( configuration.getDebug() ) {
    restClientBuilder = restClientBuilder.debug();
  }

  this.restClient = restClientBuilder.build();
  this.mapper = StreamsJacksonMapper.getInstance();
}
 
Example #11
Source File: TestHttpServer.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
public TestHttpServer responseInterceptors(List<HttpResponseInterceptor> interceptors) {
    checkNotStarted();
    this.responseInterceptors = interceptors;
    return this;
}
 
Example #12
Source File: CertificateRetriever.java    From nexus-public with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Retrieves certificate chain of specified host:port using https protocol.
 *
 * @param host to get certificate chain from (cannot be null)
 * @param port of host to connect to
 * @return certificate chain
 * @throws Exception Re-thrown from accessing the remote host
 */
public Certificate[] retrieveCertificatesFromHttpsServer(final String host, final int port) throws Exception {
  checkNotNull(host);

  log.info("Retrieving certificate from https://{}:{}", host, port);

  // setup custom connection manager so we can configure SSL to trust-all
  SSLContext sc = SSLContext.getInstance("TLS");
  sc.init(trustStore.getKeyManagers(), new TrustManager[]{ACCEPT_ALL_TRUST_MANAGER}, null);
  SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sc, NoopHostnameVerifier.INSTANCE);
  Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
      .register(HttpSchemes.HTTP, PlainConnectionSocketFactory.getSocketFactory())
      .register(HttpSchemes.HTTPS, sslSocketFactory).build();
  final HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(registry);

  try {
    final AtomicReference<Certificate[]> certificates = new AtomicReference<>();

    HttpClient httpClient = httpClientManager.create(new Customizer()
    {
      @Override
      public void customize(final HttpClientPlan plan) {
        // replace connection-manager with customized version needed to fetch SSL certificates
        plan.getClient().setConnectionManager(connectionManager);

        // add interceptor to grab peer-certificates
        plan.getClient().addInterceptorFirst(new HttpResponseInterceptor()
        {
          @Override
          public void process(final HttpResponse response, final HttpContext context)
              throws HttpException, IOException
          {
            ManagedHttpClientConnection connection =
                HttpCoreContext.adapt(context).getConnection(ManagedHttpClientConnection.class);

            // grab the peer-certificates from the session
            if (connection != null) {
              SSLSession session = connection.getSSLSession();
              if (session != null) {
                certificates.set(session.getPeerCertificates());
              }
            }
          }
        });
      }
    });

    httpClient.execute(new HttpGet("https://" + host + ":" + port));

    return certificates.get();
  }
  finally {
    // shutdown single-use connection manager
    connectionManager.shutdown();
  }
}
 
Example #13
Source File: ApacheHttpClientWithWingtipsComponentTest.java    From wingtips 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 verify_HttpClient_with_WingtipsApacheHttpClientInterceptor_traced_correctly(
    boolean spanAlreadyExistsBeforeCall, boolean subspanOptionOn
) throws IOException {

    // given
    String pathTemplateForTags = "/some/path/template/" + UUID.randomUUID().toString();

    WingtipsApacheHttpClientInterceptor interceptor = new WingtipsApacheHttpClientInterceptor(
        subspanOptionOn,
        ZipkinHttpTagStrategy.getDefaultInstance(),
        new ApacheHttpClientTagAdapterWithHttpRouteKnowledge(pathTemplateForTags)
    );

    Span parent = null;
    if (spanAlreadyExistsBeforeCall) {
        parent = Tracer.getInstance().startRequestWithRootSpan("somePreexistingParentSpan");
    }

    HttpClient httpClient = HttpClientBuilder
        .create()
        .addInterceptorFirst((HttpRequestInterceptor) interceptor)
        .addInterceptorLast((HttpResponseInterceptor) interceptor)
        .build();

    // We always expect at least one span to be completed as part of the call: the server span.
    //      We may or may not have a second span completed depending on the value of subspanOptionOn.
    int expectedNumSpansCompleted = (subspanOptionOn) ? 2 : 1;

    String fullRequestUrl = "http://localhost:" + SERVER_PORT + ENDPOINT_PATH;

    // when
    HttpResponse response = httpClient.execute(new HttpGet(fullRequestUrl));

    // then
    assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
    assertThat(responsePayloadToString(response)).isEqualTo(ENDPOINT_PAYLOAD);
    verifySpansCompletedAndReturnedInResponse(
        response, SLEEP_TIME_MILLIS, expectedNumSpansCompleted, parent, subspanOptionOn
    );

    if (subspanOptionOn) {
        verifySpanNameAndTags(
            findApacheHttpClientSpanFromCompletedSpans(),
            "GET " + pathTemplateForTags,
            "GET",
            ENDPOINT_PATH,
            fullRequestUrl,
            pathTemplateForTags,
            response.getStatusLine().getStatusCode(),
            "apache.httpclient"
        );
    }

    if (parent != null) {
        parent.close();
    }
}
 
Example #14
Source File: ApacheHttpClientFactory.java    From ibm-cos-sdk-java with Apache License 2.0 4 votes vote down vote up
@Override
public ConnectionManagerAwareHttpClient create(HttpClientSettings settings) {
    final HttpClientBuilder builder = HttpClients.custom();
    // Note that it is important we register the original connection manager with the
    // IdleConnectionReaper as it's required for the successful deregistration of managers
    // from the reaper. See https://github.com/aws/aws-sdk-java/issues/722.
    final HttpClientConnectionManager cm = cmFactory.create(settings);

    builder.setRequestExecutor(new SdkHttpRequestExecutor())
            .setKeepAliveStrategy(buildKeepAliveStrategy(settings))
            .disableRedirectHandling()
            .disableAutomaticRetries()
            .setConnectionManager(ClientConnectionManagerFactory.wrap(cm));

    // By default http client enables Gzip compression. So we disable it
    // here.
    // Apache HTTP client removes Content-Length, Content-Encoding and
    // Content-MD5 headers when Gzip compression is enabled. Currently
    // this doesn't affect S3 or Glacier which exposes these headers.
    //
    if (!(settings.useGzip())) {
        builder.disableContentCompression();
    }

    HttpResponseInterceptor itcp = new CRC32ChecksumResponseInterceptor();
    if (settings.calculateCRC32FromCompressedData()) {
        builder.addInterceptorFirst(itcp);
    } else {
        builder.addInterceptorLast(itcp);
    }

    addProxyConfig(builder, settings);

    final ConnectionManagerAwareHttpClient httpClient = new SdkHttpClient(builder.build(), cm);

    if (settings.useReaper()) {
        IdleConnectionReaper.registerConnectionManager(cm, settings.getMaxIdleConnectionTime());
    }

    return httpClient;
}
 
Example #15
Source File: MicrometerHttpClientInterceptor.java    From micrometer with Apache License 2.0 4 votes vote down vote up
public HttpResponseInterceptor getResponseInterceptor() {
    return responseInterceptor;
}
 
Example #16
Source File: HttpClientConfig.java    From vividus with Apache License 2.0 4 votes vote down vote up
public void setLastResponseInterceptor(HttpResponseInterceptor lastResponseInterceptor)
{
    this.lastResponseInterceptor = lastResponseInterceptor;
}
 
Example #17
Source File: HttpClientConfig.java    From vividus with Apache License 2.0 4 votes vote down vote up
public HttpResponseInterceptor getLastResponseInterceptor()
{
    return lastResponseInterceptor;
}
 
Example #18
Source File: WingtipsApacheHttpClientInterceptor.java    From wingtips with Apache License 2.0 3 votes vote down vote up
/**
 * Helper method for adding a default instance of this interceptor to the given builder's request *and* response
 * interceptors. The interceptors will have their subspan option set to the value of the given
 * {@code surroundCallsWithSubspan} argument.
 *
 * @param builder The builder to add the tracing interceptors to.
 * @param surroundCallsWithSubspan Pass in true to have requests surrounded in a subspan, false to disable the
 * subspan option.
 * @param <T> The type of the builder.
 * @return The same builder passed in, but with tracing interceptors added.
 */
public static <T extends HttpClientBuilder> T addTracingInterceptors(T builder, boolean surroundCallsWithSubspan) {
    WingtipsApacheHttpClientInterceptor interceptor = (surroundCallsWithSubspan)
                                                      ? DEFAULT_IMPL
                                                      : new WingtipsApacheHttpClientInterceptor(false);

    builder.addInterceptorFirst((HttpRequestInterceptor)interceptor);
    builder.addInterceptorLast((HttpResponseInterceptor)interceptor);

    return builder;
}