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 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.model;
023
024import java.io.IOException;
025import java.io.OutputStream;
026import java.io.OutputStreamWriter;
027import java.io.StringWriter;
028import java.io.UnsupportedEncodingException;
029import java.io.Writer;
030import java.net.SocketException;
031import java.util.HashMap;
032import java.util.Map;
033
034import org.apache.commons.logging.Log;
035import org.apache.commons.logging.LogFactory;
036import org.nuxeo.common.utils.ExceptionUtils;
037import org.nuxeo.ecm.core.api.NuxeoException;
038import org.nuxeo.ecm.webengine.scripting.ScriptFile;
039
040/**
041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
042 */
043public class Template {
044
045    private static final Log log = LogFactory.getLog(Template.class);
046
047    protected final Resource resource;
048
049    protected Map<String, Object> args;
050
051    protected ScriptFile script;
052
053    protected WebContext ctx;
054
055    protected Template(WebContext ctx, Resource resource, ScriptFile script) {
056        this.ctx = ctx;
057        this.resource = resource;
058        this.script = script;
059        if (this.ctx == null && this.resource != null) {
060            this.ctx = this.resource.getContext();
061        }
062    }
063
064    public Template(WebContext ctx, String fileName) {
065        this(ctx, null, null);
066        resolve(fileName);
067    }
068
069    public Template(Resource resource, String fileName) {
070        this(resource.getContext(), resource, null);
071        resolve(fileName);
072    }
073
074    public Template(WebContext ctx, ScriptFile script) {
075        this(ctx, null, script);
076    }
077
078    public Template(Resource resource, ScriptFile script) {
079        this(resource.getContext(), resource, script);
080    }
081
082    public Template arg(String key, Object value) {
083        if (args == null) {
084            args = new HashMap<String, Object>();
085        }
086        args.put(key, value);
087        return this;
088    }
089
090    public Template args(Map<String, Object> args) {
091        this.args = args;
092        return this;
093    }
094
095    public Map<String, Object> args() {
096        return args;
097    }
098
099    public Resource resource() {
100        return resource;
101    }
102
103    protected void resolve(String fileName) {
104        if (resource != null) {
105            script = resource.getType().getView(ctx.getModule(), fileName);
106        } else {
107            script = ctx.getModule().getFile(fileName);
108        }
109    }
110
111    public ScriptFile script() {
112        return script;
113    }
114
115    public void render(OutputStream out) {
116        Writer w;
117        try {
118            w = new OutputStreamWriter(out, "UTF-8");
119        } catch (UnsupportedEncodingException e) {
120            throw new NuxeoException("Failed to create output stream: unsupported encoding", e);
121        }
122        ctx.render(script(), args, w);
123        try {
124            w.flush();
125        } catch (IOException e) {
126            if (ExceptionUtils.getRootCause(e) instanceof SocketException) {
127                log.debug("Output socket closed: failed to flush response");
128            } else {
129                throw new NuxeoException("Failed to flush response", e);
130            }
131        }
132    }
133
134    public String render() {
135        StringWriter w = new StringWriter();
136        ctx.render(script(), args, w);
137        w.flush();
138        return w.getBuffer().toString();
139    }
140
141}