Skip to content

Commit 627364b

Browse files
authored
0.43.0
- Handle redirects between http and https. - Make connection constants available to subclasses.
1 parent 0e9f2d9 commit 627364b

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
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.1'
8+
version '0.43.0'
99

1010
sourceCompatibility = 1.6
1111
targetCompatibility = 1.6

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);

dslink-v2/src/main/java/org/iot/dsa/conn/DSBaseConnection.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public abstract class DSBaseConnection extends DSNode implements DSIStatus {
2424
// Class Fields
2525
///////////////////////////////////////////////////////////////////////////
2626

27-
static final String LAST_OK = "Last Ok";
28-
static final String LAST_FAIL = "Last Fail";
29-
static final String STATUS = "Status";
30-
static final String STATUS_TEXT = "Status Text";
27+
protected static final String LAST_OK = "Last Ok";
28+
protected static final String LAST_FAIL = "Last Fail";
29+
protected static final String STATUS = "Status";
30+
protected static final String STATUS_TEXT = "Status Text";
3131

3232
///////////////////////////////////////////////////////////////////////////
3333
// Instance Fields

dslink-v2/src/main/java/org/iot/dsa/conn/DSConnection.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public abstract class DSConnection extends DSBaseConnection implements Runnable
3838
// Class Fields
3939
///////////////////////////////////////////////////////////////////////////
4040

41-
static final String STATE = "State";
42-
static final String STATE_TIME = "State Time";
41+
protected static final String STATE = "State";
42+
protected static final String STATE_TIME = "State Time";
4343

4444
/**
4545
* The topic to subscribe to for connected events.
@@ -55,8 +55,8 @@ public abstract class DSConnection extends DSBaseConnection implements Runnable
5555
// Instance Fields
5656
///////////////////////////////////////////////////////////////////////////
5757

58-
private DSInfo state = getInfo(STATE);
59-
private DSInfo stateTime = getInfo(STATE_TIME);
58+
protected DSInfo state = getInfo(STATE);
59+
protected DSInfo stateTime = getInfo(STATE_TIME);
6060

6161
///////////////////////////////////////////////////////////////////////////
6262
// Public Methods
@@ -266,7 +266,7 @@ protected void declareDefaults() {
266266
protected abstract void doDisconnect();
267267

268268
/**
269-
* Override point, called by the run method. Implementations should call verify the connection
269+
* Override point, called by the run method. Implementations should verify the connection
270270
* is still valid and call connOk or connDown, but those can be async and after this method
271271
* returns. Throwing an exception will be treated as a connDown. By default, this does
272272
* nothing.
@@ -302,7 +302,7 @@ protected void onConnected() {
302302
protected void onDisconnected() {
303303
}
304304

305-
///////////////////////////////////////////////////////////////////////////
305+
///////////////////////////////////////////////////////////////////////////
306306
// Package / Private Methods
307307
///////////////////////////////////////////////////////////////////////////
308308

0 commit comments

Comments
 (0)