Skip to content

Commit 4b47515

Browse files
chrisgarviniampogo
authored andcommitted
sdk/java: replace refresh token with /hello call
Closes #3148 Author: Chris Garvin <chris.garvin@me.com> Date: Mon Apr 2 13:48:02 2018 -0700 upstream:567c92d9e9f26121eadcfbc28d98f5b9ff2bae6c
1 parent 208ceed commit 4b47515

5 files changed

Lines changed: 37 additions & 123 deletions

File tree

src/main/java/com/seq/http/Client.java

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
import java.util.concurrent.TimeUnit;
2424
import java.util.concurrent.atomic.AtomicInteger;
2525

26-
import com.seq.http.session.ProdRefresher;
27-
import com.seq.http.session.Refresher;
26+
import com.google.gson.annotations.SerializedName;
2827
import com.google.gson.Gson;
2928

3029
import com.squareup.okhttp.CertificatePinner;
@@ -47,9 +46,10 @@
4746
*/
4847
public class Client {
4948
private OkHttpClient httpClient;
50-
private String macaroon;
49+
private String credential;
5150
private String ledgerName;
52-
public Refresher refresher;
51+
private String teamName;
52+
private Boolean teamNameRequested;
5353

5454
// Used to create empty, in-memory key stores.
5555
private static final char[] DEFAULT_KEYSTORE_PASSWORD = "password".toCharArray();
@@ -60,6 +60,15 @@ private static class BuildProperties {
6060
public String version;
6161
}
6262

63+
private static class TeamResponse {
64+
@SerializedName("team_name")
65+
public String teamName;
66+
67+
public TeamResponse() {
68+
this.teamName = null;
69+
}
70+
}
71+
6372
static {
6473
InputStream in = Client.class.getClassLoader().getResourceAsStream("properties.json");
6574
if (in != null) {
@@ -76,9 +85,17 @@ public Client(Builder builder) throws ConfigurationException {
7685
throw new ConfigurationException("No credential provided");
7786
}
7887
this.ledgerName = builder.ledger;
79-
this.macaroon = builder.credential;
88+
this.credential = builder.credential;
8089
this.httpClient = buildHttpClient(builder);
81-
this.refresher = new ProdRefresher();
90+
this.teamName = null;
91+
this.teamNameRequested = false;
92+
}
93+
94+
public void getTeamName(String credential) throws ChainException {
95+
this.teamNameRequested = true;
96+
TeamResponse teamResp = new TeamResponse();
97+
teamResp = request("hello", new Object(), TeamResponse.class);
98+
this.teamName = teamResp.teamName;
8299
}
83100

84101
/**
@@ -98,18 +115,18 @@ public T create(Response response, Gson deserializer) throws IOException {
98115
}
99116
};
100117

101-
if (this.refresher.needsRefresh()) {
102-
this.refresher.refresh(this.macaroon);
118+
if(this.teamNameRequested == false && this.teamName == null) {
119+
getTeamName(this.credential);
103120
}
104121
return post(action, body, rc);
105122
}
106123

107124
/**
108-
* Returns the macaroon (possibly null).
109-
* @return the macaroon
125+
* Returns the credential (possibly null).
126+
* @return the credential
110127
*/
111-
public String macaroon() {
112-
return macaroon;
128+
public String credential() {
129+
return credential;
113130
}
114131

115132
/**
@@ -188,8 +205,10 @@ private <T> T post(String path, Object body, ResponseCreator<T> respCreator)
188205
if (urlParts.endsWith("/")) {
189206
urlParts = urlParts.substring(0, urlParts.length() - 1);
190207
}
191-
urlParts += "/" + this.refresher.teamName();
192-
urlParts += "/" + this.ledgerName;
208+
if (this.teamName != null) {
209+
urlParts += "/" + this.teamName;
210+
urlParts += "/" + this.ledgerName;
211+
}
193212
urlParts += "/" + path;
194213
endpointURL = new URL(urlParts);
195214
} catch (MalformedURLException ex) {
@@ -199,8 +218,7 @@ private <T> T post(String path, Object body, ResponseCreator<T> respCreator)
199218
Request req =
200219
new Request.Builder()
201220
.header("User-Agent", "sequence-sdk-java/" + version)
202-
.header("Macaroon", this.macaroon)
203-
.header("Discharge-Macaroon", this.refresher.dischargeMacaroon())
221+
.header("Credential", this.credential)
204222
.header("Idempotency-Key", idempotencyKey)
205223
.header("Name-Set", "camel|snake")
206224
.url(endpointURL)
@@ -353,8 +371,8 @@ private Response checkError(Response response) throws ChainException {
353371
@Override
354372
public int hashCode() {
355373
int code = 0;
356-
if (this.macaroon != null) {
357-
code = code * 31 + this.macaroon.hashCode();
374+
if (this.credential != null) {
375+
code = code * 31 + this.credential.hashCode();
358376
}
359377
return code;
360378
}
@@ -370,7 +388,7 @@ public boolean equals(Object o) {
370388
if (!(o instanceof Client)) return false;
371389

372390
Client other = (Client) o;
373-
return Objects.equals(this.macaroon, other.macaroon);
391+
return Objects.equals(this.credential, other.credential);
374392
}
375393

376394
/**

src/main/java/com/seq/http/session/ProdRefresher.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/main/java/com/seq/http/session/Refresher.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/main/java/com/seq/http/session/TestRefresher.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.seq;
22

33
import com.seq.http.Client;
4-
import com.seq.http.session.TestRefresher;
54

65
import java.io.IOException;
76
import java.io.ByteArrayInputStream;
@@ -14,23 +13,17 @@ public class TestUtils {
1413

1514
public static Client generateClient() throws Exception {
1615
String ledgerName = System.getenv("LEDGER_NAME");
17-
String teamName = System.getenv("TEAM_NAME");
1816
String credential = System.getenv("SEQCRED");
1917

2018
if (ledgerName == null || ledgerName.isEmpty()) {
2119
ledgerName = "test";
2220
}
2321

24-
if (teamName == null || teamName.isEmpty()) {
25-
teamName = "team";
26-
}
27-
2822
Client client =
2923
new Client.Builder()
3024
.setLedgerName(ledgerName)
3125
.setCredential(credential)
3226
.build();
33-
client.refresher = new TestRefresher(teamName, "");
3427
return client;
3528
}
3629
}

0 commit comments

Comments
 (0)