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; 027 028import javax.servlet.http.HttpServlet; 029import javax.servlet.http.HttpServletRequest; 030import javax.servlet.http.HttpServletResponse; 031 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034import org.nuxeo.common.utils.FileUtils; 035import org.nuxeo.ecm.core.io.download.BufferingServletOutputStream; 036import org.nuxeo.theme.Manager; 037import org.nuxeo.theme.html.Utils; 038import org.nuxeo.theme.themes.ThemeException; 039 040public final class Images extends HttpServlet implements Serializable { 041 042 private static final Log log = LogFactory.getLog(Images.class); 043 044 private static final long serialVersionUID = 1L; 045 046 @Override 047 protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws IOException { 048 doPost(request, response); 049 } 050 051 @Override 052 protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws IOException { 053 054 final String path = request.getPathInfo().substring(1); 055 byte[] data = null; 056 try { 057 data = Manager.getThemeManager().getImageResource(path); 058 } catch (ThemeException e) { 059 log.error("Image not found: " + path); 060 } 061 if (data != null) { 062 OutputStream os = response.getOutputStream(); 063 BufferingServletOutputStream.stopBuffering(os); 064 String ext = FileUtils.getFileExtension(path); 065 String mimeType = Utils.getImageMimeType(ext); 066 response.addHeader("content-type", mimeType); 067 068 // Cache headers 069 final String lifetime = "604800"; // 1 week 070 final long now = System.currentTimeMillis(); 071 response.addHeader("Cache-Control", "max-age=" + lifetime); 072 response.addHeader("Cache-Control", "must-revalidate"); 073 response.setDateHeader("Last-Modified", now); 074 response.setDateHeader("Expires", now + new Long(lifetime) * 1000L); 075 076 os.write(data); 077 os.close(); 078 } 079 } 080}