11package org .iot .dsa .dslink .restadapter ;
22
33
4- import java .io .IOException ;
4+ import java .net .CookieManager ;
5+ import java .net .CookiePolicy ;
56import java .time .Duration ;
67import org .iot .dsa .logging .DSLogger ;
78import org .iot .dsa .node .DSMap ;
89import org .iot .dsa .node .DSMap .Entry ;
9- import okhttp3 .Authenticator ;
10- import okhttp3 .Credentials ;
1110import okhttp3 .HttpUrl ;
11+ import okhttp3 .JavaNetCookieJar ;
1212import okhttp3 .MediaType ;
1313import okhttp3 .OkHttpClient ;
1414import okhttp3 .Request ;
1515import okhttp3 .RequestBody ;
1616import okhttp3 .Response ;
17- import okhttp3 .Route ;
1817
1918public class WebClientProxy extends DSLogger {
2019 private CredentialProvider credentials ;
@@ -49,11 +48,14 @@ public WebClientProxy(CredentialProvider credentials, long readTimeoutMillis, lo
4948// return new WebClientProxy(username, password, clientID, clientSecret, tokenURL, Util.AUTH_SCHEME.OAUTH2_USR_PASS);
5049// }
5150
52-
53- public Response invoke (String httpMethod , String address , DSMap urlParameters , Object body ) {
51+ public Request .Builder prepareInvoke (String httpMethod , String address , DSMap urlParameters , Object body ) {
5452 prepareClient ();
5553 Request .Builder requestBuilder = prepareRequest (address , urlParameters );
5654 requestBuilder .method (httpMethod , body == null ? null : RequestBody .create (MediaType .parse ("application/json" ), body .toString ()));
55+ return requestBuilder ;
56+ }
57+
58+ public Response completeInvoke (Request .Builder requestBuilder ) {
5759 Request request = requestBuilder .build ();
5860 Response response = null ;
5961 try {
@@ -64,6 +66,12 @@ public Response invoke(String httpMethod, String address, DSMap urlParameters, O
6466 return response ;
6567 }
6668
69+
70+ public Response invoke (String httpMethod , String address , DSMap urlParameters , Object body ) {
71+ Request .Builder requestBuilder = prepareInvoke (httpMethod , address , urlParameters , body );
72+ return completeInvoke (requestBuilder );
73+ }
74+
6775 private Request .Builder prepareRequest (String address , DSMap urlParameters ) {
6876 HttpUrl .Builder urlBuilder = HttpUrl .parse (address ).newBuilder ();
6977
@@ -84,30 +92,31 @@ private void prepareClient() {
8492 }
8593 }
8694
87- private static int responseCount (Response response ) {
88- int result = 1 ;
89- while ((response = response .priorResponse ()) != null ) {
90- result ++;
91- }
92- return result ;
93- }
95+ // private static int responseCount(Response response) {
96+ // int result = 1;
97+ // while ((response = response.priorResponse()) != null) {
98+ // result++;
99+ // }
100+ // return result;
101+ // }
94102
95103 private OkHttpClient configureAuthorization () {
96104 OkHttpClient .Builder clientBuilder = new OkHttpClient .Builder ();
97105 switch (getScheme ()) {
98106 case NO_AUTH :
99107 break ;
100108 case BASIC_USR_PASS :
101- clientBuilder .authenticator (new Authenticator () {
102- @ Override
103- public Request authenticate (Route route , Response response ) throws IOException {
104- if (responseCount (response ) >= 3 ) {
105- return null ;
106- }
107- String credential = Credentials .basic (credentials .getUsername (), credentials .getPassword ());
108- return response .request ().newBuilder ().header ("Authorization" , credential ).build ();
109- }
110- });
109+ // clientBuilder.authenticator(new Authenticator() {
110+ // @Override
111+ // public Request authenticate(Route route, Response response) throws IOException {
112+ // if (responseCount(response) >= 3) {
113+ // return null;
114+ // }
115+ // String credential = Credentials.basic(credentials.getUsername(), credentials.getPassword());
116+ // return response.request().newBuilder().header("Authorization", credential).build();
117+ // }
118+ // });
119+ clientBuilder .addInterceptor (new BasicAuthInterceptor (credentials .getUsername (), credentials .getPassword ()));
111120 break ;
112121 case OAUTH2_CLIENT :
113122 case OAUTH2_USR_PASS :
@@ -121,6 +130,9 @@ public Request authenticate(Route route, Response response) throws IOException {
121130 if (writeTimeout != null ) {
122131 clientBuilder .writeTimeout (writeTimeout );
123132 }
133+ CookieManager cookieManager = new CookieManager ();
134+ cookieManager .setCookiePolicy (CookiePolicy .ACCEPT_ALL );
135+ clientBuilder .cookieJar (new JavaNetCookieJar (cookieManager ));
124136 return clientBuilder .build ();
125137 }
126138
0 commit comments