use of com.bluenimble.platform.db.Database 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.db.Database in project serverless by bluenimble.
the class AddRecordSpi 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);
final JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
DatabaseObject dbo;
try {
Database db = space.feature(Database.class, provider, request);
dbo = db.create(sEntity);
dbo.load(payload);
dbo.save();
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(ApiOutput.Defaults.Id, dbo.getId()).set(ApiOutput.Defaults.Timestamp, Lang.toUTC(dbo.getTimestamp())));
}
use of com.bluenimble.platform.db.Database in project serverless by bluenimble.
the class CreateEntitySpi 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);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
JsonArray aFields = Json.getArray(payload, CommonSpec.Fields);
if (aFields == null || aFields.isEmpty()) {
throw new ApiServiceExecutionException("missing fields definition").status(ApiResponse.BAD_REQUEST);
}
Field[] fields = new Field[aFields.count()];
for (int i = 0; i < aFields.count(); i++) {
Object o = aFields.get(i);
if (o == null || !(o instanceof JsonObject)) {
continue;
}
final JsonObject oField = (JsonObject) o;
fields[i] = new Field() {
@Override
public String name() {
return Json.getString(oField, Spec.Field.Name);
}
@Override
public boolean required() {
return Json.getBoolean(oField, Spec.Field.Required, false);
}
@Override
public Type type() {
try {
return Type.valueOf(Json.getString(oField, Spec.Field.Type, Type.String.name()));
} catch (Exception ex) {
return Type.String;
}
}
@Override
public boolean unique() {
return Json.getBoolean(oField, Spec.Field.Unique, false);
}
};
}
try {
Database db = space.feature(Database.class, provider, request);
db.createEntity(sEntity, fields);
} catch (DatabaseException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
} catch (Exception e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Dropped, true));
}
use of com.bluenimble.platform.db.Database in project serverless by bluenimble.
the class LoginServiceSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
JsonObject config = request.getService().getCustom();
JsonObject payload = (JsonObject) request.get(ApiRequest.Payload);
Database db = api.space().feature(Database.class, Json.getString(config, Config.Database, ApiSpace.Features.Default), request);
boolean encryptPassword = Json.getBoolean(config, Config.EncryptPassword, true);
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.UserProperty, Fields.Email), payload.get(Spec.User));
where.set(Json.getString(config, Config.PasswordProperty, Fields.Password), encryptPassword ? Crypto.md5(Json.getString(payload, Spec.Password), Encodings.UTF8) : Json.getString(payload, Spec.Password));
}
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.UNAUTHORIZED);
}
boolean active = true;
boolean requiresActivation = Json.getBoolean(config, Config.RequiresActivation, false);
if (requiresActivation && account.get(Json.getString(config, Config.ActivationCodeProperty, Defaults.ActivationCode)) != null) {
active = false;
}
JsonObject oAccount = account.toJson(DefaultDatabaseObjectSerializer.Default);
oAccount.remove(Json.getString(config, Config.PasswordProperty, Spec.Password));
if (active) {
Date now = new Date();
// update lastLogin
try {
account.set(Json.getString(config, Config.LastLoginProperty, Fields.LastLogin), now);
account.save();
} catch (Exception ex) {
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
// create token
String[] tokenAndExpiration = SecurityUtils.tokenAndExpiration(api, oAccount, now);
oAccount.set(Defaults.Token, tokenAndExpiration[0]);
oAccount.set(Defaults.ExpiresOn, tokenAndExpiration[1]);
}
// call extend if any
JsonObject onFinish = Json.getObject(config, Config.onFinish.class.getSimpleName());
ApiOutput onFinishOutput = SecurityUtils.onFinish(api, consumer, request, onFinish, oAccount);
oAccount.remove(Database.Fields.Id);
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.db.Database in project serverless by bluenimble.
the class ResendActivationRequestSpi 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));
} catch (Exception ex) {
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
if (account == null) {
throw new ApiServiceExecutionException("account not found").status(ApiResponse.NOT_FOUND);
}
String email = (String) account.get(Json.getString(config, Config.UserProperty, Fields.Email));
if (Lang.isNullOrEmpty(email)) {
throw new ApiServiceExecutionException("user email not found").status(ApiResponse.NOT_FOUND);
}
JsonObject oEmail = Json.getObject(config, Config.SignupEmail);
String feature = Json.getString(oEmail, Email.Messenger);
String template = Json.getString(oEmail, Email.Template);
if (oEmail != null && !Lang.isNullOrEmpty(feature) && !Lang.isNullOrEmpty(template)) {
String fromEmail = Json.getString(oEmail, Email.FromEmail);
String fromName = Json.getString(oEmail, Email.FromName);
String subject = Json.getString(oEmail, Email.Subject, "Welcome to " + api.getName());
final Messenger messenger = api.space().feature(Messenger.class, feature, request);
final JsonObject emailTemplateData = account.toJson(null);
try {
final String fEmail = email;
api.space().executor().execute(new Callable<Void>() {
@Override
public Void call() {
try {
messenger.send(new JsonSender((JsonObject) new JsonObject().set(JsonActor.Spec.Id, fromEmail).set(JsonActor.Spec.Name, fromName)), new JsonRecipient[] { new JsonRecipient((JsonObject) new JsonObject().set(JsonActor.Spec.Id, fEmail)) }, subject, api.getResourcesManager().get(Lang.split(template, Lang.SLASH)), emailTemplateData);
} catch (Exception ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
return null;
}
}, CodeExecutor.Mode.Async);
} catch (Exception ex) {
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
}
return new JsonApiOutput(JsonObject.Blank);
}
Aggregations