All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.httpd.GwtCacheControlFilter Maven / Gradle / Ivy

There is a newer version: 3.10.0-rc4
Show newest version
// Copyright (C) 2008 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.httpd;

import com.google.gerrit.util.http.CacheHeaders;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Forces GWT resources to cache for a very long time.
 *
 * 

GWT compiled JavaScript and ImageBundles can be cached indefinitely by a browser and/or an * edge proxy, as they never contain user-specific data and are named by a unique checksum. If their * content is ever modified then the URL changes, so user agents would request a different resource. * We force these resources to have very long expiration times. * *

To use, add the following block to your {@code web.xml}: * *

 * <filter>
 *     <filter-name>CacheControl</filter-name>
 *     <filter-class>com.google.gwtexpui.server.CacheControlFilter</filter-class>
 *   </filter>
 *   <filter-mapping>
 *     <filter-name>CacheControl</filter-name>
 *     <url-pattern>/*</url-pattern>
 *   </filter-mapping>
 * 
*/ @Singleton class GwtCacheControlFilter implements Filter { @Override public void init(FilterConfig config) {} @Override public void destroy() {} @Override public void doFilter(final ServletRequest sreq, ServletResponse srsp, FilterChain chain) throws IOException, ServletException { final HttpServletRequest req = (HttpServletRequest) sreq; final HttpServletResponse rsp = (HttpServletResponse) srsp; final String pathInfo = pathInfo(req); if (cacheForever(pathInfo, req)) { CacheHeaders.setCacheable(req, rsp, 365, TimeUnit.DAYS); } else if (nocache(pathInfo)) { CacheHeaders.setNotCacheable(rsp); } chain.doFilter(req, rsp); } private static boolean cacheForever(String pathInfo, HttpServletRequest req) { if (pathInfo.endsWith(".cache.html") || pathInfo.endsWith(".cache.gif") || pathInfo.endsWith(".cache.png") || pathInfo.endsWith(".cache.css") || pathInfo.endsWith(".cache.jar") || pathInfo.endsWith(".cache.swf") || pathInfo.endsWith(".cache.txt") || pathInfo.endsWith(".cache.js")) { return true; } else if (pathInfo.endsWith(".nocache.js")) { final String v = req.getParameter("content"); return v != null && v.length() > 20; } return false; } private static boolean nocache(String pathInfo) { if (pathInfo.endsWith(".nocache.js")) { return true; } return false; } private static String pathInfo(HttpServletRequest req) { final String uri = req.getRequestURI(); final String ctx = req.getContextPath(); return uri.startsWith(ctx) ? uri.substring(ctx.length()) : uri; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy