0

I've tried the approach here without luck: As the comments in the answer there mentions the HttpServletRequest is just null.

This is my filter:

public void myFilter(ContainerRequestContext request) throws IOException 
  {
   // I don't see a way to get the IP address from the ContainerRequestContext
 }

If I try to use @Context HttpServletRequest httpServletRequest, as the answer in the other question suggests, I just get a NullPointerException.

Another similar question: How to get source address / ip from inside ContainerResponseFilter

L42
  • 3,052
  • 4
  • 28
  • 49
  • 1
    How are you registering the filter? – Paul Samsotha Jan 23 '18 at 16:04
  • In my test I'm using `resourceConfig.register(MyFilter.class)`. In my app it's just included (because the class is annotated with `@Provider` and `Priority`?). – L42 Jan 24 '18 at 07:19
  • So is it in your test, is that where it's null, or in the app, or in both? – Paul Samsotha Jan 24 '18 at 16:33
  • In the test. I'll confirm tomorrow that it's in the app as well. – L42 Jan 24 '18 at 20:15
  • Are you using the Jersey test framework? – Paul Samsotha Jan 24 '18 at 20:26
  • Yes. Thank you for digging into what I'm doing. I'll provide a reproducible example as soon as I'm able. – L42 Jan 25 '18 at 06:19
  • After a bunch of testing (and help from you in https://stackoverflow.com/questions/29386360/jersey2-unit-testing-httpservletrequest-is-null) it looks like my test setup was wrong. I chose to simply mock the httpServletRequest in our tests for now, as I can see that it does its job when our API is started as normal. Do you know of a way to make it work when we're using `jersey-test-framework-provider-jetty`? – L42 Jan 25 '18 at 09:36
  • 1
    [Doesn't look like they want to add support for it](https://github.com/jersey/jersey/issues/2764). – Paul Samsotha Jan 25 '18 at 11:40
  • Could I ask your advice on what you would do? Mock the httpServletRequests in tests, or use grizzly instead of jetty? – L42 Jan 25 '18 at 11:52
  • 1
    If you are only using the servlet request in a couple places, just mocking is OK. If you are using Jetty for the app, might be better to just use the jetty container. Though I don't think there would be any problem using the grizzly container to test. – Paul Samsotha Jan 25 '18 at 12:38
  • I posted an answer but if you would like to explain it then I'll choose your answer instead. Thank you again! – L42 Jan 26 '18 at 07:39

1 Answers1

0

The nullpointerexception only occured in our tests. peeskillet helped me realize that injecting HttpServletRequest won't work as long as we're using jersey-test-framework-provider-jetty for our tests (see for example https://github.com/jersey/jersey/issues/2764 or https://github.com/jersey/jersey/issues/3092). If we had used something like GrizzlyWebTestContainerFactory, then injecting the HttpServletRequest would work since we then would have a ServletDeploymentContext.

We went for this solution in our tests, i.e. just mocking the HttpServletRequest where needed.

L42
  • 3,052
  • 4
  • 28
  • 49