Skip to content

Commit 38bfb4e

Browse files
authored
Merge pull request #23 from iot-dsa-v2/develop
1.1.7
2 parents e159a31 + 832f1ad commit 38bfb4e

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ repositories {
2424

2525
dependencies {
2626
//implementation 'org.iot-dsa:dslink-v2-websocket:+' //for a locally installed sdk
27-
implementation 'com.github.iot-dsa-v2.sdk-dslink-java-v2:dslink-v2-websocket:+'
27+
implementation 'com.github.iot-dsa-v2.sdk-dslink-java-v2:dslink-v2-websocket:0.62.1'
2828
implementation 'commons-logging:commons-logging:+'
2929
implementation 'org.apache.commons:commons-lang3:3.8.1'
3030
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.1.6",
3+
"version": "1.1.7",
44
"description": "Java DSA to REST adpater DSLink",
55
"main": "bin/dslink-java-v2-restadapter",
66
"configs": {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void responseRecieved(ResponseWrapper resp, int rowNum) {
137137
if (resp == null) {
138138
put(lastRespCode, DSInt.valueOf(-1));
139139
put(lastRespData, DSString.valueOf("Failed to send update"));
140-
put(lastRespTs, DSString.valueOf(DSDateTime.currentTime()));
140+
put(lastRespTs, DSString.valueOf(DSDateTime.now()));
141141
} else {
142142
int status = resp.getCode();
143143
String data = resp.getData();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void responseRecieved(ResponseWrapper resp, int rowNum) {
3333
if (resp == null) {
3434
respMap.put(Constants.LAST_RESPONSE_CODE, -1);
3535
respMap.put(Constants.LAST_RESPONSE_DATA, "Failed to send update");
36-
respMap.put(Constants.LAST_RESPONSE_TS, DSDateTime.currentTime().toString());
36+
respMap.put(Constants.LAST_RESPONSE_TS, DSDateTime.now().toString());
3737
} else {
3838
int status = resp.getCode();
3939
String data = resp.getData();

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

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

33

4+
import java.io.IOException;
45
import org.iot.dsa.logging.DSLogger;
56
import org.iot.dsa.node.DSMap;
67
import org.iot.dsa.node.DSMap.Entry;
8+
import okhttp3.Authenticator;
9+
import okhttp3.Credentials;
710
import okhttp3.HttpUrl;
811
import okhttp3.MediaType;
912
import okhttp3.OkHttpClient;
1013
import okhttp3.Request;
1114
import okhttp3.RequestBody;
1215
import okhttp3.Response;
16+
import okhttp3.Route;
1317

1418
public class WebClientProxy extends DSLogger {
1519
private CredentialProvider credentials;
@@ -70,14 +74,31 @@ private void prepareClient() {
7074
client = configureAuthorization();
7175
}
7276
}
77+
78+
private static int responseCount(Response response) {
79+
int result = 1;
80+
while ((response = response.priorResponse()) != null) {
81+
result++;
82+
}
83+
return result;
84+
}
7385

7486
private OkHttpClient configureAuthorization() {
7587
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
7688
switch (getScheme()) {
7789
case NO_AUTH:
7890
break;
7991
case BASIC_USR_PASS:
80-
//TODO implement basic auth
92+
clientBuilder.authenticator(new Authenticator() {
93+
@Override
94+
public Request authenticate(Route route, Response response) throws IOException {
95+
if (responseCount(response) >= 3) {
96+
return null;
97+
}
98+
String credential = Credentials.basic(credentials.getUsername(), credentials.getPassword());
99+
return response.request().newBuilder().header("Authorization", credential).build();
100+
}
101+
});
81102
break;
82103
case OAUTH2_CLIENT:
83104
case OAUTH2_USR_PASS:

0 commit comments

Comments
 (0)