• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Jersey RESTful Web Service - How to determine remote host

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can one get the remote address and/or hostname of the client endpoint that originated a request within a Jersey Resource class?
I had a look at the injectable objects available via the @Context annotation, but none provide that sort of information.

Any pointers are much appreciated
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to Javaranch!

If Jersey is hosted by a Servlet Container you should be able to use:

@Context javax.servlet.http.HttpServletRequest request

and then use request.getRemoteAddr().

See Re: [Jersey] How to retrieve user info from your WS
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting ... how would I know that HttpServletRequest is an injectable class? Looking at the javadocs of javax.ws.rs.core.Context, it only mentions UriInfo, Request, HttpHeaders, SecurityContext and Providers. What are Clemens and I missing?
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Looking at the javadocs of javax.ws.rs.core.Context, it only mentions UriInfo, Request, HttpHeaders, SecurityContext and Providers. What are Clemens and I missing?


That is why I left the link at the end of the post.

Apparently"injectability" of HttpServletRequest is part of Jersey, not JAX-RS - so there wouldn't be any mention of it in the documentation of the javax.ws.rs.core package.
It seems that com.sun.jersey.spi.inject.PerRequestTypeInjectableProvider is behind that magic.
Paul Sandoz also mentions that it should be possible to "use a Jersey filter and set properties on @HttpContext" to avoid dependencies on HttpServletRequest - but he doesn't go into any details (though there are some hints here).
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just discovered some more hints of how Jersey filters work in the package javadocs of the com.sun.jersey.api.container.filter package.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just saw that chapter 6.1 of the JAX-RS spec actually mentions that for servlet-based implementations, @Context must be able to inject ServletConfig, ServletContext, HttpServletRequest and HttpServletResponse.
 
reply
    Bookmark Topic Watch Topic
  • New Topic