Search in sources :

Example 1 with TemplateSource

use of com.github.jknack.handlebars.io.TemplateSource in project serverless by bluenimble.

the class SmtpMessenger method send.

@Override
public void send(Sender sender, Recipient[] recipients, String subject, final ApiResource content, JsonObject data, ApiStreamSource... attachments) throws MessengerException {
    final String uuid = content.owner() + Lang.SLASH + content.name();
    CachedTemplate cTemplate = templates.get(uuid);
    if (cTemplate == null || cTemplate.timestamp < content.timestamp().getTime()) {
        cTemplate = new CachedTemplate();
        cTemplate.timestamp = content.timestamp().getTime();
        TemplateSource source = new TemplateSource() {

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

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

            @Override
            public String content() throws IOException {
                InputStream is = null;
                try {
                    is = content.toInput();
                    return IOUtils.toString(is);
                } finally {
                    IOUtils.closeQuietly(is);
                }
            }
        };
        try {
            cTemplate.template = engine.compile(source, StartDelimitter, EndDelimitter);
        } catch (IOException e) {
            throw new MessengerException(e.getMessage(), e);
        }
        templates.put(uuid, cTemplate);
    }
    StringWriter sw = new StringWriter();
    try {
        cTemplate.template.apply(data, sw);
    } catch (Exception e) {
        throw new MessengerException(e.getMessage(), e);
    }
    send(sender, recipients, subject, sw.toString(), attachments);
}
Also used : TemplateSource(com.github.jknack.handlebars.io.TemplateSource) StringWriter(java.io.StringWriter) InputStream(java.io.InputStream) MessengerException(com.bluenimble.platform.messaging.MessengerException) IOException(java.io.IOException) IOException(java.io.IOException) MessengerException(com.bluenimble.platform.messaging.MessengerException)

Example 2 with TemplateSource

use of com.github.jknack.handlebars.io.TemplateSource in project serverless by bluenimble.

the class ResourceTemplateLoader method sourceAt.

@Override
public TemplateSource sourceAt(String name) throws IOException {
    JsonObject runtime = api.getRuntime();
    String templatesPath = Json.getString(runtime, Templates, DefaultTemplatesPath);
    if (templatesPath.startsWith(Lang.SLASH)) {
        templatesPath = templatesPath.substring(1);
    }
    if (templatesPath.endsWith(Lang.SLASH)) {
        templatesPath = templatesPath.substring(0, templatesPath.length() - 1);
    }
    String templateExt = Json.getString(runtime, TemplateExtension, DotHtml);
    final String fileName = templatesPath + Lang.SLASH + name + templateExt;
    ApiResource res;
    try {
        res = api.getResourcesManager().get(Lang.split(fileName, Lang.SLASH));
    } catch (ApiResourcesManagerException e) {
        throw new IOException(e.getMessage(), e);
    }
    final ApiResource resource = res;
    return new TemplateSource() {

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

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

        @Override
        public String content() throws IOException {
            InputStream is = null;
            try {
                is = resource.toInput();
                return IOUtils.toString(is);
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
    };
}
Also used : ApiResource(com.bluenimble.platform.api.ApiResource) TemplateSource(com.github.jknack.handlebars.io.TemplateSource) InputStream(java.io.InputStream) JsonObject(com.bluenimble.platform.json.JsonObject) IOException(java.io.IOException) ApiResourcesManagerException(com.bluenimble.platform.api.ApiResourcesManagerException)

Example 3 with TemplateSource

use of com.github.jknack.handlebars.io.TemplateSource in project wcomponents by BorderTech.

the class HandlebarsCacheImpl method getCache.

/**
 * @return the cache instance
 */
protected synchronized Cache<TemplateSource, Template> getCache() {
    Cache<TemplateSource, Template> cache = Caching.getCache(CACHE_NAME, TemplateSource.class, Template.class);
    if (cache == null) {
        final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
        MutableConfiguration<TemplateSource, Template> config = new MutableConfiguration<>();
        config.setTypes(TemplateSource.class, Template.class);
        config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.HOURS, 12)));
        // Handlebars template classes are not serializable so use by ref.
        config.setStoreByValue(false);
        cache = mgr.createCache(CACHE_NAME, config);
    }
    return cache;
}
Also used : TemplateSource(com.github.jknack.handlebars.io.TemplateSource) CacheManager(javax.cache.CacheManager) Duration(javax.cache.expiry.Duration) MutableConfiguration(javax.cache.configuration.MutableConfiguration) Template(com.github.jknack.handlebars.Template)

Example 4 with TemplateSource

use of com.github.jknack.handlebars.io.TemplateSource 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

TemplateSource (com.github.jknack.handlebars.io.TemplateSource)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 JsonObject (com.bluenimble.platform.json.JsonObject)2 ApiResource (com.bluenimble.platform.api.ApiResource)1 ApiResourcesManagerException (com.bluenimble.platform.api.ApiResourcesManagerException)1 TemplateEngineException (com.bluenimble.platform.api.impls.media.engines.TemplateEngineException)1 MessengerException (com.bluenimble.platform.messaging.MessengerException)1 Template (com.github.jknack.handlebars.Template)1 StringWriter (java.io.StringWriter)1 CacheManager (javax.cache.CacheManager)1 MutableConfiguration (javax.cache.configuration.MutableConfiguration)1 Duration (javax.cache.expiry.Duration)1