|
3 | 3 | import br.com.swconsultoria.certificado.exception.CertificadoException; |
4 | 4 | import br.com.swconsultoria.certificado.util.DocumentoUtil; |
5 | 5 | import lombok.extern.java.Log; |
| 6 | +import org.apache.commons.httpclient.HttpClient; |
6 | 7 | import org.apache.commons.httpclient.protocol.Protocol; |
7 | 8 |
|
8 | 9 | import java.io.FileNotFoundException; |
@@ -35,35 +36,42 @@ public static void inicializaCertificado(Certificado certificado) throws Certifi |
35 | 36 | inicializaCertificado(certificado, CertificadoService.class.getResourceAsStream("/cacert")); |
36 | 37 | } |
37 | 38 |
|
| 39 | + /** |
| 40 | + * |
| 41 | + * <p>Inicializa o certificado para a conexão SSL/TLS.</p> |
| 42 | + * <p><b>Importante: </b>Quando NÃO estiver com o modo multithreading ativado (certificado.isModoMultithreading()) |
| 43 | + * será registrado e utilizado um único certificado em todas as conexões até a próxima chamada à |
| 44 | + * {@link #inicializaCertificado(Certificado, InputStream)}. É o modo antigo e padrão da biblioteca.</p> |
| 45 | + * <p>Quando estiver com o modo multithreading ativo o consumidor deverá obter um |
| 46 | + * {@link org.apache.commons.httpclient.HttpClient} com protocolo e certificado exclusivos para ele usando o |
| 47 | + * método {@link #getHttpsClient(Certificado, String, InputStream)}</p> |
| 48 | + * |
| 49 | + * @param certificado {@link Certificado} a ser utilizado na conexão. |
| 50 | + * @param cacert {@link java.io.InputStream} contendo o cacert |
| 51 | + * @throws CertificadoException |
| 52 | + */ |
| 53 | + |
38 | 54 | public static void inicializaCertificado(Certificado certificado, InputStream cacert) throws CertificadoException { |
| 55 | + if (certificado == null) { |
| 56 | + throw new IllegalArgumentException(CERTIFICADO_NAO_PODE_SER_NULO); |
| 57 | + } |
39 | 58 |
|
40 | | - try { |
41 | | - |
42 | | - KeyStore keyStore = getKeyStore( |
43 | | - Optional.ofNullable(certificado).orElseThrow(() -> new IllegalArgumentException(CERTIFICADO_NAO_PODE_SER_NULO))); |
44 | | - SocketFactoryDinamico socketFactory = new SocketFactoryDinamico(keyStore, certificado.getNome(), certificado.getSenha(), |
45 | | - Optional.ofNullable(cacert).orElseThrow(() -> new IllegalArgumentException("Cacert não pode ser nulo.")), |
46 | | - certificado.getSslProtocol()); |
47 | | - Protocol protocol = new Protocol("https", socketFactory, 443); |
48 | | - Protocol.registerProtocol("https", protocol); |
49 | | - |
50 | | - log. info( String. format( "JAVA-CERTIFICADO | Samuel Oliveira | [email protected] " + |
51 | | - "| VERSAO=%s | DATA_VERSAO=%s | CNPJ/CPF=%s | VENCIMENTO=%s | ALIAS=%s | TIPO=%s | CAMINHO=%s | CACERT=%s | SSL=%s", |
52 | | - "3.7", |
53 | | - "11/07/2024", |
54 | | - certificado.getCnpjCpf(), |
55 | | - certificado.getDataHoraVencimento(), |
56 | | - certificado.getNome().toUpperCase(), |
57 | | - certificado.getTipoCertificado().toString(), |
58 | | - certificado.getArquivo(), |
59 | | - cacertProprio ? "Default" : "Customizado", |
60 | | - certificado.getSslProtocol())); |
61 | | - |
62 | | - } catch (KeyStoreException | NoSuchAlgorithmException | KeyManagementException | CertificateException | |
63 | | - IOException e) { |
64 | | - throw new CertificadoException(e.getMessage(), e); |
| 59 | + if (!certificado.isModoMultithreading()) { |
| 60 | + Protocol.registerProtocol("https", getProtocoloCertificado(certificado, cacert)); |
65 | 61 | } |
66 | 62 |
|
| 63 | + log. info( String. format( "JAVA-CERTIFICADO | Samuel Oliveira | [email protected] " + |
| 64 | + "| VERSAO=%s | DATA_VERSAO=%s | CNPJ/CPF=%s | VENCIMENTO=%s | ALIAS=%s | TIPO=%s | CAMINHO=%s | CACERT=%s | SSL=%s | Multithreading=%s", |
| 65 | + "3.8", |
| 66 | + "14/10/2024", |
| 67 | + certificado.getCnpjCpf(), |
| 68 | + certificado.getDataHoraVencimento(), |
| 69 | + certificado.getNome().toUpperCase(), |
| 70 | + certificado.getTipoCertificado().toString(), |
| 71 | + certificado.getArquivo(), |
| 72 | + cacertProprio ? "Default" : "Customizado", |
| 73 | + certificado.getSslProtocol(), |
| 74 | + certificado.isModoMultithreading())); |
67 | 75 | } |
68 | 76 |
|
69 | 77 | public static Certificado certificadoPfxBytes(byte[] certificadoBytes, String senha) throws CertificadoException { |
@@ -278,4 +286,51 @@ public static Certificado getCertificadoByCnpjCpf(String cnpjCpf) throws Certifi |
278 | 286 | cnpjCpf)); |
279 | 287 | } |
280 | 288 |
|
| 289 | + private static Protocol getProtocoloCertificado(final Certificado certificado, InputStream cacert) throws CertificadoException { |
| 290 | + try { |
| 291 | + KeyStore keyStore = getKeyStore( |
| 292 | + Optional.ofNullable(certificado).orElseThrow(() -> new IllegalArgumentException(CERTIFICADO_NAO_PODE_SER_NULO))); |
| 293 | + SocketFactoryDinamico socketFactory = new SocketFactoryDinamico(keyStore, certificado.getNome(), certificado.getSenha(), |
| 294 | + Optional.ofNullable(cacert).orElseThrow(() -> new IllegalArgumentException("Cacert não pode ser nulo.")), |
| 295 | + certificado.getSslProtocol()); |
| 296 | + |
| 297 | + return new Protocol("https", socketFactory, 443); |
| 298 | + |
| 299 | + } catch (KeyStoreException | NoSuchAlgorithmException | KeyManagementException | CertificateException | |
| 300 | + IOException e) { |
| 301 | + throw new CertificadoException(e.getMessage(), e); |
| 302 | + } |
| 303 | + } |
| 304 | + |
| 305 | + /** |
| 306 | + * Utiliza cacert default da biblioteca. |
| 307 | + * @see #getHttpsClient(Certificado, String, InputStream) |
| 308 | + */ |
| 309 | + public static HttpClient getHttpsClient(Certificado certificado, String url) throws CertificadoException { |
| 310 | + return getHttpsClient(certificado, url, CertificadoService.class.getResourceAsStream("/cacert")); |
| 311 | + } |
| 312 | + |
| 313 | + /** |
| 314 | + * <p>Utilizar o {@link org.apache.commons.httpclient.HttpClient} gerado nesse método para evitar conflitos de |
| 315 | + * certificados nas conexões HTTPS/TLS/SSL, especialmente em ambientes multithreading.</p> |
| 316 | + * |
| 317 | + * <p>O consumidor desse método deverá usar esse cliente no stub desejado</p> |
| 318 | + * exemplo: |
| 319 | + * <pre> |
| 320 | + * HttpClient client = getHttpsClient(certificado, url); |
| 321 | + * NFeStatusServico4Stub stub = new NFeStatusServico4Stub(url); |
| 322 | + * stub._getServiceClient().getOptions().setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpclient); |
| 323 | + * </pre> |
| 324 | + * @param certificado {@link Certificado} a ser utilizado na conexão. |
| 325 | + * @param url {@link String} a ser executada na conexão https |
| 326 | + * @param cacert {@link java.io.InputStream} contendo o cacert |
| 327 | + * @return {@link org.apache.commons.httpclient.HttpClient} configurado para a URL e Certificados informados. |
| 328 | + * @throws CertificadoException |
| 329 | + */ |
| 330 | + public static HttpClient getHttpsClient(Certificado certificado, String url, final InputStream cacert) throws CertificadoException { |
| 331 | + Protocol protocol = getProtocoloCertificado(certificado, cacert); |
| 332 | + HttpClient httpclient = new HttpClient(); |
| 333 | + httpclient.getHostConfiguration().setHost(url, 443, protocol); |
| 334 | + return httpclient; |
| 335 | + } |
281 | 336 | } |
0 commit comments