-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(SSL): Resolução do problema relatado na Issue #12 que ocorria err…
…o durante a requisição dos endpoints dos jogos, por não conseguir validar o certificado SSL do site da caixa.
- Loading branch information
Showing
3 changed files
with
78 additions
and
11 deletions.
There are no files selected for viewing
49 changes: 41 additions & 8 deletions
49
src/main/java/com/ueby/oss/loteria/crawlers/service/AbstractCrawlerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/com/ueby/oss/loteria/crawlers/util/SSLHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |