View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.servlet;
21  
22  import org.apache.myfaces.tobago.config.TobagoConfig;
23  import org.apache.myfaces.tobago.internal.util.IoUtils;
24  import org.apache.myfaces.tobago.internal.util.MimeTypeUtils;
25  import org.apache.myfaces.tobago.internal.util.ResponseUtils;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import javax.faces.application.ProjectStage;
30  import javax.servlet.ServletConfig;
31  import javax.servlet.ServletException;
32  import javax.servlet.http.HttpServlet;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  import java.io.IOException;
36  import java.io.InputStream;
37  import java.io.OutputStream;
38  
39  /**
40   * <pre>
41   * &lt;servlet&gt;
42   *   &lt;servlet-name&gt;ResourceServlet&lt;/servlet-name&gt;
43   *   &lt;servlet-class&gt;org.apache.myfaces.tobago.servlet.ResourceServlet&lt;/servlet-class&gt;
44   *   &lt;init-param&gt;
45   *     &lt;description&gt;The value for the expires header in seconds.
46   *       The default for ProjectStage.Production is 86400 sec (24 h) otherwise no expires header.&lt;/description&gt;
47   *     &lt;param-name&gt;expires&lt;/param-name&gt;
48   *     &lt;param-value&gt;14400&lt;/param-value&gt;
49   *   &lt;/init-param&gt;
50   *   &lt;init-param&gt;
51   *     &lt;description&gt;The value for the copy buffer size.
52   *            Default is 4096.&lt;/description&gt;
53   *     &lt;param-name&gt;bufferSize&lt;/param-name&gt;
54   *     &lt;param-value&gt;4096&lt;/param-value&gt;
55   *   &lt;/init-param&gt;
56   * &lt;/servlet&gt;
57   * &lt;servlet-mapping&gt;
58   *   &lt;servlet-name&gt;ResourceServlet&lt;/servlet-name&gt;
59   *   &lt;url-pattern&gt;/org/apache/myfaces/tobago/renderkit/*&lt;/url-pattern&gt;
60   * &lt;/servlet-mapping&gt;
61   * </pre>
62   *
63   * @since 1.0.7
64   * @deprecated since Tobago 3.0.x this is no longer needed
65   */
66  @Deprecated
67  public class ResourceServlet extends HttpServlet {
68  
69    private static final long serialVersionUID = -4491419290205206466L;
70  
71    private static final Logger LOG = LoggerFactory.getLogger(ResourceServlet.class);
72  
73    private Long expires;
74    private int bufferSize;
75    private boolean nosniffHeader;
76  
77    @Override
78    public void init(final ServletConfig servletConfig) throws ServletException {
79      super.init(servletConfig);
80      final TobagoConfig tobagoConfig = TobagoConfig.getInstance(servletConfig.getServletContext());
81      if (tobagoConfig.getProjectStage() == ProjectStage.Production) {
82         expires = 24 * 60 * 60 * 1000L;
83      }
84  
85      final String expiresString = servletConfig.getInitParameter("expires");
86      if (expiresString != null) {
87        try {
88          expires = new Long(expiresString) * 1000;
89        } catch (final NumberFormatException e) {
90          LOG.error("Caught: " + e.getMessage(), e);
91        }
92      }
93      final String bufferSizeString = servletConfig.getInitParameter("bufferSize");
94      bufferSize = 1024 * 4;
95      if (bufferSizeString != null) {
96        try {
97          bufferSize = Integer.parseInt(bufferSizeString);
98        } catch (final NumberFormatException e) {
99          LOG.error("Caught: " + e.getMessage(), e);
100       }
101     }
102     nosniffHeader = tobagoConfig.isSetNosniffHeader();
103   }
104 
105     @Override
106   protected void doGet(
107       final HttpServletRequest request, final HttpServletResponse response)
108       throws ServletException, IOException {
109 
110     final String requestURI = request.getRequestURI();
111     String resource = requestURI.substring(request.getContextPath().length() + 1);
112     if (expires != null) {
113       response.setDateHeader("Last-Modified", 0);
114       response.setHeader("Cache-Control", "Public, max-age=" + expires);
115       response.setDateHeader("Expires", System.currentTimeMillis() + expires);
116     }
117     final String contentType = MimeTypeUtils.getMimeTypeForFile(requestURI);
118     if (contentType != null) {
119       response.setContentType(contentType);
120       if (nosniffHeader) {
121         ResponseUtils.ensureNosniffHeader(response);
122       }
123     } else {
124       final String message = "Unsupported mime type of resource='" + resource + "'";
125       LOG.warn(message + " (because of security reasons)");
126       response.setStatus(HttpServletResponse.SC_FORBIDDEN);
127       return;
128     }
129 
130     InputStream inputStream = null;
131     try {
132       final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
133 
134       // meta inf (like in servlet 3.0)
135       inputStream = classLoader.getResourceAsStream("META-INF/resources/" + resource);
136 
137       // "normal" classpath
138       if (inputStream == null) {
139         inputStream = classLoader.getResourceAsStream(resource);
140       }
141 
142       if (inputStream != null) {
143         copy(inputStream, response.getOutputStream());
144       } else {
145         final String message = "Resource '" + resource + "' not found!";
146         LOG.warn(message);
147         response.setStatus(HttpServletResponse.SC_NOT_FOUND);
148       }
149     } finally {
150       IoUtils.closeQuietly(inputStream);
151     }
152   }
153 
154   @Override
155   protected long getLastModified(final HttpServletRequest request) {
156     if (expires != null) {
157       return 0;
158     } else {
159       return super.getLastModified(request);
160     }
161   }
162 
163   private void copy(final InputStream inputStream, final OutputStream outputStream) throws IOException {
164     final byte[] buffer = new byte[bufferSize];
165     int count;
166     while (-1 != (count = inputStream.read(buffer))) {
167       outputStream.write(buffer, 0, count);
168     }
169   }
170 }