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);
}
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);
}
}
};
}
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;
}
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);
}
}
Aggregations