List of usage examples for org.apache.http.client.config RequestConfig custom
public static RequestConfig.Builder custom()
From source file:org.coding.git.api.CodingNetConnection.java
@NotNull private static RequestConfig createRequestConfig(@NotNull CodingNetAuthData auth) { RequestConfig.Builder builder = RequestConfig.custom(); int timeout = CodingNetSettings.getInstance().getConnectionTimeout(); builder.setConnectTimeout(timeout).setSocketTimeout(timeout); if (auth.isUseProxy()) { IdeHttpClientHelpers.ApacheHttpClient4.setProxyForUrlIfEnabled(builder, auth.getHost()); }/*from w ww .ja v a 2 s . co m*/ return builder.build(); }
From source file:org.tallison.cc.CCGetter.java
private void fetch(CCIndexRecord r, Path rootDir, BufferedWriter writer) throws IOException { Path targFile = rootDir.resolve(r.getDigest().substring(0, 2) + "/" + r.getDigest()); if (Files.isRegularFile(targFile)) { writeStatus(r, FETCH_STATUS.ALREADY_IN_REPOSITORY, writer); logger.info("already retrieved:" + targFile.toAbsolutePath()); return;//from w w w . ja v a2s .c om } String url = AWS_BASE + r.getFilename(); URI uri = null; try { uri = new URI(url); } catch (URISyntaxException e) { logger.warn("Bad url: " + url); writeStatus(r, FETCH_STATUS.BAD_URL, writer); return; } CloseableHttpClient httpClient = HttpClients.createDefault(); HttpHost target = new HttpHost(uri.getHost()); String urlPath = uri.getRawPath(); if (uri.getRawQuery() != null) { urlPath += "?" + uri.getRawQuery(); } HttpGet httpGet = null; try { httpGet = new HttpGet(urlPath); } catch (Exception e) { logger.warn("bad path " + uri.toString(), e); writeStatus(r, FETCH_STATUS.BAD_URL, writer); return; } if (proxyHost != null && proxyPort > -1) { HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http"); RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy).build(); httpGet.setConfig(requestConfig); } httpGet.addHeader("Range", r.getOffsetHeader()); HttpCoreContext coreContext = new HttpCoreContext(); CloseableHttpResponse httpResponse = null; URI lastURI = null; try { httpResponse = httpClient.execute(target, httpGet, coreContext); RedirectLocations redirectLocations = (RedirectLocations) coreContext .getAttribute(DefaultRedirectStrategy.REDIRECT_LOCATIONS); if (redirectLocations != null) { for (URI redirectURI : redirectLocations.getAll()) { lastURI = redirectURI; } } else { lastURI = httpGet.getURI(); } } catch (IOException e) { logger.warn("IOException for " + uri.toString(), e); writeStatus(r, FETCH_STATUS.FETCHED_IO_EXCEPTION, writer); return; } lastURI = uri.resolve(lastURI); if (httpResponse.getStatusLine().getStatusCode() != 200 && httpResponse.getStatusLine().getStatusCode() != 206) { logger.warn("Bad status for " + uri.toString() + " : " + httpResponse.getStatusLine().getStatusCode()); writeStatus(r, FETCH_STATUS.FETCHED_NOT_200, writer); return; } Path tmp = null; Header[] headers = null; boolean isTruncated = false; try { //this among other parts is plagiarized from centic9's CommonCrawlDocumentDownload //probably saved me hours. Thank you, Dominik! tmp = Files.createTempFile("cc-getter", ""); try (InputStream is = new GZIPInputStream(httpResponse.getEntity().getContent())) { WARCRecord warcRecord = new WARCRecord(new FastBufferedInputStream(is), "", 0); ArchiveRecordHeader archiveRecordHeader = warcRecord.getHeader(); if (archiveRecordHeader.getHeaderFields().containsKey(WARCConstants.HEADER_KEY_TRUNCATED)) { isTruncated = true; } headers = LaxHttpParser.parseHeaders(warcRecord, "UTF-8"); Files.copy(warcRecord, tmp, StandardCopyOption.REPLACE_EXISTING); } } catch (IOException e) { writeStatus(r, null, headers, 0L, isTruncated, FETCH_STATUS.FETCHED_IO_EXCEPTION_READING_ENTITY, writer); deleteTmp(tmp); return; } String digest = null; long tmpLength = 0l; try (InputStream is = Files.newInputStream(tmp)) { digest = base32.encodeAsString(DigestUtils.sha1(is)); tmpLength = Files.size(tmp); } catch (IOException e) { writeStatus(r, null, headers, tmpLength, isTruncated, FETCH_STATUS.FETCHED_IO_EXCEPTION_SHA1, writer); logger.warn("IOException during digesting: " + tmp.toAbsolutePath()); deleteTmp(tmp); return; } if (Files.exists(targFile)) { writeStatus(r, digest, headers, tmpLength, isTruncated, FETCH_STATUS.ALREADY_IN_REPOSITORY, writer); deleteTmp(tmp); return; } try { Files.createDirectories(targFile.getParent()); Files.copy(tmp, targFile); } catch (IOException e) { writeStatus(r, digest, headers, tmpLength, isTruncated, FETCH_STATUS.FETCHED_EXCEPTION_COPYING_TO_REPOSITORY, writer); deleteTmp(tmp); } writeStatus(r, digest, headers, tmpLength, isTruncated, FETCH_STATUS.ADDED_TO_REPOSITORY, writer); deleteTmp(tmp); }
From source file:com.couchbase.jdbc.core.ProtocolImpl.java
public ProtocolImpl(String url, Properties props) { if (props.containsKey(ConnectionParameters.USER)) { user = props.getProperty(ConnectionParameters.USER); }/*from w ww. j a va 2s .co m*/ if (props.containsKey(ConnectionParameters.PASSWORD)) { password = props.getProperty(ConnectionParameters.PASSWORD); } if (props.containsKey("credentials")) { credentials = props.getProperty("credentials"); } this.url = url; setConnectionTimeout(props.getProperty(ConnectionParameters.CONNECTION_TIMEOUT)); if (props.containsKey(ConnectionParameters.SCAN_CONSISTENCY)) { scanConsistency = props.getProperty(ConnectionParameters.SCAN_CONSISTENCY); } requestConfig = RequestConfig.custom().setConnectionRequestTimeout(0).setConnectTimeout(connectTimeout) .setSocketTimeout(connectTimeout).build(); if (props.containsKey(ConnectionParameters.ENABLE_SSL) && props.getProperty(ConnectionParameters.ENABLE_SSL).equals("true")) { SSLContextBuilder builder = SSLContexts.custom(); try { builder.loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }); SSLContext sslContext = builder.build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new X509HostnameVerifier() { @Override public void verify(String host, SSLSocket ssl) throws IOException { } @Override public void verify(String host, X509Certificate cert) throws SSLException { } @Override public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException { } @Override public boolean verify(String s, SSLSession sslSession) { return true; } }); Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder .<ConnectionSocketFactory>create().register("https", sslsf).build(); HttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry); httpClient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(requestConfig) .build(); ssl = true; } catch (Exception ex) { logger.error("Error creating ssl client", ex); } } else { httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build(); } }
From source file:org.apache.dubbo.rpc.protocol.rest.RestProtocol.java
@Override protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException { if (connectionMonitor == null) { connectionMonitor = new ConnectionMonitor(); }//w w w . j a v a 2 s .c o m // TODO more configs to add PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); // 20 is the default maxTotal of current PoolingClientConnectionManager connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20)); connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20)); connectionMonitor.addConnectionManager(connectionManager); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)) .setSocketTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)).build(); SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build(); CloseableHttpClient httpClient = HttpClientBuilder.create() .setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { return Long.parseLong(value) * 1000; } } // TODO constant return 30 * 1000; } }).setDefaultRequestConfig(requestConfig).setDefaultSocketConfig(socketConfig).build(); ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/*, localContext*/); ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build(); clients.add(client); client.register(RpcContextFilter.class); for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) { if (!StringUtils.isEmpty(clazz)) { try { client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim())); } catch (ClassNotFoundException e) { throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e); } } } // TODO protocol ResteasyWebTarget target = client .target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url)); return target.proxy(serviceType); }
From source file:org.drugis.addis.config.MainConfig.java
@Bean public RequestConfig requestConfig() { return RequestConfig.custom().setConnectionRequestTimeout(60000).setConnectTimeout(60000) .setSocketTimeout(60000).build(); }
From source file:org.phenotips.data.internal.MonarchPatientScorer.java
@Override public double getScore(Patient patient) { String key = getCacheKey(patient); PatientSpecificity specificity = this.cache.get(key); if (specificity != null) { return specificity.getScore(); }//from w w w . j a va2 s.c o m if (patient.getFeatures().isEmpty()) { this.cache.set(key, new PatientSpecificity(0, now(), SCORER_NAME)); return 0; } CloseableHttpResponse response = null; try { JSONObject data = new JSONObject(); JSONArray features = new JSONArray(); for (Feature f : patient.getFeatures()) { if (StringUtils.isNotEmpty(f.getId())) { JSONObject featureObj = new JSONObject(Collections.singletonMap("id", f.getId())); if (!f.isPresent()) { featureObj.put("isPresent", false); } features.put(featureObj); } } data.put("features", features); HttpPost method = new HttpPost(this.scorerURL); method.setEntity(new StringEntity("annotation_profile=" + URLEncoder.encode(data.toString(), "UTF-8"), ContentType.create("application/x-www-form-urlencoded", Consts.UTF_8))); RequestConfig config = RequestConfig.custom().setSocketTimeout(2000).build(); method.setConfig(config); response = this.client.execute(method); JSONObject score = new JSONObject( IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)); specificity = new PatientSpecificity(score.getDouble("scaled_score"), now(), SCORER_NAME); this.cache.set(key, specificity); return specificity.getScore(); } catch (Exception ex) { // Just return failure below this.logger.error( "Failed to compute specificity score for patient [{}] using the monarch server [{}]: {}", patient.getDocumentReference(), this.scorerURL, ex.getMessage()); } finally { if (response != null) { try { EntityUtils.consumeQuietly(response.getEntity()); response.close(); } catch (IOException ex) { // Not dangerous } } } return -1; }
From source file:com.caibowen.gplume.misc.test.HttpClientUtil.java
private static RequestConfig buildConfig(int connectTimeout, int soTimeout) { return RequestConfig.custom().setSocketTimeout(soTimeout * 1000).setConnectTimeout(connectTimeout * 1000) .setConnectionRequestTimeout(connectTimeout * 1000).build(); }
From source file:org.cjan.test_collector.Uploader.java
/** * Upload test run and tests to CJAN.org. * * @param groupId groupId/* w w w. j a va 2s .c o m*/ * @param artifactId artifactId * @param version version * @param envProps environment properties * @param testResults test results * @throws UploadException if it fails to upload */ public String upload(String groupId, String artifactId, String version, EnvironmentProperties envProps, TestResults testResults) throws UploadException { validate(groupId, "Missing groupId"); validate(artifactId, "Missing artifactId"); validate(version, "Missing version"); validate(envProps, "Missing environment properties"); validate(testResults, "Empty test suite"); LOGGER.info("Uploading test results to CJAN.org"); final TestRun testRun = new TestRun(groupId, artifactId, version, envProps, testResults.getGeneralStatus()); testRun.addTests(testResults.getTests()); if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.log(Level.FINEST, "Test Run: " + testRun.toString()); } final ObjectMapper mapper = new ObjectMapper(); final String json; try { json = mapper.writeValueAsString(testRun); if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.log(Level.FINEST, "Test Run: " + testRun.toString()); } } catch (JsonGenerationException e) { throw new UploadException("Error converting test run to JSON: " + e.getMessage(), e); } catch (JsonMappingException e) { throw new UploadException("Error mapping object fields: " + e.getMessage(), e); } catch (IOException e) { throw new UploadException("IO error: " + e.getMessage(), e); } final CloseableHttpClient httpclient = HttpClients.createDefault(); try { RequestConfig config = null; if (isProxyEnabled()) { LOGGER.fine("Using HTTP proxy!"); final HttpHost proxy = new HttpHost(getProxyHost(), Integer.parseInt(getProxyPort())); config = RequestConfig.custom().setProxy(proxy).build(); } final HttpPost post = new HttpPost(getUrl()); if (config != null) { post.setConfig(config); } // add access token to headers post.addHeader("x-access-token", getAccessToken()); // post testrun, get ID back from server, show to user List<NameValuePair> pairs = new ArrayList<NameValuePair>(1); pairs.add(new BasicNameValuePair("json", json)); post.setEntity(new UrlEncodedFormEntity(pairs)); CloseableHttpResponse response = httpclient.execute(post); try { HttpEntity entity = response.getEntity(); String body = EntityUtils.toString(entity); return body; } finally { response.close(); } } catch (ClientProtocolException e) { throw new UploadException("Client protocol error: " + e.getMessage(), e); } catch (IOException e) { throw new UploadException("IO error: " + e.getMessage(), e); } finally { if (httpclient != null) { try { httpclient.close(); } catch (IOException e) { LOGGER.log(Level.WARNING, "Error closing HTTP client: " + e.getMessage(), e); } } } }
From source file:com.jaeksoft.searchlib.crawler.web.spider.HttpAbstract.java
protected void execute(HttpRequestBase httpBaseRequest, CredentialItem credentialItem, List<CookieItem> cookies) throws ClientProtocolException, IOException, URISyntaxException { if (!CollectionUtils.isEmpty(cookies)) { List<Cookie> cookieList = cookieStore.getCookies(); for (CookieItem cookie : cookies) { Cookie newCookie = cookie.getCookie(); if (!cookieList.contains(newCookie)) cookieStore.addCookie(newCookie); }/*from w w w.j av a 2 s.c om*/ } this.httpBaseRequest = httpBaseRequest; // No more than one 1 minute to establish the connection // No more than 10 minutes to establish the socket // Enable stales connection checking // Cookies uses browser compatibility RequestConfig.Builder configBuilber = RequestConfig.custom().setSocketTimeout(1000 * 60 * 10) .setConnectTimeout(1000 * 60).setStaleConnectionCheckEnabled(true) .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY); if (credentialItem == null) credentialsProvider.clear(); else credentialItem.setUpCredentials(credentialsProvider); URI uri = httpBaseRequest.getURI(); if (proxyHandler != null) proxyHandler.check(configBuilber, uri, credentialsProvider); httpBaseRequest.setConfig(configBuilber.build()); httpClientContext = new HttpClientContext(); httpResponse = httpClient.execute(httpBaseRequest, httpClientContext); if (httpResponse == null) return; statusLine = httpResponse.getStatusLine(); httpEntity = httpResponse.getEntity(); }
From source file:net.sasasin.sreader.commons.util.impl.WgetHttpComponentsImpl.java
@Override public URL getOriginalUrl() { // HTTP 30x??? RequestConfig config = RequestConfig.custom().setRedirectsEnabled(false) .setConnectTimeout(DEFAULT_TIMEOUT_MILLISECONDS) .setConnectionRequestTimeout(DEFAULT_TIMEOUT_MILLISECONDS).build(); // UserAgent?MSIE List<Header> headers = new ArrayList<Header>(); headers.add(new BasicHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)")); HttpResponse responce = null;/*ww w. j a va 2 s . com*/ try (CloseableHttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config) .setDefaultHeaders(headers).build()) { responce = httpclient.execute(new HttpHead(getUrl().toString())); int httpStatusCode = responce.getStatusLine().getStatusCode(); if (httpStatusCode == HttpStatus.SC_MOVED_PERMANENTLY || httpStatusCode == HttpStatus.SC_MOVED_TEMPORARILY) { // 301,302???Location??URL?? Header h = responce.getFirstHeader("Location"); if (!getUrl().toString().equals(h.getValue())) { // URL????URL? return new URL(h.getValue()); } } } catch (IOException e) { e.printStackTrace(); } // 30x????????URL? return getUrl(); }