List of usage examples for org.apache.http.impl.conn DefaultProxyRoutePlanner DefaultProxyRoutePlanner
public DefaultProxyRoutePlanner(final HttpHost proxy)
From source file:org.eclipse.cft.server.core.internal.client.RestUtils.java
public static ClientHttpRequestFactory createRequestFactory(HttpProxyConfiguration httpProxyConfiguration, boolean trustSelfSignedCerts, boolean disableRedirectHandling) { HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties(); if (trustSelfSignedCerts) { httpClientBuilder.setSslcontext(buildSslContext()); httpClientBuilder.setHostnameVerifier(BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); }/*from w ww .ja v a2s . c o m*/ if (disableRedirectHandling) { httpClientBuilder.disableRedirectHandling(); } if (httpProxyConfiguration != null) { HttpHost proxy = new HttpHost(httpProxyConfiguration.getProxyHost(), httpProxyConfiguration.getProxyPort()); httpClientBuilder.setProxy(proxy); if (httpProxyConfiguration.isAuthRequired()) { BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials( new AuthScope(httpProxyConfiguration.getProxyHost(), httpProxyConfiguration.getProxyPort()), new UsernamePasswordCredentials(httpProxyConfiguration.getUsername(), httpProxyConfiguration.getPassword())); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); httpClientBuilder.setRoutePlanner(routePlanner); } HttpClient httpClient = httpClientBuilder.build(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( httpClient); return requestFactory; }
From source file:com.intuit.wasabi.export.rest.impl.DefaultRestDriver.java
private CloseableHttpClient createCloseableHttpClientWithProxy(final HttpClientBuilder httpClientBuilder, final String proxyHost, final Integer proxyPort) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); DefaultProxyRoutePlanner defaultProxyRoutePlanner = new DefaultProxyRoutePlanner(proxy); httpClientBuilder.setRoutePlanner(defaultProxyRoutePlanner); return httpClientBuilder.build(); }
From source file:org.commonjava.indy.httprox.AbstractHttproxFunctionalTest.java
protected CloseableHttpClient proxiedHttp(final String user, final String pass, SSLSocketFactory socketFactory) throws Exception { CredentialsProvider creds = null;/*from www . j a va 2 s . com*/ if (user != null) { creds = new BasicCredentialsProvider(); creds.setCredentials(new AuthScope(HOST, proxyPort), new UsernamePasswordCredentials(user, pass)); } HttpHost proxy = new HttpHost(HOST, proxyPort); final HttpRoutePlanner planner = new DefaultProxyRoutePlanner(proxy); HttpClientBuilder builder = HttpClients.custom().setRoutePlanner(planner) .setDefaultCredentialsProvider(creds).setProxy(proxy).setSSLSocketFactory(socketFactory); return builder.build(); }
From source file:com.evolveum.polygon.scim.ServiceAccessManager.java
/** * Used for login to the service. The data needed for this operation is * provided by the configuration.//from w w w. jav a 2s. co m * * @param configuration * The instance of "ScimConnectorConfiguration" which holds all * the provided configuration data. * * @return a Map object carrying meta information about the login session. */ public void logIntoService(ScimConnectorConfiguration configuration) { HttpPost loginInstance = new HttpPost(); Header authHeader = null; String loginAccessToken = null; String loginInstanceUrl = null; JSONObject jsonObject = null; String proxyUrl = configuration.getProxyUrl(); LOGGER.ok("proxyUrl: {0}", proxyUrl); // LOGGER.ok("Configuration: {0}", configuration); if (!"token".equalsIgnoreCase(configuration.getAuthentication())) { HttpClient httpClient; if (proxyUrl != null && !proxyUrl.isEmpty()) { HttpHost proxy = new HttpHost(proxyUrl, configuration.getProxyPortNumber()); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); httpClient = HttpClientBuilder.create().setRoutePlanner(routePlanner).build(); LOGGER.ok("Proxy enabled: {0}:{1}", proxyUrl, configuration.getProxyPortNumber()); } else { httpClient = HttpClientBuilder.create().build(); } String loginURL = new StringBuilder(configuration.getLoginURL()).append(configuration.getService()) .toString(); GuardedString guardedPassword = configuration.getPassword(); GuardedStringAccessor accessor = new GuardedStringAccessor(); guardedPassword.access(accessor); String contentUri = new StringBuilder("&client_id=").append(configuration.getClientID()) .append("&client_secret=").append(configuration.getClientSecret()).append("&username=") .append(configuration.getUserName()).append("&password=").append(accessor.getClearString()) .toString(); loginInstance = new HttpPost(loginURL); CloseableHttpResponse response = null; StringEntity bodyContent; String getResult = null; Integer statusCode = null; try { bodyContent = new StringEntity(contentUri); bodyContent.setContentType("application/x-www-form-urlencoded"); loginInstance.setEntity(bodyContent); response = (CloseableHttpResponse) httpClient.execute(loginInstance); getResult = EntityUtils.toString(response.getEntity()); statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { LOGGER.info("Login Successful"); } else { String[] loginUrlParts; String providerName = ""; if (configuration.getLoginURL() != null && !configuration.getLoginURL().isEmpty()) { loginUrlParts = configuration.getLoginURL().split("\\."); // e.g. // https://login.salesforce.com } else { loginUrlParts = configuration.getBaseUrl().split("\\."); // e.g. } // https://login.salesforce.com if (loginUrlParts.length >= 2) { providerName = loginUrlParts[1]; } if (!providerName.isEmpty()) { StrategyFetcher fetcher = new StrategyFetcher(); HandlingStrategy strategy = fetcher.fetchStrategy(providerName); strategy.handleInvalidStatus(" while loging into service", getResult, "loging into service", statusCode); } } jsonObject = (JSONObject) new JSONTokener(getResult).nextValue(); loginAccessToken = jsonObject.getString("access_token"); loginInstanceUrl = jsonObject.getString("instance_url"); } catch (UnsupportedEncodingException e) { LOGGER.error("Unsupported encoding: {0}. Occurrence in the process of login into the service", e.getLocalizedMessage()); LOGGER.info("Unsupported encoding: {0}. Occurrence in the process of login into the service", e); throw new ConnectorException( "Unsupported encoding. Occurrence in the process of login into the service", e); } catch (ClientProtocolException e) { LOGGER.error( "An protocol exception has occurred while processing the http response to the login request. Possible mismatch in interpretation of the HTTP specification: {0}", e.getLocalizedMessage()); LOGGER.info( "An protocol exception has occurred while processing the http response to the login request. Possible mismatch in interpretation of the HTTP specification: {0}", e); throw new ConnectionFailedException( "An protocol exception has occurred while processing the http response to the login request. Possible mismatch in interpretation of the HTTP specification", e); } catch (IOException ioException) { StringBuilder errorBuilder = new StringBuilder( "An error occurred while processing the query http response to the login request. "); if ((ioException instanceof SocketTimeoutException || ioException instanceof NoRouteToHostException)) { errorBuilder.insert(0, "The connection timed out. "); throw new OperationTimeoutException(errorBuilder.toString(), ioException); } else { LOGGER.error( "An error occurred while processing the query http response to the login request : {0}", ioException.getLocalizedMessage()); LOGGER.info( "An error occurred while processing the query http response to the login request : {0}", ioException); throw new ConnectorIOException(errorBuilder.toString(), ioException); } } catch (JSONException jsonException) { LOGGER.error( "An exception has occurred while setting the \"jsonObject\". Occurrence while processing the http response to the login request: {0}", jsonException.getLocalizedMessage()); LOGGER.info( "An exception has occurred while setting the \"jsonObject\". Occurrence while processing the http response to the login request: {0}", jsonException); throw new ConnectorException("An exception has occurred while setting the \"jsonObject\".", jsonException); } finally { try { response.close(); } catch (IOException e) { if ((e instanceof SocketTimeoutException || e instanceof NoRouteToHostException)) { throw new OperationTimeoutException( "The connection timed out while closing the http connection. Occurrence in the process of logging into the service", e); } else { LOGGER.error( "An error has occurred while processing the http response and closing the http connection. Occurrence in the process of logging into the service: {0}", e.getLocalizedMessage()); throw new ConnectorIOException( "An error has occurred while processing the http response and closing the http connection. Occurrence in the process of logging into the service", e); } } } authHeader = new BasicHeader("Authorization", "OAuth " + loginAccessToken); } else { loginInstanceUrl = configuration.getBaseUrl(); GuardedString guardedToken = configuration.getToken(); GuardedStringAccessor accessor = new GuardedStringAccessor(); guardedToken.access(accessor); loginAccessToken = accessor.getClearString(); authHeader = new BasicHeader("Authorization", "Bearer " + loginAccessToken); } String scimBaseUri = new StringBuilder(loginInstanceUrl).append(configuration.getEndpoint()) .append(configuration.getVersion()).toString(); this.baseUri = scimBaseUri; this.aHeader = authHeader; if (jsonObject != null) { this.loginJson = jsonObject; } }
From source file:com.okta.sdk.framework.ApiClient.java
/** * Constructor for the ApiClient./*w ww . j a va2 s.c om*/ * * Bootstraps an HTTPClient to make various requests to the Okta API. * * @param config {@link ApiClientConfiguration} */ public ApiClient(ApiClientConfiguration config) { Proxy proxy = ProxySelector.getDefault().select(URI.create(config.getBaseUrl())).iterator().next(); HttpRoutePlanner routePlanner; if (Proxy.Type.HTTP.equals(proxy.type())) { URI proxyUri = URI.create(proxy.type() + "://" + proxy.address()); HttpHost proxyHost = new HttpHost(proxyUri.getHost(), proxyUri.getPort(), proxyUri.getScheme()); routePlanner = new DefaultProxyRoutePlanner(proxyHost); } else { routePlanner = new DefaultRoutePlanner(null); } StandardHttpRequestRetryHandler requestRetryHandler = new StandardHttpRequestRetryHandler(RETRY_COUNT, true); HttpClient client = HttpClientBuilder.create().setRetryHandler(requestRetryHandler) .setUserAgent("OktaSDKJava_v" + Utils.getSdkVersion()).disableCookieManagement() .setRoutePlanner(routePlanner).build(); this.httpClient = client; this.baseUrl = config.getBaseUrl(); this.apiVersion = config.getApiVersion(); this.configuration = config; this.token = config.getApiToken(); initMarshaller(); }
From source file:net.siegmar.japtproxy.fetcher.HttpClientConfigurer.java
protected void configureProxy(final HttpClientBuilder httpClientBuilder, final String proxy) throws InitializationException { final URL proxyUrl; try {//from ww w. ja v a 2 s . co m proxyUrl = new URL(proxy); } catch (final MalformedURLException e) { throw new InitializationException("Invalid proxy url", e); } final String proxyHost = proxyUrl.getHost(); final int proxyPort = proxyUrl.getPort() != -1 ? proxyUrl.getPort() : proxyUrl.getDefaultPort(); LOG.info("Set proxy server to '{}:{}'", proxyHost, proxyPort); httpClientBuilder.setRoutePlanner(new DefaultProxyRoutePlanner(new HttpHost(proxyHost, proxyPort))); final String userInfo = proxyUrl.getUserInfo(); if (userInfo != null) { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), buildCredentials(userInfo)); httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); } }
From source file:com.digitalpebble.stormcrawler.protocol.httpclient.HttpProtocol.java
@Override public void configure(final Config conf) { super.configure(conf); this.maxContent = ConfUtils.getInt(conf, "http.content.limit", -1); String userAgent = getAgentString(ConfUtils.getString(conf, "http.agent.name"), ConfUtils.getString(conf, "http.agent.version"), ConfUtils.getString(conf, "http.agent.description"), ConfUtils.getString(conf, "http.agent.url"), ConfUtils.getString(conf, "http.agent.email")); builder = HttpClients.custom().setUserAgent(userAgent).setConnectionManager(CONNECTION_MANAGER) .setConnectionManagerShared(true).disableRedirectHandling().disableAutomaticRetries(); String proxyHost = ConfUtils.getString(conf, "http.proxy.host", null); int proxyPort = ConfUtils.getInt(conf, "http.proxy.port", 8080); boolean useProxy = proxyHost != null && proxyHost.length() > 0; // use a proxy? if (useProxy) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); builder.setRoutePlanner(routePlanner); }// w ww .j a va2s .c om int timeout = ConfUtils.getInt(conf, "http.timeout", 10000); Builder requestConfigBuilder = RequestConfig.custom(); requestConfigBuilder.setSocketTimeout(timeout); requestConfigBuilder.setConnectTimeout(timeout); requestConfigBuilder.setConnectionRequestTimeout(timeout); requestConfigBuilder.setCookieSpec(CookieSpecs.STANDARD); requestConfig = requestConfigBuilder.build(); }
From source file:com.digitalpebble.storm.crawler.protocol.httpclient.HttpProtocol.java
@Override public void configure(final Config conf) { this.maxContent = ConfUtils.getInt(conf, "http.content.limit", 64 * 1024); String userAgent = getAgentString(ConfUtils.getString(conf, "http.agent.name"), ConfUtils.getString(conf, "http.agent.version"), ConfUtils.getString(conf, "http.agent.description"), ConfUtils.getString(conf, "http.agent.url"), ConfUtils.getString(conf, "http.agent.email")); this.responseTime = ConfUtils.getBoolean(conf, "http.store.responsetime", true); this.skipRobots = ConfUtils.getBoolean(conf, "http.skip.robots", false); robots = new HttpRobotRulesParser(conf); builder = HttpClients.custom().setUserAgent(userAgent).setConnectionManager(CONNECTION_MANAGER) .setConnectionManagerShared(true).disableRedirectHandling(); String proxyHost = ConfUtils.getString(conf, "http.proxy.host", null); int proxyPort = ConfUtils.getInt(conf, "http.proxy.port", 8080); boolean useProxy = (proxyHost != null && proxyHost.length() > 0); // use a proxy? if (useProxy) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); builder.setRoutePlanner(routePlanner); }/*from w w w . j a v a2 s.com*/ int timeout = ConfUtils.getInt(conf, "http.timeout", 10000); requestConfig = RequestConfig.custom().setSocketTimeout(timeout).setConnectTimeout(timeout).build(); }