Skip to content

Commit 56ebf6e

Browse files
authored
0.43.0
- DSRuntime now uses nanoTime for it's internal clock. - Handle redirects between http and https. - Make connection constants available to subclasses.
2 parents 47d16b1 + 627364b commit 56ebf6e

File tree

8 files changed

+292
-81
lines changed

8 files changed

+292
-81
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ subprojects {
55
apply plugin: 'maven'
66

77
group 'org.iot-dsa'
8-
version '0.42.0'
8+
version '0.43.0'
99

1010
sourceCompatibility = 1.6
1111
targetCompatibility = 1.6

dslink-v2/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ artifacts {
44
}
55

66
dependencies {
7-
testImplementation 'org.testng:testng:[6.14.3,)'
7+
testImplementation 'org.testng:testng:6.14.3'
88
}
99

1010
test {

dslink-v2/src/main/java/com/acuity/iot/dsa/dslink/protocol/v1/DS1ConnectionInit.java

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,43 @@ protected void onStarted() {
6161
// Package / Private Methods
6262
///////////////////////////////////////////////////////////////////////////
6363

64+
/**
65+
* Opens an http/s connection and handles redirects that switch protocols.
66+
*/
67+
InputStream connect(URL url, int redirects) throws Exception {
68+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
69+
conn.setDoInput(true);
70+
conn.setDoOutput(true);
71+
conn.setInstanceFollowRedirects(true);
72+
conn.setUseCaches(false);
73+
conn.setRequestMethod("POST");
74+
conn.setRequestProperty("Content-Type", "application/json");
75+
//write the request map
76+
writeConnectionRequest(conn);
77+
//read response
78+
int rc = conn.getResponseCode();
79+
debug(debug() ? "Connection initialization response code " + rc : null);
80+
if ((rc >= 300) && (rc <= 307) && (rc != 306) &&
81+
(rc != HttpURLConnection.HTTP_NOT_MODIFIED)) {
82+
conn.disconnect();
83+
if (++redirects > 5) {
84+
throw new IllegalStateException("Too many redirects");
85+
}
86+
String location = conn.getHeaderField("Location");
87+
if (location == null) {
88+
throw new IllegalStateException("Redirect missing location header");
89+
}
90+
url = new URL(url, location);
91+
debug(debug() ? "Following redirect to " + url : null);
92+
return connect(url, redirects);
93+
}
94+
if ((rc < 200) || (rc >= 300)) {
95+
conn.disconnect();
96+
throwConnectionException(conn, rc);
97+
}
98+
return conn.getInputStream();
99+
}
100+
64101
DSLink getLink() {
65102
return connection.getLink();
66103
}
@@ -81,24 +118,9 @@ DSMap getResponse() {
81118
void initializeConnection() throws Exception {
82119
String uri = makeBrokerUrl();
83120
debug(debug() ? "Broker URI " + uri : null);
84-
HttpURLConnection conn = (HttpURLConnection) new URL(uri).openConnection();
85-
conn.setDoInput(true);
86-
conn.setDoOutput(true);
87-
conn.setInstanceFollowRedirects(true);
88-
conn.setUseCaches(false);
89-
conn.setRequestMethod("POST");
90-
conn.setRequestProperty("Content-Type", "application/json");
91-
//write the request map
92-
writeConnectionRequest(conn);
93-
//read response
94-
int rc = conn.getResponseCode();
95-
debug(debug() ? "Connection initialization response code " + rc : null);
96-
if ((rc < 200) || (rc >= 300)) {
97-
throwConnectionException(conn, rc);
98-
}
99121
JsonReader in = null;
100122
try {
101-
in = new JsonReader(conn.getInputStream(), "UTF-8");
123+
in = new JsonReader(connect(new URL(uri), 0), "UTF-8");
102124
response = in.getMap();
103125
put(BROKER_RES, response).setReadOnly(true);
104126
trace(trace() ? response : null);

0 commit comments

Comments
 (0)