use of freemarker.template.TemplateException in project qi4j-sdk by Qi4j.
the class ValueCompositeResponseWriter method writeResponse.
@Override
public boolean writeResponse(final Object result, final Response response) throws ResourceException {
if (result instanceof ValueComposite) {
MediaType type = getVariant(response.getRequest(), ENGLISH, supportedMediaTypes).getMediaType();
if (MediaType.APPLICATION_JSON.equals(type)) {
StringRepresentation representation = new StringRepresentation(valueSerializer.serialize(result), MediaType.APPLICATION_JSON);
response.setEntity(representation);
return true;
} else if (MediaType.TEXT_HTML.equals(type)) {
Representation rep = new WriterRepresentation(MediaType.TEXT_HTML) {
@Override
public void write(Writer writer) throws IOException {
// Look for type specific template
Template template;
try {
template = cfg.getTemplate("/rest/template/" + result.getClass().getInterfaces()[0].getSimpleName() + ".htm");
} catch (Exception e) {
// Use default
template = cfg.getTemplate("value.htm");
}
Map<String, Object> context = new HashMap<String, Object>();
context.put("request", response.getRequest());
context.put("response", response);
context.put("result", result);
context.put("util", this);
try {
template.process(context, writer);
} catch (TemplateException e) {
throw new IOException(e);
}
}
public boolean isSequence(Object obj) {
return obj instanceof Collection;
}
};
response.setEntity(rep);
return true;
}
}
return false;
}
use of freemarker.template.TemplateException in project jersey by jersey.
the class FreemarkerViewProcessor method writeTo.
@Override
public void writeTo(final Template template, final Viewable viewable, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream out) throws IOException {
try {
Object model = viewable.getModel();
if (!(model instanceof Map)) {
model = new HashMap<String, Object>() {
{
put("model", viewable.getModel());
}
};
}
Charset encoding = setContentType(mediaType, httpHeaders);
template.process(model, new OutputStreamWriter(out, encoding));
} catch (TemplateException te) {
throw new ContainerException(te);
}
}
use of freemarker.template.TemplateException in project ninja by ninjaframework.
the class TemplateEngineFreemarkerAuthenticityTokenDirective method execute.
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
if (!params.isEmpty()) {
throw new TemplateException("This directive doesn't allow parameters.", env);
}
if (loopVars.length != 0) {
throw new TemplateException("This directive doesn't allow loop variables.", env);
}
Writer out = env.getOut();
out.append(this.context.getSession().getAuthenticityToken());
}
use of freemarker.template.TemplateException in project ninja by ninjaframework.
the class TemplateEngineFreemarker method throwRenderingException.
public void throwRenderingException(Context context, Result result, Exception cause, String knownTemplateSourcePath) {
// a more useful ParseException
if (cause instanceof IOException && cause.getCause() != null && cause.getCause() instanceof ParseException) {
cause = (ParseException) cause.getCause();
}
if (cause instanceof TemplateNotFoundException) {
// inner cause will be better to display
throw new RenderingException(cause.getMessage(), cause, result, "FreeMarker template not found", knownTemplateSourcePath, -1);
} else if (cause instanceof TemplateException) {
TemplateException te = (TemplateException) cause;
String templateSourcePath = te.getTemplateSourceName();
if (templateSourcePath == null) {
templateSourcePath = knownTemplateSourcePath;
}
throw new RenderingException(cause.getMessage(), cause, result, "FreeMarker render exception", templateSourcePath, te.getLineNumber());
} else if (cause instanceof ParseException) {
ParseException pe = (ParseException) cause;
String templateSourcePath = pe.getTemplateName();
if (templateSourcePath == null) {
templateSourcePath = knownTemplateSourcePath;
}
throw new RenderingException(cause.getMessage(), cause, result, "FreeMarker parser exception", templateSourcePath, pe.getLineNumber());
}
// fallback to throwing generic rendering exception
throw new RenderingException(cause.getMessage(), cause, result, knownTemplateSourcePath, -1);
}
use of freemarker.template.TemplateException in project spark by perwendel.
the class FreeMarkerTemplateEngine method render.
@Override
public String render(ModelAndView modelAndView) {
try {
StringWriter stringWriter = new StringWriter();
Template template = configuration.getTemplate(modelAndView.getViewName());
template.process(modelAndView.getModel(), stringWriter);
return stringWriter.toString();
} catch (IOException e) {
throw new IllegalArgumentException(e);
} catch (TemplateException e) {
throw new IllegalArgumentException(e);
}
}
Aggregations