use of jdk.nashorn.internal.runtime.Undefined in project serverless by bluenimble.
the class ScriptableApiServiceSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
if (jsApi == null) {
throw new ApiServiceExecutionException("api '" + api.getNamespace() + "' doesn't support scripting");
}
SpecAndSpiPair serviceHelper = (SpecAndSpiPair) request.getService().getHelper();
Object spi = serviceHelper.spi();
if (spi == null) {
throw new ApiServiceExecutionException("service spi not found");
}
ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, request);
if (!engine.has(spi, Functions.Execute)) {
return null;
}
// invoke execute
Object result = null;
try {
result = engine.invoke(spi, Functions.Execute, jsApi, consumer, request, response);
} catch (ScriptingEngineException ex) {
ex.setScript(Json.getString(request.getService().getRuntime(), Api.Spec.Runtime.Function));
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
if (result == null || (result instanceof Undefined)) {
return null;
}
if (ApiOutput.class.isAssignableFrom(result.getClass())) {
return (ApiOutput) result;
}
if (ScriptObjectMirror.class.isAssignableFrom(result.getClass())) {
ScriptObjectMirror som = (ScriptObjectMirror) result;
Object clazz = som.get(ClassField);
if (clazz == null) {
return new ApiSomOutput(som);
}
if (clazz.equals(ApiOutputClass)) {
return (ApiOutput) som.getMember(ProxyField);
}
}
Object converted = Converters.convert(result);
if (converted instanceof JsonArray) {
converted = new JsonObject().set(ApiOutput.Defaults.Items, converted);
}
if (!(converted instanceof JsonObject)) {
throw new ApiServiceExecutionException("result should be a valid json object");
}
return new JsonApiOutput((JsonObject) converted);
}
Aggregations