in core/java8/proxy/src/main/java/org/apache/openwhisk/runtime/java/action/Proxy.java [132:214]
public void handle(HttpExchange t) throws IOException {
if (loader == null) {
Proxy.writeError(t, "Cannot invoke an uninitialized action.");
return;
}
ClassLoader cl = Thread.currentThread().getContextClassLoader();
SecurityManager sm = System.getSecurityManager();
try {
InputStream is = t.getRequestBody();
JsonParser parser = new JsonParser();
JsonObject body = parser.parse(new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))).getAsJsonObject();
JsonParser json = new JsonParser();
JsonObject payloadForJsonObject = json.parse("{}").getAsJsonObject();
JsonArray payloadForJsonArray = json.parse("[]").getAsJsonArray();
Boolean isJsonObjectParam = true;
JsonElement inputJsonElement = body.get("value");
if (inputJsonElement.isJsonObject()) {
payloadForJsonObject = inputJsonElement.getAsJsonObject();
} else {
payloadForJsonArray = inputJsonElement.getAsJsonArray();
isJsonObjectParam = false;
}
HashMap<String, String> env = new HashMap<String, String>();
Set<Map.Entry<String, JsonElement>> entrySet = body.entrySet();
for(Map.Entry<String, JsonElement> entry : entrySet){
try {
if(!entry.getKey().equalsIgnoreCase("value"))
env.put(String.format("__OW_%s", entry.getKey().toUpperCase()), entry.getValue().getAsString());
} catch (Exception e) {}
}
Thread.currentThread().setContextClassLoader(loader);
System.setSecurityManager(new WhiskSecurityManager());
Method mainMethod = null;
String mainMethodName = loader.entrypointMethodName;
if (isJsonObjectParam) {
mainMethod = loader.mainClass.getMethod(mainMethodName, new Class[] { JsonObject.class });
} else {
mainMethod = loader.mainClass.getMethod(mainMethodName, new Class[] { JsonArray.class });
}
mainMethod.setAccessible(true);
int modifiers = mainMethod.getModifiers();
if ((mainMethod.getReturnType() != JsonObject.class && mainMethod.getReturnType() != JsonArray.class) || !Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) {
throw new NoSuchMethodException(mainMethodName);
}
// User code starts running here. the return object supports JsonObject and JsonArray both.
Object output;
if (isJsonObjectParam) {
loader.augmentEnv(env);
output = mainMethod.invoke(null, payloadForJsonObject);
} else {
loader.augmentEnv(env);
output = mainMethod.invoke(null, payloadForJsonArray);
}
// User code finished running here.
if (output == null) {
throw new NullPointerException("The action returned null");
}
Proxy.writeResponse(t, 200, output.toString());
return;
} catch (InvocationTargetException ite) {
// These are exceptions from the action, wrapped in ite because
// of reflection
Throwable underlying = ite.getCause();
underlying.printStackTrace(System.err);
Proxy.writeError(t,
"An error has occurred while invoking the action (see logs for details): " + underlying);
} catch (Exception e) {
e.printStackTrace(System.err);
Proxy.writeError(t, "An error has occurred (see logs for details): " + e);
} finally {
writeLogMarkers();
System.setSecurityManager(sm);
Thread.currentThread().setContextClassLoader(cl);
}
}