use of com.bluenimble.platform.api.ApiService 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.ApiService in project serverless by bluenimble.
the class ApiImpl method describe.
@Override
public JsonObject describe(final DescribeOption... options) {
if (options == null || options.length == 0) {
return JsonObject.Blank;
}
final Map<DescribeOption.Option, DescribeOption> opts = DescribeUtils.toMap(options);
JsonObject describe = new JsonObject();
if (opts.containsKey(DescribeOption.Option.info)) {
describe.set(Api.Spec.Namespace, getNamespace());
describe.set(Api.Spec.Name, getName());
describe.set(Api.Spec.Description, getDescription());
describe.set(Api.Spec.Status, status().name());
if (failure != null) {
describe.set(Describe.Failure, failure);
}
describe.set(Api.Spec.Release, getRelease());
}
if (getSecurity() != null && opts.containsKey(DescribeOption.Option.security)) {
describe.set(Api.Spec.Security.class.getSimpleName().toLowerCase(), getSecurity().duplicate());
}
if (getTracking() != null && opts.containsKey(DescribeOption.Option.tracking)) {
describe.set(Api.Spec.Tracking.class.getSimpleName().toLowerCase(), getTracking().duplicate());
}
if (getFeatures() != null && opts.containsKey(DescribeOption.Option.features)) {
describe.set(Api.Spec.Features, getFeatures().duplicate());
}
if (getCustom() != null && opts.containsKey(DescribeOption.Option.custom)) {
describe.set(Api.Spec.Custom, getCustom().duplicate());
}
if (servicesManager.isEmpty(null)) {
return describe;
}
final JsonObject failedServices = new JsonObject();
describe.set(Describe.FailedServices, failedServices);
JsonArray aServices = new JsonArray();
describe.set(Describe.Services, aServices);
final JsonArray fServices = aServices;
final ValueHolder<Integer> apiMarkers = new ValueHolder<Integer>(0);
servicesManager.list(new Selector() {
@Override
public boolean select(ApiService service) {
if (opts.containsKey(DescribeOption.Option.services)) {
DescribeOption opt = opts.get(DescribeOption.Option.services);
JsonObject jService = service.toJson();
JsonObject sDesc = jService;
if (!opt.isVerbose()) {
sDesc = new JsonObject();
sDesc.set(ApiService.Spec.Name, jService.get(ApiService.Spec.Name));
sDesc.set(ApiService.Spec.Endpoint, jService.get(ApiService.Spec.Endpoint));
sDesc.set(ApiService.Spec.Verb, jService.get(ApiService.Spec.Verb));
sDesc.set(ApiService.Spec.Status, jService.get(ApiService.Spec.Status));
sDesc.set(ApiService.Spec.Security.class.getSimpleName().toLowerCase(), service.getSecurity());
JsonArray aMarkers = Json.getArray(jService, Api.Spec.Markers);
int markers = aMarkers != null ? aMarkers.size() : 0;
apiMarkers.set(apiMarkers.get() + markers);
if (markers > 0) {
sDesc.set(Api.Spec.Markers, markers);
}
}
fServices.add(sDesc);
}
if (ApiStatus.Failed.equals(service.status())) {
failedServices.put(service.getVerb().name() + Lang.SPACE + Json.getString(service.toJson(), ApiService.Spec.Endpoint), service.getFailure());
}
return false;
}
});
describe.set(Api.Spec.Markers, apiMarkers.get());
if (fServices.isEmpty()) {
describe.remove(Describe.Services);
}
if (failedServices.isEmpty()) {
describe.remove(Describe.FailedServices);
}
return describe;
}
use of com.bluenimble.platform.api.ApiService in project serverless by bluenimble.
the class DefaultApiServicesManager method start.
@Override
public void start(ApiVerb verb, String endpoint) throws ApiServicesManagerException {
ApiService service = get(verb, endpoint);
if (service == null) {
throw new ApiServicesManagerException("service [" + verb + " " + endpoint + "] not found");
}
if (!service.status().equals(ApiStatus.Stopped)) {
throw new ApiServicesManagerException("unnable to start service [" + verb + " " + endpoint + "]. Status=" + service.status());
}
startService(service, null, false);
}
use of com.bluenimble.platform.api.ApiService in project serverless by bluenimble.
the class DefaultApiServicesManager method stop.
@Override
public void stop(ApiVerb verb, String endpoint) throws ApiServicesManagerException {
ApiService service = get(verb, endpoint);
if (service == null) {
throw new ApiServicesManagerException("service [" + verb + " " + endpoint + "] not found");
}
if (!service.status().equals(ApiStatus.Running) || !service.status().equals(ApiStatus.Paused)) {
throw new ApiServicesManagerException("unnable to stop service [" + verb + " " + endpoint + "]. Status=" + service.status());
}
stopService(service, null, true);
}
use of com.bluenimble.platform.api.ApiService in project serverless by bluenimble.
the class DefaultApiServicesManager method get.
@Override
public ApiService get(ApiVerb verb, String endpoint) {
ApiServiceSet set = services.get(verb);
if (set == null) {
return null;
}
ApiService service = set.get(resolveEndpoint(endpoint));
if (service == null) {
return null;
}
return service;
}
Aggregations