1

Is there any way to get the request (HttpServletRequest?) programmatically? I can only find how to do it with an annotation on the endpoint method/class.

Per https://stackoverflow.com/a/5118844/190164 I can add an annotated argument to my endpoint:

@POST 
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public String showTime(
    @Context HttpServletRequest httpRequest
) {
    // The method body
}

Or I can injected in the class(https://stackoverflow.com/a/26181971/190164)

public class MyResource {

  @Context
  private HttpServletRequest httpRequest;

  @GET  
  public Response foo() {  
    httpRequest.getContentType(); //or whatever else you want to do with it
  }
}

however I would like access to the request in another class that isn't directly linked to Jersey. Adding the @Context injection like in the second example above doesn't work, as the class isn't instantiated by Jersey. I'd like to be able to do something like

HttpServletRequest.getCurrentRequest()

but I haven't been able to find any static method somewhere.

Cœur
  • 37,241
  • 25
  • 195
  • 267
zacaj
  • 1,987
  • 1
  • 19
  • 39
  • can you please clarify more with code or attempts to write code? – MrMisery Jul 20 '17 at 19:39
  • @MrMisery added a few examples – zacaj Jul 20 '17 at 19:47
  • Please show exactly _where_ you want to access it, and how it is connected to your Jersey resource – Paul Samsotha Jul 20 '17 at 22:06
  • @peeskillet I want to be able to access it from any function called during the processing of the resource, without having to make changes to all my endpoints. The place accessing it may have no 'connection' to the resource beyond that the resource function is somewhere up the call stack – zacaj Jul 20 '17 at 23:54
  • You're best bet is to use dependency injection. The HttpServletRequest can be injected into any services that are bound in the system. Apart from that, it's good practice to use DI. See https://stackoverflow.com/documentation/jersey/7016/dependency-injection-with-jersey/23632/basic-dependency-injection-using-jerseys-hk2#t=201707210838221848312 – Paul Samsotha Jul 21 '17 at 08:38

1 Answers1

1

If you are looking for some security solutions you can use servlet filters (create class that implements Filter) or you can implement ContainerRequestFilter and by overriding filter you can perform your filtering .Outside filters The context elements are always only accessible in the controller (where you place the path annotations) , there is no way to access this type of content from outside the controller other than passing it to the method or Object desired:

  @Context
private HttpServletRequest httpRequest;

  @GET  
  public Response foo() {  
    someMethod(httpRequest); //or whatever else you want to do with it
  }
}

hope this helps.

MrMisery
  • 406
  • 7
  • 19
  • I'm not doing any security stuff. I've wanted this for a few things, but I guess the simplest one is I wanted to include the request url in my logging – zacaj Jul 20 '17 at 20:04
  • as i have mentioned you can pass it as parameter to your method. it is also simple this way, as I didn't fully understand what you are up to. – MrMisery Jul 20 '17 at 20:06
  • I specifically don't want to pass it to my resource. I'd need to pass it through at least 50 functions if I did that – zacaj Jul 20 '17 at 23:52
  • you can pass it to the class containing those functions, there is no other way – MrMisery Jul 21 '17 at 10:53
  • but I could have multiple requests on different threads, so any 'globals' (in classes) wouldn't work – zacaj Jul 21 '17 at 12:48
  • if you have still not solved this i suggest you checkout this answer. https://stackoverflow.com/questions/30022078/jersey-and-hk2-injecting-current-user – MrMisery Aug 03 '17 at 06:51