List of usage examples for org.apache.http.impl.auth DigestScheme DigestScheme
@Deprecated public DigestScheme(final ChallengeState challengeState)
From source file:RestApiHttpClient.java
/** * Create an new {@link RestApiHttpClient} instance with Endpoint, username and API-Key * //from w ww . j a v a 2 s . c o m * @param apiEndpoint The Hostname and Api-Endpoint (http://www.example.com/api) * @param username Shopware Username * @param password Api-Key from User-Administration */ public RestApiHttpClient(URL apiEndpoint, String username, String password) { this.apiEndpoint = apiEndpoint; BasicHttpContext context = new BasicHttpContext(); this.localContext = HttpClientContext.adapt(context); HttpHost target = new HttpHost(this.apiEndpoint.getHost(), -1, this.apiEndpoint.getProtocol()); this.localContext.setTargetHost(target); TargetAuthenticationStrategy authStrat = new TargetAuthenticationStrategy(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password); BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); AuthScope aScope = new AuthScope(target.getHostName(), target.getPort()); credsProvider.setCredentials(aScope, creds); BasicAuthCache authCache = new BasicAuthCache(); // Digest Authentication DigestScheme digestAuth = new DigestScheme(Charset.forName("UTF-8")); authCache.put(target, digestAuth); this.localContext.setAuthCache(authCache); this.localContext.setCredentialsProvider(credsProvider); ArrayList<Header> defHeaders = new ArrayList<>(); defHeaders.add(new BasicHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON.getMimeType())); this.httpclient = HttpClients.custom().useSystemProperties().setTargetAuthenticationStrategy(authStrat) .disableRedirectHandling() // make Rest-API Endpoint GZIP-Compression enable comment this out // Response-Compression is also possible .disableContentCompression().setDefaultHeaders(defHeaders) .setDefaultCredentialsProvider(credsProvider).build(); }
From source file:org.bonitasoft.connectors.rest.RESTConnector.java
/** * Set the builder based on the request elements * /*from ww w. j a v a 2s. c om*/ * @param requestConfigurationBuilder The builder to be set * @param authorization The authentication element of the request * @param proxy The proxy element of the request * @param urlHost The URL host of the request * @param urlPort The URL post of the request * @param urlProtocol The URL protocol of the request * @param httpClientBuilder The builder to be set * @return HTTPContext The HTTP context to be set */ private HttpContext setAuthorizations(final Builder requestConfigurationBuilder, final Authorization authorization, final Proxy proxy, final String urlHost, final int urlPort, final String urlProtocol, final HttpClientBuilder httpClientBuilder) { HttpContext httpContext = HttpClientContext.create(); if (authorization != null) { if (authorization instanceof BasicDigestAuthorization) { final BasicDigestAuthorization castAuthorization = (BasicDigestAuthorization) authorization; final List<String> authPrefs = new ArrayList<>(); if (castAuthorization.isBasic()) { authPrefs.add(AuthSchemes.BASIC); } else { authPrefs.add(AuthSchemes.DIGEST); } requestConfigurationBuilder.setTargetPreferredAuthSchemes(authPrefs); final String username = castAuthorization.getUsername(); final String password = new String(castAuthorization.getPassword()); String host = urlHost; if (isStringInputValid(castAuthorization.getHost())) { host = castAuthorization.getHost(); } int port = urlPort; if (castAuthorization.getPort() != null) { port = castAuthorization.getPort(); } String realm = AuthScope.ANY_REALM; if (isStringInputValid(castAuthorization.getRealm())) { realm = castAuthorization.getRealm(); } final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(host, port, realm), new UsernamePasswordCredentials(username, password)); setProxyCrendentials(proxy, credentialsProvider); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); if (castAuthorization.isPreemptive() || proxy != null) { final AuthCache authoriationCache = new BasicAuthCache(); if (castAuthorization.isPreemptive()) { AuthSchemeBase authorizationScheme = null; if (castAuthorization.isBasic()) { authorizationScheme = new BasicScheme(ChallengeState.TARGET); } else { authorizationScheme = new DigestScheme(ChallengeState.TARGET); } authoriationCache.put(new HttpHost(host, port, urlProtocol), authorizationScheme); } if (proxy != null) { final BasicScheme basicScheme = new BasicScheme(ChallengeState.PROXY); authoriationCache.put(new HttpHost(proxy.getHost(), proxy.getPort()), basicScheme); } final HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authoriationCache); httpContext = localContext; } } } else if (proxy != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); setProxyCrendentials(proxy, credentialsProvider); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); // Make it preemptive if (proxy.hasCredentials()) { final AuthCache authoriationCache = new BasicAuthCache(); final BasicScheme basicScheme = new BasicScheme(ChallengeState.PROXY); authoriationCache.put(new HttpHost(proxy.getHost(), proxy.getPort()), basicScheme); final HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authoriationCache); httpContext = localContext; } } return httpContext; }