From 2d3dec4385fda05433e023564bb92c25343f3d77 Mon Sep 17 00:00:00 2001 From: tsvrko Date: Wed, 18 Sep 2019 17:03:20 +0300 Subject: [PATCH 1/2] GitHubHttpUtils httpclient reverted --- .../zafira/services/util/GitHubHttpUtils.java | 56 ++++++++++--------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/sources/zafira-services/src/main/java/com/qaprosoft/zafira/services/util/GitHubHttpUtils.java b/sources/zafira-services/src/main/java/com/qaprosoft/zafira/services/util/GitHubHttpUtils.java index 9e915c190..f6bbc1ec7 100644 --- a/sources/zafira-services/src/main/java/com/qaprosoft/zafira/services/util/GitHubHttpUtils.java +++ b/sources/zafira-services/src/main/java/com/qaprosoft/zafira/services/util/GitHubHttpUtils.java @@ -15,17 +15,19 @@ *******************************************************************************/ package com.qaprosoft.zafira.services.util; -import com.qaprosoft.zafira.services.exceptions.ExternalSystemException; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import org.springframework.web.util.DefaultUriBuilderFactory; +import javax.annotation.PreDestroy; import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; +import java.net.URISyntaxException; @Component public class GitHubHttpUtils { @@ -34,37 +36,37 @@ public class GitHubHttpUtils { private static final String GITHUB_ACCESS_TOKEN_PATH = "https://github.com/login/oauth/access_token"; - private HttpClient httpClient; + private CloseableHttpClient httpClient; public GitHubHttpUtils() { - this.httpClient = HttpClient.newHttpClient(); + this.httpClient = HttpClientBuilder.create().build(); } - public String getAccessToken(String code, String clientId, String secret) throws IOException { - HttpResponse httpResponse; - try { - httpResponse = this.httpClient.send(buildGetAccessTokenRequest(code, clientId, secret), HttpResponse.BodyHandlers.ofString()); - } catch (InterruptedException e) { - throw new ExternalSystemException(e.getMessage(), e); - } - return getAccessToken(httpResponse.body().toString()); + public String getAccessToken(String code, String clientId, String secret) throws URISyntaxException, IOException { + HttpResponse httpResponse = this.httpClient.execute(buildGetAccessTokenRequest(code, clientId, secret)); + return getAccessToken(EntityUtils.toString(httpResponse.getEntity())); } - private static HttpRequest buildGetAccessTokenRequest(String code, String clientId, String secret) { - URI uri = new DefaultUriBuilderFactory(GITHUB_ACCESS_TOKEN_PATH).builder() - .queryParam("client_id", clientId) - .queryParam("client_secret", secret) - .queryParam("code", code) - .queryParam("accept", "json") - .build(); - return HttpRequest.newBuilder() - .uri(uri) - .POST(HttpRequest.BodyPublishers.noBody()) - .build(); + private static HttpPost buildGetAccessTokenRequest(String code, String clientId, String secret) throws URISyntaxException { + URIBuilder uriBuilder = new URIBuilder(GITHUB_ACCESS_TOKEN_PATH); + uriBuilder.addParameter("client_id", clientId) + .addParameter("client_secret", secret) + .addParameter("code", code) + .addParameter("accept", "json"); + return new HttpPost(uriBuilder.build()); } private String getAccessToken(String response) { return response.split("access_token=")[1].split("&")[0]; } + @PreDestroy + public void close() { + try { + if (httpClient != null) + this.httpClient.close(); + } catch (IOException e) { + LOGGER.error(e.getMessage(), e); + } + } } From e4287465299724adc894c5513d34ca3409e4aa4b Mon Sep 17 00:00:00 2001 From: tsvrko Date: Wed, 18 Sep 2019 17:25:46 +0300 Subject: [PATCH 2/2] SeleniumService is reverted --- .../integration/impl/SeleniumService.java | 60 +++++++++++++------ 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/sources/zafira-services/src/main/java/com/qaprosoft/zafira/services/services/application/integration/impl/SeleniumService.java b/sources/zafira-services/src/main/java/com/qaprosoft/zafira/services/services/application/integration/impl/SeleniumService.java index 30f4aeda6..ba4577dd3 100644 --- a/sources/zafira-services/src/main/java/com/qaprosoft/zafira/services/services/application/integration/impl/SeleniumService.java +++ b/sources/zafira-services/src/main/java/com/qaprosoft/zafira/services/services/application/integration/impl/SeleniumService.java @@ -18,13 +18,15 @@ import com.qaprosoft.zafira.services.services.application.SettingsService; import com.qaprosoft.zafira.services.services.application.integration.AbstractIntegration; import com.qaprosoft.zafira.services.services.application.integration.context.SeleniumContext; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; import org.springframework.stereotype.Component; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.time.Duration; +import javax.annotation.PreDestroy; +import java.io.IOException; import static com.qaprosoft.zafira.models.db.Setting.Tool.SELENIUM; @@ -32,12 +34,15 @@ public class SeleniumService extends AbstractIntegration { private static final int TIMEOUT = 5000; - private static final HttpClient HTTP_CLIENT; + private static final CloseableHttpClient HTTP_CLIENT; + private static final RequestConfig REQUEST_CONFIG; static { - HTTP_CLIENT = HttpClient.newBuilder() - .connectTimeout(Duration.ofMillis(TIMEOUT)) - .build(); + REQUEST_CONFIG = RequestConfig.custom() + .setConnectTimeout(TIMEOUT) + .setConnectionRequestTimeout(TIMEOUT) + .build(); + HTTP_CLIENT = HttpClientBuilder.create().build(); } public SeleniumService(SettingsService settingsService, CryptoService cryptoService) { @@ -47,21 +52,40 @@ public SeleniumService(SettingsService settingsService, CryptoService cryptoServ @Override public boolean isConnected() { boolean result; - HttpRequest request; - HttpResponse response; + HttpGet request = null; + CloseableHttpResponse response = null; try { String url = context().getUrl(); - request = HttpRequest.newBuilder() - .uri(URI.create(url)) - .timeout(Duration.ofMillis(TIMEOUT)) - .GET() - .build(); - response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); - result = response.statusCode() == 200; + request = new HttpGet(url); + request.setConfig(REQUEST_CONFIG); + response = HTTP_CLIENT.execute(request); + + result = response.getStatusLine().getStatusCode() == 200; } catch (Exception e) { result = false; + } finally { + if (request != null) { + request.releaseConnection(); + } + if(response != null) { + try { + response.close(); + } catch (IOException e) { + LOGGER.error(e.getMessage(), e); + } + } } return result; } + @PreDestroy + public void close() { + try { + if (HTTP_CLIENT != null) { + HTTP_CLIENT.close(); + } + } catch (IOException e) { + LOGGER.error(e.getMessage(), e); + } + } }