Search in sources :

Example 1 with ApiResource

use of com.bluenimble.platform.api.ApiResource 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 2 with ApiResource

use of com.bluenimble.platform.api.ApiResource 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 3 with ApiResource

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

Example 4 with ApiResource

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

the class FileApiResource method children.

@Override
public List<ApiResource> children(final Selector selector) {
    if (file.isFile()) {
        return null;
    }
    File[] files = file.listFiles(new FileFilter() {

        @Override
        public boolean accept(File kid) {
            return selector.select(kid.getName(), kid.isDirectory());
        }
    });
    if (files == null || files.length == 0) {
        return null;
    }
    List<ApiResource> list = new ArrayList<ApiResource>();
    for (File f : files) {
        list.add(new FileApiResource(owner, root, f));
    }
    return list;
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) ArrayList(java.util.ArrayList) FileFilter(java.io.FileFilter) File(java.io.File)

Example 5 with ApiResource

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

the class PlainMediaProcessor method process.

@Override
public void process(Api api, ApiService service, ApiConsumer consumer, ApiOutput output, ApiRequest request, final ApiResponse response) throws ApiMediaException {
    String contentType = (String) request.get(ApiRequest.SelectedMedia);
    if (Lang.isNullOrEmpty(contentType) || Lang.STAR.equals(contentType)) {
        contentType = this.contentType;
    }
    String charset = Encodings.UTF8;
    JsonObject mediaDef = MediaRoutingUtils.pickMedia(api, service, contentType);
    try {
        String rContentType = contentType;
        if (output != null) {
            String oCharset = (String) output.get(ApiOutput.Defaults.Charset);
            if (!Lang.isNullOrEmpty(oCharset)) {
                charset = oCharset;
            }
        }
        rContentType += "; charset=" + charset;
        VariableResolver vr = new DefaultVariableResolver(request, output != null ? output.data() : null, output != null ? output.meta() : null);
        JsonObject media = MediaRoutingUtils.processMedia(request, response, vr, mediaDef, api.tracer());
        if (media != null) {
            // if there is a template
            String sTemplate = Json.getString(media, ApiService.Spec.Media.Resource);
            ApiResource template = null;
            if (!Lang.isNullOrEmpty(sTemplate)) {
                template = api.getResourcesManager().get(Lang.split(Lang.resolve(sTemplate, vr), Lang.SLASH));
            }
            if (template != null) {
                TemplateEngine engine = plugin.loockupEngine(api, Json.getString(media, ApiService.Spec.Media.Engine, MediaPlugin.HandlebarsEngine));
                response.set(ApiHeaders.ContentType, rContentType);
                response.flushHeaders();
                engine.write(consumer, request, response, output, template, media);
                response.close();
                return;
            }
        }
        if (WriteResponseUtils.writeError(response, api.tracer(), rContentType)) {
            response.close();
            return;
        }
        response.set(ApiHeaders.ContentType, rContentType);
        DataWriter dataWriter = writers.get(contentType);
        if (dataWriter == null) {
            dataWriter = writers.get(this.contentType);
        }
        dataWriter.write(output, response);
        response.close();
    } catch (Exception e) {
        throw new ApiMediaException(e.getMessage(), e);
    }
}
Also used : TemplateEngine(com.bluenimble.platform.api.impls.media.engines.TemplateEngine) ApiResource(com.bluenimble.platform.api.ApiResource) DefaultVariableResolver(com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver) JsonObject(com.bluenimble.platform.json.JsonObject) ApiMediaException(com.bluenimble.platform.api.ApiMediaException) DefaultVariableResolver(com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver) VariableResolver(com.bluenimble.platform.Lang.VariableResolver) ApiMediaException(com.bluenimble.platform.api.ApiMediaException) DataWriter(com.bluenimble.platform.api.media.DataWriter)

Aggregations

ApiResource (com.bluenimble.platform.api.ApiResource)9 ApiResourcesManagerException (com.bluenimble.platform.api.ApiResourcesManagerException)7 ApiServiceExecutionException (com.bluenimble.platform.api.ApiServiceExecutionException)5 JsonObject (com.bluenimble.platform.json.JsonObject)5 ApiManagementException (com.bluenimble.platform.api.ApiManagementException)3 InputStream (java.io.InputStream)3 VariableResolver (com.bluenimble.platform.Lang.VariableResolver)2 ApiMediaException (com.bluenimble.platform.api.ApiMediaException)2 ApiAuthenticationException (com.bluenimble.platform.api.security.ApiAuthenticationException)2 ScriptingEngine (com.bluenimble.platform.scripting.ScriptingEngine)2 ScriptingEngineException (com.bluenimble.platform.scripting.ScriptingEngineException)2 DefaultVariableResolver (com.bluenimble.platform.server.plugins.media.utils.DefaultVariableResolver)2 IOException (java.io.IOException)2 ApiResourceOutput (com.bluenimble.platform.api.impls.ApiResourceOutput)1 TemplateEngine (com.bluenimble.platform.api.impls.media.engines.TemplateEngine)1 DataWriter (com.bluenimble.platform.api.media.DataWriter)1 TemplateSource (com.github.jknack.handlebars.io.TemplateSource)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1 OutputStream (java.io.OutputStream)1