List of usage examples for org.apache.http.impl.auth BasicScheme BasicScheme
public BasicScheme()
From source file:org.apache.activemq.artemis.rest.queue.push.UriStrategy.java
protected void initAuthentication() { if (registration.getAuthenticationMechanism() != null) { if (registration.getAuthenticationMechanism().getType() instanceof BasicAuth) { BasicAuth basic = (BasicAuth) registration.getAuthenticationMechanism().getType(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(basic.getUsername(), basic.getPassword()); AuthScope authScope = new AuthScope(AuthScope.ANY); ((DefaultHttpClient) client).getCredentialsProvider().setCredentials(authScope, creds); localContext = new BasicHttpContext(); // Generate BASIC scheme object and stick it to the local execution context BasicScheme basicAuth = new BasicScheme(); localContext.setAttribute("preemptive-auth", basicAuth); // Add as the first request interceptor ((DefaultHttpClient) client).addRequestInterceptor(new PreemptiveAuth(), 0); executor.setHttpContext(localContext); }//from ww w .j a v a2s . co m } }
From source file:org.ops4j.pax.url.mvn.internal.wagon.ConfigurableHttpWagon.java
@Override protected CloseableHttpResponse execute(HttpUriRequest httpMethod) throws HttpException, IOException { setHeaders(httpMethod);/* w w w. jav a2 s. com*/ String userAgent = getUserAgent(httpMethod); if (userAgent != null) { httpMethod.setHeader(HTTP.USER_AGENT, userAgent); } RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); // WAGON-273: default the cookie-policy to browser compatible requestConfigBuilder.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY); Repository repo = getRepository(); ProxyInfo proxyInfo = getProxyInfo(repo.getProtocol(), repo.getHost()); if (proxyInfo != null) { HttpHost proxy = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort()); requestConfigBuilder.setProxy(proxy); } HttpMethodConfiguration config = getHttpConfiguration() == null ? null : getHttpConfiguration().getMethodConfiguration(httpMethod); if (config != null) { copyConfig(config, requestConfigBuilder); } else { requestConfigBuilder.setSocketTimeout(getReadTimeout()); requestConfigBuilder.setConnectTimeout(getTimeout()); } getLocalContext().setRequestConfig(requestConfigBuilder.build()); if (config != null && config.isUsePreemptive()) { HttpHost targetHost = new HttpHost(repo.getHost(), repo.getPort(), repo.getProtocol()); AuthScope targetScope = getBasicAuthScope().getScope(targetHost); if (getCredentialsProvider().getCredentials(targetScope) != null) { BasicScheme targetAuth = new BasicScheme(); targetAuth.processChallenge(new BasicHeader(AUTH.WWW_AUTH, "BASIC preemptive")); getAuthCache().put(targetHost, targetAuth); } } if (proxyInfo != null) { if (proxyInfo.getHost() != null) { HttpHost proxyHost = new HttpHost(proxyInfo.getHost(), proxyInfo.getPort()); AuthScope proxyScope = getProxyBasicAuthScope().getScope(proxyHost); String proxyUsername = proxyInfo.getUserName(); String proxyPassword = proxyInfo.getPassword(); String proxyNtlmHost = proxyInfo.getNtlmHost(); String proxyNtlmDomain = proxyInfo.getNtlmDomain(); if (proxyUsername != null && proxyPassword != null) { Credentials creds; if (proxyNtlmHost != null || proxyNtlmDomain != null) { creds = new NTCredentials(proxyUsername, proxyPassword, proxyNtlmHost, proxyNtlmDomain); } else { creds = new UsernamePasswordCredentials(proxyUsername, proxyPassword); } getCredentialsProvider().setCredentials(proxyScope, creds); BasicScheme proxyAuth = new BasicScheme(); proxyAuth.processChallenge(new BasicHeader(AUTH.PROXY_AUTH, "BASIC preemptive")); getAuthCache().put(proxyHost, proxyAuth); } } } return client.execute(httpMethod, getLocalContext()); }
From source file:ch.sbb.releasetrain.utils.http.HttpUtilImpl.java
/** * authenticates the context if user and password are set */// www . j ava 2 s . 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.pentaho.reporting.libraries.pensol.vfs.LocalFileModel.java
/** * @deprecated use {@link LocalFileModel#LocalFileModel(java.lang.String, * org.pentaho.reporting.engine.classic.core.util.HttpClientManager.HttpClientBuilderFacade, * java.lang.String, java.lang.String, java.lang.String, int) }. *//*w ww. ja va 2 s . c o m*/ @Deprecated() public LocalFileModel(final String url, final HttpClient client, final String username, final String password, final String hostName, int port) { if (url == null) { throw new NullPointerException(); } this.url = url; this.username = username; this.password = password; this.client = client; this.context = HttpClientContext.create(); if (!StringUtil.isEmpty(hostName)) { // Preemptive Basic Authentication HttpHost target = new HttpHost(hostName, port, "http"); // 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 this.context.setAuthCache(authCache); } this.client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); this.client.getParams().setParameter(ClientPNames.MAX_REDIRECTS, Integer.valueOf(10)); this.client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE); this.client.getParams().setParameter(ClientPNames.REJECT_RELATIVE_REDIRECT, Boolean.FALSE); }
From source file:org.apache.solr.client.solrj.impl.PreemptiveBasicAuthConfigurer.java
@Override public void configure(DefaultHttpClient httpClient, SolrParams config) { config = SolrParams.wrapDefaults(config, defaultParams); final String basicAuthUser = config.get(HttpClientUtil.PROP_BASIC_AUTH_USER); final String basicAuthPass = config.get(HttpClientUtil.PROP_BASIC_AUTH_PASS); if (basicAuthUser == null || basicAuthPass == null) { throw new IllegalArgumentException( "username & password must be specified with " + getClass().getName()); }// ww w .j a v a 2 s . c om super.configure(httpClient, config); httpClient.addRequestInterceptor(new PreemptiveAuth(new BasicScheme()), 0); }
From source file:ua.pp.msk.cliqr.GetProcessorImpl.java
private void init(URL targetURL, String user, String password) throws ClientSslException { this.targetUrl = targetURL; logger.debug("Initializing " + this.getClass().getName() + " with target URL " + targetURL.toString()); HttpHost htHost = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), targetUrl.getProtocol()); AuthCache aCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); aCache.put(htHost, basicAuth);//from www .j av a 2 s.co m UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, password); BasicCredentialsProvider cProvider = new BasicCredentialsProvider(); cProvider.setCredentials(new AuthScope(htHost), creds); logger.debug("Credential provider: " + cProvider.toString()); context = new BasicHttpContext(); ClientContextConfigurer cliCon = new ClientContextConfigurer(context); cliCon.setCredentialsProvider(cProvider); context.setAttribute(ClientContext.AUTH_CACHE, aCache); SSLSocketFactory sslConnectionSocketFactory = null; try { sslConnectionSocketFactory = new SSLSocketFactory(new TrustSelfSignedStrategy(), new CliQrHostnameVerifier()); } catch (KeyManagementException ex) { logger.error("Cannot manage secure keys", ex); throw new ClientSslException("Cannot manage secure keys", ex); } catch (KeyStoreException ex) { logger.error("Cannot build SSL context due to KeyStore error", ex); throw new ClientSslException("Cannot build SSL context due to KeyStore error", ex); } catch (NoSuchAlgorithmException ex) { logger.error("Unsupported security algorithm", ex); throw new ClientSslException("Unsupported security algorithm", ex); } catch (UnrecoverableKeyException ex) { logger.error("Unrecoverable key", ex); throw new ClientSslException("Unrecoverrable key", ex); } DefaultHttpClient defClient = new DefaultHttpClient(); defClient.setRedirectStrategy(new CliQrRedirectStrategy()); defClient.setCredentialsProvider(cProvider); Scheme https = new Scheme("https", 443, sslConnectionSocketFactory); defClient.getConnectionManager().getSchemeRegistry().register(https); defClient.setTargetAuthenticationStrategy(new TargetAuthenticationStrategy()); client = defClient; }
From source file:org.datagator.api.client.backend.DataGatorService.java
public DataGatorService(UsernamePasswordCredentials auth) { this();// www . j a va 2 s . c om log.info(String.format("Initializing service with authorization: %s", auth.getUserName())); // attach credentials to context HttpHost host = new HttpHost(environ.DATAGATOR_API_HOST, environ.DATAGATOR_API_PORT, environ.DATAGATOR_API_SCHEME); AuthScope scope = new AuthScope(host.getHostName(), host.getPort()); CredentialsProvider provider = new BasicCredentialsProvider(); provider.setCredentials(scope, auth); this.context.setCredentialsProvider(provider); // enable preemptive (pro-active) basic authentication AuthCache cache = new BasicAuthCache(); cache.put(host, new BasicScheme()); this.context.setAuthCache(cache); }
From source file:com.unboundid.scim.sdk.PreemptiveAuthInterceptor.java
/** * {@inheritDoc}//from ww w.j ava 2 s . c om */ @Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST); if (target.getPort() < 0) { SchemeRegistry schemeRegistry = (SchemeRegistry) context.getAttribute(ClientContext.SCHEME_REGISTRY); Scheme scheme = schemeRegistry.getScheme(target); target = new HttpHost(target.getHostName(), scheme.resolvePort(target.getPort()), target.getSchemeName()); } AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE); if (authCache == null) { authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(target, basicAuth); context.setAttribute(ClientContext.AUTH_CACHE, authCache); return; } CredentialsProvider credsProvider = (CredentialsProvider) context .getAttribute(ClientContext.CREDS_PROVIDER); if (credsProvider == null) { return; } final AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE); if (targetState != null && targetState.getState() == AuthProtocolState.UNCHALLENGED) { final AuthScheme authScheme = authCache.get(target); if (authScheme != null) { doPreemptiveAuth(target, authScheme, targetState, credsProvider); } } final HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST); final AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE); if (proxy != null && proxyState != null && proxyState.getState() == AuthProtocolState.UNCHALLENGED) { final AuthScheme authScheme = authCache.get(proxy); if (authScheme != null) { doPreemptiveAuth(proxy, authScheme, proxyState, credsProvider); } } }
From source file:org.jboss.teiid.quickstart.PortfolioClient.java
public void resteasyHttpBackendClient() { System.out.println("\nResteasy Client API with HTTP client as engine"); HttpHost targetHost = new HttpHost("localhost", 8080, "http"); // 1. Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // 2. Generate BASIC scheme object and add it to the local auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); // 3. Add AuthCache to the execution context HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); // 4. Create client executor and proxy HttpClientBuilder builder = HttpClientBuilder.create(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope scope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(USERNAME, PASSWORD); credsProvider.setCredentials(scope, credentials); builder.setDefaultCredentialsProvider(credsProvider); HttpClient httpClient = builder.build(); // 5. Create ResteasyClient with http client as engine ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient, localContext); ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build(); // 6. Execute Rest call via ResteasyClient String authString = getBasicAuthentication(); for (String api : apis) { ResteasyWebTarget target = client.target(api); Response response = target.request().header(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML) .header(HttpHeaders.AUTHORIZATION, authString).get(); if (response.getStatus() == 200) { String value = response.readEntity(String.class); System.out.println(value); response.close();//from w ww. j ava 2 s . c om } else { handleError(response); } } }
From source file:com.aliyun.oss.common.comm.DefaultServiceClient.java
public DefaultServiceClient(ClientConfiguration config) { super(config); this.connectionManager = createHttpClientConnectionManager(); this.httpClient = createHttpClient(this.connectionManager); RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectTimeout(config.getConnectionTimeout()); requestConfigBuilder.setSocketTimeout(config.getSocketTimeout()); requestConfigBuilder.setConnectionRequestTimeout(config.getConnectionRequestTimeout()); String proxyHost = config.getProxyHost(); int proxyPort = config.getProxyPort(); if (proxyHost != null && proxyPort > 0) { this.proxyHttpHost = new HttpHost(proxyHost, proxyPort); requestConfigBuilder.setProxy(proxyHttpHost); String proxyUsername = config.getProxyUsername(); String proxyPassword = config.getProxyPassword(); String proxyDomain = config.getProxyDomain(); String proxyWorkstation = config.getProxyWorkstation(); if (proxyUsername != null && proxyPassword != null) { this.credentialsProvider = new BasicCredentialsProvider(); this.credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain)); this.authCache = new BasicAuthCache(); authCache.put(this.proxyHttpHost, new BasicScheme()); }// ww w .ja v a2 s. c om } this.requestConfig = requestConfigBuilder.build(); }