001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.webengine.model.view;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.io.OutputStreamWriter;
024import java.io.StringWriter;
025import java.io.Writer;
026import java.net.URL;
027import java.util.HashMap;
028import java.util.Map;
029
030import org.nuxeo.ecm.core.api.NuxeoException;
031import org.nuxeo.ecm.platform.rendering.api.RenderingException;
032import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper;
033import org.nuxeo.ecm.webengine.WebEngine;
034import org.nuxeo.ecm.webengine.model.WebContext;
035import org.nuxeo.ecm.webengine.model.exceptions.WebResourceNotFoundException;
036import org.nuxeo.runtime.api.Framework;
037
038/**
039 * A view to be used by regular JAX-RS resources to be able to use freemarker templates.
040 *
041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
042 */
043public class TemplateView {
044
045    protected URL url;
046
047    protected WebContext ctx;
048
049    protected Object target;
050
051    protected Map<String, Object> bindings;
052
053    public static URL findTemplate(Object owner, String name) {
054        URL url = owner.getClass().getResource(name);
055        if (url == null) {
056            url = Framework.getResourceLoader().getResource(name);
057            if (url == null) {
058                throw new WebResourceNotFoundException("View not found: " + name + " for object " + owner);
059            }
060        }
061        return url;
062    }
063
064    public TemplateView(Object owner, String name) {
065        this(WebEngine.getActiveContext(), owner, name);
066    }
067
068    public TemplateView(WebContext ctx, Object owner, String name) {
069        this(ctx, owner, findTemplate(owner, name));
070    }
071
072    public TemplateView(Object owner, URL url) {
073        this(WebEngine.getActiveContext(), owner, url);
074    }
075
076    public TemplateView(WebContext ctx, Object owner, URL url) {
077        if (ctx == null) {
078            throw new NuxeoException("Not in WebEngine context");
079        }
080        this.ctx = ctx;
081        this.target = owner;
082        this.url = url;
083        bindings = new HashMap<String, Object>();
084        bindings.put("This", target);
085        bindings.put("Context", ctx);
086        bindings.put("Engine", ctx.getEngine());
087        bindings.put("basePath", ctx.getBasePath());
088        bindings.put("contextPath", VirtualHostHelper.getContextPathProperty());
089    }
090
091    public WebContext getContext() {
092        return ctx;
093    }
094
095    public TemplateView arg(String key, Object value) {
096        bindings.put(key, value);
097        return this;
098    }
099
100    public TemplateView args(Map<String, Object> args) {
101        bindings.putAll(args);
102        return this;
103    }
104
105    public void render(Writer writer) {
106        try {
107            ctx.getEngine().getRendering().render(url.toExternalForm(), bindings, writer);
108        } catch (RenderingException e) {
109            throw new NuxeoException(e);
110        }
111    }
112
113    public void render(OutputStream out) {
114        Writer writer = new OutputStreamWriter(out);
115        render(writer);
116        try {
117            writer.flush();
118        } catch (IOException e) {
119            throw new NuxeoException(e);
120        }
121    }
122
123    public String getString() {
124        StringWriter writer = new StringWriter();
125        render(writer);
126        return writer.toString();
127    }
128
129}