use of org.apache.http.client.config.RequestConfig in project libresonic by Libresonic.
the class VersionService method readLatestVersion.
/**
* Resolves the latest available Libresonic version by screen-scraping a web page.
*
* @throws IOException If an I/O error occurs.
*/
private void readLatestVersion() throws IOException {
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build();
HttpGet method = new HttpGet(VERSION_URL + "?v=" + getLocalVersion());
method.setConfig(requestConfig);
String content;
try (CloseableHttpClient client = HttpClients.createDefault()) {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
content = client.execute(method, responseHandler);
}
Pattern finalPattern = Pattern.compile("LIBRESONIC_FULL_VERSION_BEGIN(.*)LIBRESONIC_FULL_VERSION_END");
Pattern betaPattern = Pattern.compile("LIBRESONIC_BETA_VERSION_BEGIN(.*)LIBRESONIC_BETA_VERSION_END");
try (BufferedReader reader = new BufferedReader(new StringReader(content))) {
String line = reader.readLine();
while (line != null) {
Matcher finalMatcher = finalPattern.matcher(line);
if (finalMatcher.find()) {
latestFinalVersion = new Version(finalMatcher.group(1));
LOG.info("Resolved latest Libresonic final version to: " + latestFinalVersion);
}
Matcher betaMatcher = betaPattern.matcher(line);
if (betaMatcher.find()) {
latestBetaVersion = new Version(betaMatcher.group(1));
LOG.info("Resolved latest Libresonic beta version to: " + latestBetaVersion);
}
line = reader.readLine();
}
}
}
use of org.apache.http.client.config.RequestConfig in project gradle by gradle.
the class HttpClientConfigurer method configureRequestConfig.
private void configureRequestConfig(HttpClientBuilder builder) {
HttpTimeoutSettings timeoutSettings = httpSettings.getTimeoutSettings();
RequestConfig config = RequestConfig.custom().setConnectTimeout(timeoutSettings.getConnectionTimeoutMs()).setSocketTimeout(timeoutSettings.getSocketTimeoutMs()).build();
builder.setDefaultRequestConfig(config);
}
use of org.apache.http.client.config.RequestConfig in project metacat by Netflix.
the class DruidHttpClientConfig method httpClient.
/**
* Http client.
*
* @param connectorContext connector context
* @return HttpClient
*/
@Bean
public HttpClient httpClient(final ConnectorContext connectorContext) {
final int timeout = (int) TimeUtil.toTime(connectorContext.getConfiguration().getOrDefault(DruidConfigConstants.HTTP_TIMEOUT, "5s"), TimeUnit.SECONDS, TimeUnit.MILLISECONDS);
final int poolsize = Integer.parseInt(connectorContext.getConfiguration().getOrDefault(DruidConfigConstants.POOL_SIZE, "10"));
final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).setMaxRedirects(3).build();
final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(poolsize);
return HttpClientBuilder.create().setDefaultRequestConfig(config).setConnectionManager(connectionManager).build();
}
use of org.apache.http.client.config.RequestConfig in project knime-core by knime.
the class ProfileManager method downloadProfiles.
private Path downloadProfiles(final URI profileLocation) {
Path stateDir = getStateLocation();
Path profileDir = stateDir.resolve("profiles");
try {
Files.createDirectories(stateDir);
URIBuilder builder = new URIBuilder(profileLocation);
builder.addParameter("profiles", String.join(",", m_provider.getRequestedProfiles()));
URI profileUri = builder.build();
m_collectedLogs.add(() -> NodeLogger.getLogger(ProfileManager.class).info("Downloading profiles from " + profileUri));
// proxies
HttpHost proxy = ProxySelector.getDefault().select(profileUri).stream().filter(p -> p.address() != null).findFirst().map(p -> new HttpHost(((InetSocketAddress) p.address()).getAddress())).orElse(null);
// timeout; we cannot access KNIMEConstants here because that would acccess preferences
int timeout = 2000;
String to = System.getProperty("knime.url.timeout", Integer.toString(timeout));
try {
timeout = Integer.parseInt(to);
} catch (NumberFormatException ex) {
m_collectedLogs.add(() -> NodeLogger.getLogger(ProfileManager.class).warn("Illegal value for system property knime.url.timeout :" + to, ex));
}
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setProxy(proxy).setConnectionRequestTimeout(timeout).build();
try (CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig).setRedirectStrategy(new DefaultRedirectStrategy()).build()) {
HttpGet get = new HttpGet(profileUri);
if (Files.isDirectory(profileDir)) {
Instant lastModified = Files.getLastModifiedTime(profileDir).toInstant();
get.setHeader("If-Modified-Since", DateTimeFormatter.RFC_1123_DATE_TIME.format(lastModified.atZone(ZoneId.of("GMT"))));
}
try (CloseableHttpResponse response = client.execute(get)) {
int code = response.getStatusLine().getStatusCode();
if ((code >= 200) && (code < 300)) {
Header ct = response.getFirstHeader("Content-Type");
if ((ct == null) || (ct.getValue() == null) || !ct.getValue().startsWith("application/zip")) {
// no zip file - it just processes an empty zip
throw new IOException("Server did not return a ZIP file containing the selected profiles");
}
Path tempDir = PathUtils.createTempDir("profile-download", stateDir);
try (InputStream is = response.getEntity().getContent()) {
PathUtils.unzip(new ZipInputStream(is), tempDir);
}
// replace profiles only if new data has been downloaded successfully
PathUtils.deleteDirectoryIfExists(profileDir);
Files.move(tempDir, profileDir, StandardCopyOption.ATOMIC_MOVE);
} else if (code != 304) {
// 304 = Not Modified
HttpEntity body = response.getEntity();
String msg;
if (body.getContentType().getValue().startsWith("text/")) {
byte[] buf = new byte[Math.min(4096, Math.max(4096, (int) body.getContentLength()))];
int read = body.getContent().read(buf);
msg = new String(buf, 0, read, "US-ASCII").trim();
} else if (!response.getStatusLine().getReasonPhrase().isEmpty()) {
msg = response.getStatusLine().getReasonPhrase();
} else {
msg = "Server returned status " + response.getStatusLine().getStatusCode();
}
throw new IOException(msg);
}
}
}
} catch (IOException | URISyntaxException ex) {
String msg = "Could not download profiles from " + profileLocation + ": " + ex.getMessage() + ". " + (Files.isDirectory(profileDir) ? "Will use existing but potentially outdated profiles." : "No profiles will be applied.");
m_collectedLogs.add(() -> NodeLogger.getLogger(ProfileManager.class).error(msg, ex));
}
return profileDir;
}
use of org.apache.http.client.config.RequestConfig in project stocator by SparkTC.
the class SwiftAPIDirect method httpPUT.
/**
* @param path path to object
* @param inputStream input stream
* @param account JOSS Account object
* @param metadata custom metadata
* @param size the object size
* @param type the content type
* @return HTTP response code
* @throws IOException if error
*/
private static int httpPUT(String path, InputStream inputStream, JossAccount account, SwiftConnectionManager scm, Map<String, String> metadata, long size, String type) throws IOException {
LOG.debug("HTTP PUT {} request on {}", path);
HttpPut httpPut = new HttpPut(path);
httpPut.addHeader("X-Auth-Token", account.getAuthToken());
httpPut.addHeader("Content-Type", type);
httpPut.addHeader(Constants.USER_AGENT_HTTP_HEADER, Constants.STOCATOR_USER_AGENT);
if (metadata != null && !metadata.isEmpty()) {
for (Map.Entry<String, String> entry : metadata.entrySet()) {
httpPut.addHeader("X-Object-Meta-" + entry.getKey(), entry.getValue());
}
}
RequestConfig config = RequestConfig.custom().setExpectContinueEnabled(true).build();
httpPut.setConfig(config);
InputStreamEntity entity = new InputStreamEntity(inputStream, size);
httpPut.setEntity(entity);
CloseableHttpClient httpclient = scm.createHttpConnection();
CloseableHttpResponse response = httpclient.execute(httpPut);
int responseCode = response.getStatusLine().getStatusCode();
LOG.debug("HTTP PUT {} response. Status code {}", path, responseCode);
response.close();
return responseCode;
}
Aggregations