44 * ***** 
55 * Concord 
66 * ----- 
7-  * Copyright (C) 2017 - 2019  Walmart Inc. 
7+  * Copyright (C) 2017 - 2024  Walmart Inc. 
88 * ----- 
99 * Licensed under the Apache License, Version 2.0 (the "License"); 
1010 * you may not use this file except in compliance with the License. 
2626import  com .google .common .escape .Escaper ;
2727import  com .google .common .net .UrlEscapers ;
2828import  com .walmartlabs .concord .agentoperator .scheduler .QueueSelector ;
29- import  okhttp3 .* ;
29+ import  com . walmartlabs . concord . sdk . Constants ;
3030
3131import  javax .net .ssl .SSLContext ;
32- import  javax .net .ssl .SSLSocketFactory ;
3332import  javax .net .ssl .TrustManager ;
3433import  javax .net .ssl .X509TrustManager ;
3534import  java .io .IOException ;
36- import  java .util .HashMap ;
35+ import  java .net .CookieManager ;
36+ import  java .net .CookiePolicy ;
37+ import  java .net .URI ;
38+ import  java .net .http .HttpClient ;
39+ import  java .net .http .HttpRequest ;
40+ import  java .net .http .HttpResponse ;
41+ import  java .security .KeyManagementException ;
42+ import  java .security .NoSuchAlgorithmException ;
43+ import  java .security .SecureRandom ;
44+ import  java .security .cert .X509Certificate ;
3745import  java .util .List ;
38- import  java .util .Map ;
3946
4047public  class  ProcessQueueClient  {
4148
42-     private  static  final  TypeReference <List <ProcessQueueEntry >> LIST_OF_PROCESS_QUEUE_ENTRIES  = new  TypeReference <List < ProcessQueueEntry > >() {
49+     private  static  final  TypeReference <List <ProcessQueueEntry >> LIST_OF_PROCESS_QUEUE_ENTRIES  = new  TypeReference <>() {
4350    };
4451
4552    private  final  String  baseUrl ;
4653    private  final  String  apiToken ;
47-     private  final  OkHttpClient  client ;
4854    private  final  ObjectMapper  objectMapper ;
55+     private  final  HttpClient  client ;
4956
5057    public  ProcessQueueClient (String  baseUrl , String  apiToken ) {
5158        this .baseUrl  = baseUrl ;
5259        this .apiToken  = apiToken ;
53-         this .client  = initClient ();
5460        this .objectMapper  = new  ObjectMapper ();
61+         this .client  = initClient ();
5562    }
5663
5764    public  List <ProcessQueueEntry > query (String  processStatus , int  limit , QueueSelector  queueSelector ) throws  IOException  {
@@ -66,109 +73,58 @@ public List<ProcessQueueEntry> query(String processStatus, int limit, QueueSelec
6673                queryUrl .append ("&" ).append (escapeQueryParam (queryParam ));
6774            }
6875        }
69-         Request  req  = new  Request .Builder ()
70-                 .url (queryUrl .toString ())
76+ 
77+         HttpRequest  request  = HttpRequest .newBuilder ()
78+                 .uri (URI .create (queryUrl .toString ()))
7179                .header ("Authorization" , apiToken )
72-                 .addHeader ("User-Agent" , "k8s-agent-operator" )
80+                 .header ("User-Agent" , "k8s-agent-operator" )
81+                 .header (Constants .Headers .ENABLE_HTTP_SESSION , "true" )
82+                 .GET ()
7383                .build ();
7484
75-         Call  call  = client .newCall (req );
76-         try  (Response  resp  = call .execute ()) {
77-             if  (!resp .isSuccessful ()) {
78-                 throw  new  IOException ("Error while fetching the process queue data: "  + resp .code ());
79-             }
80- 
81-             ResponseBody  body  = resp .body ();
82-             if  (body  == null ) {
83-                 throw  new  IOException ("Error while fetching the process queue data: empty response" );
84-             }
85+         HttpResponse <String > response ;
86+         try  {
87+             response  = client .send (request , HttpResponse .BodyHandlers .ofString ());
88+         } catch  (InterruptedException  e ) {
89+             Thread .currentThread ().interrupt ();
90+             throw  new  IOException ("Interrupted while fetching the queue data" , e );
91+         }
8592
86-             return  objectMapper .readValue (body .byteStream (), LIST_OF_PROCESS_QUEUE_ENTRIES );
93+         if  (response .statusCode () != 200 ) {
94+             throw  new  IOException ("Error while fetching the process queue data: "  + response .statusCode ());
8795        }
96+ 
97+         return  objectMapper .readValue (response .body (), LIST_OF_PROCESS_QUEUE_ENTRIES );
8898    }
8999
90-     private  static  OkHttpClient  initClient () {
100+     private  static  HttpClient  initClient () {
91101        try  {
92102            TrustManager [] trustAllCerts  = new  TrustManager []{
93103                    new  X509TrustManager () {
94-                         @ Override 
95-                         public   void   checkClientTrusted ( java . security . cert . X509Certificate []  chain ,  String   authType ) { 
104+                         public   X509Certificate []  getAcceptedIssuers () { 
105+                              return   new   X509Certificate [ 0 ]; 
96106                        }
97107
98-                         @ Override 
99-                         public  void  checkServerTrusted (java .security .cert .X509Certificate [] chain , String  authType ) {
108+                         public  void  checkClientTrusted (X509Certificate [] certs , String  authType ) {
100109                        }
101110
102-                         @ Override 
103-                         public  java .security .cert .X509Certificate [] getAcceptedIssuers () {
104-                             return  new  java .security .cert .X509Certificate []{};
111+                         public  void  checkServerTrusted (X509Certificate [] certs , String  authType ) {
105112                        }
106113                    }
107114            };
108115
109116            SSLContext  sslContext  = SSLContext .getInstance ("SSL" );
110-             sslContext .init (null , trustAllCerts , new  java .security .SecureRandom ());
111- 
112-             SSLSocketFactory  sslSocketFactory  = sslContext .getSocketFactory ();
113- 
114-             OkHttpClient .Builder  builder  = new  OkHttpClient .Builder ();
115-             builder .sslSocketFactory (sslSocketFactory , (X509TrustManager ) trustAllCerts [0 ]);
116-             builder .hostnameVerifier ((hostname , session ) -> true );
117- 
118-             Map <String , String > cookieJar  = new  HashMap <>();
119-             builder .addInterceptor (new  AddCookiesInterceptor (cookieJar ));
120-             builder .addInterceptor (new  ReceivedCookiesInterceptor (cookieJar ));
121- 
122-             return  builder .build ();
123-         } catch  (Exception  e ) {
124-             throw  new  RuntimeException (e );
125-         }
126-     }
127- 
128-     private  static  class  AddCookiesInterceptor  implements  Interceptor  {
117+             sslContext .init (null , trustAllCerts , new  SecureRandom ());
129118
130-         private  final  Map <String , String > cookieJar ;
131- 
132-         private  AddCookiesInterceptor (Map <String , String > cookieJar ) {
133-             this .cookieJar  = cookieJar ;
134-         }
135- 
136-         @ Override 
137-         public  Response  intercept (Chain  chain ) throws  IOException  {
138-             Request .Builder  builder  = chain .request ().newBuilder ();
139-             for  (Map .Entry <String , String > cookie  : cookieJar .entrySet ()) {
140-                 builder .addHeader ("Cookie" , cookie .getValue ());
141-             }
142-             return  chain .proceed (builder .build ());
143-         }
144-     }
145- 
146-     private  static  class  ReceivedCookiesInterceptor  implements  Interceptor  {
147- 
148-         private  static  final  String  SESSION_COOKIE_NAME  = "JSESSIONID" ;
149- 
150-         private  final  Map <String , String > cookieJar ;
151- 
152-         private  ReceivedCookiesInterceptor (Map <String , String > cookieJar ) {
153-             this .cookieJar  = cookieJar ;
154-         }
155- 
156-         @ Override 
157-         public  Response  intercept (Chain  chain ) throws  IOException  {
158-             Response  resp  = chain .proceed (chain .request ());
159- 
160-             List <String > cookies  = resp .headers ("Set-Cookie" );
161-             if  (cookies .isEmpty ()) {
162-                 return  resp ;
163-             }
164- 
165-             for  (String  cookie  : cookies ) {
166-                 if  (cookie .startsWith (SESSION_COOKIE_NAME )) {
167-                     cookieJar .put (SESSION_COOKIE_NAME , cookie );
168-                 }
169-             }
119+             CookieManager  cookieManager  = new  CookieManager ();
120+             cookieManager .setCookiePolicy (CookiePolicy .ACCEPT_ALL );
170121
171-             return  resp ;
122+             return  HttpClient .newBuilder ()
123+                     .sslContext (sslContext )
124+                     .cookieHandler (cookieManager )
125+                     .build ();
126+         } catch  (NoSuchAlgorithmException  | KeyManagementException  e ) {
127+             throw  new  RuntimeException ("Error while initializing the HTTP client" , e );
172128        }
173129    }
174130
0 commit comments