Search in sources :

Example 6 with ApiManagementException

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

the class FileSystemApiServer method create.

private ApiSpace create(JsonObject oSpace, boolean save) throws ApiManagementException {
    String spaceNs = Json.getString(oSpace, Spec.Namespace);
    if (Lang.isNullOrEmpty(spaceNs)) {
        throw new ApiManagementException("space namespace not found");
    }
    if (!InstallUtils.isValidSpaceNs(spaceNs)) {
        throw new ApiManagementException("invalid space namespace '" + spaceNs + "'");
    }
    File spacesHome = new File(runtimeHome, ConfigKeys.Folders.Spaces);
    File spaceHome = new File(spacesHome, spaceNs);
    if (!spaceHome.exists()) {
        spaceHome.mkdir();
    }
    ApiSpace space = null;
    try {
        space = new ApiSpaceImpl(this, oSpace, spaceHome);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    addSpace(space);
    if (save) {
        try {
            Json.store(oSpace, new File(spaceHome, ConfigKeys.Descriptor.Space));
        } catch (Exception ex) {
            throw new ApiManagementException(ex.getMessage(), ex);
        }
    }
    // notify space creation
    try {
        if (space.isStarted()) {
            getPluginsRegistry().onEvent(Event.Create, space);
        }
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    return space;
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiSpaceImpl(com.bluenimble.platform.api.impls.ApiSpaceImpl) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) File(java.io.File) ServerStartupException(com.bluenimble.platform.server.ServerStartupException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) IOException(java.io.IOException)

Example 7 with ApiManagementException

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

the class SecurityUtils method tokenAndExpiration.

public static String[] tokenAndExpiration(Api api, JsonObject entity, Date now) throws ApiServiceExecutionException {
    String thing = salt(api, entity);
    JsonObject auth = Json.getObject(Json.getObject(Json.getObject(api.getSecurity(), Api.Spec.Security.Schemes), Schemes.Token), Api.Spec.Security.Auth);
    if (auth == null) {
        auth = Json.getObject(Json.getObject(Json.getObject(api.getSecurity(), Api.Spec.Security.Schemes), Schemes.Cookie), Api.Spec.Security.Auth);
    }
    String secretsName = Json.getString(auth, ApiSpace.Spec.secrets.class.getSimpleName(), ApiSpace.Secrets.Default);
    // encrypt
    JsonObject secrets;
    try {
        secrets = api.space().getSecrets(secretsName);
    } catch (ApiManagementException e) {
        throw new ApiServiceExecutionException(e.getMessage(), e);
    }
    if (secrets == null || !secrets.containsKey(ApiSpace.Spec.secrets.Key)) {
        throw new ApiServiceExecutionException("space secrets '" + secretsName + "' not found").status(ApiResponse.SERVICE_UNAVAILABLE);
    }
    Crypto.Algorithm alg = null;
    try {
        alg = Crypto.Algorithm.valueOf(Json.getString(secrets, ApiSpace.Spec.secrets.Algorithm, Crypto.Algorithm.AES.name()).toUpperCase());
    } catch (Exception ex) {
        alg = Crypto.Algorithm.AES;
    }
    long expiresOn = now.getTime() + Json.getLong(secrets, ApiSpace.Spec.secrets.Age, 60) * 60 * 1000;
    String toEncrypt = expiresOn + Lang.SPACE + thing;
    try {
        return new String[] { new String(Lang.encodeHex(Crypto.encrypt(toEncrypt.getBytes(), Json.getString(secrets, ApiSpace.Spec.secrets.Key), alg))), Lang.toUTC(new Date(expiresOn)) };
    } catch (Exception ex) {
        throw new ApiServiceExecutionException(ex.getMessage(), ex);
    }
}
Also used : Crypto(com.bluenimble.platform.Crypto) ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) JsonObject(com.bluenimble.platform.json.JsonObject) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) Date(java.util.Date)

Example 8 with ApiManagementException

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

the class ApiSpaceImpl method alter.

@Override
public void alter(String spaceNs, JsonObject change) throws ApiManagementException {
    try {
        ApiSpaceImpl space = (ApiSpaceImpl) space(spaceNs);
        JsonObject descriptor = space.getDescriptor();
        descriptor.putAll(change);
    } catch (Exception e) {
        throw new ApiManagementException(e.getMessage(), e);
    }
    // save
    save();
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiRequestSignerException(com.bluenimble.platform.api.security.ApiRequestSignerException) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) IOException(java.io.IOException) ApiAccessDeniedException(com.bluenimble.platform.api.ApiAccessDeniedException) SpaceKeyStoreException(com.bluenimble.platform.security.SpaceKeyStoreException) FeatureNotFoundException(com.bluenimble.platform.server.FeatureNotFoundException)

Example 9 with ApiManagementException

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

the class ApiSpaceImpl method start.

@Override
public void start(String apiNs) throws ApiManagementException {
    ApiImpl api = (ApiImpl) api(apiNs);
    if (api == null) {
        throw new ApiManagementException("api " + apiNs + " not found");
    }
    if (ApiStatus.Failed.equals(api.status())) {
        throw new ApiManagementException("can't start api " + apiNs + ". Status=" + api.status());
    }
    if (ApiStatus.Running.equals(api.status())) {
        return;
    }
    if (ApiStatus.Paused.equals(api.status())) {
        api.setStatus(ApiStatus.Running, true);
        return;
    }
    // start api
    api.start(false);
    // notify plugins
    try {
        server.getPluginsRegistry().onEvent(Event.Start, api);
    } catch (PluginRegistryException e) {
        throw new ApiManagementException(e.getMessage(), e);
    }
}
Also used : ApiManagementException(com.bluenimble.platform.api.ApiManagementException) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException)

Example 10 with ApiManagementException

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

the class ApiSpaceImpl method stop.

@Override
public void stop(String apiNs) throws ApiManagementException {
    ApiImpl api = (ApiImpl) api(apiNs);
    if (api == null) {
        throw new ApiManagementException("api " + apiNs + " not found");
    }
    if (!ApiStatus.Running.equals(api.status())) {
        throw new ApiManagementException("can't stop api " + apiNs + ". Status=" + api.status());
    }
    // notify plugins
    try {
        server.getPluginsRegistry().onEvent(Event.Stop, api);
    } catch (PluginRegistryException e) {
        throw new ApiManagementException(e.getMessage(), e);
    }
    // stop api
    api.stop(true);
}
Also used : ApiManagementException(com.bluenimble.platform.api.ApiManagementException) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException)

Aggregations

ApiManagementException (com.bluenimble.platform.api.ApiManagementException)23 JsonObject (com.bluenimble.platform.json.JsonObject)13 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)8 PluginRegistryException (com.bluenimble.platform.plugins.PluginRegistryException)8 ApiSpace (com.bluenimble.platform.api.ApiSpace)6 ApiResourcesManagerException (com.bluenimble.platform.api.ApiResourcesManagerException)5 IOException (java.io.IOException)5 ApiAccessDeniedException (com.bluenimble.platform.api.ApiAccessDeniedException)4 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)4 ScriptingEngine (com.bluenimble.platform.scripting.ScriptingEngine)4 ScriptingEngineException (com.bluenimble.platform.scripting.ScriptingEngineException)4 Crypto (com.bluenimble.platform.Crypto)3 ApiResource (com.bluenimble.platform.api.ApiResource)3 JsonApiOutput (com.bluenimble.platform.api.impls.JsonApiOutput)3 File (java.io.File)3 Date (java.util.Date)3 Api (com.bluenimble.platform.api.Api)2 ApiSpaceImpl (com.bluenimble.platform.api.impls.ApiSpaceImpl)2 ApiRequestSignerException (com.bluenimble.platform.api.security.ApiRequestSignerException)2 SpaceKeyStoreException (com.bluenimble.platform.security.SpaceKeyStoreException)2