List of usage examples for org.apache.http.impl.client HttpClientBuilder setDefaultAuthSchemeRegistry
public final HttpClientBuilder setDefaultAuthSchemeRegistry( final Lookup<AuthSchemeProvider> authSchemeRegistry)
From source file:org.openscore.content.httpclient.ScoreHttpClient.java
public HttpComponents buildHttpComponents(HttpClientInputs httpClientInputs) { buildDefaultServices();//from ww w .j av a 2s . c o m URI uri = uriBuilder.setUrl(httpClientInputs.getUrl()).setQueryParams(httpClientInputs.getQueryParams()) .setQueryParamsAreURLEncoded(httpClientInputs.getQueryParamsAreURLEncoded()).buildURI(); ContentType theContentType = contentTypeBuilder.setContentType(httpClientInputs.getContentType()) .setRequestCharacterSet(httpClientInputs.getRequestCharacterSet()).buildContentType(); HttpEntity httpEntity = httpEntityBuilder.setFormParams(httpClientInputs.getFormParams()) .setFormParamsAreURLEncoded(httpClientInputs.getFormParamsAreURLEncoded()) .setBody(httpClientInputs.getBody()).setFilePath(httpClientInputs.getSourceFile()) .setContentType(theContentType) .setMultipartValuesAreURLEncoded(httpClientInputs.getMultipartValuesAreURLEncoded()) .setMultipartBodies(httpClientInputs.getMultipartBodies()) .setMultipartFiles(httpClientInputs.getMultipartFiles()) .setMultipartBodiesContentType(httpClientInputs.getMultipartBodiesContentType()) .setMultipartFilesContentType(httpClientInputs.getMultipartFilesContentType()) .setChunkedRequestEntity(httpClientInputs.getChunkedRequestEntity()).buildEntity(); HttpRequestBase httpRequestBase = requestBuilder.setMethod(httpClientInputs.getMethod()).setUri(uri) .setEntity(httpEntity).build(); List<Header> theHeaders = headersBuilder.setHeaders(httpClientInputs.getHeaders()) .setContentType(theContentType) .setEntityContentType(httpEntity != null ? httpEntity.getContentType() : null).buildHeaders(); RequestConfig requestConfig = requestConfigBuilder .setConnectionTimeout(httpClientInputs.getConnectTimeout()) .setSocketTimeout(httpClientInputs.getSocketTimeout()) .setFollowRedirects(httpClientInputs.getFollowRedirects()) .setProxyHost(httpClientInputs.getProxyHost()).setProxyPort(httpClientInputs.getProxyPort()) .buildRequestConfig(); httpRequestBase.setConfig(requestConfig); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); AuthTypes authTypes = new AuthTypes(httpClientInputs.getAuthType()); CredentialsProvider credentialsProvider = credentialsProviderBuilder.setAuthTypes(authTypes) .setUsername(httpClientInputs.getUsername()).setPassword(httpClientInputs.getPassword()) .setHost(uri.getHost()).setPort(String.valueOf(uri.getPort())) .setProxyUsername(httpClientInputs.getProxyUsername()) .setProxyPassword(httpClientInputs.getProxyPassword()).setProxyHost(httpClientInputs.getProxyHost()) .setProxyPort(httpClientInputs.getProxyPort()).buildCredentialsProvider(); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); Lookup<AuthSchemeProvider> authSchemeLookup = authSchemeProviderLookupBuilder.setAuthTypes(authTypes) .setHost(uri.getHost()).setHeaders(theHeaders) .setSkipPortAtKerberosDatabaseLookup(httpClientInputs.getKerberosSkipPortCheck()) .setKerberosConfigFile(httpClientInputs.getKerberosConfFile()) .setKerberosLoginConfigFile(httpClientInputs.getKerberosLoginConfFile()) .setUsername(httpClientInputs.getUsername()).setPassword(httpClientInputs.getPassword()) .buildAuthSchemeProviderLookup(); httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeLookup); httpRequestBase.setHeaders(theHeaders.toArray(new Header[theHeaders.size()])); CookieStore cookieStore = httpClientInputs.getCookieStoreSessionObject() == null ? null : cookieStoreBuilder.setUseCookies(httpClientInputs.getUseCookies()) .setCookieStoreSessionObject(httpClientInputs.getCookieStoreSessionObject()) .buildCookieStore(); if (cookieStore != null) { httpClientBuilder.setDefaultCookieStore(cookieStore); } SSLConnectionSocketFactory sslConnectionSocketFactory = sslConnectionSocketFactoryBuilder .setTrustAllRoots(httpClientInputs.getTrustAllRoots()).setKeystore(httpClientInputs.getKeystore()) .setKeystorePassword(httpClientInputs.getKeystorePassword()) .setTrustKeystore(httpClientInputs.getTrustKeystore()) .setTrustPassword(httpClientInputs.getTrustPassword()) .setX509HostnameVerifier(httpClientInputs.getX509HostnameVerifier()).build(); String connectionKey = ConnectionManagerBuilder.buildConnectionManagerMapKey( httpClientInputs.getTrustAllRoots(), httpClientInputs.getX509HostnameVerifier(), httpClientInputs.getKeystore(), httpClientInputs.getTrustKeystore()); PoolingHttpClientConnectionManager connManager = poolingHttpClientConnectionManagerBuilder .setConnectionManagerMapKey(connectionKey) .setConnectionPoolHolder(httpClientInputs.getConnectionPoolSessionObject()) .setSslsf(sslConnectionSocketFactory) .setDefaultMaxPerRoute(httpClientInputs.getConnectionsMaxPerRoute()) .setTotalMax(httpClientInputs.getConnectionsMaxTotal()).buildConnectionManager(); httpClientBuilder.setConnectionManager(connManager); if (StringUtils.isEmpty(httpClientInputs.getKeepAlive()) || Boolean.parseBoolean(httpClientInputs.getKeepAlive())) { httpClientBuilder.setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE); } else { httpClientBuilder.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE); } CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); HttpClientContext context = contextBuilder.setAuthSchemeLookup(authSchemeLookup).setAuthTypes(authTypes) .setCredentialsProvider(credentialsProvider).setUri(uri) .setPreemptiveAuth(httpClientInputs.getPreemptiveAuth()).build(); HttpComponents result = new HttpComponents(); result.setCloseableHttpClient(closeableHttpClient); result.setHttpRequestBase(httpRequestBase); result.setHttpClientContext(context); result.setUri(uri); result.setConnManager(connManager); result.setCookieStore(cookieStore); return result; }
From source file:io.cloudslang.content.httpclient.ScoreHttpClient.java
public HttpComponents buildHttpComponents(HttpClientInputs httpClientInputs) { buildDefaultServices();//from www .ja va2s.com URI uri = uriBuilder.setUrl(httpClientInputs.getUrl()).setQueryParams(httpClientInputs.getQueryParams()) .setQueryParamsAreURLEncoded(httpClientInputs.getQueryParamsAreURLEncoded()) .setQueryParamsAreFormEncoded(httpClientInputs.getQueryParamsAreFormEncoded()).buildURI(); ContentType theContentType = contentTypeBuilder.setContentType(httpClientInputs.getContentType()) .setRequestCharacterSet(httpClientInputs.getRequestCharacterSet()).buildContentType(); HttpEntity httpEntity = httpEntityBuilder.setFormParams(httpClientInputs.getFormParams()) .setFormParamsAreURLEncoded(httpClientInputs.getFormParamsAreURLEncoded()) .setBody(httpClientInputs.getBody()).setFilePath(httpClientInputs.getSourceFile()) .setContentType(theContentType) .setMultipartValuesAreURLEncoded(httpClientInputs.getMultipartValuesAreURLEncoded()) .setMultipartBodies(httpClientInputs.getMultipartBodies()) .setMultipartFiles(httpClientInputs.getMultipartFiles()) .setMultipartBodiesContentType(httpClientInputs.getMultipartBodiesContentType()) .setMultipartFilesContentType(httpClientInputs.getMultipartFilesContentType()) .setChunkedRequestEntity(httpClientInputs.getChunkedRequestEntity()).buildEntity(); HttpRequestBase httpRequestBase = requestBuilder.setMethod(httpClientInputs.getMethod()).setUri(uri) .setEntity(httpEntity).build(); List<Header> theHeaders = headersBuilder.setHeaders(httpClientInputs.getHeaders()) .setContentType(theContentType) .setEntityContentType(httpEntity != null ? httpEntity.getContentType() : null).buildHeaders(); RequestConfig requestConfig = requestConfigBuilder .setConnectionTimeout(httpClientInputs.getConnectTimeout()) .setSocketTimeout(httpClientInputs.getSocketTimeout()) .setFollowRedirects(httpClientInputs.getFollowRedirects()) .setProxyHost(httpClientInputs.getProxyHost()).setProxyPort(httpClientInputs.getProxyPort()) .buildRequestConfig(); httpRequestBase.setConfig(requestConfig); HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); AuthTypes authTypes = new AuthTypes(httpClientInputs.getAuthType()); CredentialsProvider credentialsProvider = credentialsProviderBuilder.setAuthTypes(authTypes) .setUsername(httpClientInputs.getUsername()).setPassword(httpClientInputs.getPassword()) .setHost(uri.getHost()).setPort(String.valueOf(uri.getPort())) .setProxyUsername(httpClientInputs.getProxyUsername()) .setProxyPassword(httpClientInputs.getProxyPassword()).setProxyHost(httpClientInputs.getProxyHost()) .setProxyPort(httpClientInputs.getProxyPort()).buildCredentialsProvider(); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); Lookup<AuthSchemeProvider> authSchemeLookup = authSchemeProviderLookupBuilder.setAuthTypes(authTypes) .setHost(uri.getHost()).setHeaders(theHeaders) .setSkipPortAtKerberosDatabaseLookup(httpClientInputs.getKerberosSkipPortCheck()) .setKerberosConfigFile(httpClientInputs.getKerberosConfFile()) .setKerberosLoginConfigFile(httpClientInputs.getKerberosLoginConfFile()) .setUsername(httpClientInputs.getUsername()).setPassword(httpClientInputs.getPassword()) .buildAuthSchemeProviderLookup(); httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeLookup); httpRequestBase.setHeaders(theHeaders.toArray(new Header[theHeaders.size()])); CookieStore cookieStore = cookieStoreBuilder.setUseCookies(httpClientInputs.getUseCookies()) .setCookieStoreSessionObject(httpClientInputs.getCookieStoreSessionObject()).buildCookieStore(); if (cookieStore != null) { httpClientBuilder.setDefaultCookieStore(cookieStore); } SSLConnectionSocketFactory sslConnectionSocketFactory = sslConnectionSocketFactoryBuilder .setTrustAllRoots(httpClientInputs.getTrustAllRoots()).setKeystore(httpClientInputs.getKeystore()) .setKeystorePassword(httpClientInputs.getKeystorePassword()) .setTrustKeystore(httpClientInputs.getTrustKeystore()) .setTrustPassword(httpClientInputs.getTrustPassword()) .setX509HostnameVerifier(httpClientInputs.getX509HostnameVerifier()).build(); String connectionKey = ConnectionManagerBuilder.buildConnectionManagerMapKey( httpClientInputs.getTrustAllRoots(), httpClientInputs.getX509HostnameVerifier(), httpClientInputs.getKeystore(), httpClientInputs.getTrustKeystore()); PoolingHttpClientConnectionManager connManager = poolingHttpClientConnectionManagerBuilder .setConnectionManagerMapKey(connectionKey) .setConnectionPoolHolder(httpClientInputs.getConnectionPoolSessionObject()) .setSslsf(sslConnectionSocketFactory) .setDefaultMaxPerRoute(httpClientInputs.getConnectionsMaxPerRoute()) .setTotalMax(httpClientInputs.getConnectionsMaxTotal()).buildConnectionManager(); httpClientBuilder.setConnectionManager(connManager); if (StringUtils.isEmpty(httpClientInputs.getKeepAlive()) || Boolean.parseBoolean(httpClientInputs.getKeepAlive())) { httpClientBuilder.setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE); } else { httpClientBuilder.setConnectionReuseStrategy(NoConnectionReuseStrategy.INSTANCE); } CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); HttpClientContext context = contextBuilder.setAuthSchemeLookup(authSchemeLookup).setAuthTypes(authTypes) .setCredentialsProvider(credentialsProvider).setUri(uri) .setPreemptiveAuth(httpClientInputs.getPreemptiveAuth()).build(); HttpComponents result = new HttpComponents(); result.setCloseableHttpClient(closeableHttpClient); result.setHttpRequestBase(httpRequestBase); result.setHttpClientContext(context); result.setUri(uri); result.setConnManager(connManager); result.setCookieStore(cookieStore); result.setHttpClientBuilder(httpClientBuilder); return result; }
From source file:org.apache.zeppelin.livy.BaseLivyInterpreter.java
private RestTemplate createRestTemplate() { String keytabLocation = getProperty("zeppelin.livy.keytab"); String principal = getProperty("zeppelin.livy.principal"); boolean isSpnegoEnabled = StringUtils.isNotEmpty(keytabLocation) && StringUtils.isNotEmpty(principal); HttpClient httpClient = null;//from www . ja va 2 s . c o m if (livyURL.startsWith("https:")) { String keystoreFile = getProperty("zeppelin.livy.ssl.trustStore"); String password = getProperty("zeppelin.livy.ssl.trustStorePassword"); if (StringUtils.isBlank(keystoreFile)) { throw new RuntimeException("No zeppelin.livy.ssl.trustStore specified for livy ssl"); } if (StringUtils.isBlank(password)) { throw new RuntimeException("No zeppelin.livy.ssl.trustStorePassword specified " + "for livy ssl"); } FileInputStream inputStream = null; try { inputStream = new FileInputStream(keystoreFile); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(new FileInputStream(keystoreFile), password.toCharArray()); SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(trustStore).build(); SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext); HttpClientBuilder httpClientBuilder = HttpClients.custom().setSSLSocketFactory(csf); RequestConfig reqConfig = new RequestConfig() { @Override public boolean isAuthenticationEnabled() { return true; } }; httpClientBuilder.setDefaultRequestConfig(reqConfig); Credentials credentials = new Credentials() { @Override public String getPassword() { return null; } @Override public Principal getUserPrincipal() { return null; } }; CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, credentials); httpClientBuilder.setDefaultCredentialsProvider(credsProvider); if (isSpnegoEnabled) { Registry<AuthSchemeProvider> authSchemeProviderRegistry = RegistryBuilder .<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()) .build(); httpClientBuilder.setDefaultAuthSchemeRegistry(authSchemeProviderRegistry); } httpClient = httpClientBuilder.build(); } catch (Exception e) { throw new RuntimeException("Failed to create SSL HttpClient", e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { LOGGER.error("Failed to close keystore file", e); } } } } RestTemplate restTemplate = null; if (isSpnegoEnabled) { if (httpClient == null) { restTemplate = new KerberosRestTemplate(keytabLocation, principal); } else { restTemplate = new KerberosRestTemplate(keytabLocation, principal, httpClient); } } else { if (httpClient == null) { restTemplate = new RestTemplate(); } else { restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient)); } } restTemplate.getMessageConverters().add(0, new StringHttpMessageConverter(Charset.forName("UTF-8"))); return restTemplate; }
From source file:org.apache.zeppelin.submarine.hadoop.YarnClient.java
private static HttpClient buildSpengoHttpClient() { HttpClientBuilder builder = HttpClientBuilder.create(); Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create() .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory(true)).build(); builder.setDefaultAuthSchemeRegistry(authSchemeRegistry); BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(null, -1, null), new Credentials() { @Override//www. ja v a 2 s . com public Principal getUserPrincipal() { return null; } @Override public String getPassword() { return null; } }); builder.setDefaultCredentialsProvider(credentialsProvider); // Avoid output WARN: Cookie rejected RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); builder.setDefaultRequestConfig(globalConfig); CloseableHttpClient httpClient = builder.build(); return httpClient; }
From source file:org.mitre.dsmiley.httpproxy.ProxyServlet.java
/** * Called from {@link #init(javax.servlet.ServletConfig)}. HttpClient offers * many opportunities for customization. By default, <a href= * "http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/SystemDefaultHttpClient.html"> * SystemDefaultHttpClient</a> is used if available, otherwise it falls back * to:/*w w w . jav a2s. co m*/ * * <pre> * new DefaultHttpClient(new ThreadSafeClientConnManager(), hcParams) * </pre> * * SystemDefaultHttpClient uses PoolingClientConnectionManager. In any case, * it should be thread-safe. */ protected HttpClient createHttpClient(HttpParams hcParams) { try { String negotiateURL = getConfigParam("negotiate.url"); String negotiateSPN = getConfigParam("negotiate.spn"); if (negotiateURL != null && negotiateSPN != null) { System.out.println("negotiate url:" + negotiateURL); System.out.println("negotiate spn:" + negotiateSPN); // initialize the Windows security Context to get the negotiate // client token IWindowsSecurityContext clientContext = null; IWindowsCredentialsHandle clientCredentials = null; clientContext = WindowsSecurityContextImpl.getCurrent(SECURITY_PACKAGE, negotiateSPN); clientCredentials = WindowsCredentialsHandleImpl.getCurrent(SECURITY_PACKAGE); clientCredentials.initialize(); String username = WindowsAccountImpl.getCurrentUsername(); System.out.println("credentials for user " + username + " get prepared"); byte[] token = clientContext.getToken(); // encode the token with Base64 to be able to add it to the http // header String clientToken = Base64.encodeBase64String(token); System.out.println("clientToken" + clientToken); // if there is only a negotiate url the rest of the // authorization is based on cookies // so we need to support them. CookieStore cookieStore = new BasicCookieStore(); RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build(); HttpClientContext context = HttpClientContext.create(); proxyContext = context; context.setCookieStore(cookieStore); HttpClient httpClient = HttpClients.custom().disableRedirectHandling() .setDefaultRequestConfig(globalConfig).setDefaultCookieStore(cookieStore).build(); // first we need to act as a normal browser to get a http 401 // with negotiate header doActAsBrowser = true; HttpGet browserHttpGet = new HttpGet(negotiateURL); addBrowserHeader(browserHttpGet); HttpResponse rep = httpClient.execute(browserHttpGet, context); if (rep.getStatusLine().getStatusCode() == 401) { System.out.println("negotiate requested - sending negotiate client token"); HttpGet negotiateHttpGet = new HttpGet(negotiateURL); addBrowserHeader(negotiateHttpGet); negotiateHttpGet.addHeader("Authorization", "Negotiate " + clientToken); HttpResponse response = httpClient.execute(negotiateHttpGet, context); System.out.println( "http result code of negotiate request:" + response.getStatusLine().getStatusCode()); // now the url needs to be called periodically to keep the // cookie and connection alive String refreshTimeString = getConfigParam("negotiate.refreshtime"); long refreshTime = 1000000; if (refreshTimeString != null) { refreshTime = Long.parseLong(refreshTimeString); } HttpClientRefreshThread thread = new HttpClientRefreshThread(refreshTime, negotiateURL); thread.start(); List<org.apache.http.cookie.Cookie> cookies = context.getCookieStore().getCookies(); cookieString = ""; int size = cookies.size() - 1; for (int i = 0; i < cookies.size(); i++) { cookieString += cookies.get(i).getName(); cookieString += "="; cookieString += cookies.get(i).getValue(); if (i != size) cookieString += "; "; } } else { System.out.println("No negotiate requested"); } } else { if (!WinHttpClients.isWinAuthAvailable()) { System.out.println("Integrated Win auth is not supported!!!"); } else { HttpClientBuilder builder = WinHttpClients.custom(); Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create() .register(AuthSchemes.BASIC, new BasicSchemeFactory()) .register(AuthSchemes.DIGEST, new DigestSchemeFactory()) .register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()) .register(AuthSchemes.NTLM, new NTLMSchemeFactory()) .register(AuthSchemes.KERBEROS, new KerberosSchemeFactory()).build(); builder.setDefaultAuthSchemeRegistry(authSchemeRegistry); String username = getConfigParam("user"); String password = getConfigParam("password"); String domain = getConfigParam("domain"); String host = getConfigParam("host"); if (username != null) { NTCredentials cred = new NTCredentials(username, password, host, domain); CredentialsProvider credsProvider = new WindowsCredentialsProvider( new SystemDefaultCredentialsProvider()); credsProvider.setCredentials(AuthScope.ANY, cred); builder.setDefaultCredentialsProvider(credsProvider); } builder.disableCookieManagement(); builder.disableRedirectHandling(); return builder.build(); } // as of HttpComponents v4.2, this class is better since it uses // System // Properties: Class<?> clientClazz = Class.forName("org.apache.http.impl.client.SystemDefaultHttpClient"); Constructor<?> constructor = clientClazz.getConstructor(HttpParams.class); return (HttpClient) constructor.newInstance(hcParams); } } catch (ClassNotFoundException e) { // no problem; use v4.1 below } catch (Exception e) { throw new RuntimeException(e); } // Fallback on using older client: return new DefaultHttpClient(new ThreadSafeClientConnManager(), hcParams); }