Skip to content

Commit

Permalink
Merge pull request #13 from algarves/12-erro-no-acesso-da-caixa
Browse files Browse the repository at this point in the history
fix(SSL): Resolução do problema relatado na Issue #12
  • Loading branch information
algarves authored Sep 24, 2022
2 parents 2c1b2df + ac52b64 commit fd24476
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group 'com.ueby'
version '2.5-SNAPSHOT'
version '2.6-SNAPSHOT'

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,61 @@
package com.ueby.oss.loteria.crawlers.service;

import com.ueby.oss.loteria.crawlers.exception.CaixaWebCrawlerException;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;

import com.ueby.oss.loteria.crawlers.util.SSLHelper;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import lombok.extern.log4j.Log4j2;
import org.apache.http.client.fluent.Executor;
import org.apache.http.client.fluent.Request;
import org.apache.http.client.fluent.Response;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

/**
* @author Algarves, Khristian
*/
@Log4j2
public abstract class AbstractCrawlerService {

protected static final Long REQUEST_TIMEOUT = TimeUnit.MILLISECONDS.toSeconds(30); // 30sec

protected Response get(String url) throws CaixaWebCrawlerException {
protected Response get(String url) throws CaixaWebCrawlerException, IOException {
CloseableHttpClient httpClient = createHttpClient();
try {
return Request.Get(url)
.connectTimeout(REQUEST_TIMEOUT.intValue())
.socketTimeout(REQUEST_TIMEOUT.intValue())
.execute();
Executor executor = Executor.newInstance(httpClient);
return executor.execute(
Request.Get(url)
.connectTimeout(REQUEST_TIMEOUT.intValue())
.socketTimeout(REQUEST_TIMEOUT.intValue())
);
} catch (IOException ie) {
log.error("An error occurred during the request - {}", url);
throw new CaixaWebCrawlerException(ie.getLocalizedMessage(), ie);
}
}

private static CloseableHttpClient createHttpClient() {
PoolingHttpClientConnectionManager connectionManager = createConnectionManager();
return HttpClientBuilder.create()
.setConnectionManager(connectionManager)
.build();
}

private static PoolingHttpClientConnectionManager createConnectionManager() {
final Registry<ConnectionSocketFactory> sfr = RegistryBuilder.<ConnectionSocketFactory>create()
.register("https", SSLHelper.socketFactory())
.build();

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
sfr);
connectionManager.setDefaultMaxPerRoute(100);
connectionManager.setMaxTotal(200);
connectionManager.setValidateAfterInactivity(1000);
return connectionManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public CaixaWebCrawlerService byContestNumber(final int concourse) {
/**
* Call whenever the result is invalid for the request.
*
* @throws InterruptedException When the process is interrupted.
* @throws CaixaWebCrawlerException If Number of attempts exhausted.
* @throws InterruptedException When the process is interrupted.
*/
private void recall() throws InterruptedException {
if (attempts <= MAX_ATTEMPTS) {
Expand Down Expand Up @@ -98,7 +97,7 @@ private void call() {
responseStatus.getReasonPhrase());
}
} catch (CaixaWebCrawlerException e) {
log.error(e.getLocalizedMessage(), e);
log.error(e.getMessage(), e);
try {
recall();
} catch (InterruptedException ie) {
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/ueby/oss/loteria/crawlers/util/SSLHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.ueby.oss.loteria.crawlers.util;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;

public class SSLHelper {

public static SSLConnectionSocketFactory socketFactory() {
TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}

public void checkClientTrusted(X509Certificate[] certs, String authType) {
}

public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };

try {
SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

return new SSLConnectionSocketFactory(sslContext);
} catch (NoSuchAlgorithmException | KeyManagementException e) {
throw new RuntimeException("Failed to create a SSL socket factory", e);
}
}
}

0 comments on commit fd24476

Please sign in to comment.