use of org.apache.http.impl.client.CloseableHttpClient in project camel by apache.
the class CxfRsConsumerWithBeanTest method sendPutRequest.
private void sendPutRequest(String uri) throws Exception {
HttpPut put = new HttpPut(uri);
StringEntity entity = new StringEntity("string");
entity.setContentType("text/plain");
put.setEntity(entity);
CloseableHttpClient httpclient = HttpClientBuilder.create().build();
try {
HttpResponse response = httpclient.execute(put);
assertEquals(200, response.getStatusLine().getStatusCode());
assertEquals("c20string", EntityUtils.toString(response.getEntity()));
} finally {
httpclient.close();
}
}
use of org.apache.http.impl.client.CloseableHttpClient in project camel by apache.
the class HttpComponentVerifier method verifyHttpConnectivity.
private void verifyHttpConnectivity(ResultBuilder builder, Map<String, Object> parameters) throws Exception {
Optional<String> uri = getOption(parameters, "httpUri", String.class);
CloseableHttpClient httpclient = createHttpClient(parameters);
HttpUriRequest request = new HttpGet(uri.get());
try (CloseableHttpResponse response = httpclient.execute(request)) {
int code = response.getStatusLine().getStatusCode();
String okCodes = getOption(parameters, "okStatusCodeRange", String.class).orElse("200-299");
if (!HttpHelper.isStatusCodeOk(code, okCodes)) {
if (code == 401) {
// Unauthorized, add authUsername and authPassword to the list
// of parameters in error
builder.error(ResultErrorBuilder.withHttpCode(code).description(response.getStatusLine().getReasonPhrase()).parameter("authUsername").parameter("authPassword").build());
} else if (code >= 300 && code < 400) {
// redirect
builder.error(ResultErrorBuilder.withHttpCode(code).description(response.getStatusLine().getReasonPhrase()).parameter("httpUri").attribute(ComponentVerifier.HTTP_REDIRECT, true).attribute(ComponentVerifier.HTTP_REDIRECT_LOCATION, () -> HttpUtil.responseHeaderValue(response, "location")).build());
} else if (code >= 400) {
// generic http error
builder.error(ResultErrorBuilder.withHttpCode(code).description(response.getStatusLine().getReasonPhrase()).build());
}
}
} catch (UnknownHostException e) {
builder.error(ResultErrorBuilder.withException(e).parameter("httpUri").build());
}
}
use of org.apache.http.impl.client.CloseableHttpClient in project hive by apache.
the class TestHS2HttpServer method testConfStrippedFromWebUI.
@Test
public void testConfStrippedFromWebUI() throws Exception {
String pwdValFound = null;
String pwdKeyFound = null;
CloseableHttpClient httpclient = null;
try {
httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:" + webUIPort + "/conf");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
try {
HttpEntity entity1 = response1.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(entity1.getContent()));
String line;
while ((line = br.readLine()) != null) {
if (line.contains(metastorePasswd)) {
pwdValFound = line;
}
if (line.contains(ConfVars.METASTOREPWD.varname)) {
pwdKeyFound = line;
}
}
EntityUtils.consume(entity1);
} finally {
response1.close();
}
} finally {
if (httpclient != null) {
httpclient.close();
}
}
assertNotNull(pwdKeyFound);
assertNull(pwdValFound);
}
use of org.apache.http.impl.client.CloseableHttpClient in project cryptomator by cryptomator.
the class WelcomeController method checkForUpdates.
private void checkForUpdates() {
checkForUpdatesStatus.setText(localization.getString("welcome.checkForUpdates.label.currentlyChecking"));
checkForUpdatesIndicator.setVisible(true);
asyncTaskService.asyncTaskOf(() -> {
RequestConfig requestConfig = //
RequestConfig.custom().setConnectTimeout(//
5000).setConnectionRequestTimeout(//
5000).setSocketTimeout(//
5000).build();
HttpClientBuilder httpClientBuilder = //
HttpClients.custom().disableCookieManagement().setDefaultRequestConfig(//
requestConfig).setUserAgent("Cryptomator VersionChecker/" + ApplicationVersion.orElse("SNAPSHOT"));
LOG.debug("Checking for updates...");
try (CloseableHttpClient client = httpClientBuilder.build()) {
HttpGet request = new HttpGet("https://cryptomator.org/downloads/latestVersion.json");
try (CloseableHttpResponse response = client.execute(request)) {
if (response.getStatusLine().getStatusCode() == 200 && response.getEntity() != null) {
try (InputStream in = response.getEntity().getContent()) {
Gson gson = new GsonBuilder().setLenient().create();
Reader utf8Reader = new InputStreamReader(in, StandardCharsets.UTF_8);
Map<String, String> map = gson.fromJson(utf8Reader, new TypeToken<Map<String, String>>() {
}.getType());
if (map != null) {
this.compareVersions(map);
}
}
}
}
}
}).andFinally(() -> {
checkForUpdatesStatus.setText("");
checkForUpdatesIndicator.setVisible(false);
}).run();
}
use of org.apache.http.impl.client.CloseableHttpClient in project webmagic by code4craft.
the class HttpClientDownloaderTest method testGetHtmlCharset.
@Test
public void testGetHtmlCharset() throws Exception {
HttpServer server = httpserver(12306);
server.get(by(uri("/header"))).response(header("Content-Type", "text/html; charset=gbk"));
server.get(by(uri("/meta4"))).response(with(text("<html>\n" + " <head>\n" + " <meta charset='gbk'/>\n" + " </head>\n" + " <body></body>\n" + "</html>")), header("Content-Type", ""));
server.get(by(uri("/meta5"))).response(with(text("<html>\n" + " <head>\n" + " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=gbk\" />\n" + " </head>\n" + " <body></body>\n" + "</html>")), header("Content-Type", ""));
Runner.running(server, new Runnable() {
@Override
public void run() {
String charset = getCharsetByUrl("http://127.0.0.1:12306/header");
assertEquals(charset, "gbk");
charset = getCharsetByUrl("http://127.0.0.1:12306/meta4");
assertEquals(charset, "gbk");
charset = getCharsetByUrl("http://127.0.0.1:12306/meta5");
assertEquals(charset, "gbk");
}
private String getCharsetByUrl(String url) {
HttpClientDownloader downloader = new HttpClientDownloader();
Site site = Site.me();
CloseableHttpClient httpClient = new HttpClientGenerator().getClient(site, null);
// encoding in http header Content-Type
Request requestGBK = new Request(url);
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(downloader.getHttpUriRequest(requestGBK, site, null, null));
} catch (IOException e) {
e.printStackTrace();
}
String charset = null;
try {
byte[] contentBytes = IOUtils.toByteArray(httpResponse.getEntity().getContent());
charset = downloader.getHtmlCharset(httpResponse, contentBytes);
} catch (IOException e) {
e.printStackTrace();
}
return charset;
}
});
}
Aggregations