Last active
March 5, 2018 17:51
-
-
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.
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
Files for demonstrating custom binding of Jersey resource filters using 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; | |
@Target(ElementType.METHOD) | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface IE6NotSupported {} |
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.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 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 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