Java Code Examples for org.apache.http.client.protocol.HttpClientContext#setAttribute()

The following examples show how to use org.apache.http.client.protocol.HttpClientContext#setAttribute() . 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: ApacheUtils.java    From ibm-cos-sdk-java with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new HttpClientContext used for request execution.
 */
public static HttpClientContext newClientContext(HttpClientSettings settings,
                                                 Map<String, ? extends Object>
                                                         attributes) {
    final HttpClientContext clientContext = new HttpClientContext();

    if (attributes != null && !attributes.isEmpty()) {
        for (Map.Entry<String, ?> entry : attributes.entrySet()) {
            clientContext.setAttribute(entry.getKey(), entry.getValue());
        }
    }

    addPreemptiveAuthenticationProxy(clientContext, settings);

    RequestConfig.Builder builder = RequestConfig.custom();
    disableNormalizeUri(builder);

    clientContext.setRequestConfig(builder.build());
    clientContext.setAttribute(HttpContextUtils.DISABLE_SOCKET_PROXY_PROPERTY, settings.disableSocketProxy());
    return clientContext;

}
 
Example 2
Source File: HttpUriRequestConverter.java    From webmagic with Apache License 2.0 6 votes vote down vote up
private HttpClientContext convertHttpClientContext(Request request, Site site, Proxy proxy) {
    HttpClientContext httpContext = new HttpClientContext();
    if (proxy != null && proxy.getUsername() != null) {
        AuthState authState = new AuthState();
        authState.update(new BasicScheme(ChallengeState.PROXY), new UsernamePasswordCredentials(proxy.getUsername(), proxy.getPassword()));
        httpContext.setAttribute(HttpClientContext.PROXY_AUTH_STATE, authState);
    }
    if (request.getCookies() != null && !request.getCookies().isEmpty()) {
        CookieStore cookieStore = new BasicCookieStore();
        for (Map.Entry<String, String> cookieEntry : request.getCookies().entrySet()) {
            BasicClientCookie cookie1 = new BasicClientCookie(cookieEntry.getKey(), cookieEntry.getValue());
            cookie1.setDomain(UrlUtils.removePort(UrlUtils.getDomain(request.getUrl())));
            cookieStore.addCookie(cookie1);
        }
        httpContext.setCookieStore(cookieStore);
    }
    return httpContext;
}
 
Example 3
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 4
Source File: S3HttpRequestRetryHandlerTest.java    From cyberduck with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testRetryRequest() {
    final S3HttpRequestRetryHandler h = new S3HttpRequestRetryHandler(new JetS3tRequestAuthorizer() {
        @Override
        public void authorizeHttpRequest(final HttpUriRequest httpUriRequest, final HttpContext httpContext, final String s) {
            //
        }
    }, 1);
    final HttpClientContext context = new HttpClientContext();
    context.setAttribute(HttpCoreContext.HTTP_REQUEST, new HttpHead());
    assertTrue(h.retryRequest(new SSLException(new SocketException("Broken pipe")), 1, context));
}
 
Example 5
Source File: HttpContextRetryCounter.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
public void setRetryCount(HttpClientContext context, int value) {
  RetryCount count = context.getAttribute(attributeName, RetryCount.class);

  if (count == null) {
    count = new RetryCount();
    context.setAttribute(attributeName, count);
  }

  count.value = value;
}
 
Example 6
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 7
Source File: YoutubeIpRotatorFilter.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private void setRetryCount(HttpClientContext context, int value) {
  RetryCount count = context.getAttribute(RETRY_COUNT_ATTRIBUTE, RetryCount.class);

  if (count == null) {
    count = new RetryCount();
    context.setAttribute(RETRY_COUNT_ATTRIBUTE, count);
  }

  count.value = value;
}
 
Example 8
Source File: HttpClient.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Make an HTTP Get request routed over socks5 proxy.
 */
private String requestWithGETProxy(String param, Socks5Proxy socks5Proxy, @Nullable String headerKey, @Nullable String headerValue) throws IOException {
    log.debug("requestWithGETProxy param=" + param);
    // This code is adapted from:
    //  http://stackoverflow.com/a/25203021/5616248

    // Register our own SocketFactories to override createSocket() and connectSocket().
    // connectSocket does NOT resolve hostname before passing it to proxy.
    Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("http", new SocksConnectionSocketFactory())
            .register("https", new SocksSSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build();

    // Use FakeDNSResolver if not resolving DNS locally.
    // This prevents a local DNS lookup (which would be ignored anyway)
    PoolingHttpClientConnectionManager cm = socks5Proxy.resolveAddrLocally() ?
            new PoolingHttpClientConnectionManager(reg) :
            new PoolingHttpClientConnectionManager(reg, new FakeDnsResolver());
    try (CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build()) {
        InetSocketAddress socksAddress = new InetSocketAddress(socks5Proxy.getInetAddress(), socks5Proxy.getPort());

        // remove me: Use this to test with system-wide Tor proxy, or change port for another proxy.
        // InetSocketAddress socksAddress = new InetSocketAddress("127.0.0.1", 9050);

        HttpClientContext context = HttpClientContext.create();
        context.setAttribute("socks.address", socksAddress);

        HttpGet request = new HttpGet(baseUrl + param);
        if (headerKey != null && headerValue != null)
            request.setHeader(headerKey, headerValue);

        log.debug("Executing request " + request + " proxy: " + socksAddress);
        try (CloseableHttpResponse response = httpclient.execute(request, context)) {
            return convertInputStreamToString(response.getEntity().getContent());
        }
    } catch (Throwable t) {
        throw new IOException("Error at requestWithGETProxy with URL: " + (baseUrl + param) + ". Throwable=" + t.getMessage());
    }
}
 
Example 9
Source File: SessionConfigurator.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private HttpContext getContext( URI uri ) {
  HttpClientContext httpClientContext = HttpClientContext.create();
  //used by httpclient version >= 4.3
  httpClientContext
    .setAttribute( HttpClientContext.HTTP_ROUTE, new HttpRoute( new HttpHost( uri.getHost(), uri.getPort() ) ) );
  //used by httpclient version 4.2
  httpClientContext
    .setAttribute( HttpClientContext.HTTP_TARGET_HOST, new HttpHost( uri.getHost(), uri.getPort() ) );
  return httpClientContext;
}
 
Example 10
Source File: HttpClientService.java    From waterdrop with Apache License 2.0 3 votes vote down vote up
public static void execAsyncGet(String baseUrl, List<BasicNameValuePair> urlParams, FutureCallback callback)
		throws Exception {

	if (baseUrl == null) {
		logger.warn("we don't have base url, check config");
		throw new ConfigException("missing base url");
	}

	HttpRequestBase httpMethod = new HttpGet(baseUrl);
	
	CloseableHttpAsyncClient hc = null;

	try {
		hc = HttpClientFactory.getInstance().getHttpAsyncClientPool()
				.getAsyncHttpClient();

		hc.start();

		HttpClientContext localContext = HttpClientContext.create();
		BasicCookieStore cookieStore = new BasicCookieStore();

		if (null != urlParams) {

			String getUrl = EntityUtils.toString(new UrlEncodedFormEntity(urlParams));

			httpMethod.setURI(new URI(httpMethod.getURI().toString() + "?" + getUrl));
		}


		localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

		hc.execute(httpMethod, localContext, callback);

	} catch (Exception e) {
		e.printStackTrace();
	}

}