Search in sources :

Example 31 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput in project serverless by bluenimble.

the class ChangeServiceStatusSpi 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 - 2];
    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);
    }
    JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
    String sVerb = Json.getString(payload, ApiService.Spec.Verb).toUpperCase();
    String endpoint = Json.getString(payload, ApiService.Spec.Endpoint);
    ApiVerb verb = null;
    try {
        verb = ApiVerb.valueOf(sVerb);
    } catch (Exception ex) {
    // ignore
    }
    if (verb == null) {
        throw new ApiServiceExecutionException("unknown service verb " + sVerb).status(ApiResponse.BAD_REQUEST);
    }
    Api targetApi = null;
    try {
        targetApi = MgmUtils.api(consumer, api, apiNs);
        switch(action) {
            case start:
                targetApi.getServicesManager().start(verb, endpoint);
                break;
            case stop:
                targetApi.getServicesManager().stop(verb, endpoint);
                break;
            case pause:
                targetApi.getServicesManager().get(verb, endpoint).pause();
                break;
            case resume:
                targetApi.getServicesManager().get(verb, endpoint).resume();
                break;
            default:
                break;
        }
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    return new JsonApiOutput((JsonObject) new JsonObject().set(Api.Spec.Status, targetApi.getServicesManager().get(verb, endpoint).status().name()));
}
Also used : ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Api(com.bluenimble.platform.api.Api) ApiVerb(com.bluenimble.platform.api.ApiVerb) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 32 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput 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 33 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput 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 34 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput 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)

Example 35 with JsonApiOutput

use of com.bluenimble.platform.api.impls.JsonApiOutput in project serverless by bluenimble.

the class ChangePasswordSpi 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 {
        account = db.get(Json.getString(config, Config.UsersEntity, Defaults.Users), (String) consumer.get(ApiConsumer.Fields.Id));
        account.set(Json.getString(config, Config.PasswordProperty, Spec.Password), Lang.md5((String) request.get(Spec.Password)));
        account.save();
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    ApiOutput result = new JsonApiOutput(JsonObject.Blank);
    // call extend if any
    if (config.containsKey(Config.onFinish.class.getSimpleName())) {
        JsonObject oAccount = account.toJson(null);
        result = SecurityUtils.onFinish(api, consumer, request, Json.getObject(config, Config.onFinish.class.getSimpleName()), oAccount);
    }
    return result;
}
Also used : 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) JsonObject(com.bluenimble.platform.json.JsonObject) DatabaseObject(com.bluenimble.platform.db.DatabaseObject) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Aggregations

ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)39 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)39 JsonObject (com.bluenimble.platform.json.JsonObject)34 ApiSpace (com.bluenimble.platform.api.ApiSpace)27 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)25 Database (com.bluenimble.platform.db.Database)13 DatabaseObject (com.bluenimble.platform.db.DatabaseObject)9 DatabaseException (com.bluenimble.platform.db.DatabaseException)8 ApiOutput (com.bluenimble.platform.api.ApiOutput)7 Api (com.bluenimble.platform.api.Api)6 Storage (com.bluenimble.platform.storage.Storage)6 StorageException (com.bluenimble.platform.storage.StorageException)6 StorageObject (com.bluenimble.platform.storage.StorageObject)6 JsonArray (com.bluenimble.platform.json.JsonArray)5 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)4 JsonQuery (com.bluenimble.platform.db.query.impls.JsonQuery)4 Date (java.util.Date)4 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)3 Config (com.bluenimble.platform.api.impls.im.LoginServiceSpi.Config)3 Cache (com.bluenimble.platform.cache.Cache)3