001 package org.apache.myfaces.tobago.servlet; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import org.apache.commons.io.IOUtils; 021 import org.apache.commons.logging.Log; 022 import org.apache.commons.logging.LogFactory; 023 import org.apache.myfaces.tobago.util.MimeTypeUtils; 024 025 import javax.servlet.ServletException; 026 import javax.servlet.ServletConfig; 027 import javax.servlet.http.HttpServlet; 028 import javax.servlet.http.HttpServletRequest; 029 import javax.servlet.http.HttpServletResponse; 030 import java.io.IOException; 031 import java.io.InputStream; 032 import java.util.Date; 033 034 /** 035 * <p><pre> 036 * <servlet> 037 * <servlet-name>ResourceServlet</servlet-name> 038 * <servlet-class>org.apache.myfaces.tobago.servlet.ResourceServlet</servlet-class> 039 * <init-param> 040 * <description>The value for the expires header in seconds. 041 * Default is no expires header.</description> 042 * <param-name>expires</param-name> 043 * <param-value>14400</param-value> 044 * </init-param> 045 * </servlet> 046 * <servlet-mapping> 047 * <servlet-name>ResourceServlet</servlet-name> 048 * <url-pattern>/org/apache/myfaces/tobago/renderkit/*</url-pattern> 049 * </servlet-mapping> 050 *</pre><p> 051 * 052 * @since 1.0.7 053 * <p/> 054 * XXX This class is in development. Please don't use it in 055 * production environment! 056 */ 057 public class ResourceServlet extends HttpServlet { 058 059 private static final long serialVersionUID = -4491419290205206466L; 060 061 private static final Log LOG = LogFactory.getLog(ResourceServlet.class); 062 063 private Long expires; 064 065 public void init(ServletConfig servletConfig) throws ServletException { 066 super.init(servletConfig); 067 String expiresString = servletConfig.getInitParameter("expires"); 068 069 expires = null; 070 if (expiresString != null) { 071 try { 072 expires = new Long(expiresString); 073 } catch (NumberFormatException e) { 074 LOG.error("Catched: " + e.getMessage(), e); 075 } 076 } 077 } 078 079 @Override 080 protected void service( 081 HttpServletRequest request, HttpServletResponse response) 082 throws ServletException, IOException { 083 084 String requestURI = request.getRequestURI(); 085 086 String resource = requestURI.substring( 087 request.getContextPath().length() + 1); // todo: make it "stable" 088 089 if (expires != null) { 090 response.setHeader("Cache-Control", "max-age=" + expires); 091 response.setDateHeader("Expires", new Date().getTime() + (expires * 1000)); 092 } 093 String contentType = MimeTypeUtils.getMimeTypeForFile(requestURI); 094 if (contentType != null) { 095 response.setContentType(contentType); 096 } else { 097 LOG.warn("Unsupported file extension, will be ignored for security " 098 + "reasons; resource='" + resource + "'"); 099 response.setStatus(HttpServletResponse.SC_FORBIDDEN); 100 } 101 InputStream inputStream = null; 102 try { 103 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 104 inputStream = classLoader.getResourceAsStream(resource); 105 if (inputStream != null) { 106 IOUtils.copy(inputStream, response.getOutputStream()); 107 } else { 108 LOG.warn("Resource '" + resource + "' not found!"); 109 } 110 } finally { 111 IOUtils.closeQuietly(inputStream); 112 } 113 } 114 }