Search in sources :

Example 1 with ApiResourcesManagerException

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

the class DefaultApiResourcesManager method put.

@Override
public ApiResource put(String[] path, InputStream payload, boolean overwrite) throws ApiResourcesManagerException {
    if (path == null || path.length == 0) {
        throw new ApiResourcesManagerException("invalid or null resource path");
    }
    if (path.length == 1 && Reserved.contains(path[0])) {
        throw new ApiResourcesManagerException("a protected resource called " + path[0] + " already exists");
    }
    String sPath = checkAndGetPath(path);
    if (Lang.isNullOrEmpty(sPath)) {
        throw new ApiResourcesManagerException("invalid resource path " + Lang.join(path, Lang.SLASH));
    }
    File parent = null;
    String[] aParent = Lang.moveRight(path, 1);
    if (aParent == null) {
        parent = resources;
    } else {
        parent = new File(resources, Lang.join(aParent));
    }
    File file = new File(parent, path[path.length - 1]);
    if (file.exists()) {
        if (file.isFile()) {
            if (!overwrite) {
                throw new ApiResourcesManagerException("resource " + sPath + " already exists");
            }
        } else {
            throw new ApiResourcesManagerException("resource " + sPath + " already exists");
        }
    }
    if (parent.exists()) {
        if (parent.isFile()) {
            throw new ApiResourcesManagerException("parent resource " + Lang.join(aParent, Lang.SLASH) + " is a file. It should be a valid folder");
        }
    } else {
        parent.mkdirs();
    }
    FileApiResource resource = new FileApiResource(owner, resources, file);
    if (payload == null) {
        file.mkdir();
    } else {
        try {
            resource.pipe(payload, 0, -1);
        } catch (IOException e) {
            throw new ApiResourcesManagerException(e.getMessage(), e);
        }
    }
    return new FileApiResource(owner, resources, file);
}
Also used : FileApiResource(com.bluenimble.platform.server.impls.fs.FileApiResource) IOException(java.io.IOException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) File(java.io.File)

Example 2 with ApiResourcesManagerException

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

the class DefaultApiServicesManager method load.

@Override
public void load(Api api) throws ApiServicesManagerException {
    this.api = (ApiImpl) api;
    ApiResourcesManager rmgr = api.getResourcesManager();
    try {
        load(rmgr, rmgr.get(new String[] { ConfigKeys.Folders.Services }));
    } catch (ApiResourcesManagerException e) {
        throw new ApiServicesManagerException(e.getMessage(), e);
    }
}
Also used : ApiServicesManagerException(com.bluenimble.platform.api.ApiServicesManagerException) ApiResourcesManager(com.bluenimble.platform.api.ApiResourcesManager) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException)

Example 3 with ApiResourcesManagerException

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

the class ScriptableApiSpi method onStart.

@Override
public void onStart(Api api, ApiContext context) throws ApiManagementException {
    String script = Json.getString(api.getRuntime(), Api.Spec.Runtime.Function);
    if (Lang.isNullOrEmpty(script)) {
        throw new ApiManagementException("function not defined 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 spi
    Object jsSpi = null;
    try {
        jsSpi = engine.eval(Supported.Javascript, api, rScript, ScriptContext.Empty);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (jsSpi == null) {
        throw new ApiManagementException("function returned an undefined object");
    }
    // create the api object
    Object jsApi = null;
    try {
        jsApi = engine.invoke(null, Api.class.getSimpleName(), api);
    } catch (Exception ex) {
        throw new ApiManagementException(ex.getMessage(), ex);
    }
    if (jsApi == null) {
        throw new ApiManagementException("can't create 'api spec' js object");
    }
    api.setHelper(new SpecAndSpiPair(jsApi, jsSpi));
    if (!engine.has(jsSpi, Functions.OnStart)) {
        return;
    }
    // invoke onStart
    try {
        engine.invoke(jsSpi, Functions.OnStart, jsApi, 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) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine) ApiManagementException(com.bluenimble.platform.api.ApiManagementException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) 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 4 with ApiResourcesManagerException

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

the class GetResourceApiServiceSpi method execute.

@Override
public ApiOutput execute(Api api, ApiConsumer consumer, ApiRequest request, ApiResponse response) throws ApiServiceExecutionException {
    String path = (String) request.get(Spec.Path);
    if (Lang.isNullOrEmpty(path)) {
        throw new ApiServiceExecutionException("Resource / not found").status(ApiResponse.BAD_REQUEST);
    }
    String location = (String) Json.find(request.getService().getCustom(), Custom.Resources, Custom.Root);
    if (!Lang.isNullOrEmpty(location)) {
        path = location + Lang.SLASH + path;
    }
    ApiResource r;
    try {
        r = api.getResourcesManager().get(Lang.split(path, Lang.SLASH));
    } catch (ApiResourcesManagerException e) {
        throw new ApiServiceExecutionException(e.getMessage()).status(ApiResponse.BAD_REQUEST);
    }
    if (r == null) {
        throw new ApiServiceExecutionException("Resource " + path + " not found").status(ApiResponse.NOT_FOUND);
    }
    return new ApiResourceOutput(r);
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) ApiResourceOutput(com.bluenimble.platform.api.impls.ApiResourceOutput) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException)

Example 5 with ApiResourcesManagerException

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

the class MgmApiSpi method onStart.

@Override
public void onStart(Api api, ApiContext context) {
    ApiResource resource;
    try {
        resource = api.getResourcesManager().get(Consumers);
    } catch (ApiResourcesManagerException ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    }
    if (resource == null) {
        return;
    }
    InputStream stream = null;
    try {
        stream = resource.toInput();
        consumers = Json.load(stream);
    } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
    } finally {
        IOUtils.closeQuietly(stream);
    }
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) InputStream(java.io.InputStream) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException) ApiServiceExecutionException(com.bluenimble.platform.api.ApiServiceExecutionException) ApiAuthenticationException(com.bluenimble.platform.api.security.ApiAuthenticationException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException)

Aggregations

ApiResourcesManagerException (com.bluenimble.platform.api.ApiResourcesManagerException)10 ApiResource (com.bluenimble.platform.api.ApiResource)7 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)5 JsonObject (com.bluenimble.platform.json.JsonObject)4 IOException (java.io.IOException)4 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)3 InputStream (java.io.InputStream)3 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)2 ScriptingEngine (com.bluenimble.platform.scripting.ScriptingEngine)2 ScriptingEngineException (com.bluenimble.platform.scripting.ScriptingEngineException)2 File (java.io.File)2 VariableResolver (com.bluenimble.platform.Lang.VariableResolver)1 ApiMediaException (com.bluenimble.platform.api.ApiMediaException)1 ApiResourcesManager (com.bluenimble.platform.api.ApiResourcesManager)1 ApiServicesManagerException (com.bluenimble.platform.api.ApiServicesManagerException)1 ApiResourceOutput (com.bluenimble.platform.api.impls.ApiResourceOutput)1 FileApiResource (com.bluenimble.platform.server.impls.fs.FileApiResource)1 DefaultVariableResolver (com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver)1 TemplateSource (com.github.jknack.handlebars.io.TemplateSource)1 OutputStream (java.io.OutputStream)1