users@jersey.java.net

Re: [Jersey] ResourceFilter and getRemoteAdress

From: Adam Miller <adamil_at_hypolinux.se>
Date: Mon, 30 Nov 2009 20:25:41 +0100

 Thx Paul!

My solution (in pseudo code) was:

 public class ResourceFilterFactory implements ResourceFilterFactory {

    private final HttpServletRequest httpServletRequest;

    public ResourceFilterFactory(@Context HttpServletRequest hsr) {
        this.httpServletRequest = hsr;
    }

    public List<ResourceFilter> create(AbstractMethod am) {

        if(am.isAnnotationPresent(IpCheck.class)) {
            return Collections.<ResourceFilter>singletonList(new
IpRequestFilter((AbstractSubResourceMethod)am));
        } else {
            return null;
        }
    }


    private class IpRequestFilter implements ResourceFilter,
ContainerRequestFilter {
        private final AbstractSubResourceMethod asrm;

        public IpRequestFilter(AbstractSubResourceMethod asrm) {
            this.asrm = asrm;
        }

        ...

        public ContainerRequest filter(ContainerRequest request) {

           // YES it Works!!!
           String ip = httpServletRequest.getRemoteAddr());

            ...

            return request;
        }
    }

Regards Adam


On Mon, Nov 30, 2009 at 4:49 PM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:

>
> On Nov 30, 2009, at 4:40 PM, Adam Miller wrote:
>
> Thanks,
>
> do you mean inject to ResourceFilter or ResourceFilterFactory.
>
>
> Sorry, the latter, ResourceFilterFactory, i always get them mixed up :-) as
> the class implementing ResourceFilterFactory is the class that will get
> instantiated by the Jersey runtime.
>
> Paul.
>
> like in
> http://markmail.org/message/hsjknjechtlqrwja#query:+page:1+mid:7wya4skgowfx37t6+state:results
>
> i have trouble getting it to work i only get null reference to the request.
>
> regards Adam
>
>
> On Mon, Nov 30, 2009 at 2:13 PM, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:
>
>>
>> On Nov 30, 2009, at 1:47 PM, Adam Miller wrote:
>>
>> I uses ResourceFilter for my jersey resources to do access control and
>>> it's really great and easy to use.
>>> Now a would like to access remoteadress ip in my filter. I have a
>>> ContainerRequest that don't give me the ip.
>>>
>>>
>> Currently you have to inject HttpServletRequest:
>>
>> @Context HttpServletRequest hsr;
>>
>> And then call:
>>
>> hsr.getRemoteAddr()
>>
>> You can perform injection on the ResourceFilte. A thread local proxy will
>> be injected, so if you try to access it when a request is not in scope it
>> will throw an IllegalStateException.
>>
>> Paul.
>>
>>
>> Does any body know how to get the ip? Or is there another preferable way
>>> to do filtering based on ip.
>>>
>>> best regards Adam
>>>
>>
>>
>
>