Search in sources :

Example 41 with ApiServiceExecutionException

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));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 42 with ApiServiceExecutionException

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));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 43 with ApiServiceExecutionException

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);
    }
}
Also used : ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine)

Example 44 with ApiServiceExecutionException

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);
}
Also used : Endpoint(com.bluenimble.platform.api.ApiSpace.Endpoint) JsonObject(com.bluenimble.platform.json.JsonObject) ApiRequest(com.bluenimble.platform.api.ApiRequest) ApiVerb(com.bluenimble.platform.api.ApiVerb) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException)

Example 45 with ApiServiceExecutionException

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);
}
Also used : JsonQuery(com.bluenimble.platform.db.query.impls.JsonQuery) Config(com.bluenimble.platform.api.impls.im.LoginServiceSpi.Config) JsonObject(com.bluenimble.platform.json.JsonObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Date(java.util.Date) ApiOutput(com.bluenimble.platform.api.ApiOutput) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Database(com.bluenimble.platform.db.Database) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Aggregations

ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)52 JsonObject (com.bluenimble.platform.json.JsonObject)41 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)39 ApiSpace (com.bluenimble.platform.api.ApiSpace)31 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)28 Database (com.bluenimble.platform.db.Database)13 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)9 ApiOutput (com.bluenimble.platform.api.ApiOutput)8 DatabaseException (com.bluenimble.platform.db.DatabaseException)8 Storage (com.bluenimble.platform.storage.Storage)8 StorageException (com.bluenimble.platform.storage.StorageException)8 StorageObject (com.bluenimble.platform.storage.StorageObject)8 Api (com.bluenimble.platform.api.Api)6 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)5 JsonArray (com.bluenimble.platform.json.JsonArray)5 ScriptingEngine (com.bluenimble.platform.scripting.ScriptingEngine)5 ScriptingEngineException (com.bluenimble.platform.scripting.ScriptingEngineException)5 Date (java.util.Date)5 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)4 ApiVerb (com.bluenimble.platform.api.ApiVerb)4