1
1
package org .iot .dsa .dslink .restadapter ;
2
2
3
-
4
- import java .net .CookieManager ;
5
- import java .net .CookiePolicy ;
6
- import java .time .Duration ;
7
- import org .iot .dsa .logging .DSLogger ;
8
- import org .iot .dsa .node .DSMap ;
9
- import org .iot .dsa .node .DSMap .Entry ;
3
+
10
4
import okhttp3 .HttpUrl ;
11
5
import okhttp3 .JavaNetCookieJar ;
12
6
import okhttp3 .MediaType ;
13
7
import okhttp3 .OkHttpClient ;
14
8
import okhttp3 .Request ;
15
9
import okhttp3 .RequestBody ;
16
10
import okhttp3 .Response ;
11
+ import org .iot .dsa .logging .DSLogger ;
12
+ import org .iot .dsa .node .DSMap ;
13
+ import org .iot .dsa .node .DSMap .Entry ;
14
+
15
+ import java .net .CookieManager ;
16
+ import java .net .CookiePolicy ;
17
+ import java .util .concurrent .TimeUnit ;
17
18
18
19
public class WebClientProxy extends DSLogger {
20
+
19
21
private CredentialProvider credentials ;
20
- private Duration readTimeout = null ;
21
- private Duration writeTimeout = null ;
22
+ private long readTimeout = - 1 ;
23
+ private long writeTimeout = - 1 ;
22
24
23
25
private OkHttpClient client ;
24
26
25
27
public WebClientProxy (CredentialProvider credentials ) {
26
28
this .credentials = credentials ;
27
29
}
28
-
29
- public WebClientProxy (CredentialProvider credentials , long readTimeoutMillis , long writeTimeoutMillis ) {
30
+
31
+ public WebClientProxy (CredentialProvider credentials , long readTimeoutMillis ,
32
+ long writeTimeoutMillis ) {
30
33
this (credentials );
31
- this .readTimeout = Duration . ofMillis ( readTimeoutMillis ) ;
32
- this .writeTimeout = Duration . ofMillis ( writeTimeoutMillis ) ;
34
+ this .readTimeout = readTimeoutMillis ;
35
+ this .writeTimeout = writeTimeoutMillis ;
33
36
}
34
37
35
38
// public static WebClientProxy buildNoAuthClient() {
@@ -47,14 +50,16 @@ public WebClientProxy(CredentialProvider credentials, long readTimeoutMillis, lo
47
50
// public static WebClientProxy buildPasswordFlowOAuth2Client(String username, String password, String clientID, String clientSecret, String tokenURL) {
48
51
// return new WebClientProxy(username, password, clientID, clientSecret, tokenURL, Util.AUTH_SCHEME.OAUTH2_USR_PASS);
49
52
// }
50
-
51
- public Request .Builder prepareInvoke (String httpMethod , String address , DSMap urlParameters , Object body ) {
53
+
54
+ public Request .Builder prepareInvoke (String httpMethod , String address , DSMap urlParameters ,
55
+ Object body ) {
52
56
prepareClient ();
53
57
Request .Builder requestBuilder = prepareRequest (address , urlParameters );
54
- requestBuilder .method (httpMethod , body == null ? null : RequestBody .create (MediaType .parse ("application/json" ), body .toString ()));
58
+ requestBuilder .method (httpMethod , body == null ? null :
59
+ RequestBody .create (MediaType .parse ("application/json" ), body .toString ()));
55
60
return requestBuilder ;
56
61
}
57
-
62
+
58
63
public Response completeInvoke (Request .Builder requestBuilder ) {
59
64
Request request = requestBuilder .build ();
60
65
Response response = null ;
@@ -65,17 +70,17 @@ public Response completeInvoke(Request.Builder requestBuilder) {
65
70
}
66
71
return response ;
67
72
}
68
-
73
+
69
74
70
75
public Response invoke (String httpMethod , String address , DSMap urlParameters , Object body ) {
71
76
Request .Builder requestBuilder = prepareInvoke (httpMethod , address , urlParameters , body );
72
77
return completeInvoke (requestBuilder );
73
78
}
74
-
79
+
75
80
private Request .Builder prepareRequest (String address , DSMap urlParameters ) {
76
81
HttpUrl .Builder urlBuilder = HttpUrl .parse (address ).newBuilder ();
77
82
78
- for (Entry entry : urlParameters ) {
83
+ for (Entry entry : urlParameters ) {
79
84
Object value = Util .dsElementToObject (entry .getValue ());
80
85
urlBuilder .addQueryParameter (entry .getKey (), value .toString ());
81
86
}
@@ -85,13 +90,13 @@ private Request.Builder prepareRequest(String address, DSMap urlParameters) {
85
90
.addHeader ("Content-Type" , "application/json" );
86
91
return requestBuilder ;
87
92
}
88
-
93
+
89
94
private void prepareClient () {
90
95
if (client == null ) {
91
- client = configureAuthorization ();
96
+ client = configureAuthorization ();
92
97
}
93
98
}
94
-
99
+
95
100
// private static int responseCount(Response response) {
96
101
// int result = 1;
97
102
// while ((response = response.priorResponse()) != null) {
@@ -116,41 +121,43 @@ private OkHttpClient configureAuthorization() {
116
121
// return response.request().newBuilder().header("Authorization", credential).build();
117
122
// }
118
123
// });
119
- clientBuilder .addInterceptor (new BasicAuthInterceptor (credentials .getUsername (), credentials .getPassword ()));
124
+ clientBuilder .addInterceptor (new BasicAuthInterceptor (credentials .getUsername (),
125
+ credentials .getPassword ()));
120
126
break ;
121
127
case OAUTH2_CLIENT :
122
128
case OAUTH2_USR_PASS :
123
129
clientBuilder .addInterceptor (new OAuthInterceptor (this ));
124
130
// client.header(HttpHeaders.AUTHORIZATION, authManager.createAuthorizationHeader());
125
131
break ;
126
132
}
127
- if (readTimeout != null ) {
128
- clientBuilder .readTimeout (readTimeout );
133
+ if (readTimeout > 0 ) {
134
+ clientBuilder .connectTimeout (readTimeout , TimeUnit .MILLISECONDS );
135
+ clientBuilder .readTimeout (readTimeout , TimeUnit .MILLISECONDS );
129
136
}
130
- if (writeTimeout != null ) {
131
- clientBuilder .writeTimeout (writeTimeout );
137
+ if (writeTimeout > 0 ) {
138
+ clientBuilder .writeTimeout (writeTimeout , TimeUnit . MILLISECONDS );
132
139
}
133
140
CookieManager cookieManager = new CookieManager ();
134
141
cookieManager .setCookiePolicy (CookiePolicy .ACCEPT_ALL );
135
142
clientBuilder .cookieJar (new JavaNetCookieJar (cookieManager ));
136
143
return clientBuilder .build ();
137
144
}
138
-
145
+
139
146
public OkHttpClient getClient () {
140
147
if (client == null ) {
141
148
prepareClient ();
142
149
}
143
150
return client ;
144
151
}
145
-
152
+
146
153
public String getUsername () {
147
154
return credentials .getUsername ();
148
155
}
149
156
150
157
public String getPassword () {
151
158
return credentials .getPassword ();
152
159
}
153
-
160
+
154
161
public String getClientID () {
155
162
return credentials .getClientId ();
156
163
}
@@ -162,9 +169,9 @@ public String getClientSecret() {
162
169
public String getTokenURL () {
163
170
return credentials .getTokenURL ();
164
171
}
165
-
172
+
166
173
public Util .AUTH_SCHEME getScheme () {
167
174
return credentials .getAuthScheme ();
168
175
}
169
-
176
+
170
177
}
0 commit comments