Skip to content

Commit 38a5566

Browse files
authored
1.5.0
* Add support for bearer token authentication * Display subscribe path for rule node * Update dependencies
1 parent 2b4b3e6 commit 38a5566

12 files changed

+89
-20
lines changed

build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ repositories {
2222

2323
dependencies {
2424
//api 'org.iot-dsa:dslink-v2-websocket:+' //for a locally installed sdk
25-
api 'com.github.iot-dsa-v2.sdk-dslink-java-v2:dslink-v2-websocket:0.77.0'
26-
api 'com.squareup.okhttp3:okhttp:3.14.0'
27-
api 'com.squareup.okhttp3:okhttp-urlconnection:3.14.0'
25+
api 'com.github.iot-dsa-v2.sdk-dslink-java-v2:dslink-v2-websocket:0.79.2'
26+
api 'com.squareup.okhttp3:okhttp:3.14.9'
27+
api 'com.squareup.okhttp3:okhttp-urlconnection:3.14.9'
2828
api 'org.apache.commons:commons-lang3:3.11'
2929
implementation 'commons-logging:commons-logging:1.2'
30-
implementation 'commons-io:commons-io:2.8.0'
30+
implementation 'commons-io:commons-io:2.11.0'
3131

3232
testImplementation 'junit:junit:+'
3333
testImplementation 'org.mockito:mockito-all:+'

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

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public String getUsername() {
4949
return parameters.getString(Constants.USERNAME);
5050
}
5151

52+
public String getToken() {
53+
return parameters.getString(Constants.TOKEN);
54+
}
55+
5256
public WebClientProxy getWebClientProxy() {
5357
return clientProxy;
5458
}
@@ -146,19 +150,26 @@ public ActionResults invoke(DSIActionRequest req) {
146150
return null;
147151
}
148152
};
153+
149154
AUTH_SCHEME scheme = getAuthScheme();
150-
if (!Util.AUTH_SCHEME.OAUTH2_CLIENT.equals(scheme)) {
155+
if (AUTH_SCHEME.BASIC_USR_PASS.equals(scheme)) {
151156
act.addDefaultParameter(Constants.USERNAME, DSString.valueOf(getUsername()), null);
152157
act.addDefaultParameter(Constants.PASSWORD, DSString.valueOf(getPassword()), null)
153158
.setEditor("password");
154159
}
160+
155161
if (Util.AUTH_SCHEME.OAUTH2_CLIENT.equals(scheme) || Util.AUTH_SCHEME.OAUTH2_USR_PASS
156162
.equals(scheme)) {
157163
act.addDefaultParameter(Constants.CLIENT_ID, DSString.valueOf(getClientId()), null);
158164
act.addDefaultParameter(Constants.CLIENT_SECRET, DSString.valueOf(getClientSecret()),
159165
null).setEditor("password");
160166
act.addDefaultParameter(Constants.TOKEN_URL, DSString.valueOf(getTokenURL()), null);
161167
}
168+
169+
if (AUTH_SCHEME.BEARER.equals(scheme)) {
170+
act.addDefaultParameter(Constants.TOKEN_URL, DSString.valueOf(getToken()), null);
171+
}
172+
162173
return act;
163174
}
164175

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class Constants {
1010
public static final String CLIENT_ID = "Client ID";
1111
public static final String CLIENT_SECRET = "Client Secret";
1212
public static final String TOKEN_URL = "Token URL";
13+
public static final String TOKEN = "Token";
1314
public static final String CONNTYPE = "ConnType";
1415

1516
public static final String SUB_PATH = "Subscribe Path";
@@ -27,6 +28,7 @@ public class Constants {
2728

2829
//Actions
2930
public static final String ACT_ADD_BASIC_CONN = "Basic Connection";
31+
public static final String ACT_ADD_BEARER_CONN = "Bearer Connection";
3032
public static final String ACT_ADD_OAUTH_CLIENT_CONN = "OAuth2 Client Flow";
3133
public static final String ACT_ADD_OAUTH_PASSWORD_CONN = "OAuth2 Password Flow";
3234
public static final String ACT_ADD_RULE = "Add Rule";
@@ -41,6 +43,7 @@ public class Constants {
4143
public static final String LAST_RESPONSE_DATA = "Last Response Data";
4244
public static final String LAST_RESPONSE_TS = "Last Response Timestamp";
4345
public static final String LAST_RESPONSES_TABLE = "Last Responses";
46+
public static final String SUBSCRIBE_PATH = "Subscribe Path";
4447

4548
//FORMAT PLACEHOLDERS
4649
public static final String PLACEHOLDER_VALUE = "%VALUE%";

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ public interface CredentialProvider {
1414

1515
public Util.AUTH_SCHEME getAuthScheme();
1616

17+
public String getToken();
18+
1719
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.iot.dsa.dslink.DSIRequester;
66
import org.iot.dsa.dslink.DSLinkConnection;
77
import org.iot.dsa.dslink.DSMainNode;
8+
import org.iot.dsa.dslink.restadapter.Util.AUTH_SCHEME;
89
import org.iot.dsa.node.DSBool;
910
import org.iot.dsa.node.DSInfo;
1011
import org.iot.dsa.node.DSLong;
@@ -66,6 +67,7 @@ public static void setRequester(DSIRequester requester) {
6667
protected void declareDefaults() {
6768
super.declareDefaults();
6869
declareDefault(Constants.ACT_ADD_BASIC_CONN, makeAddBasicConnectionAction());
70+
declareDefault(Constants.ACT_ADD_BEARER_CONN, makeAddBearerConnectionAction());
6971
declareDefault(Constants.ACT_ADD_OAUTH_CLIENT_CONN, makeAddOauthClientConnectionAction());
7072
declareDefault(Constants.ACT_ADD_OAUTH_PASSWORD_CONN, makeAddOauthPassConnectionAction());
7173
declareDefault(Constants.BUFFER_PURGE_ENABLED, DSBool.FALSE,
@@ -98,6 +100,12 @@ private void addBasicConnection(DSMap parameters) {
98100
put(name, new ConnectionNode(parameters));
99101
}
100102

103+
private void addBearerConnection(DSMap parameters) {
104+
parameters.put(Constants.CONNTYPE, DSString.valueOf(AUTH_SCHEME.BEARER));
105+
String name = parameters.getString(Constants.NAME);
106+
put(name, new ConnectionNode(parameters));
107+
}
108+
101109
private void addOAuthClientConnection(DSMap parameters) {
102110
parameters.put(Constants.CONNTYPE, DSString.valueOf(Util.AUTH_SCHEME.OAUTH2_CLIENT));
103111
String name = parameters.getString(Constants.NAME);
@@ -124,6 +132,19 @@ public ActionResults invoke(DSIActionRequest req) {
124132
return act;
125133
}
126134

135+
private DSAction makeAddBearerConnectionAction() {
136+
DSAction act = new DSAction() {
137+
@Override
138+
public ActionResults invoke(DSIActionRequest req) {
139+
((MainNode) req.getTarget()).addBearerConnection(req.getParameters());
140+
return null;
141+
}
142+
};
143+
act.addParameter(Constants.NAME, DSString.NULL, null);
144+
act.addParameter(Constants.TOKEN, DSString.NULL, null);
145+
return act;
146+
}
147+
127148
private DSAction makeAddOauthClientConnectionAction() {
128149
DSAction act = new DSAction() {
129150
@Override

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class RuleNode extends AbstractRuleNode {
1717
private DSInfo lastRespCode = getInfo(Constants.LAST_RESPONSE_CODE);
1818
private DSInfo lastRespData = getInfo(Constants.LAST_RESPONSE_DATA);
1919
private DSInfo lastRespTs = getInfo(Constants.LAST_RESPONSE_TS);
20+
private DSInfo subscribePath = getInfo(Constants.SUB_PATH);
21+
2022
private DSMap parameters;
2123
private SubscriptionRule rule;
2224

@@ -78,6 +80,7 @@ protected void declareDefaults() {
7880
declareDefault(Constants.LAST_RESPONSE_CODE, DSInt.NULL).setReadOnly(true);
7981
declareDefault(Constants.LAST_RESPONSE_DATA, DSString.EMPTY).setReadOnly(true);
8082
declareDefault(Constants.LAST_RESPONSE_TS, DSString.EMPTY).setReadOnly(true);
83+
declareDefault(Constants.SUB_PATH, DSString.EMPTY).setReadOnly(true);
8184
}
8285

8386
protected void edit(DSMap parameters) {
@@ -97,6 +100,7 @@ protected void onStable() {
97100
rule = new SubscriptionRule(this, getSubscribePath(), getRestUrl(), getMethod(),
98101
getURLParameters(), getBody(), getMinRefreshRate(),
99102
getMaxRefreshRate(), 0);
103+
put(Constants.SUB_PATH, getSubscribePath());
100104
put(Constants.ACT_EDIT, makeEditAction()).setTransient(true);
101105
}
102106

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,27 @@ private void learnPattern() {
103103
@Override
104104
public void onClose() {
105105
super.onClose();
106-
node.info("Rule with sub path " + subPath + ": onClose called");
106+
node.debug("Rule with sub path " + subPath + ": onClose called");
107107
// close();
108108
}
109109

110110
@Override
111111
public void onError(ErrorType type, String msg) {
112112
super.onError(type, msg);
113-
node.info("Rule with sub path " + subPath + ": onError called with msg " + msg);
113+
node.debug("Rule with sub path " + subPath + ": onError called with msg " + msg);
114114
// DSException.throwRuntime(new RuntimeException(msg));
115115
}
116116

117117
@Override
118118
public void onInit(String path, DSIValue qos, OutboundStream stream) {
119119
super.onInit(path, qos, stream);
120-
node.info("Rule with sub path " + subPath + ": onInit called");
120+
node.debug("Rule with sub path " + subPath + ": onInit called");
121121
//this.stream = stream;
122122
}
123123

124124
@Override
125125
public void onUpdate(DSDateTime dateTime, DSElement value, DSStatus status) {
126-
node.info("Rule with sub path " + subPath + ": onUpdate called with value " + (value!=null ? value : "Null"));
126+
node.debug("Rule with sub path " + subPath + ": onUpdate called with value " + (value!=null ? value : "Null"));
127127
storedUpdate = new SubUpdate(dateTime.toString(), value.toString(), status.toString(), dateTime.timeInMillis());
128128
if (lastUpdateTime < 0 || System.currentTimeMillis() - lastUpdateTime >= minRefreshRate) {
129129
if (future != null) {
@@ -190,7 +190,7 @@ public boolean sendUpdate(final SubUpdate update) {
190190
body = body.replaceAll(Constants.PLACEHOLDER_BLOCK_END, "");
191191
}
192192

193-
node.info("Rule with sub path " + subPath + ": sending Update with value " + (update.value!=null ? update.value : "Null"));
193+
node.debug("Rule with sub path " + subPath + ": sending Update with value " + (update.value!=null ? update.value : "Null"));
194194

195195
ResponseWrapper resp = doSend(urlParams, body);
196196
return resp != null && resp.getCode() / 100 == 2;
@@ -230,7 +230,7 @@ public Queue<SubUpdate> sendBatchUpdate(Queue<SubUpdate> updates) {
230230
}
231231
sb.append(suffix);
232232
String body = sb.toString();
233-
node.info("Rule with sub path " + subPath + ": sending batch update");
233+
node.debug("Rule with sub path " + subPath + ": sending batch update");
234234

235235
ResponseWrapper resp = doSend(urlParams, body);
236236
if (resp != null && resp.getCode() / 100 == 2) {
@@ -255,7 +255,7 @@ protected ResponseWrapper doSend(DSMap urlParams, String body) {
255255

256256
public void close() {
257257
if (!isClosed() && getStream() != null) {
258-
node.info("Rule with sub path " + subPath + ": closing Stream");
258+
node.debug("Rule with sub path " + subPath + ": closing Stream");
259259
getStream().closeStream();
260260
}
261261
}
@@ -272,4 +272,4 @@ public AbstractRuleNode getNode() {
272272
return node;
273273
}
274274

275-
}
275+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,32 @@ protected void init() {
3939
@Override
4040
public void onClose() {
4141
super.onClose();
42-
node.info("Test Rule with sub path " + subpath + ": onClose called");
42+
node.debug("Test Rule with sub path " + subpath + ": onClose called");
4343
// close();
4444
}
4545

4646
@Override
4747
public void onError(ErrorType type, String msg) {
4848
super.onError(type, msg);
49-
node.info("Test Rule with sub path " + subpath + ": onError called with msg " + msg);
49+
node.debug("Test Rule with sub path " + subpath + ": onError called with msg " + msg);
5050
// DSException.throwRuntime(new RuntimeException(msg));
5151
}
5252

5353
@Override
5454
public void onInit(String path, DSIValue qos, OutboundStream stream) {
5555
super.onInit(path, qos, stream);
56-
node.info("Test Rule with sub path " + subpath + ": onInit called");
56+
node.debug("Test Rule with sub path " + subpath + ": onInit called");
5757
// this.stream = stream;
5858
}
5959

6060
@Override
6161
public void onUpdate(DSDateTime dateTime, DSElement value, DSStatus status) {
62-
node.info("Test Rule with sub path " + subpath + ": onUpdate called with value " + (value!=null ? value : "Null"));
62+
node.debug("Test Rule with sub path " + subpath + ": onUpdate called with value " + (value!=null ? value : "Null"));
6363
}
6464

6565
public void close() {
6666
if (!isClosed() && getStream() != null) {
67-
node.info("Test Rule with sub path " + subpath + ": closing Stream");
67+
node.debug("Test Rule with sub path " + subpath + ": closing Stream");
6868
getStream().closeStream();
6969
}
7070
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public enum AUTH_SCHEME {
2424
NO_AUTH,
2525
BASIC_USR_PASS,
2626
OAUTH2_CLIENT,
27-
OAUTH2_USR_PASS
27+
OAUTH2_USR_PASS,
28+
BEARER
2829
}
2930

3031
public static Object dsElementToObject(DSElement element) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ private OkHttpClient configureAuthorization() {
124124
clientBuilder.addInterceptor(new BasicAuthInterceptor(credentials.getUsername(),
125125
credentials.getPassword()));
126126
break;
127+
case BEARER:
128+
clientBuilder.addInterceptor(new BearerAuthInterceptor(credentials.getToken()));
129+
break;
127130
case OAUTH2_CLIENT:
128131
case OAUTH2_USR_PASS:
129132
clientBuilder.addInterceptor(new OAuthInterceptor(this));

0 commit comments

Comments
 (0)