Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/http post invoker #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 23 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -81,7 +86,24 @@
<artifactId>google-cloud-iamcredentials</artifactId>
<version>1.1.7</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.6.1</version>
<scope>test</scope>
</dependency>


</dependencies>
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/jFaaS/Gateway.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Gateway implements FaaSInvoker {
private FaaSInvoker azureInvoker;
private String azureKey;
private FaaSInvoker httpGETInvoker;
private HTTPPOSTInvoker httpPOSTInvoker;
private VMInvoker vmInvoker;

/**
Expand Down Expand Up @@ -66,7 +67,7 @@ public Gateway(String credentialsFile) {
LOGGER.log(Level.WARNING, "Could not load credentials file.");
}
httpGETInvoker = new HTTPGETInvoker();

httpPOSTInvoker = new HTTPPOSTInvoker();
}


Expand All @@ -75,8 +76,19 @@ public Gateway(String credentialsFile) {
*/
public Gateway() {
httpGETInvoker = new HTTPGETInvoker();
httpPOSTInvoker = new HTTPPOSTInvoker();
}

/**
* Gateway for constructer based dependency injection
*
* @param httpPOSTInvoker
* @param httpGETInvoker
*/
public Gateway(HTTPGETInvoker httpGETInvoker, HTTPPOSTInvoker httpPOSTInvoker) {
this.httpGETInvoker = httpGETInvoker;
this.httpPOSTInvoker = httpPOSTInvoker;
}
/**
* Detect aws lambda region
*
Expand Down Expand Up @@ -170,6 +182,12 @@ public PairResult<String, Long> invokeFunction(String function, Map<String, Obje
} else if (function.contains("fc.aliyuncs.com")) {
// TODO check for alibaba authentication. Currently no authentication is assumed
return httpGETInvoker.invokeFunction(function, functionInputs);
} else if (function.startsWith("HTTP_GET:")) {
String url = function.split(":", 2)[1];
return httpGETInvoker.invokeFunction(url, functionInputs);
} else if (function.startsWith("HTTP_POST:")) {
String url = function.split(":", 2)[1];
return httpPOSTInvoker.invokeFunction(url, functionInputs);
} else if (function.contains(":VM:")) {
if (vmInvoker == null) {
vmInvoker = new VMInvoker();
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/jFaaS/invokers/HTTPPOSTInvoker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package jFaaS.invokers;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import jFaaS.utils.PairResult;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HTTPPOSTInvoker implements FaaSInvoker {
public HTTPPOSTInvoker() {
System.setProperty("https.protocols", "TLSv1.2");
}

/**
* Makes a HTTP POST request.
*
* @return
*/
@Override
public PairResult<String, Long> invokeFunction(String function, Map<String, Object> parameters) throws IOException {
long start = System.currentTimeMillis();

URL obj = new URL(function);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();

Gson gson = new Gson();
Type gsonType = new TypeToken<Map<String, Object>>(){}.getType();
String requestBody = gson.toJson(parameters,gsonType);

con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");

// why?
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(requestBody.getBytes("utf-8"), 0, requestBody.length());
os.flush();
os.close();

BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return new PairResult<>(new Gson().fromJson(response.toString(), JsonObject.class).getAsJsonObject().toString(), System.currentTimeMillis() - start);

}
}
50 changes: 50 additions & 0 deletions src/test/java/GatewayTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import jFaaS.Gateway;
import jFaaS.invokers.HTTPGETInvoker;
import jFaaS.invokers.HTTPPOSTInvoker;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@ExtendWith(MockitoExtension.class)
public class GatewayTests {

@Mock
public static HTTPPOSTInvoker httpPOSTInvoker;

@Mock
public static HTTPGETInvoker httpGETInvoker;

@InjectMocks
public static Gateway gateway;


@Test
public void testHTTPPOSTInvocation() throws IOException {
Map<String, Object> params = new HashMap<String, Object>();
gateway.invokeFunction("HTTP_POST:https://foo.bar/api/v1/items", params);
Mockito.verify(httpPOSTInvoker).invokeFunction(Mockito.eq("https://foo.bar/api/v1/items"),
Mockito.anyMap());
}

@Test void testHTTPGETInvocation() throws IOException {
Map<String, Object> params = new HashMap<String, Object>();
gateway.invokeFunction("HTTP_GET:https://foo.bar/api/v1/items", params);
Mockito.verify(httpGETInvoker).invokeFunction(Mockito.eq("https://foo.bar/api/v1/items"),
Mockito.anyMap());
}

@Test void testAlibabaInvocation() throws IOException {
String endpoint = "164901546557.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/serviceName/functionName/";
Map<String, Object> params = new HashMap<String, Object>();
gateway.invokeFunction(endpoint, params);
Mockito.verify(httpGETInvoker).invokeFunction(Mockito.eq(endpoint),
Mockito.anyMap());
}
}