Last active
February 12, 2019 11:15
-
-
Save alexnederlof/6098121 to your computer and use it in GitHub Desktop.
Jersey Cache-Header control via Annotations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.ElementType; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.annotation.Target; | |
import java.util.concurrent.TimeUnit; | |
public final class CacheAnnotations { | |
/** | |
* Set the "Max-age" Cache header. | |
* | |
* @see <a href='http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.3'>W3C Header | |
* Field Definitions</a> | |
*/ | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
public @interface CacheMaxAge { | |
/** | |
* @return The amount of time to cache this resource. | |
*/ | |
long time(); | |
/** | |
* @return The {@link TimeUnit} for the given {@link #time()}. | |
*/ | |
TimeUnit unit(); | |
} | |
/** | |
* Sets the cache header to the value "no cache" | |
* | |
* @see <a href='http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.1'>W3c Header | |
* Field Definitions</a> | |
*/ | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.METHOD) | |
public @interface NoCache { | |
} | |
private CacheAnnotations() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Collections; | |
import java.util.List; | |
import javax.ws.rs.core.HttpHeaders; | |
import com.alexnederlof.bigcrawl.server.web.CacheAnnotations.CacheMaxAge; | |
import com.alexnederlof.bigcrawl.server.web.CacheAnnotations.NoCache; | |
import com.sun.jersey.api.model.AbstractMethod; | |
import com.sun.jersey.spi.container.ContainerRequest; | |
import com.sun.jersey.spi.container.ContainerRequestFilter; | |
import com.sun.jersey.spi.container.ContainerResponse; | |
import com.sun.jersey.spi.container.ContainerResponseFilter; | |
import com.sun.jersey.spi.container.ResourceFilter; | |
import com.sun.jersey.spi.container.ResourceFilterFactory; | |
public class CacheFilterFactory implements ResourceFilterFactory { | |
@Override | |
public List<ResourceFilter> create(AbstractMethod am) { | |
if (am.isAnnotationPresent(CacheMaxAge.class)) { | |
CacheMaxAge maxAge = am.getAnnotation(CacheMaxAge.class); | |
return newCacheFilter("max-age= " + maxAge.unit().toSeconds(maxAge.time())); | |
} else if (am.isAnnotationPresent(NoCache.class)) { | |
return newCacheFilter("no-cache"); | |
} else { | |
return Collections.emptyList(); | |
} | |
} | |
private List<ResourceFilter> newCacheFilter(String content) { | |
return Collections | |
.<ResourceFilter> singletonList(new CacheResponseFilter(content)); | |
} | |
private static class CacheResponseFilter implements ResourceFilter, ContainerResponseFilter { | |
private final String headerValue; | |
CacheResponseFilter(String headerValue) { | |
this.headerValue = headerValue; | |
} | |
@Override | |
public ContainerRequestFilter getRequestFilter() { | |
return null; | |
} | |
@Override | |
public ContainerResponseFilter getResponseFilter() { | |
return this; | |
} | |
@Override | |
public ContainerResponse filter(ContainerRequest request, ContainerResponse response) { | |
response.getHttpHeaders().putSingle(HttpHeaders.CACHE_CONTROL, headerValue); | |
return response; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment