in trinidad-api/src/main/java/org/apache/myfaces/trinidad/webapp/ResourceServlet.java [715:820]
private void _setHeaders(
URLConnection connection,
HttpServletResponse response,
ResourceLoader loader)
{
String resourcePath;
URL url;
String contentType = ResourceLoader.getContentType(loader, connection);
if (contentType == null || "content/unknown".equals(contentType))
{
url = connection.getURL();
resourcePath = url.getPath();
// 'Case' statement for unknown content types
if (resourcePath.endsWith(".css"))
contentType = "text/css";
else if (resourcePath.endsWith(".js"))
contentType = "application/x-javascript";
else if (resourcePath.endsWith(".cur") || resourcePath.endsWith(".ico"))
contentType = "image/vnd.microsoft.icon";
else
contentType = getServletContext().getMimeType(resourcePath);
// The resource has an file extension we have not
// included in the case statement above
if (contentType == null)
{
_LOG.warning("ResourceServlet._setHeaders(): " +
"Content type for {0} is NULL!\n" +
"Cause: Unknown file extension",
resourcePath);
}
}
if (contentType != null)
{
response.setContentType(contentType);
int contentLength = connection.getContentLength();
if (contentLength >= 0)
response.setContentLength(contentLength);
}
long lastModified;
try
{
lastModified = URLUtils.getLastModified(connection);
}
catch (IOException exception)
{
lastModified = 0;
}
if (lastModified == 0)
response.setDateHeader("Last-Modified", lastModified);
//Handle cache controls. So this is a bit complex and confusing. If
//either of the cache headers are provide AND we are not in debug mode
//then we should set the cache to a default value. This is to maintain
//backward compatibility. If, however, the expiration or cache control
//headers are provided, we should just use those.
String cacheControl = connection.getHeaderField("cache-control");
Long expires = connection.getExpiration();
if (!_debug && null == cacheControl && 0 == expires)
{
// We set two headers: Cache-Control and Expires.
// This combination lets browsers know that it is
// okay to cache the resource indefinitely.
// Set Cache-Control to "Public".
cacheControl = "Public";
expires = System.currentTimeMillis() + ONE_YEAR_MILLIS;
}
if(null != cacheControl)
{
response.setHeader("Cache-Control", cacheControl);
}
if(0 != expires)
{
response.setDateHeader("Expires", expires);
}
Map<String, List<String>> headerMap = connection.getHeaderFields();
if(null != headerMap) //There are some wonkey impl's out there, best to guard against null
{
//put additional headers
for(Map.Entry<String, List<String>> entry: connection.getHeaderFields().entrySet())
{
//We handled a number of headers above and there are others we might want
//to exclude. We test for those headers here.
String key = entry.getKey();
if(Arrays.binarySearch(_INCLUDED_HEADERS, key.toLowerCase()) >= 0 && null != entry.getValue())
{
//Header is not excluded, add all the entries
for(String value:entry.getValue())
{
response.addHeader(key, value);
}
}
}
}
}