Skip to content

Commit 11d0733

Browse files
authored
1.2.1
Do basic auth more efficiently (Include the auth header preemptively) Add cookie jar Allow requests made by WebClientProxy to be modified before they are sent
1 parent 9dca8c2 commit 11d0733

File tree

4 files changed

+62
-24
lines changed

4 files changed

+62
-24
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dependencies {
2626
//implementation 'org.iot-dsa:dslink-v2-websocket:+' //for a locally installed sdk
2727
api 'com.github.iot-dsa-v2.sdk-dslink-java-v2:dslink-v2-websocket:+'
2828
api 'com.squareup.okhttp3:okhttp:3.14.0'
29+
api 'com.squareup.okhttp3:okhttp-urlconnection:3.8.1'
2930
implementation 'commons-logging:commons-logging:+'
3031
implementation 'org.apache.commons:commons-lang3:3.8.1'
3132
implementation 'commons-io:commons-io:2.6'

dslink.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dslink-java-v2-restadapter",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "Java DSA to REST adapter DSLink",
55
"main": "bin/dslink-java-v2-restadapter",
66
"configs": {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.iot.dsa.dslink.restadapter;
2+
3+
import java.io.IOException;
4+
import okhttp3.Credentials;
5+
import okhttp3.Interceptor;
6+
import okhttp3.Request;
7+
import okhttp3.Response;
8+
9+
public class BasicAuthInterceptor implements Interceptor {
10+
11+
private String credentials;
12+
13+
public BasicAuthInterceptor(String user, String password) {
14+
this.credentials = Credentials.basic(user, password);
15+
}
16+
17+
@Override
18+
public Response intercept(Chain chain) throws IOException {
19+
Request request = chain.request();
20+
Request authenticatedRequest = request.newBuilder()
21+
.header("Authorization", credentials).build();
22+
return chain.proceed(authenticatedRequest);
23+
}
24+
25+
}

src/main/java/org/iot/dsa/dslink/restadapter/WebClientProxy.java

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package org.iot.dsa.dslink.restadapter;
22

33

4-
import java.io.IOException;
4+
import java.net.CookieManager;
5+
import java.net.CookiePolicy;
56
import java.time.Duration;
67
import org.iot.dsa.logging.DSLogger;
78
import org.iot.dsa.node.DSMap;
89
import org.iot.dsa.node.DSMap.Entry;
9-
import okhttp3.Authenticator;
10-
import okhttp3.Credentials;
1110
import okhttp3.HttpUrl;
11+
import okhttp3.JavaNetCookieJar;
1212
import okhttp3.MediaType;
1313
import okhttp3.OkHttpClient;
1414
import okhttp3.Request;
1515
import okhttp3.RequestBody;
1616
import okhttp3.Response;
17-
import okhttp3.Route;
1817

1918
public 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

Comments
 (0)