use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class DownloadRootKeysSpi method execute.
@Override
public ApiOutput execute(Api api, final ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
String paraphrase = (String) request.get(Spec.Paraphrase);
try {
KeyPair kp = api.space().getRootKeys();
JsonObject oKeys = new JsonObject();
oKeys.set(Output.Name, Json.getString(request.getNode(), ApiRequest.Fields.Node.Id) + " " + Json.getString(request.getNode(), ApiRequest.Fields.Node.Version));
oKeys.set(Output.Endpoint, request.getScheme() + "://" + request.getEndpoint() + Lang.SLASH + api.space().getNamespace() + Lang.SLASH + api.getNamespace());
oKeys.set(KeyPair.Fields.AccessKey, kp.accessKey());
oKeys.set(KeyPair.Fields.SecretKey, kp.secretKey());
oKeys.set(CommonSpec.Role, "SUPER");
ByteArrayOutputStream out = new ByteArrayOutputStream();
Json.encrypt(oKeys, paraphrase, out);
return new ApiByteArrayOutput(Output.KeysName + Lang.DOT + Output.KeysExt, Base64.encodeBase64(out.toByteArray()), ApiContentTypes.Stream, Output.KeysExt).set(ApiOutput.Defaults.Disposition, "attachment");
} catch (Exception e) {
throw new ApiServiceExecutionException(e.getMessage()).status(ApiResponse.FORBIDDEN);
}
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class GetKeysSpi method toOutput.
private ApiOutput toOutput(KeyPair kp, String paraphrase, ApiSpace keysSpace, Api api, ApiRequest request) throws EncryptionProviderException {
JsonObject oKeys = new JsonObject();
oKeys.set(Output.Name, keysSpace.getName());
oKeys.set(Output.Space, keysSpace.getNamespace());
oKeys.set(Output.Endpoint, request.getScheme() + "://" + request.getEndpoint() + Lang.SLASH + api.space().getNamespace() + Lang.SLASH + api.getNamespace());
oKeys.set(Output.Domain, request.getScheme() + "://" + request.getEndpoint() + Lang.SLASH + keysSpace.getNamespace());
if (kp.expiryDate() != null) {
oKeys.set(KeyPair.Fields.ExpiryDate, Lang.toUTC(kp.expiryDate()));
}
oKeys.set(KeyPair.Fields.AccessKey, kp.accessKey());
oKeys.set(KeyPair.Fields.SecretKey, kp.secretKey());
Iterator<String> props = kp.properties();
if (props != null) {
JsonObject oProps = new JsonObject();
oKeys.set(KeyPair.Fields.Properties, oProps);
while (props.hasNext()) {
String p = props.next();
oProps.set(p, kp.property(p));
}
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
Json.encrypt(oKeys, paraphrase, out);
return new ApiByteArrayOutput(keysSpace.getNamespace() + Lang.DOT + Output.KeysExt, Base64.encodeBase64(out.toByteArray()), ApiContentTypes.Stream, Output.KeysExt).set(ApiOutput.Defaults.Disposition, "attachment");
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class MgmApiSpi method findConsumer.
@Override
public void findConsumer(Api api, ApiService service, ApiRequest request, ApiConsumer consumer) throws ApiAuthenticationException {
Type type = consumer.type();
if ("container".equals(request.getChannel())) {
consumer.override((ApiConsumer) request.get(ApiRequest.Consumer));
return;
}
if (!this.isSecure(service)) {
return;
}
if (!type.equals(Type.Signature)) {
throw new ApiAuthenticationException("unsupported authentication mechanism");
}
String accessKey = (String) consumer.get(ApiConsumer.Fields.AccessKey);
final JsonObject oConsumer = Json.getObject(consumers, accessKey);
if (oConsumer == null || oConsumer.isEmpty()) {
throw new ApiAuthenticationException("accessKey not found");
}
Iterator<String> keys = oConsumer.keys();
if (keys == null) {
return;
}
while (keys.hasNext()) {
String key = keys.next();
consumer.set(key, oConsumer.get(key));
}
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class ChangeApiStatusSpi 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 - 1];
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);
}
ApiSpace space = null;
try {
space = MgmUtils.space(consumer, api);
switch(action) {
case start:
space.start(apiNs);
break;
case stop:
space.stop(apiNs);
break;
case pause:
space.pause(apiNs);
break;
case resume:
space.resume(apiNs);
break;
default:
break;
}
} catch (Exception ex) {
throw new ApiServiceExecutionException(ex.getMessage(), ex);
}
Api targetApi = space.api(apiNs);
JsonObject result = (JsonObject) new JsonObject().set(Api.Spec.Status, targetApi.status().name());
if (ApiStatus.Failed.equals(targetApi.status())) {
result.set(Output.Reason, targetApi.getFailure());
}
return new JsonApiOutput(result);
}
use of com.bluenimble.platform.json.JsonObject in project serverless by bluenimble.
the class DeleteEntrySpi 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 key = (String) request.get(Spec.Key);
ApiSpace space;
try {
space = MgmUtils.space(consumer, api);
} catch (ApiAccessDeniedException e) {
throw new ApiServiceExecutionException(e.getMessage(), e).status(ApiResponse.FORBIDDEN);
}
Cache cache = space.feature(Cache.class, provider, request);
cache.delete(key);
return new JsonApiOutput((JsonObject) new JsonObject().set(CommonOutput.Deleted, true));
}
Aggregations