File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 2922
    
    5
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
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41525
    
  53
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 - my free Android networking tools app
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
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: 41525
    
  53
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: 41525
    
  53
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.
 
wood burning stoves
 
subject: Jersey RESTful Web Service - How to determine remote host