001/*
002 * (C) Copyright 2006-2011 Nuxeo SAS <http://nuxeo.com> and others
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Jean-Marc Orliaguet, Chalmers
011 *
012 */
013
014package org.nuxeo.theme.protocol.nxtheme;
015
016import java.io.ByteArrayInputStream;
017import java.io.IOException;
018import java.io.InputStream;
019import java.net.URL;
020import java.net.URLConnection;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.theme.Manager;
025import org.nuxeo.theme.elements.Element;
026import org.nuxeo.theme.elements.ElementRenderer;
027import org.nuxeo.theme.rendering.RenderingInfo;
028import org.nuxeo.theme.themes.ThemeDescriptor;
029import org.nuxeo.theme.themes.ThemeManager;
030
031public final class Connection extends URLConnection {
032
033    private static final Log log = LogFactory.getLog(Connection.class);
034
035    private final URL url;
036
037    private final String host;
038
039    protected Connection(URL url) {
040        super(url);
041        this.url = url;
042        host = url.getHost();
043    }
044
045    @Override
046    public void connect() {
047        connected = true;
048    }
049
050    @Override
051    public long getLastModified() {
052        // cache themes until they are modified
053        if (host.equals("theme")) {
054            return Manager.getThemeManager().getLastModified(url);
055        }
056        // do not cache elements
057        else if (host.equals("element")) {
058            return System.currentTimeMillis();
059        }
060        // cache everything else
061        return 0L;
062    }
063
064    @Override
065    public InputStream getInputStream() throws IOException {
066
067        Element rendered = null;
068        boolean cache = true;
069
070        ThemeManager themeManager = Manager.getThemeManager();
071
072        log.debug(url);
073        // render a single element
074        if (host.equals("element")) {
075            rendered = ThemeManager.getElementByUrl(url);
076            cache = false;
077        }
078
079        // render the entire theme
080        else if (host.equals("theme")) {
081            // Theme not loaded yet?
082            String themeName = ThemeManager.getThemeNameByUrl(url);
083            ThemeDescriptor themeDescriptor = ThemeManager.getThemeDescriptorByThemeName(themeName);
084            if (themeDescriptor != null && !themeDescriptor.isLoaded()) {
085                ThemeManager.loadTheme(themeDescriptor);
086            }
087            rendered = themeManager.getThemeByUrl(url);
088        }
089
090        if (rendered == null) {
091            // no such element found in the theme definitions: throws
092            // IOException since it is the most semantically suited Exception
093            // declared in the java.net.URLConnection based class
094            throw new IOException(String.format("Error while rendering %s", ThemeManager.getUrlDescription(url)));
095        }
096
097        final RenderingInfo info = new RenderingInfo(rendered, url);
098        final String content = ElementRenderer.render(info, cache).getMarkup();
099        if (host.equals("theme")) {
100            if (log.isTraceEnabled()) {
101                log.trace(String.format("NXThemes output for %s: \n%s\n", url.toString(), content));
102            }
103        }
104        return new ByteArrayInputStream(content.getBytes());
105    }
106
107}