It's not a secret anymore!*
The moose likes Web Services and the fly likes Jersey RESTful Web Service - How to determine remote host Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Jersey RESTful Web Service - How to determine remote host" Watch "Jersey RESTful Web Service - How to determine remote host" New topic
Author

Jersey RESTful Web Service - How to determine remote host

Clemens Berndt
Greenhorn

Joined: Mar 03, 2009
Posts: 1
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
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2906
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


"Don't succumb to the false authority of a tool or model. There is no substitute for thinking."
Andy Hunt, Pragmatic Thinking & Learning: Refactor Your Wetware p.41
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39537
    
  27
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?


Ping & DNS - updated with new look and Ping home screen widget
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2906
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
Marshal

Joined: Mar 22, 2005
Posts: 39537
    
  27
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
Marshal

Joined: Mar 22, 2005
Posts: 39537
    
  27
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jersey RESTful Web Service - How to determine remote host
 
Similar Threads
job oppurtunity
REST in mobiles
How to consume and parse JSON in the REST?
I want to be a greedy corrupt official
About New Jersey