Using jersey @Context inside fabric3

25 views
Skip to first unread message

Andrew Forster

unread,
Nov 19, 2015, 11:45:53 AM11/19/15
to fabric3
 I was looking for a little bit of help. I have a stackoverflow question here  http://stackoverflow.com/questions/33785267/using-javax-ws-rs-core-context-with-fabric3 .  I was wondering if anyone could help with this. A little background, we are trying to implement a ContainerRequestFilter to handle piwiks analytics for our webservices. The filter works well for what we need, except we can't get access to the HttpServletRequest to get the remote ip address. The @Context isn't working and causes the HttpServletRequest to be null in the filter. So here are my main questions:
Is there a way to use @Context inside a ContainerRequestFilter?
Is a filter the right place for this?
If not, is there another way to do method and class level binding that captures all of the incoming requests?

Jim Marino

unread,
Nov 20, 2015, 9:57:22 AM11/20/15
to fabric3
Hi Andrew,

Someone else just raised the same exact question to me offline. Currently @Context injection isn't supported but that will change in the next day or two as it will be added to the 3.0 line. So, adding @Context to a filter should work once that check in is pushed.

Another option that works with the existing 3.0 line is to retrieve the HTTPServletRequest/Response headers from an injected Fabric3RequestContext#getHeader() passing either "fabric3.httpRequest" or "fabric3.httpResponse".

I'll send a note once @Context support is available.

Let me know if that solves your use case.

Jim
 

--
You received this message because you are subscribed to the Google Groups "fabric3" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fabric3+u...@googlegroups.com.
To post to this group, send email to fab...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fabric3/4a143deb-e175-417f-adb1-04b2c5b90e18%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Forster

unread,
Nov 20, 2015, 11:02:33 AM11/20/15
to fabric3
This worked perfectly. Thank you for your help.

Jim Marino

unread,
Nov 20, 2015, 3:37:02 PM11/20/15
to fabric3
FTI I checked in support for @Context injection. Filters can now be injected as with the following types:


public class TestRequestFilter implements ContainerRequestFilter {

    @Context
    protected UriInfo uriInfo;

    @Context
    protected Application application;

    @Context
    protected Request request;

    @Context
    protected HttpHeaders headers;

    @Context
    protected SecurityContext securityContext;

   // ....
}


Note that filters can be stateless or composite scoped. In the case that a filter is composite scoped, the injected context proxy will be threadsafe.

Let me know if there are any questions.

Jim


Andrew Forster

unread,
Nov 24, 2015, 5:06:47 PM11/24/15
to fabric3
 The @Context worked perfectly as I mentioned earlier. I am having issues with external configuration within the filter now. I have looked over the documentation here https://fabric3.atlassian.net/wiki/display/FABRIC/Service+Configuration+Files , and I just can't get it to work. Is it possible to reference an external configuration file from within a filter? If so, can you give a little more detailed example of how it works? I apologize if this is the incorrect location to ask questions like this, I am just having trouble finding examples out there.

Jim Marino

unread,
Nov 30, 2015, 10:42:00 AM11/30/15
to fabric3

Hi Andrew,

This should be the same as using @Property/ @Source with a systemConfig file:

1. Create a configuration file, e.g. foo.xml, and add it to the deploy directory:
 


2. In your component, create a property that uses XPath to reference a value inside the config file:


Let me know if this works for you.

Jim



On Tue, Nov 24, 2015 at 11:06 PM, Andrew Forster <andrewfo...@gmail.com> wrote:
 The @Context worked perfectly as I mentioned earlier. I am having issues with external configuration within the filter now. I have looked over the documentation here https://fabric3.atlassian.net/wiki/display/FABRIC/Service+Configuration+Files , and I just can't get it to work. Is it possible to reference an external configuration file from within a filter? If so, can you give a little more detailed example of how it works? I apologize if this is the incorrect location to ask questions like this, I am just having trouble finding examples out there.

--
You received this message because you are subscribed to the Google Groups "fabric3" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fabric3+u...@googlegroups.com.
To post to this group, send email to fab...@googlegroups.com.

Andrew Forster

unread,
Dec 3, 2015, 5:30:27 PM12/3/15
to fabric3
With Jim's help I got a solution that worked for external configuration within a filter.


The filter class with no composite files :
https://gist.github.com/bforster30/ddd45f1c290ff8d8ee38

The config file that is inside the deploy directory:
https://gist.github.com/bforster30/3180cadd9b8cc12dc68c
Reply all
Reply to author
Forward
0 new messages