Skip to content

Commit

Permalink
SeleniumService is reverted
Browse files Browse the repository at this point in the history
  • Loading branch information
tsvrko committed Sep 18, 2019
1 parent 2d3dec4 commit e428746
Showing 1 changed file with 42 additions and 18 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);
}
}
}

0 comments on commit e428746

Please sign in to comment.