List of usage examples for org.apache.http.auth AuthScope AuthScope
public AuthScope(final String host, final int port)
From source file:org.pentaho.di.core.util.HttpClientUtil.java
/** * Returns context with AuthCache or null in case of any exception was thrown. * * @param host/* w w w. j a v a 2 s . c o m*/ * @param port * @param user * @param password * @param schema * @return {@link org.apache.http.client.protocol.HttpClientContext HttpClientContext} */ public static HttpClientContext createPreemptiveBasicAuthentication(String host, int port, String user, String password, String schema) { 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)); // 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 localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); } catch (Exception e) { return null; } return localContext; }
From source file:com.icoin.trading.bitcoin.client.BitcoinClientDefaultConfig.java
private CredentialsProvider credentialsProvicer() { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(host, Integer.valueOf(port)), new UsernamePasswordCredentials(user, password)); return credsProvider; }
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();// w w w. j av a 2 s . 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:uk.ac.ox.oucs.vle.XcriPopulatorInput.java
public InputStream getInput(PopulatorContext context) throws PopulatorException { InputStream input = null;/*from w w w . ja v a 2 s . c om*/ HttpEntity entity = null; try { URL xcri = new URL(context.getURI()); HttpHost targetHost = new HttpHost(xcri.getHost(), xcri.getPort(), xcri.getProtocol()); httpClient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials(context.getUser(), context.getPassword())); HttpGet httpget = new HttpGet(xcri.toURI()); HttpResponse response = httpClient.execute(targetHost, httpget); entity = response.getEntity(); if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()) { throw new PopulatorException("Invalid Response [" + response.getStatusLine().getStatusCode() + "]"); } input = entity.getContent(); } catch (MalformedURLException e) { throw new PopulatorException(e.getLocalizedMessage()); } catch (IllegalStateException e) { throw new PopulatorException(e.getLocalizedMessage()); } catch (IOException e) { throw new PopulatorException(e.getLocalizedMessage()); } catch (URISyntaxException e) { throw new PopulatorException(e.getLocalizedMessage()); } finally { if (null == input && null != entity) { try { entity.getContent().close(); } catch (IOException e) { log.error("IOException [" + e + "]"); } } } return input; }
From source file:com.sap.core.odata.testutil.tool.core.SupaController.java
private void init() { httpClient = new DefaultHttpClient(); if (config.isProxySet()) { initProxy();// w w w . ja v a 2 s. c o m } if (config.isBasicAuthCredentialsSet()) { final URI baseUri = config.getBaseUri(); httpClient.getCredentialsProvider().setCredentials(new AuthScope(baseUri.getHost(), baseUri.getPort()), new UsernamePasswordCredentials(config.getBasicAuthCredentials())); } }
From source file:com.marklogic.client.test.util.TestServerBootstrapper.java
private void installBootstrapExtension() throws IOException { DefaultHttpClient client = new DefaultHttpClient(); client.getCredentialsProvider().setCredentials(new AuthScope("localhost", port), new UsernamePasswordCredentials(username, password)); HttpPut put = new HttpPut("http://" + host + ":" + port + "/v1/config/resources/bootstrap?method=POST"); put.setEntity(new FileEntity(new File("src/test/resources/bootstrap.xqy"), "application/xquery")); HttpResponse response = client.execute(put); @SuppressWarnings("unused") HttpEntity entity = response.getEntity(); System.out.println("Installed bootstrap extension. Response is " + response.toString()); }
From source file:com.gsma.mobileconnect.utils.RestClient.java
/** * Build a HttpClientContext that will preemptively authenticate using Basic Authentication * * @param username Username for credentials * @param password Password for credentials * @param uriForRealm uri used to determine the authentication realm * @return A HttpClientContext that will preemptively authenticate using Basic Authentication *//*from ww w .j a v a2 s. c o m*/ public HttpClientContext getHttpClientContext(String username, String password, URI uriForRealm) { String host = URIUtils.extractHost(uriForRealm).getHostName(); int port = uriForRealm.getPort(); if (port == -1) { if (Constants.HTTP_SCHEME.equalsIgnoreCase(uriForRealm.getScheme())) { port = Constants.HTTP_PORT; } else if (Constants.HTTPS_SCHEME.equalsIgnoreCase(uriForRealm.getScheme())) { port = Constants.HTTPS_PORT; } } CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(host, port), new UsernamePasswordCredentials(username, password)); AuthCache authCache = new BasicAuthCache(); authCache.put(new HttpHost(host, port, uriForRealm.getScheme()), new BasicScheme()); HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credentialsProvider); context.setAuthCache(authCache); return context; }
From source file:com.expressui.domain.RestClientService.java
/** * Create a REST client// ww w.j a v a2 s . c o m * * @param uri uri of the service * @param clazz client class * @param <T> class type * @return REST client * @throws Exception */ public <T> T create(String uri, Class<T> clazz) throws Exception { RestClientProxyFactoryBean restClientFactory = new RestClientProxyFactoryBean(); restClientFactory.setBaseUri(new URI(uri)); restClientFactory.setServiceInterface(clazz); if (applicationProperties.getHttpProxyHost() != null && applicationProperties.getHttpProxyPort() != null) { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpHost proxy = new HttpHost(applicationProperties.getHttpProxyHost(), applicationProperties.getHttpProxyPort()); if (applicationProperties.getHttpProxyUsername() != null && applicationProperties.getHttpProxyPassword() != null) { httpClient.getCredentialsProvider().setCredentials( new AuthScope(applicationProperties.getHttpProxyHost(), applicationProperties.getHttpProxyPort()), new UsernamePasswordCredentials(applicationProperties.getHttpProxyUsername(), applicationProperties.getHttpProxyPassword())); } httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); restClientFactory.setHttpClient(httpClient); } restClientFactory.afterPropertiesSet(); return (T) restClientFactory.getObject(); }
From source file:com.aliyun.oss.common.comm.HttpClientFactory.java
public HttpClient createHttpClient(ClientConfiguration config) { HttpParams httpClientParams = new BasicHttpParams(); HttpProtocolParams.setUserAgent(httpClientParams, config.getUserAgent()); HttpConnectionParams.setConnectionTimeout(httpClientParams, config.getConnectionTimeout()); HttpConnectionParams.setSoTimeout(httpClientParams, config.getSocketTimeout()); HttpConnectionParams.setStaleCheckingEnabled(httpClientParams, true); HttpConnectionParams.setTcpNoDelay(httpClientParams, true); PoolingClientConnectionManager connManager = createConnectionManager(config, httpClientParams); DefaultHttpClient httpClient = new DefaultHttpClient(connManager, httpClientParams); if (System.getProperty("com.aliyun.oss.disableCertChecking") != null) { Scheme sch = new Scheme("https", 443, getSSLSocketFactory()); httpClient.getConnectionManager().getSchemeRegistry().register(sch); }/*from www . ja va2s . co m*/ String proxyHost = config.getProxyHost(); int proxyPort = config.getProxyPort(); if (proxyHost != null && proxyPort > 0) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); String proxyUsername = config.getProxyUsername(); String proxyPassword = config.getProxyPassword(); if (proxyUsername != null && proxyPassword != null) { String proxyDomain = config.getProxyDomain(); String proxyWorkstation = config.getProxyWorkstation(); httpClient.getCredentialsProvider().setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain)); } } return httpClient; }