Forked from theotherian/ Jersey Resource Filters via Custom Annotations
Last active
January 5, 2018 15:36
-
-
Save caseyscarborough/f4266646f727fa9f7cc6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Files for demonstrating custom binding of Jersey resource filters using annotations |
This file contains hidden or 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; | |
@Target(ElementType.METHOD) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface IE6NotSupported {} |
This file contains hidden or 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.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() { | |
... | |
} | |
} |
This file contains hidden or 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 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 | |
)); | |
} | |
} |
This file contains hidden or 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 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