You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NoCacheFilter.java 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (C) 2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.http.server;
  11. import static org.eclipse.jgit.util.HttpSupport.HDR_CACHE_CONTROL;
  12. import static org.eclipse.jgit.util.HttpSupport.HDR_EXPIRES;
  13. import static org.eclipse.jgit.util.HttpSupport.HDR_PRAGMA;
  14. import java.io.IOException;
  15. import javax.servlet.Filter;
  16. import javax.servlet.FilterChain;
  17. import javax.servlet.FilterConfig;
  18. import javax.servlet.ServletException;
  19. import javax.servlet.ServletRequest;
  20. import javax.servlet.ServletResponse;
  21. import javax.servlet.http.HttpServletResponse;
  22. /** Add HTTP response headers to prevent caching by proxies/browsers. */
  23. class NoCacheFilter implements Filter {
  24. /** {@inheritDoc} */
  25. @Override
  26. public void init(FilterConfig config) throws ServletException {
  27. // Do nothing.
  28. }
  29. /** {@inheritDoc} */
  30. @Override
  31. public void destroy() {
  32. // Do nothing.
  33. }
  34. /** {@inheritDoc} */
  35. @Override
  36. public void doFilter(ServletRequest request, ServletResponse response,
  37. FilterChain chain) throws IOException, ServletException {
  38. HttpServletResponse rsp = (HttpServletResponse) response;
  39. rsp.setHeader(HDR_EXPIRES, "Fri, 01 Jan 1980 00:00:00 GMT");
  40. rsp.setHeader(HDR_PRAGMA, "no-cache");
  41. final String nocache = "no-cache, max-age=0, must-revalidate";
  42. rsp.setHeader(HDR_CACHE_CONTROL, nocache);
  43. chain.doFilter(request, response);
  44. }
  45. }