DirkM wrote:
> Some of my service calls require the Accept-Language header to be set. I want
> to return a 400 if the service is called and the header is not set.
>
> I took a look at the
> http://fisheye4.atlassian.com/browse/jersey/trunk/jersey/jersey-server/src/main/java/com/sun/jersey/api/container/filter/RolesAllowedResourceFilterFactory.java?r=2584
> source code for RolesAllowedResourceFilterFactory, and created something
> similar that checks the resource method for an annotation called
> @LanguageRequired:
>
> ...
> public class MyResource {
> @GET
> @Path("/testLanguage")
> @LocaleRequired
> public String testLanguage() {
> return "test";
> }
> }
> ...
>
> public class LocaleCheckFilterFactory implements ResourceFilterFactory {
>
> private class Filter implements ResourceFilter, ContainerRequestFilter {
> @Override
> public ContainerRequestFilter getRequestFilter() {
> return this;
> }
>
> @Override
> public ContainerResponseFilter getResponseFilter() {
> return null;
> }
>
> @Override
> public ContainerRequest filter(ContainerRequest request) {
> if(/* Accept-Language is not present */) {
> throw new MissingLocaleException("Missing or empty
> Accept-Language header");
> }
>
> return request;
> }
> }
>
>
> @Override
> public List<ResourceFilter> create(AbstractMethod am) {
>
> if (!am.isAnnotationPresent(LocaleRequired.class)) {
> return null;
> }
>
> return Collections.<ResourceFilter>singletonList(new Filter());
> }
> }
>
>
> The problem is that when I debug, the AbstractMethod parameter has the @GET
> and @Path annotations but doesn't have the @LocaleRequired annotation in its
> list of annotations.
>
> Can anyone point me to why this would be, or a better way of achieving my
> aim?
>
Did you say "@Retention(RetentionPolicy.RUNTIME)" when you defined the
@LocaleRequired annotation? If not, then it will get erased by the
compiler, and won't be visible to runtime reflection calls.
Craig
> Thanks,
> Dirk
>