use of org.apache.http.client.config.RequestConfig in project indy by Commonjava.
the class ReplicationController method newGet.
private HttpGet newGet(final String url, final ReplicationDTO dto) {
final HttpGet get = new HttpGet(url);
final int proxyPort = dto.getProxyPort();
HttpHost proxy;
if (proxyPort < 1) {
proxy = new HttpHost(dto.getProxyHost(), -1, "http");
} else {
proxy = new HttpHost(dto.getProxyHost(), dto.getProxyPort(), "http");
}
final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
get.setConfig(config);
return get;
}
use of org.apache.http.client.config.RequestConfig in project stanbol by apache.
the class RequestExecutor method execute.
/**
* Executes a {@link Request} using this executor. <p>
* Note that this cleans up all data of the previous executed request.
* @param r the request to execute
* @return this
* @throws ClientProtocolException
* @throws IOException
*/
public RequestExecutor execute(Request r) throws ClientProtocolException, IOException {
clear();
request = r.getRequest();
RequestConfig rc = RequestConfig.custom().setRedirectsEnabled(r.getRedirects()).setRelativeRedirectsAllowed(true).build();
request.setConfig(rc);
// Execute request
response = httpClient.execute(request);
entity = response.getEntity();
if (entity != null) {
// We fully read the content every time, not super efficient but
// how can we read it on demand while avoiding a (boring) cleanup()
// method on this class?
content = EntityUtils.toByteArray(entity);
contentType = ContentType.getOrDefault(entity);
charset = contentType.getCharset();
contentString = new String(content, charset != null ? charset : HTTP.DEF_CONTENT_CHARSET);
//and close the stream
entity.getContent().close();
}
return this;
}
use of org.apache.http.client.config.RequestConfig in project stanbol by apache.
the class MultiThreadedTestBase method initialiseHttpClient.
@Before
public void initialiseHttpClient() {
if (this.pooledHttpClient == null) {
//init for the first test
RequestConfig requestConfig = RequestConfig.custom().setRedirectsEnabled(true).setMaxRedirects(3).build();
SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).build();
connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setDefaultSocketConfig(socketConfig);
connectionManager.setMaxTotal(20);
connectionManager.setDefaultMaxPerRoute(20);
pooledHttpClient = HttpClientBuilder.create().setUserAgent("Stanbol Integration Test").setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build();
}
}
use of org.apache.http.client.config.RequestConfig in project opennms by OpenNMS.
the class GrafanaDashletConfigurationWindow method getGrafanaDashboards.
private Map<String, String> getGrafanaDashboards() throws GrafanaDashletException {
/**
* Loading the required properties...
*/
final String grafanaApiKey = System.getProperty("org.opennms.grafanaBox.apiKey", "");
final String grafanaProtocol = System.getProperty("org.opennms.grafanaBox.protocol", "http");
final String grafanaHostname = System.getProperty("org.opennms.grafanaBox.hostname", "localhost");
final int grafanaPort = Integer.parseInt(System.getProperty("org.opennms.grafanaBox.port", "3000"));
final int grafanaConnectionTimeout = Integer.parseInt(System.getProperty("org.opennms.grafanaBox.connectionTimeout", "500"));
final int grafanaSoTimeout = Integer.parseInt(System.getProperty("org.opennms.grafanaBox.soTimeout", "500"));
if (!"".equals(grafanaApiKey) && !"".equals(grafanaHostname) && !"".equals(grafanaProtocol) && ("http".equals(grafanaProtocol) || "https".equals(grafanaProtocol))) {
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
final RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(grafanaConnectionTimeout).setSocketTimeout(grafanaSoTimeout).build();
final URI uri = new URIBuilder().setScheme(grafanaProtocol).setHost(grafanaHostname).setPort(grafanaPort).setPath("/api/search/").build();
final HttpGet httpGet = new HttpGet(uri);
httpGet.setConfig(requestConfig);
/**
* Adding the API key...
*/
httpGet.setHeader("Authorization", "Bearer " + grafanaApiKey);
final Map<String, String> resultSet = new TreeMap<>();
try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
/**
* Fill the result set...
*/
final String responseString = IOUtils.toString(httpEntity.getContent(), StandardCharsets.UTF_8.name());
if (!Strings.isNullOrEmpty(responseString)) {
try {
final JSONArray arr = new JSONObject("{dashboards:" + responseString + "}").getJSONArray("dashboards");
for (int i = 0; i < arr.length(); i++) {
resultSet.put(arr.getJSONObject(i).getString("title"), arr.getJSONObject(i).getString("uri"));
}
} catch (JSONException e) {
throw new GrafanaDashletException(e.getMessage());
}
}
}
}
return resultSet;
} catch (Exception e) {
throw new GrafanaDashletException(e.getMessage());
}
} else {
throw new GrafanaDashletException("Invalid configuration");
}
}
use of org.apache.http.client.config.RequestConfig in project libresonic by Libresonic.
the class ProxyController method handleRequest.
@RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
String url = ServletRequestUtils.getRequiredStringParameter(request, "url");
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(15000).setSocketTimeout(15000).build();
HttpGet method = new HttpGet(url);
method.setConfig(requestConfig);
InputStream in = null;
try (CloseableHttpClient client = HttpClients.createDefault()) {
try (CloseableHttpResponse resp = client.execute(method)) {
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
response.sendError(statusCode);
} else {
in = resp.getEntity().getContent();
IOUtils.copy(in, response.getOutputStream());
}
}
} finally {
IOUtils.closeQuietly(in);
}
return null;
}
Aggregations