Search in sources :

Example 1 with TemplateEngineException

use of com.bluenimble.platform.api.impls.media.engines.TemplateEngineException in project serverless by bluenimble.

the class JavascriptEngine method write.

@Override
public void write(ApiConsumer consumer, ApiRequest request, final ApiResponse response, ApiOutput output, final ApiResource template, final JsonObject mediaSpec) throws TemplateEngineException {
    try {
        ScriptingEngine engine = api.space().feature(ScriptingEngine.class, ApiSpace.Features.Default, null);
        MapScriptContext vars = new MapScriptContext();
        vars.set(Json.getString(features, I18n, I18n), api.i18n(request.getLang()));
        vars.set(Json.getString(features, Request, Request), request.toJson());
        if (consumer != null) {
            vars.set(Json.getString(features, Consumer, Consumer), consumer.toJson());
        }
        JsonObject jOutput = null;
        if (output != null) {
            jOutput = output.data();
        }
        if (output != null) {
            vars.set(Json.getString(features, Output, Output), jOutput);
            vars.set(Json.getString(features, Meta, Meta), output.meta());
        }
        vars.set(Json.getString(features, Error, Error), response.getError());
        vars.set(Json.getString(features, Response, Response), response);
        JsonObject result;
        Object oResult = engine.eval(ScriptingEngine.Supported.Javascript, api, template, vars);
        if (oResult != null) {
            if (oResult instanceof JsonObject) {
                result = (JsonObject) oResult;
            } else if (oResult instanceof JsonArray) {
                result = new JsonObject();
                result.set(ApiOutput.Defaults.Items, oResult);
            } else {
                result = new JsonObject();
                result.set(Output, oResult);
            }
        } else {
            result = jOutput;
        }
        if (response.getError() != null) {
            result = response.getError();
        }
        boolean doNotWrite = Json.getBoolean(jOutput, DoNotWrite, false);
        if (!doNotWrite) {
            doNotWrite = Json.getBoolean(mediaSpec, DoNotWrite, false);
        }
        if (result != null && !doNotWrite) {
            response.flushHeaders();
            result.write(new AbstractEmitter() {

                @Override
                public JsonEmitter write(String chunk) {
                    try {
                        response.write(chunk);
                    } catch (IOException e) {
                        throw new RuntimeException(e.getMessage(), e);
                    }
                    return this;
                }
            });
        }
    } catch (Exception e) {
        throw new TemplateEngineException(e.getMessage(), e);
    }
}
Also used : AbstractEmitter(com.bluenimble.platform.json.AbstractEmitter) JsonObject(com.bluenimble.platform.json.JsonObject) IOException(java.io.IOException) IOException(java.io.IOException) TemplateEngineException(com.bluenimble.platform.api.impls.media.engines.TemplateEngineException) MapScriptContext(com.bluenimble.platform.scripting.MapScriptContext) JsonArray(com.bluenimble.platform.json.JsonArray) JsonEmitter(com.bluenimble.platform.json.JsonEmitter) JsonObject(com.bluenimble.platform.json.JsonObject) TemplateEngineException(com.bluenimble.platform.api.impls.media.engines.TemplateEngineException) ScriptingEngine(com.bluenimble.platform.scripting.ScriptingEngine)

Example 2 with TemplateEngineException

use of com.bluenimble.platform.api.impls.media.engines.TemplateEngineException in project serverless by bluenimble.

the class HandlebarsTemplateEngine method write.

@Override
public void write(ApiConsumer consumer, ApiRequest request, ApiResponse response, ApiOutput output, final ApiResource template, final JsonObject mediaSpec) throws TemplateEngineException {
    final String uuid = template.path();
    CachedTemplate cTemplate = templates.get(uuid);
    if (cTemplate == null || cTemplate.timestamp < template.timestamp().getTime()) {
        cTemplate = new CachedTemplate();
        cTemplate.timestamp = template.timestamp().getTime();
        TemplateSource source = new TemplateSource() {

            @Override
            public long lastModified() {
                return template.timestamp().getTime();
            }

            @Override
            public String filename() {
                return uuid;
            }

            @Override
            public String content() throws IOException {
                InputStream is = null;
                try {
                    is = template.toInput();
                    return IOUtils.toString(is);
                } finally {
                    IOUtils.closeQuietly(is);
                }
            }
        };
        try {
            cTemplate.template = engine.compile(source);
        } catch (IOException e) {
            throw new TemplateEngineException(e.getMessage(), e);
        }
        templates.put(uuid, cTemplate);
    }
    try {
        JsonObject vars = new JsonObject();
        vars.set(Json.getString(features, I18n, I18n), api.i18n(request.getLang()));
        vars.set(Json.getString(features, Request, Request), request.toJson());
        if (consumer != null) {
            vars.set(Json.getString(features, Consumer, Consumer), consumer.toJson());
        }
        if (output != null) {
            vars.set(Json.getString(features, Output, Output), output.data());
            vars.set(Json.getString(features, Meta, Meta), output.meta());
        }
        vars.set(Json.getString(features, Error, Error), response.getError());
        cTemplate.template.apply(vars, response.toWriter());
    } catch (Exception e) {
        throw new TemplateEngineException(e.getMessage(), e);
    }
}
Also used : TemplateSource(com.github.jknack.handlebars.io.TemplateSource) InputStream(java.io.InputStream) JsonObject(com.bluenimble.platform.json.JsonObject) TemplateEngineException(com.bluenimble.platform.api.impls.media.engines.TemplateEngineException) IOException(java.io.IOException) IOException(java.io.IOException) TemplateEngineException(com.bluenimble.platform.api.impls.media.engines.TemplateEngineException)

Aggregations

TemplateEngineException (com.bluenimble.platform.api.impls.media.engines.TemplateEngineException)2 JsonObject (com.bluenimble.platform.json.JsonObject)2 IOException (java.io.IOException)2 AbstractEmitter (com.bluenimble.platform.json.AbstractEmitter)1 JsonArray (com.bluenimble.platform.json.JsonArray)1 JsonEmitter (com.bluenimble.platform.json.JsonEmitter)1 MapScriptContext (com.bluenimble.platform.scripting.MapScriptContext)1 ScriptingEngine (com.bluenimble.platform.scripting.ScriptingEngine)1 TemplateSource (com.github.jknack.handlebars.io.TemplateSource)1 InputStream (java.io.InputStream)1