Search in sources :

Example 1 with Api

use of com.bluenimble.platform.api.Api 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);
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) Api(com.bluenimble.platform.api.Api) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 2 with Api

use of com.bluenimble.platform.api.Api in project serverless by bluenimble.

the class DescribeApiSpi 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);
    }
    return new JsonApiOutput(uApi.describe(MgmUtils.options((String) request.get(CommonSpec.Options), DescribeOption.Info)));
}
Also used : ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Api(com.bluenimble.platform.api.Api) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 3 with Api

use of com.bluenimble.platform.api.Api in project serverless by bluenimble.

the class InstallApiFromFileSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String spaceFolder = (String) request.get(Spec.SpaceFolder);
    String apiFile = (String) request.get(Spec.ApiFile);
    Boolean start = (Boolean) request.get(Spec.Start);
    if (start == null) {
        start = true;
    }
    ApiSpace targetSpace = null;
    Api installed = null;
    try {
        targetSpace = MgmUtils.space(consumer, api);
        installed = targetSpace.install(spaceFolder, apiFile);
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    if (installed == null) {
        throw new ApiServiceExecutionException("can't install api. Server can't return a valid api object");
    }
    try {
        if (start) {
            targetSpace.start(installed.getNamespace());
        }
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    return new JsonApiOutput(installed.describe(MgmUtils.options((String) request.get(CommonSpec.Options), DescribeOption.Info)));
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Api(com.bluenimble.platform.api.Api) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 4 with Api

use of com.bluenimble.platform.api.Api in project serverless by bluenimble.

the class InstallApiSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    ApiStreamSource stream = (ApiStreamSource) request.get(ApiRequest.Payload, Scope.Stream);
    Boolean start = (Boolean) request.get(Spec.Start);
    if (start == null) {
        start = false;
    }
    ApiSpace targetSpace = null;
    Api installed = null;
    try {
        targetSpace = MgmUtils.space(consumer, api);
        installed = targetSpace.install(stream);
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    if (installed == null) {
        throw new ApiServiceExecutionException("can't install api. Server can't return a valid api object");
    }
    try {
        if (start) {
            targetSpace.start(installed.getNamespace());
        }
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
    return new JsonApiOutput(installed.describe(MgmUtils.options((String) request.get(CommonSpec.Options), DescribeOption.Info)));
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) Api(com.bluenimble.platform.api.Api) ApiStreamSource(com.bluenimble.platform.api.ApiStreamSource) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonApiOutput(com.bluenimble.platform.api.impls.JsonApiOutput)

Example 5 with Api

use of com.bluenimble.platform.api.Api 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)

Aggregations

Api (com.bluenimble.platform.api.Api)12 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)7 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)6 ApiSpace (com.bluenimble.platform.api.ApiSpace)4 JsonObject (com.bluenimble.platform.json.JsonObject)4 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)3 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)3 IOException (java.io.IOException)3 ApiStatus (com.bluenimble.platform.api.ApiStatus)2 ApiVerb (com.bluenimble.platform.api.ApiVerb)2 PluginRegistryException (com.bluenimble.platform.plugins.PluginRegistryException)2 SpaceKeyStoreException (com.bluenimble.platform.security.SpaceKeyStoreException)2 FeatureNotFoundException (com.bluenimble.platform.server.FeatureNotFoundException)2 File (java.io.File)2 ApiResponse (com.bluenimble.platform.api.ApiResponse)1 Status (com.bluenimble.platform.api.ApiResponse.Status)1 ApiService (com.bluenimble.platform.api.ApiService)1 ApiStreamSource (com.bluenimble.platform.api.ApiStreamSource)1 CodeExecutorException (com.bluenimble.platform.api.CodeExecutorException)1 DescribeOption (com.bluenimble.platform.api.DescribeOption)1