Skip to content

Commit 27ca248

Browse files
Merge pull request #154 from skyflowapi/SK-1829-fixes-for-java-sdk-v2
SK-1829 Fix identified issues in Java SDK v2
2 parents 32545bd + 73b5e22 commit 27ca248

File tree

8 files changed

+74
-68
lines changed

8 files changed

+74
-68
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.skyflow.errors;
2+
3+
public enum HttpStatus {
4+
BAD_REQUEST("Bad Request");
5+
6+
private final String httpStatus;
7+
8+
HttpStatus(String httpStatus) {
9+
this.httpStatus = httpStatus;
10+
}
11+
12+
public String getHttpStatus() {
13+
return httpStatus;
14+
}
15+
}

src/main/java/com/skyflow/errors/SkyflowException.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package com.skyflow.errors;
22

3-
import com.google.gson.JsonArray;
4-
import com.google.gson.JsonObject;
5-
import com.google.gson.JsonParser;
6-
import com.google.gson.JsonSyntaxException;
3+
import com.google.gson.*;
74

85
import java.util.List;
96
import java.util.Map;
10-
import java.util.Objects;
117

128
public class SkyflowException extends Exception {
139
private String requestId;
@@ -37,28 +33,27 @@ public SkyflowException(int code, String message) {
3733
super(message);
3834
this.httpCode = code;
3935
this.message = message;
36+
this.httpStatus = HttpStatus.BAD_REQUEST.getHttpStatus();
37+
this.details = new JsonArray();
4038
}
4139

4240
public SkyflowException(int httpCode, Throwable cause, Map<String, List<String>> responseHeaders, String responseBody) {
43-
this(cause);
41+
super(cause);
4442
this.httpCode = httpCode;
45-
String contentType = responseHeaders.get("content-type").get(0);
4643
setRequestId(responseHeaders);
47-
if (Objects.equals(contentType, "application/json")) {
48-
setResponseBody(responseBody);
49-
} else if (Objects.equals(contentType, "text/plain")) {
50-
this.message = responseBody;
51-
}
44+
setResponseBody(responseBody);
5245
}
5346

5447
private void setResponseBody(String responseBody) {
5548
try {
5649
if (responseBody != null) {
5750
this.responseBody = JsonParser.parseString(responseBody).getAsJsonObject();
58-
setGrpcCode();
59-
setHttpStatus();
60-
setMessage();
61-
setDetails();
51+
if (this.responseBody.get("error") != null) {
52+
setGrpcCode();
53+
setHttpStatus();
54+
setMessage();
55+
setDetails();
56+
}
6257
}
6358
} catch (JsonSyntaxException e) {
6459
throw new RuntimeException(e);
@@ -72,24 +67,28 @@ public String getRequestId() {
7267
private void setRequestId(Map<String, List<String>> responseHeaders) {
7368
if (responseHeaders != null) {
7469
List<String> ids = responseHeaders.get("x-request-id");
75-
this.requestId = ids.get(0);
70+
this.requestId = ids == null ? null : ids.get(0);
7671
}
7772
}
7873

7974
private void setMessage() {
80-
this.message = ((JsonObject) responseBody.get("error")).get("message").getAsString();
75+
JsonElement messageElement = ((JsonObject) responseBody.get("error")).get("message");
76+
this.message = messageElement == null ? null : messageElement.getAsString();
8177
}
8278

8379
private void setGrpcCode() {
84-
this.grpcCode = ((JsonObject) responseBody.get("error")).get("grpc_code").getAsInt();
80+
JsonElement grpcElement = ((JsonObject) responseBody.get("error")).get("grpc_code");
81+
this.grpcCode = grpcElement == null ? null : grpcElement.getAsInt();
8582
}
8683

8784
private void setHttpStatus() {
88-
this.httpStatus = ((JsonObject) responseBody.get("error")).get("http_status").getAsString();
85+
JsonElement statusElement = ((JsonObject) responseBody.get("error")).get("http_status");
86+
this.httpStatus = statusElement == null ? null : statusElement.getAsString();
8987
}
9088

9189
private void setDetails() {
92-
this.details = ((JsonObject) responseBody.get("error")).get("details").getAsJsonArray();
90+
JsonElement detailsElement = ((JsonObject) responseBody.get("error")).get("details");
91+
this.details = detailsElement == null ? null : detailsElement.getAsJsonArray();
9392
}
9493

9594
public int getHttpCode() {

src/main/java/com/skyflow/utils/HttpUtility.java

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package com.skyflow.utils;
22

3+
import com.google.gson.JsonElement;
34
import com.google.gson.JsonObject;
4-
import com.google.gson.JsonParser;
55
import com.skyflow.errors.SkyflowException;
66

77
import java.io.*;
88
import java.net.HttpURLConnection;
99
import java.net.URL;
1010
import java.nio.charset.StandardCharsets;
1111
import java.util.HashMap;
12+
import java.util.List;
1213
import java.util.Map;
14+
import java.util.Objects;
1315

1416
public final class HttpUtility {
1517

@@ -33,18 +35,18 @@ public static String sendRequest(String method, URL url, JsonObject params, Map<
3335
connection.setRequestProperty("content-type", "application/json");
3436
connection.setRequestProperty("Accept", "*/*");
3537

36-
if (headers != null && headers.size() > 0) {
38+
if (headers != null && !headers.isEmpty()) {
3739
for (Map.Entry<String, String> entry : headers.entrySet())
3840
connection.setRequestProperty(entry.getKey(), entry.getValue());
3941

4042
// append dynamic boundary if content-type is multipart/form-data
4143
if (headers.containsKey("content-type")) {
42-
if (headers.get("content-type") == "multipart/form-data") {
44+
if (Objects.equals(headers.get("content-type"), "multipart/form-data")) {
4345
connection.setRequestProperty("content-type", "multipart/form-data; boundary=" + boundary);
4446
}
4547
}
4648
}
47-
if (params != null && params.size() > 0) {
49+
if (params != null && !params.isEmpty()) {
4850
connection.setDoOutput(true);
4951
try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
5052
byte[] input = null;
@@ -63,12 +65,12 @@ public static String sendRequest(String method, URL url, JsonObject params, Map<
6365
}
6466
}
6567

66-
int status = connection.getResponseCode();
68+
int httpCode = connection.getResponseCode();
6769
String requestID = connection.getHeaderField("x-request-id");
6870
HttpUtility.requestID = requestID.split(",")[0];
69-
71+
Map<String, List<String>> responseHeaders = connection.getHeaderFields();
7072
Reader streamReader;
71-
if (status > 299) {
73+
if (httpCode > 299) {
7274
if (connection.getErrorStream() != null)
7375
streamReader = new InputStreamReader(connection.getErrorStream());
7476
else {
@@ -86,9 +88,8 @@ public static String sendRequest(String method, URL url, JsonObject params, Map<
8688
response.append(inputLine);
8789
}
8890

89-
if (status > 299) {
90-
String errorMsg = appendRequestIdToErrorObj(status, response.toString(), requestID);
91-
throw new SkyflowException(errorMsg);
91+
if (httpCode > 299) {
92+
throw new SkyflowException(httpCode, new Throwable(), responseHeaders, response.toString());
9293
}
9394
} finally {
9495
if (in != null) {
@@ -127,13 +128,14 @@ public static String formatJsonToMultiPartFormDataString(JsonObject requestBody,
127128

128129
private static HashMap<String, String> convertJsonToMap(JsonObject json, String rootKey) {
129130
HashMap<String, String> currentMap = new HashMap<>();
130-
for (Object key : json.keySet()) {
131-
Object currentValue = json.get((String) key);
132-
String currentKey = rootKey.length() != 0 ? rootKey + '[' + key.toString() + ']' : rootKey + key.toString();
133-
if (currentValue instanceof JsonObject) {
131+
Map<String, JsonElement> jsonMap = json.asMap();
132+
for (String key : jsonMap.keySet()) {
133+
JsonElement currentValue = jsonMap.get(key);
134+
String currentKey = !rootKey.isEmpty() ? rootKey + '[' + key + ']' : rootKey + key;
135+
if (currentValue.isJsonObject()) {
134136
currentMap.putAll(convertJsonToMap((JsonObject) currentValue, currentKey));
135137
} else {
136-
currentMap.put(currentKey, currentValue.toString());
138+
currentMap.put(currentKey, currentValue.getAsString());
137139
}
138140
}
139141
return currentMap;
@@ -156,22 +158,6 @@ public static String appendRequestId(String message, String requestId) {
156158
return message;
157159
}
158160

159-
public static String appendRequestIdToErrorObj(int status, String error, String requestId) throws SkyflowException {
160-
if (requestId != null && !requestId.isEmpty()) {
161-
JsonObject errorObject = (JsonObject) new JsonParser().parse(error);
162-
JsonObject tempError = (JsonObject) errorObject.get("error");
163-
if (tempError != null) {
164-
String message = String.valueOf(tempError.get("message"));
165-
message = message + " - requestId: " + requestId;
166-
167-
tempError.addProperty("message", message);
168-
errorObject.add("error", tempError);
169-
}
170-
error = errorObject.toString();
171-
}
172-
return error;
173-
}
174-
175161
private static String makeFormEncodeKeyValuePair(String key, String value) {
176162
return key + "=" + value + "&";
177163
}

src/main/java/com/skyflow/vault/connection/InvokeConnectionResponse.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
5-
import com.google.gson.JsonObject;
5+
6+
import java.util.HashMap;
67

78
public class InvokeConnectionResponse {
8-
private final JsonObject data;
9-
private final JsonObject metadata;
9+
private final Object data;
10+
private final HashMap<String, String> metadata;
1011

11-
public InvokeConnectionResponse(JsonObject data, JsonObject metadata) {
12+
public InvokeConnectionResponse(Object data, HashMap<String, String> metadata) {
1213
this.data = data;
1314
this.metadata = metadata;
1415
}
1516

16-
public JsonObject getData() {
17+
public Object getData() {
1718
return data;
1819
}
1920

20-
public JsonObject getMetadata() {
21+
public HashMap<String, String> getMetadata() {
2122
return metadata;
2223
}
2324

src/main/java/com/skyflow/vault/controller/ConnectionController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import java.util.HashMap;
2727
import java.util.Map;
2828

29-
public class ConnectionController extends ConnectionClient {
29+
public final class ConnectionController extends ConnectionClient {
3030
public ConnectionController(ConnectionConfig connectionConfig, Credentials credentials) {
3131
super(connectionConfig, credentials);
3232
}
@@ -65,8 +65,8 @@ public InvokeConnectionResponse invoke(InvokeConnectionRequest invokeConnectionR
6565

6666
String response = HttpUtility.sendRequest(requestMethod.name(), new URL(filledURL), requestBody, headers);
6767
JsonObject data = JsonParser.parseString(response).getAsJsonObject();
68-
JsonObject metadata = new JsonObject();
69-
metadata.addProperty("requestId", HttpUtility.getRequestID());
68+
HashMap<String, String> metadata = new HashMap<>();
69+
metadata.put("requestId", HttpUtility.getRequestID());
7070
connectionResponse = new InvokeConnectionResponse(data, metadata);
7171
LogUtil.printInfoLog(InfoLogs.INVOKE_CONNECTION_REQUEST_RESOLVED.getLog());
7272
} catch (IOException e) {

src/test/java/com/skyflow/utils/HttpUtilityTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class HttpUtilityTests {
4343
@Before
4444
public void setup() throws IOException {
4545
expected = "{\"status\":\"success\"}";
46-
expectedError = "{\"status\":\"something went wrong\"}";
46+
expectedError = "{\"error\":{\"grpc_code\":123,\"http_code\":500,\"message\":\"something went wrong\",\"http_status\":\"internal server error\",\"details\":[]}}\n";
4747
mockConnection = Mockito.mock(HttpURLConnection.class);
4848
given(mockConnection.getInputStream()).willReturn(new ByteArrayInputStream(expected.getBytes()));
4949
given(mockConnection.getErrorStream()).willReturn(new ByteArrayInputStream(expectedError.getBytes()));
@@ -115,7 +115,11 @@ public void testSendRequestError() {
115115
given(mockConnection.getResponseCode()).willReturn(500);
116116
String response = httpUtility.sendRequest("GET", url, null, null);
117117
} catch (SkyflowException e) {
118-
Assert.assertEquals(expectedError, e.getMessage());
118+
Assert.assertEquals(500, e.getHttpCode());
119+
Assert.assertEquals(new Integer(123), e.getGrpcCode());
120+
Assert.assertEquals("internal server error", e.getHttpStatus());
121+
Assert.assertEquals("something went wrong", e.getMessage());
122+
Assert.assertTrue(e.getDetails().isEmpty());
119123
} catch (Exception e) {
120124
fail(INVALID_EXCEPTION_THROWN);
121125
}

src/test/java/com/skyflow/vault/connection/InvokeConnectionTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,12 @@ public void testInvokeConnectionResponse() {
423423
JsonObject data = new JsonObject();
424424
data.addProperty("test_key_1", "test_value_1");
425425
data.addProperty("test_key_2", "test_value_2");
426-
JsonObject metadata = new JsonObject();
427-
metadata.addProperty("requestId", "12345");
426+
HashMap<String, String> metadata = new HashMap<>();
427+
metadata.put("requestId", "12345");
428428
InvokeConnectionResponse connectionResponse = new InvokeConnectionResponse(data, metadata);
429429
String responseString = "{\"data\":{\"test_key_1\":\"test_value_1\",\"test_key_2\":\"test_value_2\"}," +
430430
"\"metadata\":{\"requestId\":\"12345\"}}";
431-
Assert.assertEquals(2, connectionResponse.getData().size());
431+
Assert.assertNotNull(connectionResponse.getData());
432432
Assert.assertEquals(responseString, connectionResponse.toString());
433433
Assert.assertEquals(1, connectionResponse.getMetadata().size());
434434
} catch (Exception e) {

src/test/java/com/skyflow/vault/controller/VaultControllerTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.skyflow.enums.LogLevel;
88
import com.skyflow.errors.ErrorCode;
99
import com.skyflow.errors.ErrorMessage;
10+
import com.skyflow.errors.HttpStatus;
1011
import com.skyflow.errors.SkyflowException;
1112
import com.skyflow.generated.rest.ApiClient;
1213
import com.skyflow.generated.rest.api.TokensApi;
@@ -155,8 +156,8 @@ public void testInvalidRequestInTokenizeMethod() {
155156
);
156157
Assert.assertNull(e.getRequestId());
157158
Assert.assertNull(e.getGrpcCode());
158-
Assert.assertNull(e.getHttpStatus());
159-
Assert.assertNull(e.getDetails());
159+
Assert.assertTrue(e.getDetails().isEmpty());
160+
Assert.assertEquals(HttpStatus.BAD_REQUEST.getHttpStatus(), e.getHttpStatus());
160161
}
161162
}
162163

0 commit comments

Comments
 (0)