Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save theotherian/5882658 to your computer and use it in GitHub Desktop.
Save theotherian/5882658 to your computer and use it in GitHub Desktop.
A DynamicFeature implementation for binding filters to resources with Jersey 2.0, in this case flagging certain resources as not supporting Internet Explorer 6.
Files for demonstrating custom binding of Jersey resource filters using annotations
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface IE6NotSupported {}
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
public class IE6NotSupportedFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
if (isIE6Browser(requestContext.getHeaderString("User-Agent"))) {
requestContext.abortWith(Response.status(Status.PRECONDITION_FAILED).entity(getFailurePage()).build());
}
}
private boolean isIE6Browser(String headerString) {
...
}
private Object getFailurePage() {
...
}
}
import org.glassfish.jersey.server.ResourceConfig;
import com.google.common.collect.Sets;
public class MyApplication extends ResourceConfig {
public MyApplication() {
super(Sets.<Class<?>>newHashSet(
// resources, other features and providers would also go here
ResourceFilterBindingFeature.class
));
}
}
import javax.ws.rs.container.DynamicFeature;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;
@Provider
public class ResourceFilterBindingFeature implements DynamicFeature {
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
if (resourceInfo.getResourceMethod().isAnnotationPresent(IE6NotSupported.class)) {
context.register(IE6NotSupportedFilter.class);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment