Skip to content

Commit

Permalink
Merge pull request #1765 from qaprosoft/hotfix/http-client
Browse files Browse the repository at this point in the history
java httpclient is reverted
  • Loading branch information
nsidorevich authored Sep 18, 2019
2 parents 937814e + e428746 commit f119d59
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,31 @@
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;

@Component
public class SeleniumService extends AbstractIntegration<SeleniumContext> {

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) {
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}
}

0 comments on commit f119d59

Please sign in to comment.