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
1 change: 0 additions & 1 deletion src/main/java/com/skyflow/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public final class Constants {
public static final String QUOTE = "\"";

public static final class HttpUtilityExtra {
public static final String RAW_BODY_KEY = "__raw_body__";
public static final String SDK_GENERATED_PREFIX = "SDK-Generated-";
private HttpUtilityExtra() {}
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/skyflow/utils/HttpUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

public final class HttpUtility {

Expand Down Expand Up @@ -56,9 +57,7 @@ public static String sendRequest(String method, URL url, JsonObject params, Map<
byte[] input = null;
String requestContentType = connection.getRequestProperty("content-type");

if (params.has(Constants.HttpUtilityExtra.RAW_BODY_KEY) && params.size() == 1) {
input = params.get(Constants.HttpUtilityExtra.RAW_BODY_KEY).getAsString().getBytes(StandardCharsets.UTF_8);
} else if (requestContentType != null && requestContentType.contains("application/x-www-form-urlencoded")) {
if (requestContentType != null && requestContentType.contains("application/x-www-form-urlencoded")) {
input = formatJsonToFormEncodedString(params).getBytes(StandardCharsets.UTF_8);
} else if (requestContentType != null && requestContentType.contains("multipart/form-data")) {
input = formatJsonToMultiPartFormDataString(params, boundary).getBytes(StandardCharsets.UTF_8);
Expand All @@ -73,7 +72,11 @@ public static String sendRequest(String method, URL url, JsonObject params, Map<

int httpCode = connection.getResponseCode();
String requestID = connection.getHeaderField("x-request-id");
HttpUtility.requestID = requestID != null ? requestID.split(",")[0] : null;
if (requestID != null) {
HttpUtility.requestID = requestID.split(",")[0];
} else {
HttpUtility.requestID = Constants.HttpUtilityExtra.SDK_GENERATED_PREFIX + UUID.randomUUID();
}
Map<String, List<String>> responseHeaders = connection.getHeaderFields();
Reader streamReader;
if (httpCode > 299) {
Expand Down
17 changes: 2 additions & 15 deletions src/main/java/com/skyflow/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
Expand Down Expand Up @@ -121,12 +119,7 @@ public static String constructConnectionURL(ConnectionConfig config, InvokeConne
for (Map.Entry<String, String> entry : invokeConnectionRequest.getPathParams().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
try {
String encodedValue = URLEncoder.encode(value, StandardCharsets.UTF_8.name());
filledURL = new StringBuilder(filledURL.toString().replace(String.format(Constants.CURLY_PLACEHOLDER, key), encodedValue));
} catch (Exception e) {
filledURL = new StringBuilder(filledURL.toString().replace(String.format(Constants.CURLY_PLACEHOLDER, key), value));
}
filledURL = new StringBuilder(filledURL.toString().replace(String.format(Constants.CURLY_PLACEHOLDER, key), value));
}
}

Expand All @@ -135,13 +128,7 @@ public static String constructConnectionURL(ConnectionConfig config, InvokeConne
for (Map.Entry<String, String> entry : invokeConnectionRequest.getQueryParams().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
try {
String encodedKey = URLEncoder.encode(key, StandardCharsets.UTF_8.name());
String encodedValue = URLEncoder.encode(value, StandardCharsets.UTF_8.name());
filledURL.append(encodedKey).append("=").append(encodedValue).append("&");
} catch (Exception e) {
filledURL.append(key).append("=").append(value).append("&");
}
filledURL.append(key).append("=").append(value).append("&");
}
filledURL = new StringBuilder(filledURL.substring(0, filledURL.length() - 1));
}
Expand Down
25 changes: 6 additions & 19 deletions src/main/java/com/skyflow/utils/validations/Validations.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.regex.Pattern;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.skyflow.config.ConnectionConfig;
import com.skyflow.config.Credentials;
Expand Down Expand Up @@ -150,24 +149,12 @@ public static void validateInvokeConnectionRequest(InvokeConnectionRequest invok
if (requestBody.getClass().equals(Object.class)) {
return;
}
if (requestBody instanceof String) {
String bodyStr = (String) requestBody;
if (bodyStr.trim().isEmpty()) {
LogUtil.printErrorLog(Utils.parameterizedString(
ErrorLogs.EMPTY_REQUEST_BODY.getLog(), InterfaceName.INVOKE_CONNECTION.getName()));
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyRequestBody.getMessage());
}
} else {
Gson gson = new Gson();
JsonElement bodyElement = gson.toJsonTree(requestBody);
if (bodyElement.isJsonObject()) {
JsonObject bodyObject = bodyElement.getAsJsonObject();
if (bodyObject.isEmpty()) {
LogUtil.printErrorLog(Utils.parameterizedString(
ErrorLogs.EMPTY_REQUEST_BODY.getLog(), InterfaceName.INVOKE_CONNECTION.getName()));
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyRequestBody.getMessage());
}
}
Gson gson = new Gson();
JsonObject bodyObject = gson.toJsonTree(requestBody).getAsJsonObject();
if (bodyObject.isEmpty()) {
LogUtil.printErrorLog(Utils.parameterizedString(
ErrorLogs.EMPTY_REQUEST_BODY.getLog(), InterfaceName.INVOKE_CONNECTION.getName()));
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.EmptyRequestBody.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,11 @@ public InvokeConnectionResponse invoke(InvokeConnectionRequest invokeConnectionR
Object requestBodyObject = invokeConnectionRequest.getRequestBody();

if (requestBodyObject != null) {
if (requestBodyObject instanceof String) {
String contentType = headers.getOrDefault("content-type", "");
if (!contentType.isEmpty() && !contentType.toLowerCase().contains("application/json")) {
requestBody = new JsonObject();
requestBody.addProperty(Constants.HttpUtilityExtra.RAW_BODY_KEY, (String) requestBodyObject);
} else {
try {
requestBody = convertObjectToJson(requestBodyObject);
} catch (Exception e) {
LogUtil.printErrorLog(ErrorLogs.INVALID_REQUEST_HEADERS.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.InvalidRequestBody.getMessage());
}
}
} else {
try {
requestBody = convertObjectToJson(requestBodyObject);
} catch (Exception e) {
LogUtil.printErrorLog(ErrorLogs.INVALID_REQUEST_HEADERS.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.InvalidRequestBody.getMessage());
}
try {
requestBody = convertObjectToJson(requestBodyObject);
} catch (Exception e) {
LogUtil.printErrorLog(ErrorLogs.INVALID_REQUEST_HEADERS.getLog());
throw new SkyflowException(ErrorCode.INVALID_INPUT.getCode(), ErrorMessage.InvalidRequestBody.getMessage());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/skyflow/utils/HttpUtilityTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void testSendRequestWithNullRequestId() {
params.addProperty("key", "value");
String response = httpUtility.sendRequest("GET", url, params, headers);
Assert.assertEquals(expected, response);
Assert.assertNull(HttpUtility.getRequestID());
Assert.assertNotNull(HttpUtility.getRequestID());
} catch (Exception e) {
fail(INVALID_EXCEPTION_THROWN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,44 +137,6 @@ public void testInvoke_successWithPutMethod() throws Exception {
Assert.assertNotNull(response);
}

@Test
public void testInvoke_successWithStringBodyAndJsonContentType() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("{\"parsed\":true}");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);

Map<String, String> headers = new HashMap<>();
headers.put("content-type", "application/json");

InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.method(RequestMethod.POST)
.requestHeaders(headers)
.requestBody("{\"key\":\"value\"}")
.build();
InvokeConnectionResponse response = controller.invoke(request);

Assert.assertNotNull(response);
}

@Test
public void testInvoke_successWithStringBodyAndNonJsonContentType() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
.thenReturn("ok");
when(HttpUtility.getRequestID()).thenReturn(REQUEST_ID);

Map<String, String> headers = new HashMap<>();
headers.put("content-type", "text/plain");

InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.method(RequestMethod.POST)
.requestHeaders(headers)
.requestBody("raw body content")
.build();
InvokeConnectionResponse response = controller.invoke(request);

Assert.assertNotNull(response);
}

@Test
public void testInvoke_successWithObjectBody() throws Exception {
when(HttpUtility.sendRequest(anyString(), any(URL.class), any(), any()))
Expand Down Expand Up @@ -364,20 +326,6 @@ public void testInvoke_emptyQueryParamsThrowsSkyflowException() {
}
}

@Test
public void testInvoke_emptyStringBodyThrowsSkyflowException() {
try {
InvokeConnectionRequest request = InvokeConnectionRequest.builder()
.requestBody(" ")
.build();
controller.invoke(request);
Assert.fail(EXCEPTION_NOT_THROWN);
} catch (SkyflowException e) {
Assert.assertEquals(ErrorCode.INVALID_INPUT.getCode(), e.getHttpCode());
Assert.assertEquals(ErrorMessage.EmptyRequestBody.getMessage(), e.getMessage());
}
}

@Test
public void testInvoke_emptyHashMapBodyThrowsSkyflowException() {
try {
Expand Down
Loading