Skip to content

Allow specificying extensions to exclude #6

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

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
29 changes: 29 additions & 0 deletions src/main/java/com/samaxes/filter/CacheFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@
package com.samaxes.filter;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collector;
import java.util.stream.Collectors;

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;
import javax.servlet.http.HttpServletResponseWrapper;

Expand Down Expand Up @@ -177,6 +183,8 @@ public class CacheFilter implements Filter {

private String vary;

private List<String> excludeExtensions;

/**
* {@inheritDoc}
*/
Expand All @@ -194,6 +202,14 @@ public void init(FilterConfig filterConfig) throws ServletException {
: Cacheability.PUBLIC;
mustRevalidate = Boolean.valueOf(filterConfig.getInitParameter(CacheConfigParameter.MUST_REVALIDATE.getName()));
vary = filterConfig.getInitParameter(CacheConfigParameter.VARY.getName());
excludeExtensions = Arrays.stream(
Optional.ofNullable(
filterConfig.getInitParameter(CacheConfigParameter.EXCLUDE_EXTENSIONS.getName()))
.orElse("")
.split(" ")
)
.filter(ext -> !ext.isEmpty())
.collect(Collectors.toList());
}

/**
Expand All @@ -205,6 +221,19 @@ public void init(FilterConfig filterConfig) throws ServletException {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
// If extensions provided filter on them
if (!excludeExtensions.isEmpty()) {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String uri = httpServletRequest.getRequestURI();
String ext = uri.substring(uri.lastIndexOf("."));
// if found, continue filter chain and skip our the rest of our cache filter
if (excludeExtensions.contains(ext)) {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
}


HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
StringBuilder cacheControl = new StringBuilder(cacheability.getValue()).append(", max-age=").append(expiration);
if (mustRevalidate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ public enum CacheConfigParameter {
* Cache directive to instructs proxies to cache different versions of the same resource based on specific
* request-header fields.
*/
VARY("vary");
VARY("vary"),
/**
* Cache directive to define any file extensions that should be ignored. Expect a space delimited list.
*
* Example: .html .map .xyz
*/
EXCLUDE_EXTENSIONS("exclude-extensions"),
;

private final String name;

Expand Down