List of usage examples for org.apache.http.client.protocol HttpClientContext setAttribute
public void setAttribute(String str, Object obj)
From source file:com.lxf.spider.client.ClientExecuteSOCKS.java
public static void main(String[] args) throws Exception { Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", new MyConnectionSocketFactory()).build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg); CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build(); try {//from w ww . j av a 2 s . c om InetSocketAddress socksaddr = new InetSocketAddress("mysockshost", 1234); HttpClientContext context = HttpClientContext.create(); context.setAttribute("socks.address", socksaddr); HttpHost target = new HttpHost("localhost", 80, "http"); HttpGet request = new HttpGet("/"); System.out.println("Executing request " + request + " to " + target + " via SOCKS proxy " + socksaddr); CloseableHttpResponse response = httpclient.execute(target, request); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:interoperabilite.webservice.client.ClientExecuteSOCKS.java
public static void main(String[] args) throws Exception { Registry<ConnectionSocketFactory> reg = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", new MyConnectionSocketFactory()).build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(reg); CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build(); try {//www. ja v a 2 s .c o m InetSocketAddress socksaddr = new InetSocketAddress("mysockshost", 1234); HttpClientContext context = HttpClientContext.create(); context.setAttribute("socks.address", socksaddr); HttpHost target = new HttpHost("httpbin.org", 80, "http"); HttpGet request = new HttpGet("/"); System.out.println("Executing request " + request + " to " + target + " via SOCKS proxy " + socksaddr); CloseableHttpResponse response = httpclient.execute(target, request, context); try { System.out.println("----------------------------------------"); System.out.println(response.getStatusLine()); EntityUtils.consume(response.getEntity()); } finally { response.close(); } } finally { httpclient.close(); } }
From source file:com.ksc.http.apache.utils.ApacheUtils.java
/** * Returns a new HttpClientContext used for request execution. *//* w w w . ja v a2 s . co m*/ 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); return clientContext; }
From source file:org.opensaml.security.httpclient.HttpClientSecuritySupport.java
/** * Set the supplied attribute value in the client context. * /*from w w w . jav a 2 s. c om*/ * @param context the client context instance * @param attributeName the context attribute name to * @param attributeValue the context attribute value to set, may be null * @param replace whether a non-null argument value should replace an existing context value */ public static void setContextValue(@Nonnull final HttpClientContext context, @Nonnull final String attributeName, @Nullable Object attributeValue, boolean replace) { if (attributeValue == null) { return; } Constraint.isNotNull(context, "HttpClientContext was null"); Constraint.isNotNull(attributeName, "Context attribute name was null"); if (replace || context.getAttribute(attributeName) == null) { context.setAttribute(attributeName, attributeValue); } }
From source file:com.ibm.team.build.internal.hjplugin.util.HttpUtils.java
/** * Creates and returns a new HttpContext with a new cookie store, for use with the singleton HTTP_CLIENT. * /*ww w. j a v a 2 s.c o m*/ * @return a new HttpContext */ private static HttpClientContext createHttpContext() { CookieStore cookieStore = new BasicCookieStore(); HttpClientContext httpContext = new HttpClientContext(); httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); return httpContext; }
From source file:org.dcache.srm.client.GsiHttpClientSender.java
@Override protected HttpClientContext createHttpContext(MessageContext msgContext, URI uri) { HttpClientContext context = super.createHttpContext(msgContext, uri); context.setAttribute(HttpClientTransport.TRANSPORT_HTTP_DELEGATION, msgContext.getProperty(HttpClientTransport.TRANSPORT_HTTP_DELEGATION)); return context; }
From source file:org.pentaho.di.trans.ael.websocket.SessionConfigurator.java
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; }
From source file:org.opensaml.security.httpclient.HttpClientSecuritySupportTest.java
@Test(dataProvider = "tlsCredentialEvaluatedData") public void testCheckTLSCredentialEvaluated(MockTrustEngine trustEngine, String scheme, Boolean trusted, boolean shouldPass) { HttpClientContext context = new HttpClientContext(); context.setAttribute(CONTEXT_KEY_TRUST_ENGINE, trustEngine); context.setAttribute(CONTEXT_KEY_SERVER_TLS_CREDENTIAL_TRUSTED, trusted); if (shouldPass) { try {/* w w w. ja v a 2 s .c o m*/ HttpClientSecuritySupport.checkTLSCredentialEvaluated(context, scheme); } catch (SSLPeerUnverifiedException e) { Assert.fail("Exception thrown unexpectedly"); } } else { try { HttpClientSecuritySupport.checkTLSCredentialEvaluated(context, scheme); Assert.fail("Exception was expected but not seen"); } catch (SSLPeerUnverifiedException e) { // expected } } }
From source file:org.opensaml.security.httpclient.HttpClientSecuritySupportTest.java
@Test public void testMarshalSecurityParametersWithReplacement() { HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(new BasicCredentialsProvider()); context.setAttribute(CONTEXT_KEY_TRUST_ENGINE, new MockTrustEngine()); context.setAttribute(CONTEXT_KEY_CRITERIA_SET, new CriteriaSet()); context.setAttribute(CONTEXT_KEY_TLS_PROTOCOLS, Lists.newArrayList("foo")); context.setAttribute(CONTEXT_KEY_TLS_CIPHER_SUITES, Lists.newArrayList("foo")); context.setAttribute(CONTEXT_KEY_CLIENT_TLS_CREDENTIAL, new BasicX509Credential(cert)); context.setAttribute(CONTEXT_KEY_HOSTNAME_VERIFIER, new StrictHostnameVerifier()); HttpClientSecurityParameters params = new HttpClientSecurityParameters(); params.setCredentialsProvider(new BasicCredentialsProvider()); params.setTLSTrustEngine(new MockTrustEngine()); params.setTLSCriteriaSet(new CriteriaSet()); params.setTLSProtocols(Lists.newArrayList("foo")); params.setTLSCipherSuites(Lists.newArrayList("foo")); params.setClientTLSCredential(new BasicX509Credential(cert)); params.setHostnameVerifier(new StrictHostnameVerifier()); HttpClientSecuritySupport.marshalSecurityParameters(context, params, true); Assert.assertSame(context.getCredentialsProvider(), params.getCredentialsProvider()); Assert.assertSame(context.getAttribute(CONTEXT_KEY_TRUST_ENGINE), params.getTLSTrustEngine()); Assert.assertSame(context.getAttribute(CONTEXT_KEY_CRITERIA_SET), params.getTLSCriteriaSet()); Assert.assertSame(context.getAttribute(CONTEXT_KEY_TLS_PROTOCOLS), params.getTLSProtocols()); Assert.assertSame(context.getAttribute(CONTEXT_KEY_TLS_CIPHER_SUITES), params.getTLSCipherSuites()); Assert.assertSame(context.getAttribute(CONTEXT_KEY_CLIENT_TLS_CREDENTIAL), params.getClientTLSCredential()); Assert.assertSame(context.getAttribute(CONTEXT_KEY_HOSTNAME_VERIFIER), params.getHostnameVerifier()); }
From source file:com.ibm.team.build.internal.hjplugin.util.HttpUtils.java
/** * For Basic auth, configure the context to do pre-emptive auth with the * credentials given./*www . j a va 2s . c o m*/ * @param httpContext The context in use for the requests. * @param serverURI The RTC server we will be authenticating against * @param userId The userId to login with * @param password The password for the User * @param listener A listen to log messages to, Not required * @throws IOException Thrown if anything goes wrong */ private static void handleBasicAuthChallenge(HttpClientContext httpContext, String serverURI, String userId, String password, TaskListener listener) throws IOException { URI uri; try { uri = new URI(serverURI); } catch (URISyntaxException e) { throw new IOException(Messages.HttpUtils_invalid_server(serverURI), e); } HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(userId, password)); httpContext.setAttribute(HttpClientContext.CREDS_PROVIDER, credsProvider); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(target, basicAuth); // Add AuthCache to the execution context httpContext.setAuthCache(authCache); }