use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class RenameObjectSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
String provider = (String) request.get(CommonSpec.Provider);
String path = (String) request.get(Spec.Object);
String name = (String) request.get(StorageObject.Fields.Name);
Storage storage = space.feature(Storage.class, provider, request);
StorageObject so = null;
try {
so = storage.root().get(path);
} catch (StorageException e) {
throw new ApiServiceExecutionException("storage object '" + path + "' not found", e).status(ApiResponse.NOT_FOUND);
}
try {
so.rename(name);
} catch (StorageException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Renamed, true));
}
use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class UpdateObjectSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
String provider = (String) request.get(CommonSpec.Provider);
String path = (String) request.get(Spec.Object);
Boolean append = (Boolean) request.get(Spec.Append);
if (append == null) {
append = false;
}
ApiStreamSource ss = (ApiStreamSource) request.get(ApiRequest.Payload, Scope.Stream);
Storage storage = space.feature(Storage.class, provider, request);
StorageObject so = null;
try {
so = storage.root().get(path);
} catch (StorageException e) {
throw new ApiServiceExecutionException("storage object '" + path + "' not found", e).status(ApiResponse.NOT_FOUND);
}
if (so.isFolder() && ss != null) {
throw new ApiServiceExecutionException("object '" + path + "' isn't a valid content aware storage object").status(ApiResponse.BAD_REQUEST);
}
InputStream stream = ss.stream();
try {
so.update(stream, append);
} catch (StorageException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(stream);
}
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Updated, true));
}
use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class DescribeServiceSpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String apiNs = (String) request.get(CommonSpec.Api);
Api uApi;
try {
uApi = MgmUtils.api(consumer, api, apiNs);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.NOT_FOUND);
}
if (uApi == null) {
throw new ApiServiceExecutionException("api '" + apiNs + "' not found").status(ApiResponse.NOT_FOUND);
}
String sVerb = (String) request.get(CommonSpec.Verb);
ApiVerb verb = null;
try {
verb = ApiVerb.valueOf(sVerb.toUpperCase());
} catch (Exception ex) {
verb = ApiVerb.GET;
}
String endpoint = (String) request.get(CommonSpec.Endpoint);
ApiService service = uApi.getServicesManager().get(verb, Lang.SLASH + endpoint);
if (service == null) {
throw new ApiServiceExecutionException("service '" + verb + Lang.SPACE + endpoint + "' not found").status(ApiResponse.NOT_FOUND);
}
return new JsonApiOutput(service.toJson());
}
use of com.bluenimble.platform.api.ApiServiceExecutionException in project serverless by bluenimble.
the class CreateSpaceSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String namespace = (String) request.get(Spec.Space);
JsonObject oSpace = (JsonObject) spaceModel.duplicate().set(ApiSpace.Spec.Namespace, namespace);
// set default secrets
JsonObject defaultSecrets = Json.getObject(Json.getObject(oSpace, ApiSpace.Spec.secrets.class.getSimpleName()), ApiSpace.Secrets.Default);
if (defaultSecrets != null) {
defaultSecrets.set(ApiSpace.Spec.secrets.Key, Lang.UUID(16));
}
// create space
ApiSpace newSpace = null;
try {
newSpace = api.space().create(oSpace);
} catch (ApiManagementException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
// create root keys
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(CommonSpec.Role, Role.ADMIN.name());
List<KeyPair> keys = null;
try {
keys = newSpace.keystore().create(1, null, properties);
} catch (Exception e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
JsonObject result = newSpace.describe(DescribeOption.Info);
if (keys != null) {
result.set(CommonOutput.Keys, keys.get(0).toJson());
}
return new JsonApiOutput(result);
}
use of com.bluenimble.platform.api.ApiServiceExecutionException 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);
}
Aggregations