use of com.bluenimble.platform.api.impls.JsonApiOutput 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);
}
use of com.bluenimble.platform.api.impls.JsonApiOutput in project serverless by bluenimble.
the class GetRecordSpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String provider = (String) request.get(CommonSpec.Provider);
String sEntity = (String) request.get(CommonSpec.Entity);
String record = (String) request.get(Spec.Record);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
DatabaseObject dbo = null;
try {
Database db = space.feature(Database.class, provider, request);
dbo = db.get(sEntity, record);
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
if (dbo == null) {
return null;
}
return new JsonApiOutput(dbo.toJson(null));
}
use of com.bluenimble.platform.api.impls.JsonApiOutput in project serverless by bluenimble.
the class ChangeApiStatusSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String apiNs = (String) request.get(CommonSpec.Api);
String sAction = (String) request.getResource()[request.getResource().length - 1];
Action action = null;
try {
action = Action.valueOf(sAction);
} catch (Exception ex) {
// ignore
}
if (action == null) {
throw new ApiServiceExecutionException("unknown change-status action " + sAction).status(ApiResponse.BAD_REQUEST);
}
ApiSpace space = null;
try {
space = MgmUtils.space(consumer, api);
switch(action) {
case start:
space.start(apiNs);
break;
case stop:
space.stop(apiNs);
break;
case pause:
space.pause(apiNs);
break;
case resume:
space.resume(apiNs);
break;
default:
break;
}
} catch (Exception ex) {
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
Api targetApi = space.api(apiNs);
JsonObject result = (JsonObject) new JsonObject().set(Api.Spec.Status, targetApi.status().name());
if (ApiStatus.Failed.equals(targetApi.status())) {
result.set(Output.Reason, targetApi.getFailure());
}
return new JsonApiOutput(result);
}
use of com.bluenimble.platform.api.impls.JsonApiOutput in project serverless by bluenimble.
the class DescribeApiSpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String apiNs = (String) request.get(CommonSpec.Api);
Api uApi;
try {
uApi = MgmUtils.api(consumer, api, apiNs);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
if (uApi == null) {
throw new ApiServiceExecutionException("api '" + apiNs + "' not found").status(ApiResponse.NOT_FOUND);
}
return new JsonApiOutput(uApi.describe(MgmUtils.options((String) request.get(CommonSpec.Options), DescribeOption.Info)));
}
use of com.bluenimble.platform.api.impls.JsonApiOutput in project serverless by bluenimble.
the class InstallApiFromFileSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String spaceFolder = (String) request.get(Spec.SpaceFolder);
String apiFile = (String) request.get(Spec.ApiFile);
Boolean start = (Boolean) request.get(Spec.Start);
if (start == null) {
start = true;
}
ApiSpace targetSpace = null;
Api installed = null;
try {
targetSpace = MgmUtils.space(consumer, api);
installed = targetSpace.install(spaceFolder, apiFile);
} catch (Exception ex) {
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
if (installed == null) {
throw new ApiServiceExecutionException("can't install api. Server can't return a valid api object");
}
try {
if (start) {
targetSpace.start(installed.getNamespace());
}
} catch (Exception ex) {
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
return new JsonApiOutput(installed.describe(MgmUtils.options((String) request.get(CommonSpec.Options), DescribeOption.Info)));
}
Aggregations