in knative-build/runtimes/java/core/java8/proxy/src/main/java/org/apache/openwhisk/runtime/java/action/Proxy.java [161:234]
public void handle(HttpExchange t) throws IOException {
long startTime = Debug.start();
if (loader == null) {
// check if the Jar file contents are set in the environment
// OW_AUTO_INIT: Jar file with absolute/relative path
// OW_AUTO_INIT_MAIN: name of the function in the "OW_AUTO_INIT" to call as the action handler
String ow_auto_init = System.getenv(OW_AUTO_INIT);
String ow_auto_init_main = System.getenv(OW_AUTO_INIT_MAIN);
if (ow_auto_init == null || ow_auto_init.isEmpty()) {
Proxy.writeError(t, "Cannot invoke an uninitialized action.");
return;
} else {
try {
Path jarPath = Paths.get(ow_auto_init);
loader = new JarLoader(jarPath, ow_auto_init_main);
} catch (Exception e) {
e.printStackTrace(System.err);
writeLogMarkers();
Proxy.writeError(t, "An error has occurred (see logs for details): " + e);
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();
JsonObject inputObject = body.getAsJsonObject("value");
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());
// User code starts running here.
JsonObject output = loader.invokeMain(inputObject, env);
// 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) {
// When you invoke a method using reflection (as we do for the Action function)
// and it throws an exception, you must check for it using InvocationTargetException as follows:
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);
Debug.end(startTime);
}
}