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