List of usage examples for org.apache.http.client.config RequestConfig DEFAULT
RequestConfig DEFAULT
To view the source code for org.apache.http.client.config RequestConfig DEFAULT.
Click Source Link
From source file:de.stklcode.jvault.connector.HTTPVaultConnector.java
/** * Execute prepared HTTP request and return result. * * @param base Prepares Request/* w w w.j a v a 2 s . co m*/ * @param retries number of retries * @return HTTP response * @throws VaultConnectorException on connection error */ private String request(final HttpRequestBase base, final int retries) throws VaultConnectorException { /* Set JSON Header */ base.addHeader("accept", "application/json"); CloseableHttpResponse response = null; try (CloseableHttpClient httpClient = HttpClientBuilder.create() .setSSLSocketFactory(createSSLSocketFactory()).build()) { /* Set custom timeout, if defined */ if (this.timeout != null) base.setConfig(RequestConfig.copy(RequestConfig.DEFAULT).setConnectTimeout(timeout).build()); /* Execute request */ response = httpClient.execute(base); /* Check if response is valid */ if (response == null) throw new InvalidResponseException("Response unavailable"); switch (response.getStatusLine().getStatusCode()) { case 200: return handleResult(response); case 204: return ""; case 403: throw new PermissionDeniedException(); default: if (response.getStatusLine().getStatusCode() >= 500 && response.getStatusLine().getStatusCode() < 600 && retries > 0) { /* Retry on 5xx errors */ return request(base, retries - 1); } else { /* Fail on different error code and/or no retries left */ handleError(response); /* Throw exception withoud details, if response entity is empty. */ throw new InvalidResponseException(Error.RESPONSE_CODE, response.getStatusLine().getStatusCode()); } } } catch (IOException e) { throw new InvalidResponseException(Error.READ_RESPONSE, e); } finally { if (response != null && response.getEntity() != null) try { EntityUtils.consume(response.getEntity()); } catch (IOException ignored) { // Exception ignored. } } }
From source file:net.www_eee.portal.channels.ProxyChannel.java
/** * Construct and configure the {@link RequestConfig} to be used for * {@linkplain #createProxyRequestObject(Page.Request, Mode, URL) creating} the {@link HttpUriRequest} to * {@linkplain #doProxyRequest(Page.Request, Channel.Mode) proxy} content while fulfilling the specified * <code>pageRequest</code>. * /*from w w w . jav a 2 s .c o m*/ * @param pageRequest The {@link net.www_eee.portal.Page.Request Request} currently being processed. * @param mode The {@link net.www_eee.portal.Channel.Mode Mode} of the request. * @return The created {@link RequestConfig}. * @throws WWWEEEPortal.Exception If a problem occurred while determining the result. * @see #createProxyClient(Page.Request) * @see #PROXY_CLIENT_REQUEST_CONFIG_HOOK */ protected RequestConfig createProxyClientRequestConfig(final Page.Request pageRequest, final Mode mode) throws WWWEEEPortal.Exception { final Object[] context = new Object[] { mode }; RequestConfig.Builder requestConfig = PROXY_CLIENT_REQUEST_CONFIG_HOOK.value(plugins, context, pageRequest); if (requestConfig == null) { requestConfig = RequestConfig.copy(RequestConfig.DEFAULT); requestConfig = requestConfig.setConnectTimeout(getConnectTimeout(pageRequest)); requestConfig = requestConfig.setSocketTimeout(getReadTimeout(pageRequest)); requestConfig = requestConfig.setRedirectsEnabled(isFollowRedirectsEnabled(pageRequest)); requestConfig = requestConfig.setAuthenticationEnabled(false); } requestConfig = PROXY_CLIENT_REQUEST_CONFIG_HOOK.requireFilteredResult( PROXY_CLIENT_REQUEST_CONFIG_HOOK.filter(plugins, context, pageRequest, requestConfig)); return requestConfig.build(); }
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.//w ww .j ava 2 s .c om */ 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.olat.restapi.RestConnection.java
private void decorateHttpMessage(HttpRequestBase msg, String accept, String langage, boolean cookie) { if (cookie) { RequestConfig config = RequestConfig.copy(RequestConfig.DEFAULT) .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY).build(); msg.setConfig(config);//from ww w . j a v a 2 s.c o m } if (StringHelper.containsNonWhitespace(accept)) { msg.addHeader("Accept", accept); } if (StringHelper.containsNonWhitespace(langage)) { msg.addHeader("Accept-Language", langage); } }
From source file:org.openhab.binding.fritzboxtr064.internal.Tr064Comm.java
/** * Creates an Apache HTTP Client object, ignoring SSL Exceptions like self signed * certificates, and sets Auth. Scheme to Digest Auth. * * @param fboxUrl//from w w w .java 2 s. c o m * the URL from config file of fbox to connect to * @return the ready-to-use httpclient for tr064 requests */ private synchronized CloseableHttpClient createTr064HttpClient(String fboxUrl) { CloseableHttpClient hc = null; // Convert URL String from config in easy explotable URI object URIBuilder uriFbox = null; try { uriFbox = new URIBuilder(fboxUrl); } catch (URISyntaxException e) { logger.error("Invalid FritzBox URL! {}", e.getMessage()); return null; } // Create context of the http client _httpClientContext = HttpClientContext.create(); CookieStore cookieStore = new BasicCookieStore(); _httpClientContext.setCookieStore(cookieStore); // SETUP AUTH // Auth is specific for this target HttpHost target = new HttpHost(uriFbox.getHost(), uriFbox.getPort(), uriFbox.getScheme()); // Add digest authentication with username/pw from global config CredentialsProvider credp = new BasicCredentialsProvider(); credp.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(_user, _pw)); // Create AuthCache instance. Manages authentication based on server response AuthCache authCache = new BasicAuthCache(); // Generate DIGEST scheme object, initialize it and add it to the local auth // cache. Digeste is standard for fbox auth SOAP DigestScheme digestAuth = new DigestScheme(); digestAuth.overrideParamter("realm", "HTTPS Access"); // known from fbox specification digestAuth.overrideParamter("nonce", ""); // never known at first request authCache.put(target, digestAuth); // Add AuthCache to the execution context _httpClientContext.setAuthCache(authCache); // SETUP SSL TRUST SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); SSLConnectionSocketFactory sslsf = null; try { sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy()); // accept self signed certs // dont verify hostname against cert CN sslsf = new SSLConnectionSocketFactory(sslContextBuilder.build(), null, null, new NoopHostnameVerifier()); } catch (Exception ex) { logger.error(ex.getMessage()); } // Set timeout values RequestConfig rc = RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout(4000).setConnectTimeout(4000) .setConnectionRequestTimeout(4000).build(); // BUILDER // setup builder with parameters defined before hc = HttpClientBuilder.create().setSSLSocketFactory(sslsf) // set the SSL options which trust every self signed // cert .setDefaultCredentialsProvider(credp) // set auth options using digest .setDefaultRequestConfig(rc) // set the request config specifying timeout .build(); return hc; }