private CredentialsProvider makeCredentialsProvider(String host, int port, String username, String password) { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( new AuthScope(host, port), new UsernamePasswordCredentials(username, password) ); return credentialsProvider; }
public HttpClient createHttpClient( String user, String password, String proxyHost, int proxyPort, AuthScope authScope ) { HttpHost httpHost = new HttpHost( proxyHost, proxyPort ); RequestConfig requestConfig = RequestConfig.custom() .setProxy( httpHost ) .build(); CredentialsProvider provider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials( user, password ); provider.setCredentials( authScope, credentials ); return HttpClientBuilder .create() .setDefaultCredentialsProvider( provider ) .setDefaultRequestConfig( requestConfig ) .setConnectionManager( manager ) .build(); }
connectionManager.setDefaultMaxPerRoute(config.getMaxConnectionsPerHost()); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); if (config.getCookieStore() != null) { clientBuilder.setDefaultCookieStore(config.getCookieStore()); clientBuilder.setDefaultRequestConfig(requestConfig); clientBuilder.setConnectionManager(connectionManager); clientBuilder.setUserAgent(config.getUserAgentString()); if (config.getProxyHost() != null) { if (config.getProxyUsername() != null) { AuthScope authScope = new AuthScope(config.getProxyHost(), config.getProxyPort()); Credentials credentials = new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()); credentialsMap.put(authScope, credentials); HttpHost proxy = new HttpHost(config.getProxyHost(), config.getProxyPort()); clientBuilder.setProxy(proxy); logger.debug("Working through Proxy: {}", proxy.getHostName()); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsMap.forEach((AuthScope authscope, Credentials credentials) -> { credentialsProvider.setCredentials(authscope, credentials); }); clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setConnectionManager(getConnectionManager(client, config, sslContext)); clientBuilder.setConnectionManagerShared( PropertiesHelper.getValue(config.getProperties(), ApacheClientProperties.CONNECTION_MANAGER_SHARED, false, null)); clientBuilder.setSslcontext(sslContext); if (proxyUri != null) { final URI u = getProxyUri(proxyUri); final HttpHost proxy = new HttpHost(u.getHost(), u.getPort(), u.getScheme()); final String userName; userName = ClientProperties.getValue(config.getProperties(), ClientProperties.PROXY_USERNAME, String.class); final CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(u.getHost(), u.getPort()), new UsernamePasswordCredentials(userName, password) ); clientBuilder.setDefaultCredentialsProvider(credsProvider);
final HttpHost target = HttpHost.create(crowdServer.toString()); final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setDefaultMaxPerRoute(crowdProps.getHttpMaxConnections()); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(crowdProps.getApplicationName(), crowdProps.getApplicationPassword())); final AuthCache authCache = new BasicAuthCache(); authCache.put(target, new BasicScheme()); proxyString = proxyHost.toString(); if (StringUtils.isNotBlank(crowdProps.getHttpProxyUsername()) || StringUtils.isNotBlank(crowdProps.getHttpProxyPassword())) { credentialsProvider.setCredentials(new AuthScope(crowdProps.getHttpProxyHost(), crowdProps.getHttpProxyPort()), new UsernamePasswordCredentials(crowdProps.getHttpProxyUsername(), crowdProps.getHttpProxyPassword())); client = HttpClientBuilder.create() .setConnectionManager(connectionManager) .setDefaultCredentialsProvider(credentialsProvider) .setRoutePlanner(httpRoutePlanner) .build();
this.configuration = configuration; this.executor = Executors.newFixedThreadPool(configuration.getConnectionPoolSize()); target = new HttpHost(configuration.getHost(), configuration.getPort(), configuration.getSsl() ? "https" : "http"); HttpClientBuilder builder = HttpClients.custom(); if (configuration.getUsername() != null && configuration.getPassword() != null) { LOG.info("Adding basic auth credentials provider"); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(configuration.getUsername(), configuration.getPassword()) ); builder.setDefaultCredentialsProvider(credsProvider); sslBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslBuilder.build()); builder.setSSLSocketFactory(sslsf); } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) { LOG.error("Failed to init socket factory {}", ex.getMessage(), ex); cm.setDefaultMaxPerRoute(configuration.getConnectionPoolSize()); cm.setMaxTotal(configuration.getConnectionPoolSize()); builder.setConnectionManager(cm); this.client = builder.build();
final HttpClientBuilder hcBuilder = HttpClientBuilder.create(); final RequestConfig.Builder rcBuilder = RequestConfig.custom(); final RequestBuilder reqBuilder = RequestBuilder.create( hcBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)); proxy.acquire(); if (proxy.isEnabled()) { final HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort(), "http"); if (proxy.isAuthEnabled()) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(proxy.getHost(), proxy.getPort()), new UsernamePasswordCredentials(proxy.getUsername(), new String(proxy.getPassword()))); hcBuilder.setDefaultCredentialsProvider(credsProvider); hcBuilder.setProxy(proxyHost); String realm = StringUtil.isEmpty(a.getRealm()) ? AuthScope.ANY_REALM : a.getRealm(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(host, urlPort, realm), new UsernamePasswordCredentials(uid, pwd)); hcBuilder.setDefaultCredentialsProvider(credsProvider); String pwd = new String(a.getPassword());
HttpClientContext localContext = null; try { HttpHost target = new HttpHost( host, port, schema ); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope( target.getHostName(), target.getPort() ), new UsernamePasswordCredentials( user, password ) );
schemeRegistry.register(new Scheme("http", Integer.parseInt(port), ssf)); PoolingClientConnectionManager ccm = new PoolingClientConnectionManager(schemeRegistry); httpClient = new DefaultHttpClient(ccm); httpHost = new HttpHost(hosts[0], Integer.parseInt(port), "http"); ((AbstractHttpClient) httpClient).getCredentialsProvider().setCredentials( new AuthScope(hosts[0], Integer.parseInt(port)), new UsernamePasswordCredentials(userName, password)); ((DefaultHttpClient) httpClient).addRequestInterceptor(new HttpRequestInterceptor() ((DefaultHttpClient) httpClient).addResponseInterceptor(new HttpResponseInterceptor()
public CloudianClient(final String host, final Integer port, final String scheme, final String username, final String password, final boolean validateSSlCertificate, final int timeout) throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { final CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); final HttpHost adminHost = new HttpHost(host, port, scheme); final AuthCache authCache = new BasicAuthCache(); authCache.put(adminHost, new BasicScheme()); this.adminApiUrl = adminHost.toURI(); this.httpContext = HttpClientContext.create(); this.httpContext.setCredentialsProvider(provider); sslcontext.init(null, new X509TrustManager[]{new TrustAllManager()}, new SecureRandom()); final SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(sslcontext, NoopHostnameVerifier.INSTANCE); this.httpClient = HttpClientBuilder.create() .setDefaultCredentialsProvider(provider) .setDefaultRequestConfig(config) .setSSLSocketFactory(factory) .build();
public static void main(String[] args) { /* * This is out ot date - Just keeping * it in case it's helpful... */ final String authUser = "username"; final String authPassword = "password"; CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("10.10.10.10", 8080), new UsernamePasswordCredentials(authUser, authPassword)); HttpHost myProxy = new HttpHost("10.10.10.10", 8080); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder .setProxy(myProxy) .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()) .setDefaultCredentialsProvider(credsProvider) .disableCookieManagement(); CloseableHttpClient httpClient = clientBuilder.build(); FhirContext ctx = FhirContext.forDstu2(); String serverBase = "http://spark.furore.com/fhir/"; ctx.getRestfulClientFactory().setHttpClient(httpClient); IGenericClient client = ctx.newRestfulGenericClient(serverBase); IdDt id = new IdDt("Patient", "123"); client.read(Patient.class, id); }
private void init(String host, int port, String userName, String password) { this.host = host; this.port = port; this.userName = userName; this.password = password; this.baseUrl = SCHEME_HTTP + host + ":" + port + KYLIN_API_PATH; final HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setSoTimeout(httpParams, httpSocketTimeoutMs); HttpConnectionParams.setConnectionTimeout(httpParams, httpConnectionTimeoutMs); final PoolingClientConnectionManager cm = new PoolingClientConnectionManager(); KylinConfig config = KylinConfig.getInstanceFromEnv(); cm.setDefaultMaxPerRoute(config.getRestClientDefaultMaxPerRoute()); cm.setMaxTotal(config.getRestClientMaxTotal()); client = new DefaultHttpClient(cm, httpParams); if (userName != null && password != null) { CredentialsProvider provider = new BasicCredentialsProvider(); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userName, password); provider.setCredentials(AuthScope.ANY, credentials); client.setCredentialsProvider(provider); } }
private DefaultHttpClient getHttpClient() { HttpHost target = new HttpHost(hostname, port, "http"); DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient.getCredentialsProvider().setCredentials (new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(USERNAME, PASSWORD)); return httpClient; } }
public synchronized HttpClient getNativeHttpClient() { if (myHttpClient == null) { //FIXME potential resoource leak PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); connectionManager.setMaxTotal(getPoolMaxTotal()); connectionManager.setDefaultMaxPerRoute(getPoolMaxPerRoute()); // @formatter:off //TODO: Use of a deprecated method should be resolved. RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(getSocketTimeout()) .setConnectTimeout(getConnectTimeout()).setConnectionRequestTimeout(getConnectionRequestTimeout()) .setStaleConnectionCheckEnabled(true).setProxy(myProxy).build(); HttpClientBuilder builder = HttpClients.custom().setConnectionManager(connectionManager) .setDefaultRequestConfig(defaultRequestConfig).disableCookieManagement(); if (myProxy != null && StringUtils.isNotBlank(getProxyUsername()) && StringUtils.isNotBlank(getProxyPassword())) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(myProxy.getHostName(), myProxy.getPort()), new UsernamePasswordCredentials(getProxyUsername(), getProxyPassword())); builder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); builder.setDefaultCredentialsProvider(credsProvider); } myHttpClient = builder.build(); // @formatter:on } return myHttpClient; }
private CloseableHttpClient getCloseableHttpClient(HttpProxy proxy) { if (null != proxy && proxy.isProxy()) { HttpHost proxyHost = new HttpHost(proxy.getHost(), proxy.getPort()); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); if (null != proxy.getUser() && null != proxy.getPassword()) { credentialsProvider.setCredentials(new AuthScope(proxy.getHost(), proxy.getPort()), new UsernamePasswordCredentials(proxy.getUser(), proxy.getPassword())); } return HttpClients.custom().setProxy(proxyHost).setDefaultCredentialsProvider(credentialsProvider).build(); } return HttpClients.custom().setConnectionManager(connManager).build(); }
/** * this handles rest calls. each request creates a different httpClient object */ public HttpResponse doRequest(HttpRequestBase request, String username, String password) { HttpClientBuilder clientBuilder = HttpClients.custom(); HttpClientContext clientContext = HttpClientContext.create(); // configures the clientBuilder and clientContext if (username != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(bindAddress, restPort), new UsernamePasswordCredentials(username, password)); clientBuilder.setDefaultCredentialsProvider(credsProvider); } try { if (useSsl) { SSLContext ctx = SSLContext.getInstance("TLS"); ctx.init(new KeyManager[0], new TrustManager[] {new DefaultTrustManager()}, new SecureRandom()); clientBuilder.setSSLContext(ctx); clientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier()); } return clientBuilder.build().execute(host, request, clientContext); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }
final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setConnectionManager(conMan); clientBuilder.setUserAgent(userAgent); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); clientBuilder.setDefaultCredentialsProvider(credentialsProvider); if (username != null) { credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
protected HttpClient createHttpClient(GoogleAnalyticsConfig config) { ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(); connManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute(config)); BasicHttpParams params = new BasicHttpParams(); if (isNotEmpty(config.getUserAgent())) { params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgent()); } if (isNotEmpty(config.getProxyHost())) { params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(config.getProxyHost(), config.getProxyPort())); } DefaultHttpClient client = new DefaultHttpClient(connManager, params); if (isNotEmpty(config.getProxyUserName())) { BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()), new UsernamePasswordCredentials(config.getProxyUserName(), config.getProxyPassword())); client.setCredentialsProvider(credentialsProvider); } return client; }
RestTemplate restTemplate = new RestTemplate(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("proxyHost", "proxyPort"), new UsernamePasswordCredentials("proxyUser", "proxyPass") ); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.useSystemProperties(); clientBuilder.setProxy(new HttpHost("proxyHost", "proxyPort")); clientBuilder.setDefaultCredentialsProvider(credsProvider); clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); CloseableHttpClient client = clientBuilder.build(); HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); factory.setHttpClient(client); restTemplate.setRequestFactory(factory);
public AbstractHtmlClientImpl(String baseUrl, String user, String password) throws URISyntaxException { this.baseUrl = new URI(baseUrl); HttpHost targetHost = URIUtils.extractHost(this.baseUrl); BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(user, password)); this.client = HttpClientBuilder.create() .setDefaultCredentialsProvider(credsProvider) .setRedirectStrategy(new LaxRedirectStrategy()) .build(); }