17
17
import java .time .Duration ;
18
18
import java .util .Optional ;
19
19
import java .util .zip .GZIPInputStream ;
20
+ import javax .net .ssl .SSLContext ;
21
+ import javax .net .ssl .TrustManagerFactory ;
22
+ import java .security .KeyStore ;
20
23
21
24
public class ReportingApiHTTP extends ReportingApi {
22
25
private final Logger logger = LogManager .getLogger (ReportingApiHTTP .class );
@@ -30,10 +33,23 @@ public ReportingApiHTTP(String reportingUrl, int timeoutInSec, Token token) {
30
33
this .token = token ;
31
34
}
32
35
36
+ private SSLContext createDefaultSSLContext () throws Exception {
37
+ // Get the default TrustManagerFactory
38
+ TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
39
+ trustManagerFactory .init ((KeyStore ) null ); // Use the default trust store
40
+
41
+ // Create an SSLContext with the default TrustManager
42
+ SSLContext sslContext = SSLContext .getInstance ("TLS" );
43
+ sslContext .init (null , trustManagerFactory .getTrustManagers (), null );
44
+
45
+ return sslContext ;
46
+ }
47
+
33
48
public Optional <APIResponse > fetchNewConfig () {
34
49
try {
35
50
HttpClient httpClient = HttpClient .newBuilder ()
36
51
.connectTimeout (Duration .ofSeconds (timeoutInSec ))
52
+ .sslContext (createDefaultSSLContext ())
37
53
.build ();
38
54
39
55
URI uri = URI .create (reportingUrl + "api/runtime/config" );
@@ -54,6 +70,7 @@ public Optional<APIResponse> report(APIEvent event) {
54
70
try {
55
71
HttpClient httpClient = HttpClient .newBuilder ()
56
72
.connectTimeout (Duration .ofSeconds (timeoutInSec ))
73
+ .sslContext (createDefaultSSLContext ())
57
74
.build ();
58
75
59
76
URI uri = URI .create (reportingUrl + "api/runtime/events" );
@@ -75,25 +92,32 @@ public Optional<APIListsResponse> fetchBlockedLists() {
75
92
return Optional .empty ();
76
93
}
77
94
try {
78
- // Make a GET request to api/runtime/firewall/lists
79
- URL url = new URL ( reportingUrl + "api/runtime/firewall/lists" );
80
- HttpURLConnection connection = ( HttpURLConnection ) url . openConnection ();
81
- connection . setRequestMethod ( "GET" );
95
+ HttpClient httpClient = HttpClient . newBuilder ()
96
+ . connectTimeout ( Duration . ofSeconds ( timeoutInSec ))
97
+ . sslContext ( createDefaultSSLContext ())
98
+ . build ( );
82
99
83
- // Set the Accept-Encoding header to gzip
84
- connection .setRequestProperty ("Accept-Encoding" , "gzip" );
85
- connection .setRequestProperty ("Authorization" , token .get ());
100
+ URI uri = URI .create (reportingUrl + "api/runtime/firewall/lists" );
101
+ HttpRequest request = HttpRequest .newBuilder ()
102
+ .uri (uri )
103
+ .timeout (Duration .ofSeconds (timeoutInSec ))
104
+ .header ("Accept-Encoding" , "gzip" )
105
+ .header ("Authorization" , token .get ())
106
+ .build ();
86
107
87
- if (connection .getResponseCode () != HttpURLConnection .HTTP_OK ) {
108
+ // Send the request and get the response
109
+ HttpResponse <InputStream > httpResponse = httpClient .send (request , HttpResponse .BodyHandlers .ofInputStream ());
110
+ if (httpResponse .statusCode () != HttpURLConnection .HTTP_OK ) {
88
111
return Optional .empty ();
89
112
}
90
- InputStream inputStream = connection .getInputStream ();
113
+
114
+ InputStream inputStream = httpResponse .body ();
91
115
// Check if the response is gzipped
92
- if ("gzip" .equalsIgnoreCase (connection . getContentEncoding ( ))) {
116
+ if ("gzip" .equalsIgnoreCase (httpResponse . headers (). firstValue ( "Content-Encoding" ). orElse ( "" ))) {
93
117
inputStream = new GZIPInputStream (inputStream );
94
118
}
95
119
96
- // Read the response :
120
+ // Read the response
97
121
APIListsResponse res = gson .fromJson (new InputStreamReader (inputStream ), APIListsResponse .class );
98
122
return Optional .of (res );
99
123
} catch (Exception e ) {
0 commit comments