Skip to content

Commit f5caf78

Browse files
committed
WS-3314: Duplicate a few more headers. Add extra properties to samples.
1 parent c720871 commit f5caf78

File tree

9 files changed

+57
-27
lines changed

9 files changed

+57
-27
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Versions, of the form `x.y.z`, where `z` is greater than or equal to `100`, are
4646
them without consultation with Babel Street.
4747

4848
#### Examples
49-
View small example programs for each Rosette endpoint in the
49+
View small example programs for each Analytics endpoint in the
5050
[examples](examples/src/main/java/com/basistech/rosette/examples) directory.
5151

5252
#### Documentation & Support

all/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</parent>
2424
<artifactId>rosette-api-all</artifactId>
2525
<name>rosette-api-all</name>
26-
<description>Analytics API all modules combined</description>
26+
<description>Babel Street Analytics API all modules combined</description>
2727
<dependencies>
2828
<dependency>
2929
<groupId>com.basistech</groupId>

api/src/main/java/com/basistech/rosette/api/HttpRosetteAPI.java

+28-4
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@
8484
public class HttpRosetteAPI extends AbstractRosetteAPI {
8585

8686
public static final String DEFAULT_URL_BASE = "https://analytics.babelstreet.com/rest/v1";
87-
public static final String SERVICE_NAME = "RosetteAPI";
87+
public static final String SERVICE_NAME = "Babel-Street-Analytics-API";
8888
public static final String BINDING_VERSION = getVersion();
8989
public static final String USER_AGENT_STR = SERVICE_NAME + "-Java/" + BINDING_VERSION + "/"
9090
+ System.getProperty("java.version");
9191
private static final Logger LOG = LoggerFactory.getLogger(HttpRosetteAPI.class);
92-
private static final String IO_EXCEPTION_MESSAGE = "IO Exception communicating with the Rosette API";
92+
private static final String IO_EXCEPTION_MESSAGE = "IO Exception communicating with the Babel Street Analytics API";
9393
private static final Pattern TRAILING_SLASHES = Pattern.compile("/+$");
9494
private String urlBase = DEFAULT_URL_BASE;
9595
private int failureRetries = 1;
@@ -198,6 +198,9 @@ private void initHeaders(String key, List<Header> additionalHeaders) {
198198
this.additionalHeaders.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip"));
199199
if (key != null) {
200200
this.additionalHeaders.add(new BasicHeader("X-BabelStreetAPI-Key", key));
201+
this.additionalHeaders.add(new BasicHeader("X-BabelStreetAPI-Binding", "java"));
202+
this.additionalHeaders.add(new BasicHeader("X-BabelStreetAPI-Binding-Version", BINDING_VERSION));
203+
// TODO: Remove in a future release.
201204
this.additionalHeaders.add(new BasicHeader("X-RosetteAPI-Binding", "java"));
202205
this.additionalHeaders.add(new BasicHeader("X-RosetteAPI-Binding-Version", BINDING_VERSION));
203206
}
@@ -401,10 +404,15 @@ private <T> T sendPostRequest(Object request, String urlStr, Class<T> clazz)
401404
while (numRetries-- > 0) {
402405
try (CloseableHttpResponse response = httpClient.execute(post)) {
403406
T resp = getResponse(response, clazz);
407+
// TODO: Remove in a future release
404408
Header ridHeader = response.getFirstHeader("X-RosetteAPI-DocumentRequest-Id");
405409
if (ridHeader != null && ridHeader.getValue() != null) {
406410
LOG.debug("DocumentRequest ID {}", ridHeader.getValue());
407411
}
412+
Header bsidHeader = response.getFirstHeader("X-BabelStreetAPI-DocumentRequest-Id");
413+
if (bsidHeader != null && bsidHeader.getValue() != null) {
414+
LOG.debug("DocumentRequest ID {}", bsidHeader.getValue());
415+
}
408416
if (resp instanceof Response) {
409417
responseHeadersToExtendedInformation((Response)resp, response);
410418
}
@@ -556,15 +564,31 @@ private <T extends Object> T getResponse(HttpResponse httpResponse, Class<T> cla
556564
InputStream stream = httpResponse.getEntity().getContent();
557565
InputStream inputStream = "gzip".equalsIgnoreCase(encoding) ? new GZIPInputStream(stream) : stream) {
558566
String ridHeader = headerValueOrNull(httpResponse.getFirstHeader("X-RosetteAPI-DocumentRequest-Id"));
567+
String bsidHeader = headerValueOrNull(httpResponse.getFirstHeader("X-BabelStreetAPI-DocumentRequest-Id"));
559568
if (HTTP_OK != status) {
560-
String ecHeader = headerValueOrNull(httpResponse.getFirstHeader("X-RosetteAPI-Status-Code"));
561-
String emHeader = headerValueOrNull(httpResponse.getFirstHeader("X-RosetteAPI-Status-Message"));
569+
String ecHeader;
570+
if (headerValueOrNull(httpResponse.getFirstHeader("X-BabelStreetAPI-Status-Code")) != null) {
571+
ecHeader = headerValueOrNull(httpResponse.getFirstHeader("X-BabelStreetAPI-Status-Code"));
572+
} else {
573+
// TODO: Remove in a future release
574+
ecHeader = headerValueOrNull(httpResponse.getFirstHeader("X-RosetteAPI-Status-Code"));
575+
}
576+
String emHeader;
577+
if (headerValueOrNull(httpResponse.getFirstHeader("X-BabelStreetAPI-Status-Message")) != null) {
578+
emHeader = headerValueOrNull(httpResponse.getFirstHeader("X-BabelStreetAPI-Status-Message"));
579+
} else {
580+
// TODO: Remove in a future release
581+
emHeader = headerValueOrNull(httpResponse.getFirstHeader("X-RosetteAPI-Status-Message"));
582+
}
562583
String responseContentType = headerValueOrNull(httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE));
563584
if ("application/json".equals(responseContentType)) {
564585
ErrorResponse errorResponse = mapper.readValue(inputStream, ErrorResponse.class);
565586
if (ridHeader != null) {
566587
LOG.debug("DocumentRequest ID {}", ridHeader);
567588
}
589+
if (bsidHeader != null) {
590+
LOG.debug("DocumentRequest ID {}", bsidHeader);
591+
}
568592
if (ecHeader != null) {
569593
errorResponse.setCode(ecHeader);
570594
}

api/src/test/java/com/basistech/rosette/api/BasicTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Basis Technology Corp.
2+
* Copyright 2024 Basis Technology Corp.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -138,7 +138,7 @@ void testHeaders() throws Exception {
138138
.withHeader(HttpHeaders.USER_AGENT, HttpRosetteAPI.USER_AGENT_STR))
139139
.respond(HttpResponse.response()
140140
.withHeader("Content-Type", "application/json")
141-
.withHeader("X-RosetteAPI-Concurrency", "5")
141+
.withHeader("X-BabelStreetAPI-Concurrency", "5")
142142
.withStatusCode(200)
143143
.withBody("{\"message\":\"Rosette API at your service\",\"time\":1461788498633}", StandardCharsets.UTF_8));
144144

@@ -148,7 +148,7 @@ void testHeaders() throws Exception {
148148
.additionalHeader("X-Foo", "Bar")
149149
.build();
150150
var resp = api.ping();
151-
assertEquals("5", resp.getExtendedInformation().get("X-RosetteAPI-Concurrency"));
151+
assertEquals("5", resp.getExtendedInformation().get("X-BabelStreetAPI-Concurrency"));
152152
}
153153

154154
@Test
@@ -185,7 +185,7 @@ void testExtendedInfo() throws Exception {
185185
.withHeader("Content-Type", "application/json")
186186
.withHeader("X-Foo", "Bar")
187187
.withHeader("X-FooMulti", "Bar1", "Bar2")
188-
.withHeader("X-RosetteAPI-Concurrency", "5")
188+
.withHeader("X-BabelStreetAPI-Concurrency", "5")
189189
.withBody("{\"message\":\"Rosette API at your service\",\"time\":1461788498633}", StandardCharsets.UTF_8));
190190
api = new HttpRosetteAPI.Builder()
191191
.key("foo-key")

api/src/test/java/com/basistech/rosette/api/InvalidErrorTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Basis Technology Corp.
2+
* Copyright 2024 Basis Technology Corp.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,7 +50,7 @@ void notJsonError() throws Exception {
5050
mockServer.when(HttpRequest.request().withPath(".*/{2,}.*"))
5151
.respond(HttpResponse.response()
5252
.withBody("Invalid path; '//'")
53-
.withHeader("X-RosetteAPI-Concurrency", "5")
53+
.withHeader("X-BabelStreetAPI-Concurrency", "5")
5454
.withStatusCode(HTTP_NOT_FOUND));
5555
mockServer.when(HttpRequest.request()
5656
.withMethod("GET")
@@ -59,7 +59,7 @@ void notJsonError() throws Exception {
5959
.respond(HttpResponse.response()
6060
.withBody("{\"message\":\"Rosette API at your service\",\"time\":1461788498633}", StandardCharsets.UTF_8)
6161
.withStatusCode(HTTP_OK)
62-
.withHeader("X-RosetteAPI-Concurrency", "5"));
62+
.withHeader("X-BabelStreetAPI-Concurrency", "5"));
6363
String mockServiceUrl = "http://localhost:" + mockServer.getPort() + "/rest//v1";
6464
boolean exceptional = false;
6565
try {

api/src/test/java/com/basistech/rosette/api/RosetteAPITest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 Basis Technology Corp.
2+
* Copyright 2024 Basis Technology Corp.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -100,14 +100,14 @@ public void setUp(MockServerClient mockServer) {
100100
.withBody("{\"message\":\"Rosette API at your service\",\"time\":1461788498633}",
101101
StandardCharsets.UTF_8)
102102
.withStatusCode(HTTP_OK)
103-
.withHeader("X-RosetteAPI-Concurrency", "5"));
103+
.withHeader("X-BabelStreetAPI-Concurrency", "5"));
104104

105105
this.mockServer.when(HttpRequest.request()
106106
.withPath("/info"))
107107
.respond(HttpResponse.response()
108108
.withStatusCode(HTTP_OK)
109109
.withHeader("Content-Type", "application/json")
110-
.withHeader("X-RosetteAPI-Concurrency", "5")
110+
.withHeader("X-BabelStreetAPI-Concurrency", "5")
111111
.withBody(INFO_RESPONSE, StandardCharsets.UTF_8));
112112

113113
api = new HttpRosetteAPI.Builder()
@@ -145,14 +145,14 @@ private void setStatusCodeResponse(String responseStr, int statusCode) throws IO
145145
.respond(HttpResponse.response()
146146
.withHeader("Content-Type", "application/json")
147147
.withHeader("Content-Encoding", "gzip")
148-
.withHeader("X-RosetteAPI-Concurrency", "5")
148+
.withHeader("X-BabelStreetAPI-Concurrency", "5")
149149
.withStatusCode(statusCode).withBody(gzip(responseStr)));
150150

151151
} else {
152152
mockServer.when(HttpRequest.request().withPath("^(?!/info).+"))
153153
.respond(HttpResponse.response()
154154
.withHeader("Content-Type", "application/json")
155-
.withHeader("X-RosetteAPI-Concurrency", "5")
155+
.withHeader("X-BabelStreetAPI-Concurrency", "5")
156156
.withStatusCode(statusCode).withBody(responseStr, StandardCharsets.UTF_8));
157157
}
158158

api/src/test/java/com/basistech/rosette/api/RosetteRequestTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 Basis Technology Corp.
2+
* Copyright 2024 Basis Technology Corp.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,7 +61,7 @@ private void setupResponse(String requestPath, String responseString, int status
6161
this.mockServer.when(HttpRequest.request().withPath(requestPath), Times.exactly(requestTimes))
6262
.respond(HttpResponse.response()
6363
.withHeader("Content-Type", "application/json")
64-
.withHeader("X-RosetteAPI-Concurrency", "5")
64+
.withHeader("X-BabelStreetAPI-Concurrency", "5")
6565
.withStatusCode(statusCode)
6666
.withBody(responseString)
6767
.withDelay(TimeUnit.MILLISECONDS, delayMillis));

api/src/test/mock-data/mockgen.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def capture(req, endpoints, filename_prefix):
5050

5151
headers = {"Content-Type": "application/json"}
5252
if args.api_key:
53-
headers['X-RosetteAPI-Key'] = args.api_key
53+
headers['X-BabelStreetAPI-Key'] = args.api_key
5454

5555
# prep & clean up
5656
for folder in ["request", "response"]:

examples/src/main/java/com/basistech/rosette/examples/ExampleBase.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,21 @@
2727
*/
2828
@SuppressWarnings("java:S106")
2929
public abstract class ExampleBase {
30-
private static final String KEY_PROP_NAME = "rosette.api.key";
31-
private static final String URL_PROP_NAME = "rosette.api.altUrl";
30+
private static final String KEY_PROP_NAME = "analytics.api.key";
31+
private static final String URL_PROP_NAME = "analytics.api.altUrl";
32+
private static final String LEGACY_KEY_PROP_NAME = "rosette.api.key";
33+
private static final String LEGACY_URL_PROP_NAME = "rosette.api.altUrl";
3234
private static final String USAGE_STR = "Usage: java -cp rosette-api-examples.jar:lib/rosette-api-manifest.jar "
3335
+ "-D" + KEY_PROP_NAME + "=<required_api_key> " + "-D" + URL_PROP_NAME + "=<optional_alternate_url> ";
3436

3537
/**
3638
* @return api key using system property {@value com.basistech.rosette.examples.ExampleBase#KEY_PROP_NAME}
3739
*/
3840
protected String getApiKeyFromSystemProperty() {
39-
String apiKeyStr = System.getProperty(KEY_PROP_NAME);
40-
if (apiKeyStr == null || apiKeyStr.trim().length() < 1) {
41+
String apiKeyStr = System.getProperty(KEY_PROP_NAME) != null
42+
? System.getProperty(KEY_PROP_NAME)
43+
: System.getProperty(LEGACY_KEY_PROP_NAME);
44+
if (apiKeyStr == null || apiKeyStr.trim().isEmpty()) {
4145
showUsage(getClass());
4246
System.exit(1);
4347
}
@@ -48,8 +52,10 @@ protected String getApiKeyFromSystemProperty() {
4852
* @return alternate url using system property {@value com.basistech.rosette.examples.ExampleBase#URL_PROP_NAME}
4953
*/
5054
protected String getAltUrlFromSystemProperty() {
51-
String altUrlStr = System.getProperty(URL_PROP_NAME);
52-
if (altUrlStr == null || altUrlStr.trim().length() < 1) {
55+
String altUrlStr = System.getProperty(URL_PROP_NAME) != null
56+
? System.getProperty(URL_PROP_NAME)
57+
: System.getProperty(LEGACY_URL_PROP_NAME);
58+
if (altUrlStr == null || altUrlStr.trim().isEmpty()) {
5359
altUrlStr = "https://analytics.babelstreet.com/rest/v1";
5460
}
5561
return altUrlStr.trim();

0 commit comments

Comments
 (0)