List of usage examples for org.apache.http.impl.conn BasicHttpClientConnectionManager BasicHttpClientConnectionManager
public BasicHttpClientConnectionManager()
From source file:com.haulmont.cuba.web.security.idp.BaseIdpSessionFilter.java
@Nullable protected IdpSession getIdpSession(String idpTicket) throws IdpActivationException { String idpBaseURL = webIdpConfig.getIdpBaseURL(); if (!idpBaseURL.endsWith("/")) { idpBaseURL += "/"; }// w w w.j a v a2 s. com String idpTicketActivateUrl = idpBaseURL + "service/activate"; HttpPost httpPost = new HttpPost(idpTicketActivateUrl); httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType()); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity( Arrays.asList(new BasicNameValuePair("serviceProviderTicket", idpTicket), new BasicNameValuePair( "trustedServicePassword", webIdpConfig.getIdpTrustedServicePassword())), StandardCharsets.UTF_8); httpPost.setEntity(formEntity); HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); HttpClient client = HttpClientBuilder.create().setConnectionManager(connectionManager).build(); String idpResponse; try { HttpResponse httpResponse = client.execute(httpPost); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == 410) { // used old ticket return null; } if (statusCode != 200) { throw new IdpActivationException("Idp respond with status " + statusCode); } idpResponse = new BasicResponseHandler().handleResponse(httpResponse); } catch (IOException e) { throw new IdpActivationException(e); } finally { connectionManager.shutdown(); } IdpSession session; try { session = new Gson().fromJson(idpResponse, IdpSession.class); } catch (JsonSyntaxException e) { throw new IdpActivationException("Unable to parse idp response", e); } return session; }
From source file:com.haulmont.restapi.idp.IdpAuthController.java
@Nullable protected IdpSession getIdpSession(String idpTicket) throws InvalidGrantException { String idpBaseURL = this.idpBaseURL; if (!idpBaseURL.endsWith("/")) { idpBaseURL += "/"; }/* www. j a v a 2 s . c o m*/ String idpTicketActivateUrl = idpBaseURL + "service/activate"; HttpPost httpPost = new HttpPost(idpTicketActivateUrl); httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType()); UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity( Arrays.asList(new BasicNameValuePair("serviceProviderTicket", idpTicket), new BasicNameValuePair("trustedServicePassword", idpTrustedServicePassword)), StandardCharsets.UTF_8); httpPost.setEntity(formEntity); HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); HttpClient client = HttpClientBuilder.create().setConnectionManager(connectionManager).build(); String idpResponse; try { HttpResponse httpResponse = client.execute(httpPost); int statusCode = httpResponse.getStatusLine().getStatusCode(); if (statusCode == 410) { // used old ticket return null; } if (statusCode != 200) { throw new RuntimeException("Idp respond with status " + statusCode); } idpResponse = new BasicResponseHandler().handleResponse(httpResponse); } catch (IOException e) { throw new RuntimeException("Unable to connect to IDP", e); } finally { connectionManager.shutdown(); } IdpSession session; try { session = new Gson().fromJson(idpResponse, IdpSession.class); } catch (JsonSyntaxException e) { throw new RuntimeException("Unable to parse idp response", e); } return session; }
From source file:org.duracloud.common.web.RestHttpHelper.java
private HttpResponse executeRequest(String url, Method method, HttpEntity requestEntity, Map<String, String> headers) throws IOException { if (url == null || url.length() == 0) { throw new IllegalArgumentException("URL must be a non-empty value"); }/* w w w . j av a 2 s. c o m*/ HttpRequestBase httpRequest = method.getMethod(url, requestEntity); if (headers != null && headers.size() > 0) { addHeaders(httpRequest, headers); } if (log.isDebugEnabled()) { log.debug(loggingRequestText(url, method, requestEntity, headers)); } org.apache.http.HttpResponse response; if (null != credsProvider) { HttpClientBuilder builder = HttpClients.custom().setDefaultCredentialsProvider(credsProvider); if (socketTimeoutMs > -1) { BasicHttpClientConnectionManager cm = new BasicHttpClientConnectionManager(); cm.setSocketConfig(SocketConfig.custom().setSoTimeout(socketTimeoutMs).build()); builder.setConnectionManager(cm); } CloseableHttpClient httpClient = buildClient(builder, method); // Use preemptive basic auth URI requestUri = httpRequest.getURI(); HttpHost target = new HttpHost(requestUri.getHost(), requestUri.getPort(), requestUri.getScheme()); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); authCache.put(target, basicAuth); HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); response = httpClient.execute(httpRequest, localContext); } else { CloseableHttpClient httpClient = buildClient(HttpClients.custom(), method); response = httpClient.execute(httpRequest); } HttpResponse httpResponse = new HttpResponse(response); if (log.isDebugEnabled()) { log.debug(loggingResponseText(httpResponse)); } return httpResponse; }
From source file:net.sf.jasperreports.data.http.HttpDataService.java
protected CloseableHttpClient createHttpClient(Map<String, Object> parameters) { HttpClientBuilder clientBuilder = HttpClients.custom(); // single connection BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager(); clientBuilder.setConnectionManager(connManager); // ignore cookies for now RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); clientBuilder.setDefaultRequestConfig(requestConfig); setAuthentication(parameters, clientBuilder); CloseableHttpClient client = clientBuilder.build(); return client; }
From source file:org.apache.gobblin.service.modules.orchestration.AzkabanClient.java
/** * Create a {@link CloseableHttpClient} used to communicate with Azkaban server. * Derived class can configure different http client by overriding this method. * * @return A closeable http client./*from w w w . j ava 2 s . co m*/ */ protected CloseableHttpClient getClient() throws AzkabanClientException { try { // SSLSocketFactory using custom TrustStrategy that ignores warnings about untrusted certificates // Self sign SSL SSLContextBuilder sslcb = new SSLContextBuilder(); sslcb.loadTrustMaterial(null, (TrustStrategy) new TrustSelfSignedStrategy()); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcb.build()); HttpClientBuilder builder = HttpClientBuilder.create(); RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(10000) .setConnectTimeout(10000).setConnectionRequestTimeout(10000).build(); builder.disableCookieManagement().useSystemProperties().setDefaultRequestConfig(requestConfig) .setConnectionManager(new BasicHttpClientConnectionManager()).setSSLSocketFactory(sslsf); return builder.build(); } catch (Exception e) { throw new AzkabanClientException("HttpClient cannot be created", e); } }
From source file:org.apache.nifi.processors.standard.GetHTTP.java
@Override public void onTrigger(final ProcessContext context, final ProcessSessionFactory sessionFactory) throws ProcessException { final ComponentLog logger = getLogger(); final ProcessSession session = sessionFactory.createSession(); final FlowFile incomingFlowFile = session.get(); if (incomingFlowFile != null) { session.transfer(incomingFlowFile, REL_SUCCESS); logger.warn("found FlowFile {} in input queue; transferring to success", new Object[] { incomingFlowFile }); }//from w w w .j a v a2 s .c om // get the URL final String url = context.getProperty(URL).evaluateAttributeExpressions().getValue(); final URI uri; String source = url; try { uri = new URI(url); source = uri.getHost(); } catch (final URISyntaxException swallow) { // this won't happen as the url has already been validated } // get the ssl context service final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE) .asControllerService(SSLContextService.class); // create the connection manager final HttpClientConnectionManager conMan; if (sslContextService == null) { conMan = new BasicHttpClientConnectionManager(); } else { final SSLContext sslContext; try { sslContext = createSSLContext(sslContextService); } catch (final Exception e) { throw new ProcessException(e); } final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); // Also include a plain socket factory for regular http connections (especially proxies) final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder .<ConnectionSocketFactory>create().register("https", sslsf) .register("http", PlainConnectionSocketFactory.getSocketFactory()).build(); conMan = new BasicHttpClientConnectionManager(socketFactoryRegistry); } try { // build the request configuration final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setConnectionRequestTimeout( context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); requestConfigBuilder.setConnectTimeout( context.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); requestConfigBuilder.setSocketTimeout( context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue()); requestConfigBuilder.setRedirectsEnabled(context.getProperty(FOLLOW_REDIRECTS).asBoolean()); switch (context.getProperty(REDIRECT_COOKIE_POLICY).getValue()) { case STANDARD_COOKIE_POLICY_STR: requestConfigBuilder.setCookieSpec(CookieSpecs.STANDARD); break; case STRICT_COOKIE_POLICY_STR: requestConfigBuilder.setCookieSpec(CookieSpecs.STANDARD_STRICT); break; case NETSCAPE_COOKIE_POLICY_STR: requestConfigBuilder.setCookieSpec(CookieSpecs.NETSCAPE); break; case IGNORE_COOKIE_POLICY_STR: requestConfigBuilder.setCookieSpec(CookieSpecs.IGNORE_COOKIES); break; case DEFAULT_COOKIE_POLICY_STR: default: requestConfigBuilder.setCookieSpec(CookieSpecs.DEFAULT); } // build the http client final HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setConnectionManager(conMan); // include the user agent final String userAgent = context.getProperty(USER_AGENT).getValue(); if (userAgent != null) { clientBuilder.setUserAgent(userAgent); } // set the ssl context if necessary if (sslContextService != null) { clientBuilder.setSslcontext(sslContextService.createSSLContext(ClientAuth.REQUIRED)); } final String username = context.getProperty(USERNAME).getValue(); final String password = context.getProperty(PASSWORD).getValue(); // set the credentials if appropriate if (username != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); if (password == null) { credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username)); } else { credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); } clientBuilder.setDefaultCredentialsProvider(credentialsProvider); } // Set the proxy if specified if (context.getProperty(PROXY_HOST).isSet() && context.getProperty(PROXY_PORT).isSet()) { final String host = context.getProperty(PROXY_HOST).getValue(); final int port = context.getProperty(PROXY_PORT).asInteger(); clientBuilder.setProxy(new HttpHost(host, port)); } // create request final HttpGet get = new HttpGet(url); get.setConfig(requestConfigBuilder.build()); final StateMap beforeStateMap; try { beforeStateMap = context.getStateManager().getState(Scope.LOCAL); final String lastModified = beforeStateMap.get(LAST_MODIFIED + ":" + url); if (lastModified != null) { get.addHeader(HEADER_IF_MODIFIED_SINCE, parseStateValue(lastModified).getValue()); } final String etag = beforeStateMap.get(ETAG + ":" + url); if (etag != null) { get.addHeader(HEADER_IF_NONE_MATCH, parseStateValue(etag).getValue()); } } catch (final IOException ioe) { throw new ProcessException(ioe); } final String accept = context.getProperty(ACCEPT_CONTENT_TYPE).getValue(); if (accept != null) { get.addHeader(HEADER_ACCEPT, accept); } // Add dynamic headers PropertyValue customHeaderValue; for (PropertyDescriptor customProperty : customHeaders) { customHeaderValue = context.getProperty(customProperty).evaluateAttributeExpressions(); if (StringUtils.isNotBlank(customHeaderValue.getValue())) { get.addHeader(customProperty.getName(), customHeaderValue.getValue()); } } // create the http client try (final CloseableHttpClient client = clientBuilder.build()) { // NOTE: including this inner try in order to swallow exceptions on close try { final StopWatch stopWatch = new StopWatch(true); final HttpResponse response = client.execute(get); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == NOT_MODIFIED) { logger.info( "content not retrieved because server returned HTTP Status Code {}: Not Modified", new Object[] { NOT_MODIFIED }); context.yield(); // doing a commit in case there were flow files in the input queue session.commit(); return; } final String statusExplanation = response.getStatusLine().getReasonPhrase(); if ((statusCode >= 300) || (statusCode == 204)) { logger.error("received status code {}:{} from {}", new Object[] { statusCode, statusExplanation, url }); // doing a commit in case there were flow files in the input queue session.commit(); return; } FlowFile flowFile = session.create(); flowFile = session.putAttribute(flowFile, CoreAttributes.FILENAME.key(), context.getProperty(FILENAME).evaluateAttributeExpressions().getValue()); flowFile = session.putAttribute(flowFile, this.getClass().getSimpleName().toLowerCase() + ".remote.source", source); flowFile = session.importFrom(response.getEntity().getContent(), flowFile); final Header contentTypeHeader = response.getFirstHeader("Content-Type"); if (contentTypeHeader != null) { final String contentType = contentTypeHeader.getValue(); if (!contentType.trim().isEmpty()) { flowFile = session.putAttribute(flowFile, CoreAttributes.MIME_TYPE.key(), contentType.trim()); } } final long flowFileSize = flowFile.getSize(); stopWatch.stop(); final String dataRate = stopWatch.calculateDataRate(flowFileSize); session.getProvenanceReporter().receive(flowFile, url, stopWatch.getDuration(TimeUnit.MILLISECONDS)); session.transfer(flowFile, REL_SUCCESS); logger.info("Successfully received {} from {} at a rate of {}; transferred to success", new Object[] { flowFile, url, dataRate }); session.commit(); updateStateMap(context, response, beforeStateMap, url); } catch (final IOException e) { context.yield(); session.rollback(); logger.error("Failed to retrieve file from {} due to {}; rolling back session", new Object[] { url, e.getMessage() }, e); throw new ProcessException(e); } catch (final Throwable t) { context.yield(); session.rollback(); logger.error("Failed to process due to {}; rolling back session", new Object[] { t.getMessage() }, t); throw t; } } catch (final IOException e) { logger.debug("Error closing client due to {}, continuing.", new Object[] { e.getMessage() }); } } finally { conMan.shutdown(); } }
From source file:org.jboss.as.test.integration.security.common.Utils.java
/** * Creates request against SPNEGO protected web-app with FORM fallback. It tries to login using SPNEGO first - if it fails, * FORM is used./*from w w w. jav a2s. co m*/ * * @param contextUrl * @param page * @param user * @param pass * @param expectedStatusCode * @return * @throws IOException * @throws URISyntaxException * @throws PrivilegedActionException * @throws LoginException */ public static String makeHttpCallWithFallback(final String contextUrl, final String page, final String user, final String pass, final int expectedStatusCode) throws IOException, URISyntaxException, PrivilegedActionException, LoginException { final String strippedContextUrl = StringUtils.stripEnd(contextUrl, "/"); final String url = strippedContextUrl + page; LOGGER.trace("Requesting URL: " + url); String unauthorizedPageBody = null; final Krb5LoginConfiguration krb5Configuration = new Krb5LoginConfiguration(getLoginConfiguration()); Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create() .register(AuthSchemes.SPNEGO, new JBossNegotiateSchemeFactory(true)).build(); CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(null, -1, null), new NullHCCredentials()); final CloseableHttpClient httpClient = HttpClientBuilder.create() .setDefaultAuthSchemeRegistry(authSchemeRegistry).setDefaultCredentialsProvider(credentialsProvider) .setRedirectStrategy(REDIRECT_STRATEGY).setConnectionManager(new BasicHttpClientConnectionManager()) .build(); try { final HttpGet httpGet = new HttpGet(url); final HttpResponse response = httpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); if (HttpServletResponse.SC_UNAUTHORIZED != statusCode || StringUtils.isEmpty(user)) { assertEquals("Unexpected HTTP response status code.", expectedStatusCode, statusCode); return EntityUtils.toString(response.getEntity()); } final Header[] authnHeaders = response.getHeaders("WWW-Authenticate"); assertTrue("WWW-Authenticate header is present", authnHeaders != null && authnHeaders.length > 0); final Set<String> authnHeaderValues = new HashSet<String>(); for (final Header header : authnHeaders) { authnHeaderValues.add(header.getValue()); } assertTrue("WWW-Authenticate: Negotiate header is missing", authnHeaderValues.contains("Negotiate")); LOGGER.debug("HTTP response was SC_UNAUTHORIZED, let's authenticate the user " + user); unauthorizedPageBody = EntityUtils.toString(response.getEntity()); // Use our custom configuration to avoid reliance on external config Configuration.setConfiguration(krb5Configuration); // 1. Authenticate to Kerberos. final LoginContext lc = loginWithKerberos(krb5Configuration, user, pass); // 2. Perform the work as authenticated Subject. final String responseBody = Subject.doAs(lc.getSubject(), new PrivilegedExceptionAction<String>() { public String run() throws Exception { final HttpResponse response = httpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); assertEquals("Unexpected status code returned after the authentication.", expectedStatusCode, statusCode); return EntityUtils.toString(response.getEntity()); } }); lc.logout(); return responseBody; } catch (LoginException e) { assertNotNull(unauthorizedPageBody); assertTrue(unauthorizedPageBody.contains("j_security_check")); HttpPost httpPost = new HttpPost(strippedContextUrl + "/j_security_check"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("j_username", user)); nameValuePairs.add(new BasicNameValuePair("j_password", pass)); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); final HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); assertEquals("Unexpected status code returned after the authentication.", expectedStatusCode, statusCode); return EntityUtils.toString(response.getEntity()); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpClient.close(); // reset login configuration krb5Configuration.resetConfiguration(); } }
From source file:org.xwiki.extension.repository.http.internal.DefaultHttpClientFactory.java
@Override public HttpClientBuilder createHttpClientBuilder(Map<String, String> properties) { HttpClientBuilder httpClientBuilder = createHttpClientBuilder(properties.get("auth.user"), properties.get("auth.password")); // Set socket timeouts BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(); SocketConfig.Builder socketConfigBuilder = SocketConfig.custom(); socketConfigBuilder.setSoTimeout(getIntProperty(properties, SOCKET_TIMEOUT, 30000)); connectionManager.setSocketConfig(socketConfigBuilder.build()); httpClientBuilder.setConnectionManager(connectionManager); // Set request timeouts RequestConfig.Builder requestBuilder = RequestConfig.custom(); requestBuilder = requestBuilder.setConnectTimeout(getIntProperty(properties, CONNECTION_TIMEOUT, 30000)); httpClientBuilder.setDefaultRequestConfig(requestBuilder.build()); return httpClientBuilder; }