use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class AddFeatureSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
JsonObject oFeature = (JsonObject) request.get(ApiRequest.Payload);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
try {
space.addFeature(Json.getString(oFeature, Spec.Name), Json.getString(oFeature, Spec.Feature), Json.getString(oFeature, ApiSpace.Features.Provider), Json.getObject(oFeature, ApiSpace.Features.Spec));
} catch (ApiManagementException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.BAD_REQUEST);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Added, true));
}
use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class AddSecretsSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
JsonObject oSecrets = (JsonObject) request.get(ApiRequest.Payload);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
try {
space.addSecrets((String) request.get(Spec.Name), oSecrets);
} catch (ApiManagementException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.BAD_REQUEST);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Added, true));
}
use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class ScriptableApiSpi method onExecute.
@Override
public void onExecute(Api api, ApiConsumer consumer, ApiService service, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
Object spi = ((SpecAndSpiPair) api.getHelper()).spi();
if (spi == null) {
return;
}
ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, request);
if (!engine.has(spi, Functions.OnExecute)) {
return;
}
Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
if (jsApi == null) {
throw new ApiServiceExecutionException("api or spi not attached on Api OnStart");
}
try {
// update consumer id
if (consumer.getReference() != null) {
engine.invoke(consumer.getReference(), ConsumerSet, ApiConsumer.Fields.Id, consumer.get(ApiConsumer.Fields.Id));
}
// invoke onExecute
engine.invoke(spi, Functions.OnExecute, jsApi, consumer, service, request, response);
} catch (ScriptingEngineException ex) {
ex.setScript(Json.getString(api.getRuntime(), Api.Spec.Runtime.Function));
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
}
use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class ApiUtils method call.
public static ApiOutput call(final Api api, final ApiConsumer consumer, final ApiRequest pRequest, final JsonObject oRequest) throws ApiServiceExecutionException {
ApiRequest request = api.space().request(pRequest, consumer, new Endpoint() {
@Override
public String space() {
return Json.getString(oRequest, Spec.Space, api.space().getNamespace());
}
@Override
public String api() {
return Json.getString(oRequest, Spec.Api, api.getNamespace());
}
@Override
public String[] resource() {
String resource = Json.getString(oRequest, Spec.Service);
if (resource.startsWith(Lang.SLASH)) {
resource = resource.substring(1);
}
if (resource.endsWith(Lang.SLASH)) {
resource = resource.substring(0, resource.length() - 1);
}
if (Lang.isNullOrEmpty(resource)) {
return null;
}
return Lang.split(resource, Lang.SLASH);
}
@Override
public ApiVerb verb() {
try {
return ApiVerb.valueOf(Json.getString(oRequest, Spec.Verb, ApiVerb.POST.name()).toUpperCase());
} catch (Exception ex) {
return ApiVerb.POST;
}
}
});
JsonObject parameters = Json.getObject(oRequest, Spec.Parameters);
if (!Json.isNullOrEmpty(parameters)) {
Iterator<String> keys = parameters.keys();
while (keys.hasNext()) {
String key = keys.next();
request.set(key, parameters.get(key));
}
}
JsonObject headers = Json.getObject(oRequest, Spec.Headers);
if (!Json.isNullOrEmpty(headers)) {
Iterator<String> keys = headers.keys();
while (keys.hasNext()) {
String key = keys.next();
request.set(key, headers.get(key), Scope.Header);
}
}
return api.call(request);
}
use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class ActivateServiceSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
JsonObject config = request.getService().getCustom();
Database db = api.space().feature(Database.class, Json.getString(config, Config.Database, ApiSpace.Features.Default), request);
DatabaseObject account = null;
try {
JsonObject query = Json.getObject(config, Config.Query);
if (query == null) {
query = new JsonObject();
JsonObject where = new JsonObject();
query.set(Query.Construct.where.name(), where);
where.set(Json.getString(config, Config.ActivationCodeProperty, Defaults.ActivationCode), request.get(Spec.ActivationCode));
}
account = db.findOne(Json.getString(config, Config.UsersEntity, Defaults.Users), new JsonQuery(query));
} catch (Exception ex) {
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
if (account == null) {
throw new ApiServiceExecutionException("account not found").status(ApiResponse.NOT_FOUND);
}
Date now = new Date();
// remove activationCode
account.remove(Json.getString(config, Config.ActivationCodeProperty, Defaults.ActivationCode));
// update lastLogin
try {
account.set(Json.getString(config, Config.LastLoginProperty, Fields.LastLogin), now);
account.save();
} catch (Exception ex) {
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
JsonObject oAccount = account.toJson(DefaultDatabaseObjectSerializer.Default);
// create token
String[] tokenAndExpiration = SecurityUtils.tokenAndExpiration(api, oAccount, now);
oAccount.remove(Json.getString(config, Config.PasswordProperty, Spec.Password));
oAccount.set(Defaults.Token, tokenAndExpiration[0]);
oAccount.set(Defaults.ExpiresOn, tokenAndExpiration[1]);
// call onFinish if any
JsonObject onFinish = Json.getObject(config, Config.onFinish.class.getSimpleName());
ApiOutput onFinishOutput = SecurityUtils.onFinish(api, consumer, request, onFinish, oAccount);
if (onFinishOutput != null) {
oAccount.set(Json.getString(onFinish, Config.onFinish.ResultProperty, Config.onFinish.class.getSimpleName()), onFinishOutput.data());
}
return new JsonApiOutput(oAccount);
}
Aggregations