001/* 002 * (C) Copyright 2006-2007 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 * $Id$ 013 */ 014 015package org.nuxeo.theme.html.servlets; 016 017import java.io.IOException; 018import java.io.OutputStream; 019import java.io.Serializable; 020import java.util.zip.GZIPOutputStream; 021 022import javax.servlet.http.HttpServlet; 023import javax.servlet.http.HttpServletRequest; 024import javax.servlet.http.HttpServletResponse; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.ecm.core.io.download.BufferingServletOutputStream; 029import org.nuxeo.theme.ApplicationType; 030import org.nuxeo.theme.Manager; 031import org.nuxeo.theme.html.Utils; 032import org.nuxeo.theme.html.ui.ThemeStyles; 033import org.nuxeo.theme.themes.ThemeDescriptor; 034import org.nuxeo.theme.themes.ThemeManager; 035import org.nuxeo.theme.types.TypeFamily; 036 037public final class Styles extends HttpServlet implements Serializable { 038 039 private static final Log log = LogFactory.getLog(Styles.class); 040 041 private static final long serialVersionUID = 1L; 042 043 @Override 044 protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException { 045 doPost(request, response); 046 } 047 048 @Override 049 protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws IOException { 050 051 // headers 052 response.addHeader("content-type", "text/css"); 053 054 final String themeName = request.getParameter("theme"); 055 if (themeName == null) { 056 response.sendError(404); 057 log.error("Theme name not set"); 058 return; 059 } 060 061 final String collectionName = request.getParameter("collection"); 062 063 // Load theme if needed 064 ThemeDescriptor themeDescriptor = ThemeManager.getThemeDescriptorByThemeName(themeName); 065 if (themeDescriptor == null) { 066 throw new IOException("Theme not found: " + themeName); 067 } 068 if (!themeDescriptor.isLoaded()) { 069 ThemeManager.loadTheme(themeDescriptor); 070 } 071 072 // cache control 073 final String applicationPath = request.getParameter("path"); 074 if (applicationPath != null) { 075 final ApplicationType application = (ApplicationType) Manager.getTypeRegistry().lookup( 076 TypeFamily.APPLICATION, applicationPath); 077 if (application != null) { 078 Utils.setCacheHeaders(response, application.getStyleCaching()); 079 } 080 } 081 082 // compression 083 OutputStream os = response.getOutputStream(); 084 BufferingServletOutputStream.stopBuffering(os); 085 if (Utils.supportsGzip(request)) { 086 response.setHeader("Content-Encoding", "gzip"); 087 // Needed by proxy servers 088 response.setHeader("Vary", "Accept-Encoding"); 089 os = new GZIPOutputStream(os); 090 } 091 092 final String basePath = request.getParameter("basepath"); 093 094 final ThemeManager themeManager = Manager.getThemeManager(); 095 String rendered = themeManager.getCachedStyles(themeName, basePath, collectionName); 096 097 if (rendered == null) { 098 rendered = ThemeStyles.generateThemeStyles(themeName, themeDescriptor, basePath, collectionName, true); 099 themeManager.setCachedStyles(themeName, basePath, collectionName, rendered); 100 } 101 102 os.write(rendered.getBytes()); 103 os.close(); 104 } 105}