Skip to content

Commit 4bf92a0

Browse files
committed
new httpclient
1 parent 38df331 commit 4bf92a0

File tree

7 files changed

+64
-156
lines changed

7 files changed

+64
-156
lines changed

build.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<project name="mvn-caller-pai-auth-ws-client" default="build" basedir=".">
22

33
<property name="maven.executable" value="${maven.home}/bin/mvn" />
4-
<property name="jarName" value="pai-auth-ws-client-1.2.0.jar" />
4+
<property name="jarName" value="pai-auth-ws-client-1.4.0.jar" />
55

66
<target name="build" description="build" depends="copy"> </target>
77

pom.xml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.github.LACNIC</groupId>
66
<artifactId>pai-auth-ws-client</artifactId>
7-
<version>1.3.0</version>
7+
<version>1.4.0</version>
88
<name>PAI Authentication Client</name>
99
<description>Client for handling authentication requests and managing access
1010
to the PAI services.</description>
@@ -32,11 +32,15 @@
3232
</dependency>
3333

3434
<dependency>
35-
<groupId>org.apache.httpcomponents</groupId>
36-
<artifactId>httpclient</artifactId>
37-
<version>4.5.14</version>
35+
<groupId>org.apache.httpcomponents.client5</groupId>
36+
<artifactId>httpclient5</artifactId>
37+
<version>5.4.4</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.apache.httpcomponents.core5</groupId>
41+
<artifactId>httpcore5</artifactId>
42+
<version>5.3.4</version>
3843
</dependency>
39-
4044
<dependency>
4145
<groupId>com.fasterxml.jackson.core</groupId>
4246
<artifactId>jackson-core</artifactId>
@@ -97,8 +101,14 @@
97101
<dependency>
98102
<groupId>org.slf4j</groupId>
99103
<artifactId>slf4j-api</artifactId>
100-
<version>1.7.36</version>
104+
<version>2.0.16</version>
101105
</dependency>
106+
107+
<!-- <dependency>-->
108+
<!-- <groupId>org.apache.logging.log4j</groupId>-->
109+
<!-- <artifactId>log4j-core</artifactId>-->
110+
<!-- <version>2.24.1</version>-->
111+
<!-- </dependency>-->
102112
</dependencies>
103113

104114
<build>

src/main/java/net/lacnic/portal/auth/client/MySSLSocketFactory.java

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 15 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,24 @@
11
package net.lacnic.portal.auth.client;
22

3-
import java.security.KeyStore;
3+
import javax.net.ssl.SSLContext;
44

5-
import org.apache.http.HttpVersion;
6-
import org.apache.http.client.HttpClient;
7-
import org.apache.http.client.params.ClientPNames;
8-
import org.apache.http.conn.ClientConnectionManager;
9-
import org.apache.http.conn.scheme.PlainSocketFactory;
10-
import org.apache.http.conn.scheme.Scheme;
11-
import org.apache.http.conn.scheme.SchemeRegistry;
12-
import org.apache.http.conn.ssl.SSLSocketFactory;
13-
import org.apache.http.impl.client.DefaultHttpClient;
14-
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
15-
import org.apache.http.params.BasicHttpParams;
16-
import org.apache.http.params.CoreConnectionPNames;
17-
import org.apache.http.params.HttpParams;
18-
import org.apache.http.params.HttpProtocolParams;
19-
import org.apache.http.protocol.HTTP;
20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
5+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
6+
import org.apache.hc.client5.http.impl.classic.HttpClients;
7+
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
8+
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
9+
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
10+
import org.apache.hc.client5.http.ssl.TrustAllStrategy;
11+
import org.apache.hc.core5.ssl.SSLContextBuilder;
2212

23-
@SuppressWarnings("deprecation")
2413
public class PortalHttpClient {
25-
private static final Logger logger = LoggerFactory.getLogger(PortalHttpClient.class);
2614

27-
public static HttpClient getNewHttpClient() {
28-
try {
29-
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
30-
trustStore.load(null, null);
15+
public static CloseableHttpClient createInsecureClient() throws Exception {
16+
// Confía en todos los certificados
17+
SSLContext sslContext = SSLContextBuilder.create().loadTrustMaterial(null, TrustAllStrategy.INSTANCE).build();
3118

32-
MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
33-
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
19+
// Crea el socket factory ignorando validación del hostname
20+
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1.2", "TLSv1.3" }, null, NoopHostnameVerifier.INSTANCE);
3421

35-
HttpParams params = new BasicHttpParams();
36-
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
37-
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
38-
39-
SchemeRegistry registry = new SchemeRegistry();
40-
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
41-
registry.register(new Scheme("https", sf, 443));
42-
43-
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
44-
try (DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params)) {
45-
int timeout = 40; // seconds
46-
HttpParams httpParams = httpClient.getParams();
47-
httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout * 1000);
48-
httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout * 1000);
49-
httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, (long) timeout * 1000);
50-
return httpClient;
51-
}
52-
} catch (Exception e) {
53-
logger.error("An error occurred: {}", e.getMessage(), e);
54-
return new DefaultHttpClient();
55-
}
22+
return HttpClients.custom().setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create().setSSLSocketFactory(sslSocketFactory).build()).build();
5623
}
57-
}
24+
}

src/main/java/net/lacnic/portal/auth/client/PortalWSClient.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import java.util.concurrent.ConcurrentHashMap;
1010
import java.util.concurrent.TimeUnit;
1111

12-
import org.apache.http.HttpResponse;
13-
import org.apache.http.client.HttpClient;
14-
import org.apache.http.client.methods.HttpGet;
12+
import org.apache.hc.client5.http.classic.methods.HttpGet;
13+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
14+
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
1515
import org.slf4j.Logger;
1616
import org.slf4j.LoggerFactory;
1717

@@ -109,15 +109,14 @@ public static String readUrl(String urlString, String username, String password)
109109
}
110110

111111
private static String readUrl(String urlString, String username, String password, String totp) {
112-
try {
113-
HttpClient client = PortalHttpClient.getNewHttpClient();
112+
try (CloseableHttpClient client = PortalHttpClient.createInsecureClient()) {
114113
HttpGet request = new HttpGet(urlString);
115114
request.setHeader(PORTAL_APIKEY, getAuthToken());
116115
request.setHeader(PORTAL_USUARIO, username);
117116
request.setHeader(PORTAL_PASS, password);
118117
request.setHeader(PORTAL_TOTP, totp);
119118

120-
HttpResponse response = client.execute(request);
119+
CloseableHttpResponse response = client.execute(request);
121120
StringBuilder result = new StringBuilder();
122121

123122
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
@@ -136,15 +135,14 @@ private static String readUrl(String urlString, String username, String password
136135
}
137136

138137
private static String readUrlToken(String urlString, String token) {
139-
try {
138+
try (CloseableHttpClient client = PortalHttpClient.createInsecureClient()) {
140139
if (!token.startsWith("Bearer")) {
141140
token = "Bearer " + token;
142141
}
143-
HttpClient client = PortalHttpClient.getNewHttpClient();
144142
HttpGet request = new HttpGet(urlString);
145143
request.setHeader(PORTAL_AUTHORIZATION, token);
146144

147-
HttpResponse response = client.execute(request);
145+
CloseableHttpResponse response = client.execute(request);
148146
StringBuilder result = new StringBuilder();
149147

150148
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<jboss-deployment-structure>
3+
<ear-subdeployments-isolated>false</ear-subdeployments-isolated>
4+
<deployment>
5+
<dependencies>
6+
<module name="org.apache.httpcomponents5" />
7+
<!-- <module name="org.slf4j" /> -->
8+
<!-- <module name="org.jboss.ejb-client" /> -->
9+
<!-- <module name="org.apache.velocity" /> -->
10+
<!-- <module name="org.joda.time" /> -->
11+
<!-- <module name="org.apache.commons.validator" /> -->
12+
<!-- <module name="org.apache.poi" /> -->
13+
<!-- <module name="org.apache.poi-ooxml" /> -->
14+
<!-- <module name="org.bouncycastle" /> -->
15+
<!-- <module name="com.itextpdf" /> -->
16+
<!-- <module name="com.javadocx" /> -->
17+
18+
<module name="jakarta.enterprise.api" export="true" />
19+
<module name="jakarta.inject.api" export="true" />
20+
<module name="jakarta.ejb.api" export="true" />
21+
<module name="jakarta.transaction.api" export="true" />
22+
</dependencies>
23+
</deployment>
24+
25+
</jboss-deployment-structure>

src/test/java/net/lacnic/portal/auth/client/PortalHttpClientTest.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)