org.apache.http.conn.routing.HttpRoute Java Examples

The following examples show how to use org.apache.http.conn.routing.HttpRoute. 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: ClientFactory.java    From galaxy-sdk-java with Apache License 2.0 6 votes vote down vote up
public static HttpClient generateHttpClient(final int maxTotalConnections,
                                            final int maxTotalConnectionsPerRoute, int connTimeout) {
  HttpParams params = new BasicHttpParams();
  ConnManagerParams.setMaxTotalConnections(params, maxTotalConnections);
  ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRoute() {
    @Override
    public int getMaxForRoute(HttpRoute route) {
      return maxTotalConnectionsPerRoute;
    }
  });
  HttpConnectionParams
      .setConnectionTimeout(params, connTimeout);
  SchemeRegistry schemeRegistry = new SchemeRegistry();
  schemeRegistry.register(
      new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
  SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
  sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
  ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schemeRegistry);
  return new DefaultHttpClient(conMgr, params);
}
 
Example #2
Source File: TracingProtocolExec.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public CloseableHttpResponse execute(HttpRoute route,
  org.apache.http.client.methods.HttpRequestWrapper req,
  HttpClientContext context, HttpExecutionAware execAware)
  throws IOException, HttpException {
  HttpRequestWrapper request = new HttpRequestWrapper(req, context.getTargetHost());
  Span span = tracer.nextSpan(httpSampler, request);
  context.setAttribute(Span.class.getName(), span);

  CloseableHttpResponse response = null;
  Throwable error = null;
  try (SpanInScope ws = tracer.withSpanInScope(span)) {
    return response = protocolExec.execute(route, req, context, execAware);
  } catch (Throwable e) {
    error = e;
    throw e;
  } finally {
    handler.handleReceive(new HttpResponseWrapper(response, context, error), span);
  }
}
 
Example #3
Source File: HttpProtocolParent.java    From dtsopensource with Apache License 2.0 6 votes vote down vote up
private CloseableHttpClient createHttpClient(String hostname, int port) {
    ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
    LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
            .register("http", plainsf).register("https", sslsf).build();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
    // 将最大连接数增加
    cm.setMaxTotal(maxTotal);
    // 将每个路由基础的连接增加
    cm.setDefaultMaxPerRoute(maxPerRoute);
    HttpHost httpHost = new HttpHost(hostname, port);
    // 将目标主机的最大连接数增加
    cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
    // 请求重试处理
    return HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).build();
}
 
Example #4
Source File: PingCheckMonitor.java    From ranger with Apache License 2.0 6 votes vote down vote up
/**
 * @param timeEntity                how often the {@link #monitor()} check needs to be executed
 * @param httpRequest               http request that will be called at regular intervals
 * @param pingTimeoutInMilliseconds timeout in milliseconds for http request execution (ping response)
 * @param pingWindowSize            rolling window frame, which needs to be maintained
 * @param maxFailures               maximum failures allowed in the rolling window frame
 * @param host                      host name (could be localhost)
 * @param port                      port
 */
public PingCheckMonitor(TimeEntity timeEntity,
                        HttpRequest httpRequest,
                        Integer pingTimeoutInMilliseconds,
                        Integer pingWindowSize,
                        Integer maxFailures,
                        String host,
                        Integer port) {
    super(PingCheckMonitor.class.getSimpleName(), timeEntity);
    this.httpRequest = httpRequest;
    this.pingTimeoutInMilliseconds = pingTimeoutInMilliseconds;
    this.host = host;
    this.port = port;
    this.rollingWindowHealthQueue = new RollingWindowHealthQueue(pingWindowSize, maxFailures);
    this.executorService = Executors.newSingleThreadExecutor();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost(host, port)), 2);
    this.httpClient = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .build();
}
 
Example #5
Source File: SimpleHttpClientConnectionManager.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
@Override
public void connect(
    HttpClientConnection connection,
    HttpRoute route,
    int connectTimeout,
    HttpContext context
) throws IOException {
  HttpHost host;

  if (route.getProxyHost() != null) {
    host = route.getProxyHost();
  } else {
    host = route.getTargetHost();
  }

  InetSocketAddress localAddress = route.getLocalSocketAddress();

  ManagedHttpClientConnection managed = (ManagedHttpClientConnection) connection;
  this.connectionOperator.connect(managed, host, localAddress, connectTimeout, this.socketConfig, context);
}
 
Example #6
Source File: LibRequestDirector.java    From YiBo with Apache License 2.0 6 votes vote down vote up
protected void rewriteRequestURI(
        final RequestWrapper request,
        final HttpRoute route) throws ProtocolException {
    try {

        URI uri = request.getURI();
        if (route.getProxyHost() != null && !route.isTunnelled()) {
            // Make sure the request URI is absolute
            if (!uri.isAbsolute()) {
                HttpHost target = route.getTargetHost();
                uri = URIUtils.rewriteURI(uri, target);
                request.setURI(uri);
            }
        } else {
            // Make sure the request URI is relative
            if (uri.isAbsolute()) {
                uri = URIUtils.rewriteURI(uri, null);
                request.setURI(uri);
            }
        }

    } catch (URISyntaxException ex) {
        throw new ProtocolException("Invalid URI: "
        		+ request.getRequestLine().getUri(), ex);
    }
}
 
Example #7
Source File: SimpleHttpClientConnectionManager.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
@Override
public ConnectionRequest requestConnection(HttpRoute route, Object state) {
  return new ConnectionRequest() {

    @Override
    public boolean cancel() {
      // Nothing to do.
      return false;
    }

    @Override
    public HttpClientConnection get(final long timeout, final TimeUnit timeUnit) {
      return connectionFactory.create(route, connectionConfig);
    }
  };
}
 
Example #8
Source File: PGPKeysServerClient.java    From pgpverify-maven-plugin with Apache License 2.0 6 votes vote down vote up
private void processOnRetry(RetryEvent event, Duration waitInterval,
                            HttpRoutePlanner planer, OnRetryConsumer onRetryConsumer) {

    InetAddress targetAddress = null;
    if (planer instanceof RoundRobinRouterPlaner) {
        // inform planer about error on last roue
        HttpRoute httpRoute = ((RoundRobinRouterPlaner)planer).lastRouteCauseError();
        targetAddress = Try.of(() -> httpRoute.getTargetHost().getAddress()).getOrElse((InetAddress) null);
    } else if (proxy != null) {
        targetAddress = Try.of(() -> InetAddress.getByName(proxy.getHost())).getOrElse((InetAddress) null);
    }

    // inform caller about retry
    if (onRetryConsumer != null) {
        onRetryConsumer.onRetry(targetAddress, event.getNumberOfRetryAttempts(),
                waitInterval, event.getLastThrowable());
    }
}
 
Example #9
Source File: TracingManagedHttpClientConnectionFactory.java    From caravan with Apache License 2.0 6 votes vote down vote up
@Override
public ManagedHttpClientConnection create(final HttpRoute route, final ConnectionConfig config) {
    final ConnectionConfig cconfig = config != null ? config : ConnectionConfig.DEFAULT;
    CharsetDecoder chardecoder = null;
    CharsetEncoder charencoder = null;
    final Charset charset = cconfig.getCharset();
    final CodingErrorAction malformedInputAction = cconfig.getMalformedInputAction() != null ? cconfig.getMalformedInputAction() : CodingErrorAction.REPORT;
    final CodingErrorAction unmappableInputAction = cconfig.getUnmappableInputAction() != null ? cconfig.getUnmappableInputAction()
            : CodingErrorAction.REPORT;
    if (charset != null) {
        chardecoder = charset.newDecoder();
        chardecoder.onMalformedInput(malformedInputAction);
        chardecoder.onUnmappableCharacter(unmappableInputAction);
        charencoder = charset.newEncoder();
        charencoder.onMalformedInput(malformedInputAction);
        charencoder.onUnmappableCharacter(unmappableInputAction);
    }
    final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
    return new TracingManagedHttpClientConnection(id, cconfig.getBufferSize(), cconfig.getFragmentSizeHint(), chardecoder, charencoder,
            cconfig.getMessageConstraints(), incomingContentStrategy, outgoingContentStrategy, requestWriterFactory, responseParserFactory, logFunc);
}
 
Example #10
Source File: ApacheHttpClientEdgeGridRoutePlanner.java    From AkamaiOPEN-edgegrid-java with Apache License 2.0 6 votes vote down vote up
@Override
public HttpRoute determineRoute(HttpHost host, HttpRequest request, HttpContext context) throws HttpException {
    try {
        ClientCredential clientCredential = binding.getClientCredentialProvider().getClientCredential(binding.map(request));
        String hostname = clientCredential.getHost();
        int port = -1;
        final int pos = hostname.lastIndexOf(":");
        if (pos > 0) {
            try {
                port = Integer.parseInt(hostname.substring(pos + 1));
            } catch (NumberFormatException ex) {
                throw new IllegalArgumentException("Host contains invalid port number: " + hostname);
            }
            hostname = hostname.substring(0, pos);
        }
        HttpHost target = new HttpHost(hostname, port, "https");
        return super.determineRoute(target, request, context);
    } catch (NoMatchingCredentialException e) {
        throw new RuntimeException(e);
    }
}
 
Example #11
Source File: RoundRobinRouterPlanerIT.java    From pgpverify-maven-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReturnTheSameAddressForSequentialCall() throws UnknownHostException, HttpException {

    InetAddress[] expected = InetAddress.getAllByName(TEST_HOST);

    RoundRobinRouterPlaner routerPlaner = new RoundRobinRouterPlaner();
    HttpHost httpHost = new HttpHost(TEST_HOST);

    // first call
    HttpRoute firstRoute = routerPlaner.determineRoute(httpHost, null, null);

    for (int i = 0; i < expected.length; i++) {
        HttpRoute httpRouteNext = routerPlaner.determineRoute(httpHost, null, null);
        assertEquals(httpRouteNext.getTargetHost().getAddress(), firstRoute.getTargetHost().getAddress());
    }
}
 
Example #12
Source File: ProxyFeedBackClientExecChain.java    From vscrawler with Apache License 2.0 6 votes vote down vote up
@Override
public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request, HttpClientContext clientContext,
        HttpExecutionAware execAware) throws IOException, HttpException {
    Proxy proxy = (Proxy) clientContext.getAttribute(VSCrawlerConstant.VSCRAWLER_AVPROXY_KEY);
    if (proxy != null) {
        proxy.recordUsage();
    }
    try {
        return delegate.execute(route, request, clientContext, execAware);
    } catch (IOException ioe) {
        if (proxy != null) {
            proxy.recordFailed();
        }
        throw ioe;
    }
}
 
Example #13
Source File: ExtendedJestClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void getAsyncConnectionManagerConfiguresMaxTotalPerRouteIfConfigured() {

    // given
    HttpClientConfig.Builder config = createDefaultTestHttpClientConfigBuilder();

    HttpRoute expectedHttpRoute = new HttpRoute(new HttpHost("localhost"));
    int expectedMaxTotalConnection = random.nextInt(100) + 10;
    config.maxTotalConnectionPerRoute(expectedHttpRoute, expectedMaxTotalConnection);

    WrappedHttpClientConfig.Builder builder = createDefaultTestWrappedHttpClientConfigBuilder(config.build());
    ExtendedJestClientFactory factory = spy(new ExtendedJestClientFactory(builder.build()));

    PoolingNHttpClientConnectionManager mockedNHttpConnectionManager = mock(PoolingNHttpClientConnectionManager.class);
    when(factory.createUnconfiguredPoolingNHttpClientConnectionManager())
            .thenReturn(mockedNHttpConnectionManager);

    // when
    factory.getAsyncConnectionManager();

    // then
    verify(mockedNHttpConnectionManager).setMaxPerRoute(eq(expectedHttpRoute), eq(expectedMaxTotalConnection));

}
 
Example #14
Source File: HttpContextUtils.java    From micrometer with Apache License 2.0 6 votes vote down vote up
static Tags generateTagsForRoute(HttpContext context) {
    String targetScheme = "UNKNOWN";
    String targetHost = "UNKNOWN";
    String targetPort = "UNKNOWN";
    Object routeAttribute = context.getAttribute("http.route");
    if (routeAttribute instanceof HttpRoute) {
        HttpHost host = ((HttpRoute) routeAttribute).getTargetHost();
        targetScheme = host.getSchemeName();
        targetHost = host.getHostName();
        targetPort = String.valueOf(host.getPort());
    }
    return Tags.of(
            "target.scheme", targetScheme,
            "target.host", targetHost,
            "target.port", targetPort
    );
}
 
Example #15
Source File: ExtendedHttpClientBuilder.java    From lavaplayer with Apache License 2.0 6 votes vote down vote up
private static HttpClientConnectionManager createDefaultConnectionManager(
    HttpClientConnectionOperator operator,
    HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connectionFactory
) {
  PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(
      operator,
      connectionFactory,
      -1,
      TimeUnit.MILLISECONDS
  );

  manager.setMaxTotal(3000);
  manager.setDefaultMaxPerRoute(1500);

  return manager;
}
 
Example #16
Source File: RestClient.java    From light with Apache License 2.0 6 votes vote down vote up
private CloseableHttpClient httpClient() throws Exception {

        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry());

        Map<String, Object> httpClientMap = (Map<String, Object>)configMap.get(REST_TEMPLATE);
        connectionManager.setMaxTotal((Integer)httpClientMap.get(MAX_CONNECTION_TOTAL));
        connectionManager.setDefaultMaxPerRoute((Integer) httpClientMap.get(MAX_CONNECTION_PER_ROUTE));
        // Now handle all the specific route defined.
        Map<String, Object> routeMap = (Map<String, Object>)httpClientMap.get(ROUTES);
        Iterator<String> it = routeMap.keySet().iterator();
        while (it.hasNext()) {
            String route = it.next();
            Integer maxConnection = (Integer)routeMap.get(route);
            connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost(
                    route)), maxConnection);
        }
        RequestConfig config = RequestConfig.custom()
                .setConnectTimeout((Integer)httpClientMap.get(TIMEOUT_MILLISECONDS))
                .build();

       return HttpClientBuilder.create()
                .setConnectionManager(connectionManager)
                .setDefaultRequestConfig(config).build();
    }
 
Example #17
Source File: MetricsClientFactory.java    From galaxy-sdk-java with Apache License 2.0 6 votes vote down vote up
public static HttpClient generateHttpClient(final int maxTotalConnections,
    final int maxTotalConnectionsPerRoute, int connTimeout) {
  HttpParams params = new BasicHttpParams();
  ConnManagerParams.setMaxTotalConnections(params, maxTotalConnections);
  ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRoute() {
    @Override
    public int getMaxForRoute(HttpRoute route) {
      return maxTotalConnectionsPerRoute;
    }
  });
  HttpConnectionParams
      .setConnectionTimeout(params, connTimeout);
  SchemeRegistry schemeRegistry = new SchemeRegistry();
  schemeRegistry.register(
      new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
  SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
  sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
  ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schemeRegistry);
  return new DefaultHttpClient(conMgr, params);
}
 
Example #18
Source File: ApacheHttpClientInstrumentation.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@Advice.OnMethodEnter(suppress = Throwable.class)
private static void onBeforeExecute(@Advice.Argument(0) HttpRoute route,
                                    @Advice.Argument(1) HttpRequestWrapper request,
                                    @Advice.Local("span") Span span) {
    if (tracer == null || tracer.getActive() == null) {
        return;
    }
    final AbstractSpan<?> parent = tracer.getActive();
    span = HttpClientHelper.startHttpClientSpan(parent, request.getMethod(), request.getURI(), route.getTargetHost().getHostName());
    TextHeaderSetter<HttpRequest> headerSetter = headerSetterHelperClassManager.getForClassLoaderOfClass(HttpRequest.class);
    TextHeaderGetter<HttpRequest> headerGetter = headerGetterHelperClassManager.getForClassLoaderOfClass(HttpRequest.class);
    if (span != null) {
        span.activate();
        if (headerSetter != null) {
            span.propagateTraceContext(request, headerSetter);
        }
    } else if (headerGetter != null && !TraceContext.containsTraceContextTextHeaders(request, headerGetter)
        && headerSetter != null && parent != null) {
        // re-adds the header on redirects
        parent.propagateTraceContext(request, headerSetter);
    }
}
 
Example #19
Source File: ExtendedJestClientFactory.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected NHttpClientConnectionManager getAsyncConnectionManager() {

    PoolingNHttpClientConnectionManager connectionManager = createUnconfiguredPoolingNHttpClientConnectionManager();

    HttpClientConfig httpClientConfig = this.wrappedHttpClientConfig.getHttpClientConfig();

    final Integer maxTotal = httpClientConfig.getMaxTotalConnection();
    if (maxTotal != null) {
        connectionManager.setMaxTotal(maxTotal);
    }
    final Integer defaultMaxPerRoute = httpClientConfig.getDefaultMaxTotalConnectionPerRoute();
    if (defaultMaxPerRoute != null) {
        connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
    }
    final Map<HttpRoute, Integer> maxPerRoute = httpClientConfig.getMaxTotalConnectionPerRoute();
    for (Map.Entry<HttpRoute, Integer> entry : maxPerRoute.entrySet()) {
        connectionManager.setMaxPerRoute(entry.getKey(), entry.getValue());
    }

    return connectionManager;
}
 
Example #20
Source File: HttpClientAdapter.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public HttpRoute determineRoute(final HttpHost host, final HttpRequest request,
        final HttpContext context) throws HttpException {

    Boolean ignoreNoProxy = (Boolean) context.getAttribute(IGNORE_NO_PROXY);
    URI requestUri = (URI) context.getAttribute(REQUEST_URI);

    if (proxyHost != null &&
            (Boolean.TRUE.equals(ignoreNoProxy) || useProxyFor(requestUri))) {
        if (log.isDebugEnabled()) {
            log.debug("Using proxy: " + proxyHost);
        }
        return super.determineRoute(host, request, context);
    }
    if (log.isDebugEnabled()) {
        log.debug("Using a direct connection (no proxy)");
    }
    // Return direct route
    return new HttpRoute(host);
}
 
Example #21
Source File: TracingMainExec.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public CloseableHttpResponse execute(HttpRoute route,
  org.apache.http.client.methods.HttpRequestWrapper request,
  HttpClientContext context, HttpExecutionAware execAware)
  throws IOException, HttpException {
  Span span = (Span) context.removeAttribute(Span.class.getName());

  if (span != null) {
    handler.handleSend(new HttpRequestWrapper(request, route.getTargetHost()), span);
  }

  CloseableHttpResponse response = mainExec.execute(route, request, context, execAware);
  if (span != null) {
    if (isRemote(context, span)) {
      if (serverName != null) span.remoteServiceName(serverName);
      parseTargetAddress(route.getTargetHost(), span);
    } else {
      span.kind(null); // clear as cache hit
    }
  }
  return response;
}
 
Example #22
Source File: AbstractRoutePlanner.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
@Override
public HttpRoute determineRoute(final HttpHost host, final HttpRequest request, final HttpContext context) throws HttpException {
  Args.notNull(request, "Request");
  if (host == null) {
    throw new ProtocolException("Target host is not specified");
  }
  final HttpClientContext clientContext = HttpClientContext.adapt(context);
  final RequestConfig config = clientContext.getRequestConfig();
  int remotePort;
  if (host.getPort() <= 0) {
    try {
      remotePort = schemePortResolver.resolve(host);
    } catch (final UnsupportedSchemeException e) {
      throw new HttpException(e.getMessage());
    }
  } else
    remotePort = host.getPort();

  final Tuple<Inet4Address, Inet6Address> remoteAddresses = IpAddressTools.getRandomAddressesFromHost(host);
  final Tuple<InetAddress, InetAddress> addresses = determineAddressPair(remoteAddresses);

  final HttpHost target = new HttpHost(addresses.r, host.getHostName(), remotePort, host.getSchemeName());
  final HttpHost proxy = config.getProxy();
  final boolean secure = target.getSchemeName().equalsIgnoreCase("https");
  clientContext.setAttribute(CHOSEN_IP_ATTRIBUTE, addresses.l);
  log.debug("Setting route context attribute to {}", addresses.l);
  if (proxy == null) {
    return new HttpRoute(target, addresses.l, secure);
  } else {
    return new HttpRoute(target, addresses.l, proxy, secure);
  }
}
 
Example #23
Source File: NFHttpClient.java    From ribbon with Apache License 2.0 5 votes vote down vote up
protected NFHttpClient(String host, int port){
	super(new ThreadSafeClientConnManager());
	this.name = "UNNAMED_" + numNonNamedHttpClients.incrementAndGet();
	httpHost = new HttpHost(host, port);
	httpRoute = new HttpRoute(httpHost);

	init(createDefaultConfig(), false);
}
 
Example #24
Source File: NamedConnectionPool.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Override
protected BasicPoolEntry getEntryBlocking(HttpRoute route, Object state,
        long timeout, TimeUnit tunit, WaitingThreadAborter aborter)
        throws ConnectionPoolTimeoutException, InterruptedException {
    Stopwatch stopWatch = requestTimer.start();
    try {
        return super.getEntryBlocking(route, state, timeout, tunit, aborter);
    } finally {
        stopWatch.stop();
    }
}
 
Example #25
Source File: HttpClientConnectionManagerConnectMethodInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
protected void doInBeforeTrace(SpanEventRecorder recorder, Object target, Object[] args) {
    if (args != null && args.length >= 2 && args[1] instanceof HttpRoute) {
        final HttpRoute route = (HttpRoute) args[1];
        final String hostAndPort = EndPointUtils.getHostAndPort(route);
        recorder.recordAttribute(AnnotationKey.HTTP_INTERNAL_DISPLAY, hostAndPort);
    }
    recorder.recordApi(methodDescriptor);
    recorder.recordServiceType(HttpClient4Constants.HTTP_CLIENT_4_INTERNAL);
}
 
Example #26
Source File: ManagedClientConnectionOpenMethodInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
protected void doInBeforeTrace(SpanEventRecorder recorder, Object target, Object[] args) {
    if (args != null && args.length >= 1 && args[0] instanceof HttpRoute) {
        final HttpRoute route = (HttpRoute) args[0];
        final String hostAndPort = EndPointUtils.getHostAndPort(route);
        recorder.recordAttribute(AnnotationKey.HTTP_INTERNAL_DISPLAY, hostAndPort);
    }
    recorder.recordApi(methodDescriptor);
    recorder.recordServiceType(HttpClient4Constants.HTTP_CLIENT_4_INTERNAL);
}
 
Example #27
Source File: EndPointUtilsTest.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
public void getHostAndPort() throws Exception {
    // TODO Support final class mocking
    HttpRoute httpRoute = mock(HttpRoute.class);
    HttpHost httpHost = mock(HttpHost.class);
    when(httpHost.getHostName()).thenReturn("127.0.0.1");
    when(httpHost.getPort()).thenReturn(-1);
    when(httpRoute.getProxyHost()).thenReturn(httpHost);
    String hostAndPort = EndPointUtils.getHostAndPort(httpRoute);
}
 
Example #28
Source File: LibRequestDirector.java    From YiBo with Apache License 2.0 5 votes vote down vote up
/**
 * Establish connection either directly or through a tunnel and retry in case of
 * a recoverable I/O failure
 */
private void tryConnect(
        final RoutedRequest req, final HttpContext context) throws HttpException, IOException {
    HttpRoute route = req.getRoute();

    int connectCount = 0;
    for (;;) {
        // Increment connect count
        connectCount++;
        try {
            if (!managedConn.isOpen()) {
                managedConn.open(route, context, params);
            } else {
                managedConn.setSocketTimeout(HttpConnectionParams.getSoTimeout(params));
            }
            establishRoute(route, context);
            break;
        } catch (IOException ex) {
            try {
                managedConn.close();
            } catch (IOException ignore) {
            }
            if (retryHandler.retryRequest(ex, connectCount, context)) {
            	if (DEBUG) {
            		Logger.debug("I/O exception ({}) caught when connecting to the target host: {}",
            				ex.getClass().getName(), ex.getMessage());
            		Logger.debug(ex.getMessage(), ex);
            		Logger.debug("Retrying connect");
            	}
            } else {
                throw ex;
            }
        }
    }
}
 
Example #29
Source File: SimpleHttpClientConnectionManager.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
public SimpleHttpClientConnectionManager(
    HttpClientConnectionOperator connectionOperator,
    HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> factory
) {
  this.connectionOperator = connectionOperator;
  this.connectionFactory = factory != null ? factory : ManagedHttpClientConnectionFactory.INSTANCE;
}
 
Example #30
Source File: SignatureExec.java    From wechatpay-apache-httpclient with Apache License 2.0 5 votes vote down vote up
@Override
public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request,
    HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException {
  if (request.getURI().getHost().endsWith(".mch.weixin.qq.com")) {
    return executeWithSignature(route, request, context, execAware);
  } else {
    return mainExec.execute(route, request, context, execAware);
  }
}