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
76 changes: 64 additions & 12 deletions src/main/java/com/skyflow/common/utils/Helpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
*/
package com.skyflow.common.utils;

import com.skyflow.entities.InsertInput;
import com.skyflow.entities.InsertOptions;
import com.skyflow.entities.InsertRecordInput;
import com.skyflow.entities.UpsertOption;
import com.skyflow.entities.*;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.SkyflowException;
import com.skyflow.logs.DebugLogs;
import com.skyflow.logs.ErrorLogs;

import org.apache.commons.codec.binary.Base64;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
Expand All @@ -23,21 +20,21 @@
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.*;
import org.apache.commons.codec.binary.Base64;

public final class Helpers {

private static final String LINE_FEED = "\r\n";

private static String getUpsertColumn(String tableName, UpsertOption[] upsertOptions){
private static String getUpsertColumn(String tableName, UpsertOption[] upsertOptions) {
String upsertColumn = "";

for (UpsertOption upsertOption : upsertOptions) {
if(Objects.equals(tableName, upsertOption.getTable()))
upsertColumn = upsertOption.getColumn();
if (Objects.equals(tableName, upsertOption.getTable())) {
upsertColumn = upsertOption.getColumn();
}
}
return upsertColumn;
};
}

public static JSONObject constructInsertRequest(InsertInput recordsInput, InsertOptions options)
throws SkyflowException {
Expand Down Expand Up @@ -65,8 +62,8 @@ public static JSONObject constructInsertRequest(InsertInput recordsInput, Insert
postRequestInput.put("quorum", true);
postRequestInput.put("tableName", record.getTable());
postRequestInput.put("fields", record.getFields());
if(options.getUpsertOptions() != null)
postRequestInput.put("upsert",getUpsertColumn(record.getTable(),options.getUpsertOptions()));
if (options.getUpsertOptions() != null)
postRequestInput.put("upsert", getUpsertColumn(record.getTable(), options.getUpsertOptions()));
requestBodyContent.add(postRequestInput);

if (isTokens) {
Expand All @@ -83,6 +80,61 @@ public static JSONObject constructInsertRequest(InsertInput recordsInput, Insert
return finalRequest;
}

public static JSONObject constructUpdateRequest(UpdateRecordInput record, UpdateOptions options) throws SkyflowException {
if (record == null) {
LogUtil.printErrorLog(ErrorLogs.InvalidUpdateInput.getLog());
throw new SkyflowException(ErrorCode.EmptyRecords);
}
if (record.getId() == null || record.getId().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.InvalidSkyflowId.getLog());
throw new SkyflowException(ErrorCode.InvalidSkyflowId);
}
if (record.getTable() == null || record.getTable().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.InvalidTable.getLog());
throw new SkyflowException(ErrorCode.InvalidTable);
}
if (record.getFields() == null || record.getFields().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.InvalidField.getLog());
throw new SkyflowException(ErrorCode.InvalidFields);
}

JSONObject postRequestInput = new JSONObject();
postRequestInput.put("fields", record.getFields());
return postRequestInput;

}

public static StringBuilder constructGetByIdRequestURLParams(GetByIdRecordInput record) {
StringBuilder paramsList = new StringBuilder();

for (String id : record.getIds()) {
paramsList.append("skyflow_ids=").append(id).append("&");
}

paramsList.append("redaction=").append(record.getRedaction());
return paramsList;
}

public static StringBuilder constructGetRequestURLParams(GetRecordInput record) {
StringBuilder paramsList = new StringBuilder();

if (record.getIds() != null) {
for (String id : record.getIds()) {
paramsList.append("skyflow_ids=").append(id).append("&");
}
}

if (record.getColumnName() != null && record.getColumnValues() != null) {
paramsList.append("column_name=").append(record.getColumnName()).append("&");
for (String value : record.getColumnValues()) {
paramsList.append("column_values=").append(value).append("&");
}
}

paramsList.append("redaction=").append(record.getRedaction());
return paramsList;
}

public static JSONObject constructInsertResponse(JSONObject response, List requestRecords, boolean tokens) {

JSONArray responses = (JSONArray) response.get("responses");
Expand Down
40 changes: 37 additions & 3 deletions src/main/java/com/skyflow/common/utils/Validators.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
*/
package com.skyflow.common.utils;

import com.skyflow.entities.RequestMethod;
import com.skyflow.entities.SkyflowConfiguration;
import com.skyflow.entities.UpsertOption;
import com.skyflow.entities.*;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.SkyflowException;
import com.skyflow.logs.ErrorLogs;
Expand Down Expand Up @@ -100,4 +98,40 @@ private static boolean isInvalidURL(String configURL) {
}
return false;
}

public static void validateGetByIdRequestRecord(GetByIdRecordInput record) throws SkyflowException {
String table = record.getTable();

if (table == null || table.trim().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.InvalidTable.getLog());
throw new SkyflowException(ErrorCode.InvalidTable);
}
}

public static void validateGetRequestRecord(GetRecordInput record) throws SkyflowException {
String[] ids = record.getIds();
String table = record.getTable();
String columnName = record.getColumnName();
String[] columnValues = record.getColumnValues();

if (table == null || table.trim().isEmpty()) {
LogUtil.printErrorLog(ErrorLogs.InvalidTable.getLog());
throw new SkyflowException(ErrorCode.InvalidTable);
}

if (ids == null && columnName == null && columnValues == null) {
LogUtil.printErrorLog(ErrorLogs.MissingIdAndColumnName.getLog());
throw new SkyflowException(ErrorCode.MissingIdAndColumnName);
}

if (columnName != null && columnValues == null) {
LogUtil.printErrorLog(ErrorLogs.MissingRecordColumnValue.getLog());
throw new SkyflowException(ErrorCode.MissingRecordColumnValue);
}

if (columnName == null && columnValues != null) {
LogUtil.printErrorLog(ErrorLogs.MissingRecordColumnName.getLog());
throw new SkyflowException(ErrorCode.MissingRecordColumnName);
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/skyflow/entities/GetByIdInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package com.skyflow.entities;

public class GetByIdInput {
private GetByIdRecordInput records[];
private GetByIdRecordInput[] records;

public GetByIdRecordInput[] getRecords() {
return records;
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/skyflow/entities/GetInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Copyright (c) 2022 Skyflow, Inc.
*/
package com.skyflow.entities;

public class GetInput {
private GetRecordInput[] records;

public GetRecordInput[] getRecords() {
return records;
}

public void setRecords(GetRecordInput[] records) {
this.records = records;
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/skyflow/entities/GetRecordInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright (c) 2022 Skyflow, Inc.
*/
package com.skyflow.entities;

public class GetRecordInput {
private String[] ids;
private String table;
private String columnName;
private String[] columnValues;
private RedactionType redaction;

public String[] getIds() {
return ids;
}

public void setIds(String[] ids) {
this.ids = ids;
}

public String getTable() {
return table;
}

public void setTable(String table) {
this.table = table;
}

public String getColumnName() {
return columnName;
}

public void setColumnName(String columnName) {
this.columnName = columnName;
}

public String[] getColumnValues() {
return columnValues;
}

public void setColumnValues(String[] columnValues) {
this.columnValues = columnValues;
}

public RedactionType getRedaction() {
return redaction;
}

public void setRedaction(RedactionType redaction) {
this.redaction = redaction;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/skyflow/entities/UpdateInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.skyflow.entities;

public class UpdateInput {
private UpdateRecordInput[] records;

public UpdateRecordInput[] getRecords() {
return records;
}

public void setRecords(UpdateRecordInput[] records) {
this.records = records;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/skyflow/entities/UpdateOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.skyflow.entities;

public class UpdateOptions {
private boolean tokens;

public UpdateOptions() {
this.tokens = true;
}

public UpdateOptions(boolean tokens) {
this.tokens = tokens;
}

public boolean isTokens() {
return tokens;
}

}
35 changes: 35 additions & 0 deletions src/main/java/com/skyflow/entities/UpdateRecordInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.skyflow.entities;

import org.json.simple.JSONObject;

public class UpdateRecordInput {
private String id;
private String table;
private JSONObject fields;

public String getTable() {
return table;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public void setTable(String table) {
this.table = table;
}

public JSONObject getFields() {
return fields;
}

public void setFields(JSONObject fields) {
this.fields = fields;
}


}
20 changes: 13 additions & 7 deletions src/main/java/com/skyflow/errors/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ public enum ErrorCode {
EmptyRecords(400, "Records cannot be empty"),
InvalidTable(400, "Table name is missing"),
InvalidFields(400, "Fields are missing"),
InvalidSkyflowId(400, "Skyflow id are missing"),
InvalidToken(400, "Token is empty"),
InvalidDetokenizeInput(400, "Invalid Detokenize Input"),
InvalidInsertInput(400, "Invalid insert input"),
InvalidUpdateInput(400, "Invalid update input"),
InvalidGetByIdInput(400, "Invalid getById input"),
InvalidGetInput(400, "Invalid get input"),
MissingIdAndColumnName(400, "Provide either Ids or column name to get records."),
MissingRecordColumnValue(400, "Column Values can not be empty when Column Name is specified."),
MissingRecordColumnName(400, "Column Name can not be empty when Column Values are specified."),
ResponseParsingError(500, "Unable to parse response"),
ThreadInterruptedException(500, "Thread was interrupted"),
ThreadExecutionException(500, "ThreadExecution exception"),
Server(500, "Internal server error"),
ServerReturnedErrors(500, "Server returned errors, check SkyflowException.getData() for more"),
ConnectionURLMissing(400, "connectionURL is required"),
InvalidConnectionURL(400, "Invalid connectionURL"),
MethodNameMissing(400, "methodName is required"),
Expand All @@ -38,13 +45,12 @@ public enum ErrorCode {
InvalidJSONStringFormat(400, "credentials string is not a valid json string format"),
EmptyFilePath(400, "file path cannot be empty or null"),
EmptyContext(400, "ctx claim field is missing from the jwt assertion"),
IncorrectRole(400,"Requested scope cannot be granted"),

IncorrectCredentials(400,"Incorrect credentials provided"),
InvalidUpsertOptionType(400,"upsert options should be an non empty UpsertOption array."),
InvalidUpsertObjectType(400,"upsert option cannot be null, should be an UpsertOption object."),
InvalidTableInUpsertOption(400,"Invalid table in upsert object, non empty string is required."),
InvalidColumnInUpsertOption(400,"Invalid column in upsert object, non empty string is required.");
IncorrectRole(400, "Requested scope cannot be granted"),
IncorrectCredentials(400, "Incorrect credentials provided"),
InvalidUpsertOptionType(400, "upsert options should be an non empty UpsertOption array."),
InvalidUpsertObjectType(400, "upsert option cannot be null, should be an UpsertOption object."),
InvalidTableInUpsertOption(400, "Invalid table in upsert object, non empty string is required."),
InvalidColumnInUpsertOption(400, "Invalid column in upsert object, non empty string is required.");

private final int code;
private final String description;
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/com/skyflow/logs/ErrorLogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ public enum ErrorLogs {
InvalidVaultURL("invalid vault url"),
InvalidTokenProvider("invalid TokenProvider. TokenProvider cannot be null"),
InvalidInsertInput("invalid insert input"),
InvalidUpdateInput("invalid update input"),
InvalidDetokenizeInput("invalid detokenize input"),
ResponseParsingError("Unable to parse response in %s1 method"),
ThreadInterruptedException("Thread was interrupted in %s1 method"),
ThreadExecutionException("ThreadExecution exception in %s1 method"),
InvalidGetByIdInput("Invalid getById input"),
InvalidGetInput("Invalid get input"),
MissingIdAndColumnName("Provide either Ids or column name to get records."),
MissingRecordColumnValue("Column Values can not be empty when Column Name is specified."),
MissingRecordColumnName("Column Name can not be empty when Column Values are specified."),
InvalidInvokeConnectionInput("Invalid invokeConnection Input"),
ConnectionURLMissing("connectionURL is required"),
InvalidConnectionURL("Invalid connectionURL"),
Expand All @@ -34,10 +39,14 @@ public enum ErrorLogs {
InvalidBearerToken("Invalid Bearer token"),
InvalidTable("Table name is missing"),
Server("Internal server error"),
ServerReturnedErrors("Server returned errors, check SkyflowException.getData() for more"),
InvalidUpsertOptionType("upsert options cannot be null, should be an non empty UpsertOption array."),
InvalidTableInUpsertOption("Invalid table in upsert object, non empty string is required."),
InvalidColumnInUpsertOption("Invalid column in upsert object, non empty string is required."),
InvalidUpsertObjectType("upsert option cannot be null, should be an UpsertOption object.");
InvalidUpsertObjectType("upsert option cannot be null, should be an UpsertOption object."),
InvalidSkyflowId("Skyflow Id is missing"),
InvalidField("Fields missing");

private final String log;

ErrorLogs(String log) {
Expand Down
Loading