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