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()));
}
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));
}
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));
}
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);
}
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;
}
Aggregations