List of usage examples for org.apache.http.client CredentialsProvider setCredentials
void setCredentials(AuthScope authscope, Credentials credentials);
From source file:ch.sbb.releasetrain.utils.http.HttpUtilImpl.java
/** * authenticates the context if user and password are set *///from w w w . j a va 2s .c o m private HttpClientContext initAuthIfNeeded(String url) { HttpClientContext context = HttpClientContext.create(); if (this.user.isEmpty() || this.password.isEmpty()) { log.debug( "http connection without autentication, because no user / password ist known to HttpUtilImpl ..."); return context; } CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(AuthScope.ANY), new UsernamePasswordCredentials(user, password)); URL url4Host; try { url4Host = new URL(url); } catch (MalformedURLException e) { log.error(e.getMessage(), e); return context; } HttpHost targetHost = new HttpHost(url4Host.getHost(), url4Host.getPort(), url4Host.getProtocol()); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); context.setCredentialsProvider(credsProvider); context.setAuthCache(authCache); return context; }
From source file:org.miloss.fgsms.presentation.StatusHelper.java
/** * determines if an fgsms service is currently accessible. not for use * with other services./* w w w . j a va2 s . co m*/ * * @param endpoint * @return */ public String sendGetRequest(String endpoint) { // String result = null; if (endpoint.startsWith("http")) { // Send a GET request to the servlet try { URL url = new URL(endpoint); int port = url.getPort(); if (port <= 0) { if (endpoint.startsWith("https:")) { port = 443; } else { port = 80; } } HttpClientBuilder create = HttpClients.custom(); if (mode == org.miloss.fgsms.common.Constants.AuthMode.UsernamePassword) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(url.getHost(), port), new UsernamePasswordCredentials(username, Utility.DE(password))); create.setDefaultCredentialsProvider(credsProvider); ; } CloseableHttpClient c = create.build(); CloseableHttpResponse response = c.execute(new HttpHost(url.getHost(), port), new HttpGet(endpoint)); c.close(); int status = response.getStatusLine().getStatusCode(); if (status == 200) { return "OK"; } return String.valueOf(status); } catch (Exception ex) { Logger.getLogger(Helper.class).log(Level.WARN, "error fetching http doc from " + endpoint + ex.getLocalizedMessage()); return "offline"; } } else { return "undeterminable"; } }
From source file:com.adobe.ags.curly.controller.AuthHandler.java
private CredentialsProvider getCredentialsProvider() { CredentialsProvider creds = new BasicCredentialsProvider(); creds.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(model.userNameProperty().get(), model.passwordProperty().get())); return creds; }
From source file:org.elasticsearch.xpack.security.transport.ssl.SslIntegrationTests.java
public void testThatConnectionToHTTPWorks() throws Exception { Settings.Builder builder = Settings.builder(); addSSLSettingsForStore(builder,/*w w w.j a v a2s. c o m*/ "/org/elasticsearch/xpack/security/transport/ssl/certs/simple/testclient.jks", "testclient"); SSLService service = new SSLService(builder.build(), null); CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(nodeClientUsername(), new String(nodeClientPassword().getChars()))); try (CloseableHttpClient client = HttpClients.custom() .setSSLSocketFactory(new SSLConnectionSocketFactory(service.sslSocketFactory(Settings.EMPTY), SSLConnectionSocketFactory.getDefaultHostnameVerifier())) .setDefaultCredentialsProvider(provider).build(); CloseableHttpResponse response = SocketAccess .doPrivileged(() -> client.execute(new HttpGet(getNodeUrl())))) { assertThat(response.getStatusLine().getStatusCode(), is(200)); String data = Streams .copyToString(new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)); assertThat(data, containsString("You Know, for Search")); } }
From source file:org.hawkular.client.core.jaxrs.RestFactory.java
public T createAPI(ClientInfo clientInfo) { final HttpClient httpclient; if (clientInfo.getEndpointUri().toString().startsWith("https")) { httpclient = getHttpClient();/* ww w .j a v a 2s . c om*/ } else { httpclient = HttpClientBuilder.create().build(); } final ResteasyClient client; if (clientInfo.getUsername().isPresent() && clientInfo.getPassword().isPresent()) { HttpHost targetHost = new HttpHost(clientInfo.getEndpointUri().getHost(), clientInfo.getEndpointUri().getPort()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(clientInfo.getUsername().get(), clientInfo.getPassword().get())); // 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(targetHost, basicAuth); // Add AuthCache to the execution context HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); context.setAuthCache(authCache); ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpclient, context); client = new ResteasyClientBuilder().httpEngine(engine).build(); } else { ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(getHttpClient()); client = new ResteasyClientBuilder().httpEngine(engine).build(); } client.register(JacksonJaxbJsonProvider.class); client.register(JacksonObjectMapperProvider.class); client.register(RequestLoggingFilter.class); client.register(new RequestHeadersFilter(clientInfo.getHeaders())); client.register(ResponseLoggingFilter.class); client.register(HCJacksonJson2Provider.class); client.register(ConvertersProvider.class); ProxyBuilder<T> proxyBuilder = client.target(clientInfo.getEndpointUri()).proxyBuilder(apiClassType); if (classLoader != null) { proxyBuilder = proxyBuilder.classloader(classLoader); } return proxyBuilder.build(); }
From source file:org.hawkular.client.RestFactory.java
public T createAPI(URI uri, String userName, String password) { HttpClient httpclient = null;/*from w ww. j a v a2 s . c o m*/ if (uri.toString().startsWith("https")) { httpclient = getHttpClient(); } else { httpclient = HttpClientBuilder.create().build(); } ResteasyClient client = null; if (userName != null) { HttpHost targetHost = new HttpHost(uri.getHost(), uri.getPort()); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(userName, password)); // 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(targetHost, basicAuth); // Add AuthCache to the execution context HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credsProvider); context.setAuthCache(authCache); ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpclient, context); client = new ResteasyClientBuilder().httpEngine(engine).build(); } else { ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(getHttpClient()); client = new ResteasyClientBuilder().httpEngine(engine).build(); } client.register(JacksonJaxbJsonProvider.class); client.register(JacksonObjectMapperProvider.class); client.register(RestRequestFilter.class); client.register(RestResponseFilter.class); client.register(HCJacksonJson2Provider.class); ProxyBuilder<T> proxyBuilder = client.target(uri).proxyBuilder(apiClassType); if (classLoader != null) { proxyBuilder = proxyBuilder.classloader(classLoader); } return proxyBuilder.build(); }
From source file:ca.uhn.fhir.rest.client.apache.ApacheRestfulClientFactory.java
public synchronized HttpClient getNativeHttpClient() { if (myHttpClient == null) { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(5000, TimeUnit.MILLISECONDS); connectionManager.setMaxTotal(getPoolMaxTotal()); connectionManager.setDefaultMaxPerRoute(getPoolMaxPerRoute()); // @formatter:off 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); }/*from ww w . java 2s . com*/ myHttpClient = builder.build(); // @formatter:on } return myHttpClient; }
From source file:com.bosch.iot.things.tutorial.ui.ProxyServlet.java
/** * Create http client/*from ww w . j a va 2 s . co m*/ */ private synchronized CloseableHttpClient getHttpClient() { if (httpClient == null) { HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); if (props.getProperty("proxyHost") != null) { httpClientBuilder.setProxy(new HttpHost(props.getProperty("proxyHost"), Integer.parseInt(props.getProperty("proxyPort")))); } if (props.getProperty("proxyUser") != null) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(targetHost), new UsernamePasswordCredentials( props.getProperty("proxyUser"), props.getProperty("proxyPwd"))); httpClientBuilder.setDefaultCredentialsProvider(credsProvider); } httpClient = httpClientBuilder.build(); } return httpClient; }
From source file:io.uploader.drive.config.proxy.Proxy.java
@Override public CredentialsProvider getCredentialsProvider() { if (isActive()) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(getHost(), getPort()), new UsernamePasswordCredentials(getUsername(), getPassword())); return credsProvider; } else {/* w ww. j a va2 s . c o m*/ return null; } }
From source file:no.kantega.publishing.jobs.xmlimport.XMLImportJob.java
@PostConstruct private void init() { int timeout = configuration.getInt("httpclient.connectiontimeout", 10000); String proxyHost = configuration.getString("httpclient.proxy.host"); String proxyPort = configuration.getString("httpclient.proxy.port"); String proxyUser = configuration.getString("httpclient.proxy.username"); String proxyPassword = configuration.getString("httpclient.proxy.password"); if (isNotBlank(proxyHost)) { HttpHost proxy = new HttpHost(proxyHost, Integer.parseInt(proxyPort)); httpClientBuilder = HttpClients.custom() .setDefaultRequestConfig( RequestConfig.custom().setRedirectsEnabled(true).setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build()) .setProxy(proxy);//from w w w . j a v a 2 s .co m if (isNotBlank(proxyUser)) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxyUser, proxyPassword)); httpClientBuilder.setDefaultCredentialsProvider(credsProvider); } } else { httpClientBuilder = HttpClients.custom().setDefaultRequestConfig( RequestConfig.custom().setRedirectsEnabled(true).setConnectTimeout(timeout) .setSocketTimeout(timeout).setConnectionRequestTimeout(timeout).build()); } }