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.MimeTypeUtils;
24  import org.apache.myfaces.tobago.internal.util.ResponseUtils;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import javax.faces.application.ProjectStage;
29  import javax.servlet.ServletConfig;
30  import javax.servlet.ServletException;
31  import javax.servlet.http.HttpServlet;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import java.io.IOException;
35  import java.io.InputStream;
36  import java.io.OutputStream;
37  
38  /**
39   * <pre>
40   * &lt;servlet&gt;
41   *   &lt;servlet-name&gt;ResourceServlet&lt;/servlet-name&gt;
42   *   &lt;servlet-class&gt;org.apache.myfaces.tobago.servlet.ResourceServlet&lt;/servlet-class&gt;
43   *   &lt;init-param&gt;
44   *     &lt;description&gt;The value for the expires header in seconds.
45   *       The default for ProjectStage.Production is 86400 sec (24 h) otherwise no expires header.&lt;/description&gt;
46   *     &lt;param-name&gt;expires&lt;/param-name&gt;
47   *     &lt;param-value&gt;14400&lt;/param-value&gt;
48   *   &lt;/init-param&gt;
49   *   &lt;init-param&gt;
50   *     &lt;description&gt;The value for the copy buffer size.
51   *            Default is 4096.&lt;/description&gt;
52   *     &lt;param-name&gt;bufferSize&lt;/param-name&gt;
53   *     &lt;param-value&gt;4096&lt;/param-value&gt;
54   *   &lt;/init-param&gt;
55   * &lt;/servlet&gt;
56   * &lt;servlet-mapping&gt;
57   *   &lt;servlet-name&gt;ResourceServlet&lt;/servlet-name&gt;
58   *   &lt;url-pattern&gt;/org/apache/myfaces/tobago/renderkit/*&lt;/url-pattern&gt;
59   * &lt;/servlet-mapping&gt;
60   * </pre>
61   *
62   * @since 1.0.7
63   * @deprecated since Tobago 3.0.x this is no longer needed
64   */
65  @Deprecated
66  public class ResourceServlet extends HttpServlet {
67  
68    private static final long serialVersionUID = -4491419290205206466L;
69  
70    private static final Logger LOG = LoggerFactory.getLogger(ResourceServlet.class);
71  
72    private Long expires;
73    private int bufferSize;
74    private boolean nosniffHeader;
75  
76    @Override
77    public void init(final ServletConfig servletConfig) throws ServletException {
78      super.init(servletConfig);
79      final TobagoConfig tobagoConfig = TobagoConfig.getInstance(servletConfig.getServletContext());
80      if (tobagoConfig.getProjectStage() == ProjectStage.Production) {
81        expires = 24 * 60 * 60 * 1000L;
82      }
83  
84      final String expiresString = servletConfig.getInitParameter("expires");
85      if (expiresString != null) {
86        try {
87          expires = new Long(expiresString) * 1000;
88        } catch (final NumberFormatException e) {
89          LOG.error("Caught: " + e.getMessage(), e);
90        }
91      }
92      final String bufferSizeString = servletConfig.getInitParameter("bufferSize");
93      bufferSize = 1024 * 4;
94      if (bufferSizeString != null) {
95        try {
96          bufferSize = Integer.parseInt(bufferSizeString);
97        } catch (final NumberFormatException e) {
98          LOG.error("Caught: " + e.getMessage(), e);
99        }
100     }
101     nosniffHeader = tobagoConfig.isSetNosniffHeader();
102   }
103 
104   @Override
105   protected void doGet(
106       final HttpServletRequest request, final HttpServletResponse response)
107       throws ServletException, IOException {
108 
109     final String requestURI = request.getRequestURI();
110     final String resource = requestURI.substring(request.getContextPath().length() + 1);
111     if (expires != null) {
112       response.setDateHeader("Last-Modified", 0);
113       response.setHeader("Cache-Control", "Public, max-age=" + expires);
114       response.setDateHeader("Expires", System.currentTimeMillis() + expires);
115     }
116     final String contentType = MimeTypeUtils.getMimeTypeForFile(requestURI);
117     if (contentType != null) {
118       response.setContentType(contentType);
119       if (nosniffHeader) {
120         ResponseUtils.ensureNosniffHeader(response);
121       }
122     } else {
123       final String message = "Unsupported mime type of resource='" + resource + "'";
124       LOG.warn(message + " (because of security reasons)");
125       response.setStatus(HttpServletResponse.SC_FORBIDDEN);
126       return;
127     }
128 
129     try (final InputStream inputStream = locateResource(resource)) {
130       if (inputStream != null) {
131         copy(inputStream, response.getOutputStream());
132       } else {
133         final String message = "Resource '" + resource + "' not found!";
134         LOG.warn(message);
135         response.setStatus(HttpServletResponse.SC_NOT_FOUND);
136       }
137     }
138   }
139 
140   private InputStream locateResource(String resource) {
141     final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
142     InputStream inputStream; // meta inf (like in servlet 3.0)
143     inputStream = classLoader.getResourceAsStream("META-INF/resources/" + resource);
144 
145     // "normal" classpath
146     if (inputStream == null) {
147       inputStream = classLoader.getResourceAsStream(resource);
148     }
149     return inputStream;
150   }
151 
152   @Override
153   protected long getLastModified(final HttpServletRequest request) {
154     if (expires != null) {
155       return 0;
156     } else {
157       return super.getLastModified(request);
158     }
159   }
160 
161   private void copy(final InputStream inputStream, final OutputStream outputStream) throws IOException {
162     final byte[] buffer = new byte[bufferSize];
163     int count;
164     while (-1 != (count = inputStream.read(buffer))) {
165       outputStream.write(buffer, 0, count);
166     }
167   }
168 }