Search in sources :

Example 1 with ApiService

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());
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiService(com.bluenimble.platform.api.ApiService) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Api(com.bluenimble.platform.api.Api) ApiVerb(com.bluenimble.platform.api.ApiVerb) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 2 with ApiService

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;
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ValueHolder(com.bluenimble.platform.ValueHolder) JsonArray(com.bluenimble.platform.json.JsonArray) ApiService(com.bluenimble.platform.api.ApiService) DescribeOption(com.bluenimble.platform.api.DescribeOption) DescribeOption(com.bluenimble.platform.api.DescribeOption) Selector(com.bluenimble.platform.api.ApiServicesManager.Selector)

Example 3 with ApiService

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);
}
Also used : ApiServicesManagerException(com.bluenimble.platform.api.ApiServicesManagerException) ApiService(com.bluenimble.platform.api.ApiService)

Example 4 with ApiService

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);
}
Also used : ApiServicesManagerException(com.bluenimble.platform.api.ApiServicesManagerException) ApiService(com.bluenimble.platform.api.ApiService)

Example 5 with ApiService

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;
}
Also used : ApiService(com.bluenimble.platform.api.ApiService) ApiServiceSet(com.bluenimble.platform.server.impls.fs.ApiServiceSet)

Aggregations

ApiService (com.bluenimble.platform.api.ApiService)11 ApiServicesManagerException (com.bluenimble.platform.api.ApiServicesManagerException)4 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)3 Selector (com.bluenimble.platform.api.ApiServicesManager.Selector)3 JsonArray (com.bluenimble.platform.json.JsonArray)3 JsonObject (com.bluenimble.platform.json.JsonObject)3 ValueHolder (com.bluenimble.platform.ValueHolder)2 ApiVerb (com.bluenimble.platform.api.ApiVerb)2 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)2 ApiServiceValidatorException (com.bluenimble.platform.api.validation.ApiServiceValidatorException)2 ApiServiceSet (com.bluenimble.platform.server.impls.fs.ApiServiceSet)2 Recyclable (com.bluenimble.platform.Recyclable)1 Api (com.bluenimble.platform.api.Api)1 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)1 ApiContext (com.bluenimble.platform.api.ApiContext)1 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)1 ApiOutput (com.bluenimble.platform.api.ApiOutput)1 ApiResourcesManagerException (com.bluenimble.platform.api.ApiResourcesManagerException)1 ApiResponse (com.bluenimble.platform.api.ApiResponse)1 ApiStatus (com.bluenimble.platform.api.ApiStatus)1