1717import java .time .Duration ;
1818import java .util .Optional ;
1919import java .util .zip .GZIPInputStream ;
20+ import javax .net .ssl .SSLContext ;
21+ import javax .net .ssl .TrustManagerFactory ;
22+ import java .security .KeyStore ;
2023
2124public class ReportingApiHTTP extends ReportingApi {
2225 private final Logger logger = LogManager .getLogger (ReportingApiHTTP .class );
@@ -30,10 +33,23 @@ public ReportingApiHTTP(String reportingUrl, int timeoutInSec, Token token) {
3033 this .token = token ;
3134 }
3235
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+
3348 public Optional <APIResponse > fetchNewConfig () {
3449 try {
3550 HttpClient httpClient = HttpClient .newBuilder ()
3651 .connectTimeout (Duration .ofSeconds (timeoutInSec ))
52+ .sslContext (createDefaultSSLContext ())
3753 .build ();
3854
3955 URI uri = URI .create (reportingUrl + "api/runtime/config" );
@@ -54,6 +70,7 @@ public Optional<APIResponse> report(APIEvent event) {
5470 try {
5571 HttpClient httpClient = HttpClient .newBuilder ()
5672 .connectTimeout (Duration .ofSeconds (timeoutInSec ))
73+ .sslContext (createDefaultSSLContext ())
5774 .build ();
5875
5976 URI uri = URI .create (reportingUrl + "api/runtime/events" );
@@ -75,25 +92,32 @@ public Optional<APIListsResponse> fetchBlockedLists() {
7592 return Optional .empty ();
7693 }
7794 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 ( );
8299
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 ();
86107
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 ) {
88111 return Optional .empty ();
89112 }
90- InputStream inputStream = connection .getInputStream ();
113+
114+ InputStream inputStream = httpResponse .body ();
91115 // Check if the response is gzipped
92- if ("gzip" .equalsIgnoreCase (connection . getContentEncoding ( ))) {
116+ if ("gzip" .equalsIgnoreCase (httpResponse . headers (). firstValue ( "Content-Encoding" ). orElse ( "" ))) {
93117 inputStream = new GZIPInputStream (inputStream );
94118 }
95119
96- // Read the response :
120+ // Read the response
97121 APIListsResponse res = gson .fromJson (new InputStreamReader (inputStream ), APIListsResponse .class );
98122 return Optional .of (res );
99123 } catch (Exception e ) {
0 commit comments