Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions src/main/java/com/skyflow/errors/SkyflowException.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,20 @@ private void setRequestId(Map<String, List<String>> responseHeaders) {
}

private void setMessage() {
JsonElement messageElement = ((JsonObject) responseBody.get("error")).get("message");
JsonObject errorObj = ((JsonObject) responseBody.get("error"));
JsonElement messageElement = errorObj.has("message") ? errorObj.get("message") : null;
this.message = messageElement == null ? null : messageElement.getAsString();
}

private void setGrpcCode() {
JsonElement grpcElement = ((JsonObject) responseBody.get("error")).get("grpc_code");
JsonObject errorObj = ((JsonObject) responseBody.get("error"));
JsonElement grpcElement = errorObj.has("grpc_code") ? errorObj.get("grpc_code") : errorObj.has("grpcCode") ? errorObj.get("grpcCode") : null;
this.grpcCode = grpcElement == null ? null : grpcElement.getAsInt();
}

private void setHttpStatus() {
JsonElement statusElement = ((JsonObject) responseBody.get("error")).get("http_status");
JsonObject errorObj = ((JsonObject) responseBody.get("error"));
JsonElement statusElement = errorObj.has("http_status") ? errorObj.get("http_status") : errorObj.has("httpStatus") ? errorObj.get("httpStatus") : null;
this.httpStatus = statusElement == null ? null : statusElement.getAsString();
}

Expand All @@ -98,11 +101,38 @@ public JsonArray getDetails() {
}

private void setDetails(Map<String, List<String>> responseHeaders) {
JsonElement detailsElement = ((JsonObject) responseBody.get("error")).get("details");
JsonObject errorObj = ((JsonObject) responseBody.get("error"));
JsonElement detailsElement = errorObj.has("details") ? errorObj.get("details") : null;
List<String> errorFromClientHeader = responseHeaders.get(Constants.ERROR_FROM_CLIENT_HEADER_KEY);

if (detailsElement != null) {
this.details = detailsElement.getAsJsonArray();
if (detailsElement.isJsonArray()) {
this.details = detailsElement.getAsJsonArray();
if (this.details.isEmpty()) this.details = new JsonArray();
} else if (detailsElement.isJsonObject()) {
JsonObject obj = detailsElement.getAsJsonObject();
boolean allEmpty = true;
for (String key : obj.keySet()) {
if (obj.get(key).isJsonArray() && !obj.get(key).getAsJsonArray().isEmpty()) {
allEmpty = false;
break;
} else if (!obj.get(key).isJsonArray() && !obj.get(key).isJsonNull()) {
allEmpty = false;
break;
}
}
if (obj.isEmpty() || allEmpty) {
this.details = new JsonArray();
} else {
JsonArray arr = new JsonArray();
arr.add(obj);
this.details = arr;
}
}
} else {
this.details = new JsonArray();
}

if (errorFromClientHeader != null) {
this.details = this.details == null ? new JsonArray() : this.details;
String errorFromClient = errorFromClientHeader.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public String getSignedToken() {

@Override
public String toString() {
Gson gson = new Gson();
Gson gson = new Gson().newBuilder().serializeNulls().create();
return gson.toJson(this);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/skyflow/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public final class Constants {
public static final String SDK_METRICS_HEADER_KEY = "sky-metadata";
public static final String REQUEST_ID_HEADER_KEY = "x-request-id";
public static final String PROCESSED_FILE_NAME_PREFIX = "processed-";
public static final String ERROR_FROM_CLIENT_HEADER_KEY = "eror-from-client";
public static final String DEIDENTIFIED_FILE_PREFIX = "deidentified";;
public static final String ERROR_FROM_CLIENT_HEADER_KEY = "error-from-client";
public static final String DEIDENTIFIED_FILE_PREFIX = "deidentified";
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.HashMap;

public class InvokeConnectionResponse {
private final Object data;
private final HashMap<String, String> metadata;
private final ArrayList<HashMap<String, Object>> errors;

public InvokeConnectionResponse(Object data, HashMap<String, String> metadata) {
public InvokeConnectionResponse(Object data, HashMap<String, String> metadata, ArrayList<HashMap<String, Object>> errors) {
this.data = data;
this.metadata = metadata;
this.errors = errors;
}

public Object getData() {
Expand All @@ -22,9 +25,13 @@ public HashMap<String, String> getMetadata() {
return metadata;
}

public ArrayList<HashMap<String, Object>> getErrors() {
return errors;
}

@Override
public String toString() {
Gson gson = new GsonBuilder().serializeNulls().create();
Gson gson = new Gson().newBuilder().serializeNulls().create();
return gson.toJson(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public InvokeConnectionResponse invoke(InvokeConnectionRequest invokeConnectionR
JsonObject data = JsonParser.parseString(response).getAsJsonObject();
HashMap<String, String> metadata = new HashMap<>();
metadata.put("requestId", HttpUtility.getRequestID());
connectionResponse = new InvokeConnectionResponse(data, metadata);
connectionResponse = new InvokeConnectionResponse(data, metadata, null);
LogUtil.printInfoLog(InfoLogs.INVOKE_CONNECTION_REQUEST_RESOLVED.getLog());
} catch (IOException e) {
LogUtil.printErrorLog(ErrorLogs.INVOKE_CONNECTION_REQUEST_REJECTED.getLog());
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/com/skyflow/vault/controller/DetectController.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
import java.util.*;

public final class DetectController extends VaultClient {
private static final Gson gson = new GsonBuilder().serializeNulls().create();
Gson gson = new GsonBuilder()
.registerTypeAdapter(Optional.class, (JsonSerializer<Optional<?>>) (src, typeOfSrc, context) ->
src.map(context::serialize).orElse(null))
.serializeNulls()
.create();

public DetectController(VaultConfig vaultConfig, Credentials credentials) {
super(vaultConfig, credentials);
Expand All @@ -55,7 +59,7 @@ public DeidentifyTextResponse deidentifyText(DeidentifyTextRequest deidentifyTex
deidentifyTextResponse = getDeIdentifyTextResponse(deidentifyStringResponse);
LogUtil.printInfoLog(InfoLogs.DEIDENTIFY_TEXT_REQUEST_RESOLVED.getLog());
} catch (ApiClientApiException ex) {
String bodyString = gson.toJson(ex.body());
String bodyString = extractBodyAsString(ex.body());
LogUtil.printErrorLog(ErrorLogs.DEIDENTIFY_TEXT_REQUEST_REJECTED.getLog());
throw new SkyflowException(ex.statusCode(), ex, ex.headers(), bodyString);
}
Expand All @@ -82,7 +86,7 @@ public ReidentifyTextResponse reidentifyText(ReidentifyTextRequest reidentifyTex
reidentifyTextResponse = new ReidentifyTextResponse(reidentifyStringResponse.getText().orElse(null));
LogUtil.printInfoLog(InfoLogs.REIDENTIFY_TEXT_REQUEST_RESOLVED.getLog());
} catch (ApiClientApiException ex) {
String bodyString = gson.toJson(ex.body());
String bodyString = extractBodyAsString(ex.body());
LogUtil.printErrorLog(ErrorLogs.REIDENTIFY_TEXT_REQUEST_REJECTED.getLog());
throw new SkyflowException(ex.statusCode(), ex, ex.headers(), bodyString);
}
Expand Down Expand Up @@ -145,7 +149,7 @@ public DeidentifyFileResponse deidentifyFile(DeidentifyFileRequest request) thro
}
}
} catch (ApiClientApiException e) {
String bodyString = gson.toJson(e.body());
String bodyString = extractBodyAsString(e.body());
LogUtil.printErrorLog(ErrorLogs.DEIDENTIFY_FILE_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
}
Expand Down Expand Up @@ -287,6 +291,14 @@ private static synchronized List<FileEntityInfo> getEntities(DeidentifyStatusRes
return entities;
}

private String extractBodyAsString(Object body) {
if (body instanceof String) {
return (String) body;
} else {
return gson.toJson(body);
}
}

private com.skyflow.generated.rest.types.DeidentifyFileResponse processFileByType(String fileExtension, String base64Content, DeidentifyFileRequest request, String vaultId) throws SkyflowException {
switch (fileExtension.toLowerCase()) {
case "txt":
Expand Down Expand Up @@ -367,7 +379,7 @@ public DeidentifyFileResponse getDetectRun(GetDetectRunRequest request) throws S

return parseDeidentifyFileResponse(apiResponse, runId, apiResponse.getStatus().toString());
} catch (ApiClientApiException e) {
String bodyString = gson.toJson(e.body());
String bodyString = extractBodyAsString(e.body());
LogUtil.printErrorLog(ErrorLogs.GET_DETECT_RUN_REQUEST_REJECTED.getLog());
throw new SkyflowException(e.statusCode(), e, e.headers(), bodyString);
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/skyflow/vault/controller/VaultController.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public VaultController(VaultConfig vaultConfig, Credentials credentials) {
super(vaultConfig, credentials);
}

private static synchronized HashMap<String, Object> getFormattedBatchInsertRecord(Object record, int requestIndex) {
private static synchronized HashMap<String, Object> getFormattedBatchInsertRecord(Object record, Integer requestIndex) {
HashMap<String, Object> insertRecord = new HashMap<>();
String jsonString = gson.toJson(record);
JsonObject bodyObject = JsonParser.parseString(jsonString).getAsJsonObject().get("Body").getAsJsonObject();
Expand Down Expand Up @@ -95,11 +95,9 @@ private static synchronized HashMap<String, Object> getFormattedUpdateRecord(V1U

private static synchronized HashMap<String, Object> getFormattedQueryRecord(V1FieldRecords record) {
HashMap<String, Object> queryRecord = new HashMap<>();
Object fields = record.getFields();
if (fields != null) {
String fieldsString = gson.toJson(fields);
JsonObject fieldsObject = JsonParser.parseString(fieldsString).getAsJsonObject();
queryRecord.putAll(fieldsObject.asMap());
Optional<Map<String, Object>> fieldsOpt = record.getFields();
if (fieldsOpt.isPresent()) {
queryRecord.putAll(fieldsOpt.get());
}
return queryRecord;
}
Expand All @@ -124,14 +122,15 @@ public InsertResponse insert(InsertRequest insertRequest) throws SkyflowExceptio
if (records.isPresent()) {
List<Map<String, Object>> recordList = records.get();

for (int index = 0; index < recordList.size(); index++) {
for (Integer index = (Integer) 0; index < recordList.size(); index++) {
Map<String, Object> record = recordList.get(index);
HashMap<String, Object> insertRecord = getFormattedBatchInsertRecord(record, index);

if (insertRecord.containsKey("skyflowId")) {
insertedFields.add(insertRecord);
} else {
insertRecord.put("requestId", batchInsertResult.headers().get("x-request-id").get(0));
insertRecord.put("httpCode", 400);
errorFields.add(insertRecord);
}
}
Expand Down Expand Up @@ -232,6 +231,7 @@ public GetResponse get(GetRequest getRequest) throws SkyflowException {
.downloadUrl(getRequest.getDownloadURL())
.columnName(getRequest.getColumnName())
.columnValues(getRequest.getColumnValues())
.fields(getRequest.getFields())
.orderBy(RecordServiceBulkGetRecordRequestOrderBy.valueOf(getRequest.getOrderBy()))
.build();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/skyflow/vault/data/GetRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public String getOrderBy() {
public static final class GetRequestBuilder {
private String table;
private ArrayList<String> ids;
private RedactionType redactionType;
private RedactionType redactionType = RedactionType.PLAIN_TEXT;
private Boolean returnTokens;
private ArrayList<String> fields;
private String offset;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/skyflow/vault/data/GetResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ArrayList<HashMap<String, Object>> getErrors() {

@Override
public String toString() {
Gson gson = new Gson();
Gson gson = new Gson().newBuilder().serializeNulls().create();
return gson.toJson(this);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/skyflow/vault/data/InsertResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ArrayList<HashMap<String, Object>> getErrors() {

@Override
public String toString() {
Gson gson = new Gson();
Gson gson = new Gson().newBuilder().serializeNulls().create();
return gson.toJson(this);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/skyflow/vault/tokens/DetokenizeData.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class DetokenizeData {

public DetokenizeData(String token) {
this.token = token;
this.redactionType = RedactionType.PLAIN_TEXT;
this.redactionType = RedactionType.MASKED;
}

public DetokenizeData(String token, RedactionType redactionType) {
Expand Down
31 changes: 30 additions & 1 deletion src/main/java/com/skyflow/vault/tokens/DetokenizeResponse.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.skyflow.vault.tokens;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;

Expand All @@ -23,7 +26,33 @@ public ArrayList<DetokenizeRecordResponse> getErrors() {

@Override
public String toString() {
Gson gson = new Gson();
Gson gson = new GsonBuilder()
.serializeNulls()
.registerTypeAdapter(
DetokenizeRecordResponse.class,
(com.google.gson.JsonSerializer<DetokenizeRecordResponse>) (src, typeOfSrc, context) -> {
com.google.gson.JsonObject obj = new com.google.gson.JsonObject();
obj.addProperty("token", src.getToken());
// Only include value if not null
if (src.getValue() != null) {
obj.addProperty("value", src.getValue());
}
// Only include type if not null
if (src.getType() != null) {
obj.addProperty("type", src.getType());
}
// Only include error if not null
if (src.getError() != null) {
obj.add("error", context.serialize(src.getError()));
}
// Only include requestId if not null
if (src.getRequestId() != null) {
obj.addProperty("requestId", src.getRequestId());
}
return obj;
}
)
.create();
return gson.toJson(this);
}
}
Loading
Loading