Search in sources :

Example 16 with ApiManagementException

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

the class ScriptableApiServiceSpi method onStart.

@Override
public void onStart(Api api, ApiService service, ApiContext context) throws ApiManagementException {
    SpecAndSpiPair helper = (SpecAndSpiPair) api.getHelper();
    if (helper == null) {
        throw new ApiManagementException("api '" + api.getNamespace() + "' doesn't support scripting or couldn't start correctly.");
    }
    Object jsApi = helper.spec();
    if (jsApi == null) {
        throw new ApiManagementException("api '" + api.getNamespace() + "' doesn't support scripting");
    }
    JsonObject runtime = service.getRuntime();
    String script = Json.getString(runtime, Api.Spec.Runtime.Function);
    if (Lang.isNullOrEmpty(script)) {
        throw new ApiManagementException("script not found in " + ApiUtils.RuntimeKey);
    }
    String[] path = Lang.split(script, Lang.SLASH);
    ApiResource rScript = null;
    try {
        rScript = api.getResourcesManager().get(path);
    } catch (ApiResourcesManagerException ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (rScript == null) {
        throw new ApiManagementException("function '" + Lang.join(path, Lang.SLASH) + "' not found");
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, context);
    // create the spec
    Object jsService = null;
    try {
        jsService = engine.invoke(null, ApiService.class.getSimpleName(), service);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (jsService == null) {
        throw new ApiManagementException("can't create 'service spec' js object");
    }
    // create the spi
    Object spi = null;
    try {
        spi = engine.eval(Supported.Javascript, api, rScript, ScriptContext.Empty);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (spi == null) {
        throw new ApiManagementException("script returned an undefined object");
    }
    service.setHelper(new SpecAndSpiPair(jsService, spi));
    if (!engine.has(spi, Functions.OnStart)) {
        return;
    }
    // invoke onStart
    try {
        engine.invoke(spi, Functions.OnStart, jsApi, jsService, context);
    } catch (ScriptingEngineException ex) {
        ex.setScript(script);
        throw new ApiManagementException(ex.getMessage(), ex);
    }
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) JsonObject(com.bluenimble.platform.json.JsonObject) JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException)

Example 17 with ApiManagementException

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

the class ScriptableApiSpi method onStop.

@Override
public void onStop(Api api, ApiContext context) throws ApiManagementException {
    Object spi = ((SpecAndSpiPair) api.getHelper()).spi();
    if (spi == null) {
        return;
    }
    ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, context);
    if (!engine.has(spi, Functions.OnStop)) {
        return;
    }
    Object jsApi = ((SpecAndSpiPair) api.getHelper()).spec();
    if (jsApi == null) {
        return;
    }
    // invoke onStop
    try {
        engine.invoke(spi, Functions.OnStop, jsApi, context);
    } catch (Exception ex) {
        api.tracer().log(Level.Error, Lang.BLANK, ex);
    }
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ScriptingEngineException(com.bluenimble.platform.scripting.ScriptingEngineException) ApiAuthenticationException(com.bluenimble.platform.api.security.ApiAuthenticationException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException)

Example 18 with ApiManagementException

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

the class FileSystemApiServer method drop.

@Override
public void drop(String spaceNs) throws ApiManagementException {
    if (Spaces.Sys.equals(spaceNs)) {
        throw new ApiManagementException("access denied");
    }
    ApiSpace space = space(spaceNs);
    if (space == null) {
        throw new ApiManagementException("space " + spaceNs + " not found");
    }
    ApiSpaceImpl spaceImpl = (ApiSpaceImpl) space;
    // stop space
    spaceImpl.stop();
    try {
        FileUtils.delete(spaceImpl.home());
    } catch (IOException e) {
        throw new ApiManagementException(e.getMessage(), e);
    }
}
Also used : ApiSpace(com.bluenimble.platform.api.ApiSpace) ApiSpaceImpl(com.bluenimble.platform.api.impls.ApiSpaceImpl) IOException(java.io.IOException) ApiManagementException(com.bluenimble.platform.api.ApiManagementException)

Example 19 with ApiManagementException

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

the class ApiSpaceImpl method addFeature.

@Override
public void addFeature(String name, String feature, String provider, JsonObject spec) throws ApiManagementException {
    feature = feature.toLowerCase();
    provider = provider.toLowerCase();
    tracer.log(Tracer.Level.Info, "add feature [{0}] -> [{1} / provider {2}] ", name, feature, provider);
    JsonObject oFeature = Json.getObject(getFeatures(), feature);
    if (oFeature == null) {
        oFeature = new JsonObject();
        getFeatures().set(feature, oFeature);
    }
    if (oFeature.containsKey(name)) {
        throw new ApiManagementException("feature '" + feature + "/" + name + "/" + provider + "' already available for space " + getNamespace());
    }
    JsonObject uFeature = new JsonObject();
    uFeature.set(ApiSpace.Features.Provider, provider).set(ApiSpace.Features.Spec, spec);
    oFeature.set(name, uFeature);
    try {
        server.getPluginsRegistry().onEvent(Event.AddFeature, this);
    } catch (PluginRegistryException e) {
        throw new ApiManagementException(e.getMessage(), e);
    }
    // save
    save();
}
Also used : JsonObject(com.bluenimble.platform.json.JsonObject) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException)

Example 20 with ApiManagementException

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

the class ApiSpaceImpl method install.

/*
	@Override
	public Api install (ApiStreamSource source) throws ApiManagementException {
		
		boolean started = isStarted ();
		
		// if the space is new, start it before 
		if (!started) {
			
			// start executor (this is important if the space just created since it will be down)
			try {
				started = start ();
			} catch (Exception e) {
				throw new ApiManagementException (e.getMessage (), e);
			}
			
			// notify space creation if it's new
			if (started) {
				try {
					server.getPluginsRegistry ().onEvent (Event.Create, this);
				} catch (Exception ex) {
					throw new ApiManagementException (ex.getMessage (), ex);
				} 
			}
		}
		
		if (!started) {
			throw new ApiManagementException ("Can't install Api, couldn't start owner space");
		}
		
		return _install (source);
	}
	*/
@Override
public Api install(ApiStreamSource source) throws ApiManagementException {
    if (source == null) {
        throw new ApiManagementException("missing api payload");
    }
    InputStream stream = source.stream();
    if (stream == null) {
        throw new ApiManagementException("missing api stream");
    }
    String name = source.name();
    if (Lang.isNullOrEmpty(name)) {
        name = getNamespace() + Lang.UNDERSCORE + Lang.UUID(40);
    }
    tracer.log(Tracer.Level.Info, "install api {0} / {1}", getNamespace(), source.name());
    File apiHome = new File(home, name);
    try {
        ArchiveUtils.decompress(stream, apiHome);
    } catch (Exception e) {
        throw new ApiManagementException(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(stream);
    }
    Api api = install(apiHome);
    return api;
}
Also used : InputStream(java.io.InputStream) Api(com.bluenimble.platform.api.Api) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) File(java.io.File) 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)

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