How to get remote client IP with embedded Grizzly server

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get remote client IP with embedded Grizzly server

Hou, Chunxiao

Hi there,

 

I understand I can get client IP from HttpServletRequest but I am running an embedded grizzly server and have packagesResourceConfigs for it:

 

Grizzly http Server:

ResourceConfig rc = new PackagesResourceConfig(RESOURCE_DIR);

rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, rc);

 

Resource:

public abstract class AbstractGetRiskResource {

                private static final Logger logger = LoggerFactory.getLogger(AbstractGetRiskResource.class);

               

                @GET

                public Response getMessage(@Context final UriInfo uriInfo, @Context HttpHeaders hh) {

                                String requestUri = uriInfo.getRequestUri().toString();

}

 

I’ve tried to add @Context HttpServletRequest to the Resource GET method parameter list, but it doesn’t work.

Can someone please suggest how I can retrieve client IP info from the resource GET method? Many thanks!

 

Regards,

David

                               

This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.

Reply | Threaded
Open this post in threaded view
|

Re: How to get remote client IP with embedded Grizzly server

oleksiys
Administrator
Hi David,

according to
https://java.net/jira/browse/JERSEY-1170

it works in Jersey 2.0+ only.


the code looks like:

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import org.glassfish.grizzly.http.server.Request;

@Path("/test")
public class TestResource {

    @GET
    @Produces("text/plain")
    public String getRemoteAddr(@Context Request request) {
        return request.getRemoteAddr() + ":" + request.getRemotePort();
    }
}


WBR,
Alexey.


On 20.09.13 10:20, Hou, Chunxiao wrote:

Hi there,

 

I understand I can get client IP from HttpServletRequest but I am running an embedded grizzly server and have packagesResourceConfigs for it:

 

Grizzly http Server:

ResourceConfig rc = new PackagesResourceConfig(RESOURCE_DIR);

rc.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

httpServer = GrizzlyServerFactory.createHttpServer(BASE_URI, rc);

 

Resource:

public abstract class AbstractGetRiskResource {

                private static final Logger logger = LoggerFactory.getLogger(AbstractGetRiskResource.class);

               

                @GET

                public Response getMessage(@Context final UriInfo uriInfo, @Context HttpHeaders hh) {

                                String requestUri = uriInfo.getRequestUri().toString();

}

 

I’ve tried to add @Context HttpServletRequest to the Resource GET method parameter list, but it doesn’t work.

Can someone please suggest how I can retrieve client IP info from the resource GET method? Many thanks!

 

Regards,

David

                               

This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email.