7
7
8
8
import com .neovisionaries .ws .client .*;
9
9
10
- import org .apache .http .Header ;
11
- import org .apache .http .HttpResponse ;
12
- import org .apache .http .HttpStatus ;
13
- import org .apache .http .NameValuePair ;
14
- import org .apache .http .client .HttpClient ;
15
- import org .apache .http .client .entity .UrlEncodedFormEntity ;
16
- import org .apache .http .client .methods .HttpPost ;
17
- import org .apache .http .conn .ssl .SSLConnectionSocketFactory ;
18
- import org .apache .http .impl .client .HttpClients ;
19
- import org .apache .http .message .BasicNameValuePair ;
20
- import org .apache .http .ssl .SSLContextBuilder ;
21
- import org .apache .http .util .EntityUtils ;
10
+ import org .apache .hc .client5 .http .classic .HttpClient ;
11
+ import org .apache .hc .client5 .http .classic .methods .HttpPost ;
12
+ import org .apache .hc .client5 .http .entity .UrlEncodedFormEntity ;
13
+ import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
14
+ import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManager ;
15
+ import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManagerBuilder ;
16
+ import org .apache .hc .client5 .http .ssl .SSLConnectionSocketFactoryBuilder ;
17
+ import org .apache .hc .core5 .http .*;
18
+ import org .apache .hc .core5 .http .io .entity .EntityUtils ;
19
+ import org .apache .hc .core5 .http .message .BasicNameValuePair ;
22
20
import org .json .JSONObject ;
23
21
24
22
import javax .net .ssl .SSLParameters ;
25
23
import java .io .IOException ;
26
24
import java .net .Inet4Address ;
25
+ import java .nio .charset .StandardCharsets ;
27
26
import java .security .NoSuchAlgorithmException ;
28
27
import java .util .ArrayList ;
29
28
import java .util .List ;
48
47
* providing the updated token to the Real-Time endpoint before token expiration.
49
48
*/
50
49
public class WebSocketConnector {
51
-
52
-
53
50
Properties connectionProperties ;
54
51
public JSONObject authJson ;
55
52
56
-
57
53
public String position ;
58
54
public String scope = "" ;
59
55
public String server = "" ;
@@ -63,8 +59,6 @@ public class WebSocketConnector {
63
59
public WebSocketConnector (Properties connectionProperties ) throws Exception {
64
60
this .connectionProperties = connectionProperties ;
65
61
this .position = Inet4Address .getLocalHost ().getHostAddress ();
66
-
67
-
68
62
}
69
63
70
64
public WebSocket getWebSocket () throws Exception {
@@ -84,10 +78,8 @@ public String getPosition(){
84
78
85
79
public WebSocketConnector initAuthJson () {
86
80
try {
87
-
88
81
// Connect to Live Market Data Platform and authenticate (using our username and password)
89
82
this .authJson = getAuthenticationInfo (null , connectionProperties .get ("AUTHURL" ).toString ());
90
-
91
83
} catch (Exception e ) {
92
84
e .printStackTrace ();
93
85
}
@@ -134,17 +126,20 @@ public WebSocket initWebSocketConnection() throws IOException, WebSocketExceptio
134
126
public JSONObject getAuthenticationInfo (JSONObject previousAuthResponseJson , String url ) {
135
127
try
136
128
{
137
- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (new SSLContextBuilder ().build ());
129
+ PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder .create ()
130
+ .setSSLSocketFactory (SSLConnectionSocketFactoryBuilder .create ().build ())
131
+ .build ();
138
132
139
- HttpClient httpclient = HttpClients .custom ().setSSLSocketFactory (sslsf ).build ();
133
+ HttpClient httpclient = HttpClientBuilder .create ()
134
+ .setConnectionManager (connectionManager ).build ();
140
135
HttpPost httppost = new HttpPost (url );
141
136
/* HttpParams httpParams = new BasicHttpParams();
142
137
143
138
// Disable redirect
144
139
httpParams.setParameter(ClientPNames.HANDLE_REDIRECTS, false);*/
145
140
146
141
// Set request parameters.
147
- List <NameValuePair > params = new ArrayList <NameValuePair >(2 );
142
+ List <NameValuePair > params = new ArrayList <>(2 );
148
143
params .add (new BasicNameValuePair ("client_id" , connectionProperties .get ("CLIENTID" ).toString ()));
149
144
params .add (new BasicNameValuePair ("username" , connectionProperties .get ("USER" ).toString ()));
150
145
@@ -164,12 +159,12 @@ public JSONObject getAuthenticationInfo(JSONObject previousAuthResponseJson, Str
164
159
}
165
160
166
161
//httppost.setParams(httpParams);
167
- httppost .setEntity (new UrlEncodedFormEntity (params , "UTF-8" ));
162
+ httppost .setEntity (new UrlEncodedFormEntity (params , StandardCharsets . UTF_8 ));
168
163
169
164
//Execute and get the response.
170
- HttpResponse response = httpclient .execute ( httppost );
165
+ ClassicHttpResponse response = httpclient .executeOpen ( null , httppost , null );
171
166
172
- int statusCode = response .getStatusLine (). getStatusCode ();
167
+ int statusCode = response .getCode ();
173
168
174
169
switch ( statusCode ) {
175
170
case HttpStatus .SC_OK : // 200
@@ -198,7 +193,7 @@ public JSONObject getAuthenticationInfo(JSONObject previousAuthResponseJson, Str
198
193
case HttpStatus .SC_BAD_REQUEST : // 400
199
194
case HttpStatus .SC_UNAUTHORIZED : // 401
200
195
// Retry with username and password
201
- System .out .println ("Refinitiv Data Platform authentication HTTP code: " + response .getStatusLine (). getStatusCode () + " " + response . getStatusLine () .getReasonPhrase ());
196
+ System .out .println ("Refinitiv Data Platform authentication HTTP code: " + response .getCode () + " " + response .getReasonPhrase ());
202
197
if (previousAuthResponseJson != null ) {
203
198
System .out .println ("Retry with username and password" );
204
199
return getAuthenticationInfo (null , connectionProperties .get ("AUTHURL" ).toString ());
@@ -209,12 +204,12 @@ public JSONObject getAuthenticationInfo(JSONObject previousAuthResponseJson, Str
209
204
case HttpStatus .SC_GONE : // 410
210
205
case 451 : // 451 Unavailable For Legal Reasons
211
206
// Stop retrying with the request
212
- System .out .println ("Refinitiv Data Platform authentication HTTP code: " + response .getStatusLine (). getStatusCode () + " " + response . getStatusLine () .getReasonPhrase ());
207
+ System .out .println ("Refinitiv Data Platform authentication HTTP code: " + response .getCode () + " " + response .getReasonPhrase ());
213
208
System .out .println ("Stop retrying with the request" );
214
209
return null ;
215
210
default :
216
211
// Retry the request to Refinitiv Data Platform
217
- System .out .println ("Refinitiv Data Platform authentication HTTP code: " + response .getStatusLine (). getStatusCode () + " " + response . getStatusLine () .getReasonPhrase ());
212
+ System .out .println ("Refinitiv Data Platform authentication HTTP code: " + response .getCode () + " " + response .getReasonPhrase ());
218
213
Thread .sleep (5000 );
219
214
// CAUTION: This is sample code with infinite retries.
220
215
System .out .println ("Retry the request to Refinitiv Data Platform" );
0 commit comments