Search in sources :

Example 1 with HttpRoute

use of org.apache.http.conn.routing.HttpRoute in project cas by apereo.

the class SimpleHttpClientFactoryBean method buildHttpClient.

/**
     * Build a HTTP client based on the current properties.
     *
     * @return the built HTTP client
     */
private CloseableHttpClient buildHttpClient() {
    try {
        final ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
        final LayeredConnectionSocketFactory sslsf = this.sslSocketFactory;
        final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", plainsf).register("https", sslsf).build();
        final PoolingHttpClientConnectionManager connMgmr = new PoolingHttpClientConnectionManager(registry);
        connMgmr.setMaxTotal(this.maxPooledConnections);
        connMgmr.setDefaultMaxPerRoute(this.maxConnectionsPerRoute);
        connMgmr.setValidateAfterInactivity(DEFAULT_TIMEOUT);
        final HttpHost httpHost = new HttpHost(InetAddress.getLocalHost());
        final HttpRoute httpRoute = new HttpRoute(httpHost);
        connMgmr.setMaxPerRoute(httpRoute, MAX_CONNECTIONS_PER_ROUTE);
        final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(this.readTimeout).setConnectTimeout(Long.valueOf(this.connectionTimeout).intValue()).setConnectionRequestTimeout(Long.valueOf(this.connectionTimeout).intValue()).setCircularRedirectsAllowed(this.circularRedirectsAllowed).setRedirectsEnabled(this.redirectsEnabled).setAuthenticationEnabled(this.authenticationEnabled).build();
        final HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connMgmr).setDefaultRequestConfig(requestConfig).setSSLSocketFactory(sslsf).setSSLHostnameVerifier(this.hostnameVerifier).setRedirectStrategy(this.redirectionStrategy).setDefaultCredentialsProvider(this.credentialsProvider).setDefaultCookieStore(this.cookieStore).setConnectionReuseStrategy(this.connectionReuseStrategy).setConnectionBackoffStrategy(this.connectionBackoffStrategy).setServiceUnavailableRetryStrategy(this.serviceUnavailableRetryStrategy).setProxyAuthenticationStrategy(this.proxyAuthenticationStrategy).setDefaultHeaders(this.defaultHeaders).useSystemProperties();
        return builder.build();
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
        throw Throwables.propagate(e);
    }
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute) RequestConfig(org.apache.http.client.config.RequestConfig) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) LayeredConnectionSocketFactory(org.apache.http.conn.socket.LayeredConnectionSocketFactory) HttpHost(org.apache.http.HttpHost) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Example 2 with HttpRoute

use of org.apache.http.conn.routing.HttpRoute in project dropwizard by dropwizard.

the class HttpClientBuilderTest method checkProxy.

private CloseableHttpClient checkProxy(HttpClientConfiguration config, HttpHost target, HttpHost expectedProxy) throws Exception {
    CloseableHttpClient httpClient = builder.using(config).build("test");
    HttpRoutePlanner routePlanner = (HttpRoutePlanner) FieldUtils.getField(httpClient.getClass(), "routePlanner", true).get(httpClient);
    HttpRoute route = routePlanner.determineRoute(target, new HttpGet(target.toURI()), new BasicHttpContext());
    assertThat(route.getProxyHost()).isEqualTo(expectedProxy);
    assertThat(route.getTargetHost()).isEqualTo(target);
    assertThat(route.getHopCount()).isEqualTo(expectedProxy != null ? 2 : 1);
    return httpClient;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpRoute(org.apache.http.conn.routing.HttpRoute) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet)

Example 3 with HttpRoute

use of org.apache.http.conn.routing.HttpRoute in project pinpoint by naver.

the class ManagedClientConnectionOpenMethodInterceptor method doInBeforeTrace.

@Override
protected void doInBeforeTrace(SpanEventRecorder recorder, Object target, Object[] args) {
    if (args != null && args.length >= 1 && args[0] != null && args[0] instanceof HttpRoute) {
        final HttpRoute route = (HttpRoute) args[0];
        final StringBuilder sb = new StringBuilder();
        if (route.getProxyHost() != null) {
            sb.append(route.getProxyHost().getHostName());
            if (route.getProxyHost().getPort() > 0) {
                sb.append(":").append(route.getProxyHost().getPort());
            }
        } else {
            if (route.getTargetHost() != null) {
                sb.append(route.getTargetHost().getHostName());
                if (route.getTargetHost().getPort() > 0) {
                    sb.append(":").append(route.getTargetHost().getPort());
                }
            }
        }
        recorder.recordAttribute(AnnotationKey.HTTP_INTERNAL_DISPLAY, sb.toString());
    }
    recorder.recordApi(methodDescriptor);
    recorder.recordServiceType(HttpClient4Constants.HTTP_CLIENT_4_INTERNAL);
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute)

Example 4 with HttpRoute

use of org.apache.http.conn.routing.HttpRoute in project robovm by robovm.

the class DefaultHttpRoutePlanner method determineRoute.

// non-javadoc, see interface HttpRoutePlanner
public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
    if (request == null) {
        throw new IllegalStateException("Request must not be null.");
    }
    // If we have a forced route, we can do without a target.
    HttpRoute route = ConnRouteParams.getForcedRoute(request.getParams());
    if (route != null)
        return route;
    if (target == null) {
        throw new IllegalStateException("Target host must not be null.");
    }
    final InetAddress local = ConnRouteParams.getLocalAddress(request.getParams());
    final HttpHost proxy = ConnRouteParams.getDefaultProxy(request.getParams());
    final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
    // as it is typically used for TLS/SSL, we assume that
    // a layered scheme implies a secure connection
    final boolean secure = schm.isLayered();
    if (proxy == null) {
        route = new HttpRoute(target, local, secure);
    } else {
        route = new HttpRoute(target, local, proxy, secure);
    }
    return route;
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute) Scheme(org.apache.http.conn.scheme.Scheme) HttpHost(org.apache.http.HttpHost) InetAddress(java.net.InetAddress)

Example 5 with HttpRoute

use of org.apache.http.conn.routing.HttpRoute in project robovm by robovm.

the class ProxySelectorRoutePlanner method determineRoute.

// non-javadoc, see interface HttpRoutePlanner
public HttpRoute determineRoute(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
    if (request == null) {
        throw new IllegalStateException("Request must not be null.");
    }
    // If we have a forced route, we can do without a target.
    HttpRoute route = ConnRouteParams.getForcedRoute(request.getParams());
    if (route != null)
        return route;
    if (target == null) {
        throw new IllegalStateException("Target host must not be null.");
    }
    final InetAddress local = ConnRouteParams.getLocalAddress(request.getParams());
    // BEGIN android-changed
    //     If the client or request explicitly specifies a proxy (or no
    //     proxy), prefer that over the ProxySelector's VM-wide default.
    HttpHost proxy = (HttpHost) request.getParams().getParameter(ConnRoutePNames.DEFAULT_PROXY);
    if (proxy == null) {
        proxy = determineProxy(target, request, context);
    } else if (ConnRouteParams.NO_HOST.equals(proxy)) {
        // value is explicitly unset
        proxy = null;
    }
    // END android-changed
    final Scheme schm = this.schemeRegistry.getScheme(target.getSchemeName());
    // as it is typically used for TLS/SSL, we assume that
    // a layered scheme implies a secure connection
    final boolean secure = schm.isLayered();
    if (proxy == null) {
        route = new HttpRoute(target, local, secure);
    } else {
        route = new HttpRoute(target, local, proxy, secure);
    }
    return route;
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute) Scheme(org.apache.http.conn.scheme.Scheme) HttpHost(org.apache.http.HttpHost) InetAddress(java.net.InetAddress)

Aggregations

HttpRoute (org.apache.http.conn.routing.HttpRoute)33 HttpHost (org.apache.http.HttpHost)17 HttpRequest (org.apache.http.HttpRequest)9 AbortableHttpRequest (org.apache.http.client.methods.AbortableHttpRequest)8 BasicHttpRequest (org.apache.http.message.BasicHttpRequest)8 InetAddress (java.net.InetAddress)7 IOException (java.io.IOException)6 HttpException (org.apache.http.HttpException)6 Scheme (org.apache.http.conn.scheme.Scheme)6 HttpResponse (org.apache.http.HttpResponse)5 AuthenticationException (org.apache.http.auth.AuthenticationException)5 RedirectException (org.apache.http.client.RedirectException)5 ClientConnectionRequest (org.apache.http.conn.ClientConnectionRequest)5 HttpParams (org.apache.http.params.HttpParams)5 InterruptedIOException (java.io.InterruptedIOException)4 URI (java.net.URI)4 Header (org.apache.http.Header)4 HttpEntity (org.apache.http.HttpEntity)4 CredentialsProvider (org.apache.http.client.CredentialsProvider)4 NonRepeatableRequestException (org.apache.http.client.NonRepeatableRequestException)4