use of com.bluenimble.platform.api.impls.JsonApiOutput 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.impls.JsonApiOutput 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.impls.JsonApiOutput 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.api.impls.JsonApiOutput in project serverless by bluenimble.
the class OAuthServiceSpi method execute.
@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
JsonObject config = request.getService().getCustom();
JsonObject providers = Json.getObject(config, Providers);
JsonObject provider = Json.getObject(providers, (String) request.get(Spec.Provider));
if (provider == null || provider.isEmpty()) {
throw new ApiServiceExecutionException("provider " + request.get(Spec.Provider) + " not supported").status(ApiResponse.NOT_ACCEPTABLE);
}
JsonObject oAuthKeys = Json.getObject(provider, OAuth.Keys);
if (oAuthKeys == null || oAuthKeys.isEmpty()) {
throw new ApiServiceExecutionException("provider " + request.get(Spec.Provider) + ". client_id and client_secret not found").status(ApiResponse.NOT_ACCEPTABLE);
}
JsonObject oAuthEndpoints = Json.getObject(provider, OAuth.Endpoints);
if (oAuthEndpoints == null || oAuthEndpoints.isEmpty()) {
throw new ApiServiceExecutionException("provider " + request.get(Spec.Provider) + ". oAuth endpoints authorize and profile not configured").status(ApiResponse.NOT_ACCEPTABLE);
}
JsonObject endpoint = Json.getObject(oAuthEndpoints, OAuth.Urls.Authorize);
if (endpoint == null || endpoint.isEmpty()) {
throw new ApiServiceExecutionException("provider " + request.get(Spec.Provider) + ". oAuth authorize endpoint not configured").status(ApiResponse.NOT_ACCEPTABLE);
}
JsonObject data = (JsonObject) new JsonObject().set(OAuth.Code, request.get(Spec.AuthCode)).set(OAuth.ClientId, Json.getString(oAuthKeys, OAuth.ClientId)).set(OAuth.ClientSecret, Json.getString(oAuthKeys, OAuth.ClientSecret));
if (provider.containsKey(OAuth.Redirect)) {
data.set(OAuth.RedirectUri, Json.getString(provider, OAuth.Redirect));
}
JsonObject params = Json.getObject(endpoint, OAuth.Endpoint.Parameters);
if (params != null && !params.isEmpty()) {
Iterator<String> keys = params.keys();
while (keys.hasNext()) {
String p = keys.next();
data.set(p, params.get(p));
}
}
JsonObject hRequest = (JsonObject) new JsonObject().set(OAuth.Endpoint.Url, Json.getString(endpoint, OAuth.Endpoint.Url)).set(OAuth.Endpoint.Headers, new JsonObject().set(HttpHeaders.ACCEPT, ContentTypes.Json)).set(OAuth.Endpoint.Data, data);
HttpResponse hResponse = null;
try {
hResponse = Http.post(hRequest, null);
} catch (HttpClientException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
if (hResponse.getStatus() != 200) {
throw new ApiServiceExecutionException("invalid authorization code");
}
InputStream out = hResponse.getBody().get(0).toInputStream();
JsonObject oAuthResult = null;
try {
oAuthResult = new JsonObject(out);
} catch (Exception e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(out);
}
// get profile
endpoint = Json.getObject(oAuthEndpoints, OAuth.Urls.Profile);
if (endpoint == null || endpoint.isEmpty()) {
return new JsonApiOutput(oAuthResult);
}
String accessToken = Json.getString(oAuthResult, OAuth.AccessToken);
data.clear();
data.set(OAuth.AccessToken, accessToken);
hRequest = (JsonObject) new JsonObject().set(OAuth.Endpoint.Url, Json.getString(endpoint, OAuth.Endpoint.Url)).set(OAuth.Endpoint.Headers, new JsonObject().set(HttpHeaders.ACCEPT, ContentTypes.Json)).set(OAuth.Endpoint.Data, data);
try {
hResponse = Http.post(hRequest, null);
} catch (HttpClientException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
if (hResponse.getStatus() != 200) {
throw new ApiServiceExecutionException("invalid access token");
}
out = hResponse.getBody().get(0).toInputStream();
try {
oAuthResult = new JsonObject(out);
} catch (Exception e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(out);
}
// email endpoint
endpoint = Json.getObject(oAuthEndpoints, OAuth.Urls.Email);
if (endpoint == null || endpoint.isEmpty()) {
return new JsonApiOutput(oAuthResult);
}
hRequest = (JsonObject) new JsonObject().set(OAuth.Endpoint.Url, Json.getString(endpoint, OAuth.Endpoint.Url)).set(OAuth.Endpoint.Headers, new JsonObject().set(HttpHeaders.ACCEPT, ContentTypes.Json)).set(OAuth.Endpoint.Data, data);
try {
hResponse = Http.post(hRequest, null);
} catch (HttpClientException e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
}
if (hResponse.getStatus() != 200) {
throw new ApiServiceExecutionException("invalid access token");
}
out = hResponse.getBody().get(0).toInputStream();
JsonObject oEmail = null;
try {
oEmail = new JsonObject(out);
} catch (Exception e) {
throw new ApiServiceExecutionException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(out);
}
Iterator<String> keys = oEmail.keys();
while (keys.hasNext()) {
String k = keys.next();
oAuthResult.set(k, oEmail.get(k));
}
// call extend if any
JsonObject onFinish = Json.getObject(config, Config.onFinish.class.getSimpleName());
ApiOutput onFinishOutput = SecurityUtils.onFinish(api, consumer, request, onFinish, oAuthResult);
if (onFinishOutput != null) {
oAuthResult.set(Json.getString(onFinish, Config.onFinish.ResultProperty, Config.onFinish.class.getSimpleName()), onFinishOutput.data());
}
return new JsonApiOutput(oAuthResult);
}
use of com.bluenimble.platform.api.impls.JsonApiOutput 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