Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make no-cache and no-store optional #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion src/main/java/com/samaxes/filter/NoCacheFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package com.samaxes.filter;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
Expand All @@ -28,12 +30,40 @@
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import com.samaxes.filter.util.Cacheability;
import com.samaxes.filter.util.HTTPCacheHeader;
import com.samaxes.filter.util.StringUtil;

/**
* <p>
* Filter allowing to completely disable browser caching.
* </p>
* <h2>Options</h2>
* <table summary="Filter options" border="1">
* <tr>
* <th>Option</th>
* <th>Required</th>
* <th>Default</th>
* <th>Since</th>
* <th>Description</th>
* </tr>
* <tr>
* <td>{@code no-cache}</td>
* <td>No</td>
* <td>{@code true}</td>
* <td>2.4</td>
* <td>Forces caches to submit the request to the origin server for validation before releasing a cached copy.
* </td>
* </tr>
* <tr>
* <td>{@code no-store}</td>
* <td>No</td>
* <td>{@code true}</td>
* <td>2.4</td>
* <td>The cache should not store anything about the client request or server response.
* </td>
* </tr>
* </table>
* <h2>Sample configuration:</h2>
* <p>
* Declare the filter in your web descriptor file {@code web.xml}:
Expand Down Expand Up @@ -71,11 +101,25 @@
*/
public class NoCacheFilter implements Filter {

private static Cacheability[] ALLOWED_DIRECTIVES = new Cacheability[]{Cacheability.NO_CACHE, Cacheability.NO_STORE};

private String cacheControl;

/**
* {@inheritDoc}
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
List<String> cacheControlDirectives = new LinkedList<String>();

for (Cacheability allowedDirective : ALLOWED_DIRECTIVES) {
String directive = allowedDirective.getValue();
String filterConfigValue = filterConfig.getInitParameter(directive);
if (filterConfigValue == null || Boolean.valueOf(filterConfigValue)) {
cacheControlDirectives.add(directive);
}
}
cacheControl = StringUtil.join(", ", cacheControlDirectives.toArray(new String[]{}));
}

/**
Expand All @@ -90,7 +134,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;

// set cache directives
httpServletResponse.setHeader(HTTPCacheHeader.CACHE_CONTROL.getName(), "no-cache, no-store");
httpServletResponse.setHeader(HTTPCacheHeader.CACHE_CONTROL.getName(), cacheControl);
httpServletResponse.setDateHeader(HTTPCacheHeader.EXPIRES.getName(), 0L);

filterChain.doFilter(servletRequest, servletResponse);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/samaxes/filter/util/Cacheability.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@
* @version 2.3.1
*/
public enum Cacheability {
NO_CACHE("no-cache"),

NO_STORE("no-store"),

/**
* Indicates that the response MAY be cached by any cache, even if it would normally be non-cacheable or cacheable
* only within a non-shared cache.
*/
PUBLIC("public"),

/**
* Indicates that all or part of the response message is intended for a single user and MUST NOT be cached by a
* shared cache. This allows an origin server to state that the specified parts of the response are intended for
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/samaxes/filter/util/StringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.samaxes.filter.util;

public class StringUtil
{
/**
* In abscence of {@link String#join} in 1.6
* @param delimiter the delimiter that separates each element
* @param elements the elements to join together.
*
* @return a new {@code String} that is composed of the {@code elements}
* separated by the {@code delimiter}
*/
public static String join(CharSequence delimiter, CharSequence... elements) {
boolean first = true;
StringBuilder str = new StringBuilder();
for (CharSequence s : elements) {
if (!first) str.append(delimiter);
str.append(s);
first = false;
}
return str.toString();
}
}