use of com.bluenimble.platform.api.ApiVerb 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.ApiVerb in project serverless by bluenimble.
the class SecurityUtils method onFinish.
public static ApiOutput onFinish(Api api, ApiConsumer consumer, ApiRequest pRequest, final JsonObject onFinish, JsonObject account) throws ApiServiceExecutionException {
if (onFinish == null || onFinish.isEmpty()) {
return null;
}
ApiRequest request = api.space().request(pRequest, consumer, new Endpoint() {
@Override
public String space() {
return Json.getString(onFinish, Config.onFinish.Space, api.space().getNamespace());
}
@Override
public String api() {
return Json.getString(onFinish, Config.onFinish.Api, api.getNamespace());
}
@Override
public String[] resource() {
String resource = Json.getString(onFinish, Config.onFinish.Resource);
if (resource.startsWith(Lang.SLASH)) {
resource = resource.substring(1);
}
if (resource.endsWith(Lang.SLASH)) {
resource = resource.substring(0, resource.length() - 1);
}
if (Lang.isNullOrEmpty(resource)) {
return null;
}
return Lang.split(resource, Lang.SLASH);
}
@Override
public ApiVerb verb() {
try {
return ApiVerb.valueOf(Json.getString(onFinish, Config.onFinish.Verb, ApiVerb.POST.name()).toUpperCase());
} catch (Exception ex) {
return ApiVerb.POST;
}
}
});
request.set(ApiRequest.Payload, account);
return api.call(request);
}
use of com.bluenimble.platform.api.ApiVerb in project serverless by bluenimble.
the class ApiImpl method lockup.
public ApiService lockup(ApiRequest request) {
if (request == null) {
return null;
}
ApiVerb verb = request.getVerb();
if (verb == null) {
verb = ApiVerb.GET;
}
ApiService service = lockup(verb, request.getResource());
resolveParameters(service, request);
return service;
}
use of com.bluenimble.platform.api.ApiVerb 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.ApiVerb in project serverless by bluenimble.
the class ApiUtils method call.
public static ApiOutput call(final Api api, final ApiConsumer consumer, final ApiRequest pRequest, final JsonObject oRequest) throws ApiServiceExecutionException {
ApiRequest request = api.space().request(pRequest, consumer, new Endpoint() {
@Override
public String space() {
return Json.getString(oRequest, Spec.Space, api.space().getNamespace());
}
@Override
public String api() {
return Json.getString(oRequest, Spec.Api, api.getNamespace());
}
@Override
public String[] resource() {
String resource = Json.getString(oRequest, Spec.Service);
if (resource.startsWith(Lang.SLASH)) {
resource = resource.substring(1);
}
if (resource.endsWith(Lang.SLASH)) {
resource = resource.substring(0, resource.length() - 1);
}
if (Lang.isNullOrEmpty(resource)) {
return null;
}
return Lang.split(resource, Lang.SLASH);
}
@Override
public ApiVerb verb() {
try {
return ApiVerb.valueOf(Json.getString(oRequest, Spec.Verb, ApiVerb.POST.name()).toUpperCase());
} catch (Exception ex) {
return ApiVerb.POST;
}
}
});
JsonObject parameters = Json.getObject(oRequest, Spec.Parameters);
if (!Json.isNullOrEmpty(parameters)) {
Iterator<String> keys = parameters.keys();
while (keys.hasNext()) {
String key = keys.next();
request.set(key, parameters.get(key));
}
}
JsonObject headers = Json.getObject(oRequest, Spec.Headers);
if (!Json.isNullOrEmpty(headers)) {
Iterator<String> keys = headers.keys();
while (keys.hasNext()) {
String key = keys.next();
request.set(key, headers.get(key), Scope.Header);
}
}
return api.call(request);
}
Aggregations