Search in sources :

Example 1 with TemplateException

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;
}
Also used : TemplateException(freemarker.template.TemplateException) StringRepresentation(org.restlet.representation.StringRepresentation) Representation(org.restlet.representation.Representation) WriterRepresentation(org.restlet.representation.WriterRepresentation) IOException(java.io.IOException) ValueComposite(org.qi4j.api.value.ValueComposite) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) ResourceException(org.restlet.resource.ResourceException) Template(freemarker.template.Template) StringRepresentation(org.restlet.representation.StringRepresentation) WriterRepresentation(org.restlet.representation.WriterRepresentation) MediaType(org.restlet.data.MediaType) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer)

Example 2 with TemplateException

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);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) ContainerException(org.glassfish.jersey.server.ContainerException) Charset(java.nio.charset.Charset) OutputStreamWriter(java.io.OutputStreamWriter) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map)

Example 3 with TemplateException

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());
}
Also used : TemplateException(freemarker.template.TemplateException) Writer(java.io.Writer)

Example 4 with TemplateException

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);
}
Also used : TemplateException(freemarker.template.TemplateException) RenderingException(ninja.exceptions.RenderingException) TemplateNotFoundException(freemarker.template.TemplateNotFoundException) IOException(java.io.IOException) ParseException(freemarker.core.ParseException)

Example 5 with TemplateException

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);
    }
}
Also used : StringWriter(java.io.StringWriter) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) Template(freemarker.template.Template)

Aggregations

TemplateException (freemarker.template.TemplateException)39 IOException (java.io.IOException)34 Template (freemarker.template.Template)20 HashMap (java.util.HashMap)16 Writer (java.io.Writer)15 StringWriter (java.io.StringWriter)12 Map (java.util.Map)8 Configuration (freemarker.template.Configuration)7 WriterRepresentation (org.restlet.representation.WriterRepresentation)6 File (java.io.File)5 OutputStreamWriter (java.io.OutputStreamWriter)4 MediaType (org.restlet.data.MediaType)4 Representation (org.restlet.representation.Representation)4 BufferedWriter (java.io.BufferedWriter)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileWriter (java.io.FileWriter)3 JSONException (org.json.JSONException)3 StringRepresentation (org.restlet.representation.StringRepresentation)3 ParseException (freemarker.core.ParseException)2 DefaultObjectWrapper (freemarker.template.DefaultObjectWrapper)2