You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Yair Ben-Meir <ya...@sundaysky.com> on 2008/07/16 14:35:49 UTC

getting the ip address in the action method

Hi

I have a class implementing an interface annotated as a web service. In the
method of this class, I handle the request.

Now, I want to use the IP of the client in the logic of the method. So I
wrote an interceptor that gets the IP:

public class IpCxfInterceptor extends AbstractPhaseInterceptor<Message>

{

      @Override

      public void handleMessage(Message message) throws Fault

      {

            HttpServletRequest request =

 

            (HttpServletRequest)
message.get(AbstractHTTPDestination.HTTP_REQUEST);

            String clientAddress = null;

            if (null != request)

            {

                  clientAddress = request.getRemoteAddr();

            }

            System.out.println("IP="

                + clientAddress);

      }

      

      public IpCxfInterceptor()

      {

            super(Phase.RECIEVE);

      }

}

 

The problem is - how can I pass the IP to the method? The method's arguments
are according to the WSDL, I can't change them and add "ip" or something
like that.

 

Thanks

Yair

 

 


RE: getting the ip address in the action method

Posted by Yair Ben-Meir <ya...@sundaysky.com>.
mmmm.... didn't thought of that...
Thanks!

-----Original Message-----
From: Daniel Kulp [mailto:dkulp@apache.org] 
Sent: Wednesday, July 16, 2008 5:31 PM
To: users@cxf.apache.org
Subject: Re: getting the ip address in the action method


Why go through the trouble of using an interceptor for this?

If you do
@Resource
WebServiceContext ctx;


The ctx itself has the HttpServletRequest in it as well.    
MessageContext.SERVLET_REQUEST key.


Dan



On Jul 16, 2008, at 8:35 AM, Yair Ben-Meir wrote:

> Hi
>
> I have a class implementing an interface annotated as a web service.  
> In the
> method of this class, I handle the request.
>
> Now, I want to use the IP of the client in the logic of the method.  
> So I
> wrote an interceptor that gets the IP:
>
> public class IpCxfInterceptor extends  
> AbstractPhaseInterceptor<Message>
>
> {
>
>      @Override
>
>      public void handleMessage(Message message) throws Fault
>
>      {
>
>            HttpServletRequest request =
>
>
>
>            (HttpServletRequest)
> message.get(AbstractHTTPDestination.HTTP_REQUEST);
>
>            String clientAddress = null;
>
>            if (null != request)
>
>            {
>
>                  clientAddress = request.getRemoteAddr();
>
>            }
>
>            System.out.println("IP="
>
>                + clientAddress);
>
>      }
>
>
>
>      public IpCxfInterceptor()
>
>      {
>
>            super(Phase.RECIEVE);
>
>      }
>
> }
>
>
>
> The problem is - how can I pass the IP to the method? The method's  
> arguments
> are according to the WSDL, I can't change them and add "ip" or  
> something
> like that.
>
>
>
> Thanks
>
> Yair
>
>
>
>
>

---
Daniel Kulp
dkulp@apache.org
http://www.dankulp.com/blog






Re: getting the ip address in the action method

Posted by Daniel Kulp <dk...@apache.org>.
Why go through the trouble of using an interceptor for this?

If you do
@Resource
WebServiceContext ctx;


The ctx itself has the HttpServletRequest in it as well.    
MessageContext.SERVLET_REQUEST key.


Dan



On Jul 16, 2008, at 8:35 AM, Yair Ben-Meir wrote:

> Hi
>
> I have a class implementing an interface annotated as a web service.  
> In the
> method of this class, I handle the request.
>
> Now, I want to use the IP of the client in the logic of the method.  
> So I
> wrote an interceptor that gets the IP:
>
> public class IpCxfInterceptor extends  
> AbstractPhaseInterceptor<Message>
>
> {
>
>      @Override
>
>      public void handleMessage(Message message) throws Fault
>
>      {
>
>            HttpServletRequest request =
>
>
>
>            (HttpServletRequest)
> message.get(AbstractHTTPDestination.HTTP_REQUEST);
>
>            String clientAddress = null;
>
>            if (null != request)
>
>            {
>
>                  clientAddress = request.getRemoteAddr();
>
>            }
>
>            System.out.println("IP="
>
>                + clientAddress);
>
>      }
>
>
>
>      public IpCxfInterceptor()
>
>      {
>
>            super(Phase.RECIEVE);
>
>      }
>
> }
>
>
>
> The problem is - how can I pass the IP to the method? The method's  
> arguments
> are according to the WSDL, I can't change them and add "ip" or  
> something
> like that.
>
>
>
> Thanks
>
> Yair
>
>
>
>
>

---
Daniel Kulp
dkulp@apache.org
http://www.dankulp.com/blog





RE: getting the ip address in the action method

Posted by David Soroko <Da...@betfair.com>.
Beauty is in the eye of the beholder, same goes for elegance. 
You could probably append the IP information to the incoming message
using CXF, maybe there are other options, I am not a CXF expert. 

As you are not willing to expose this functionality as part of you
public interface (WSDL), most (all?) solutions will have a hacky feeling
to them.

-- david




-----Original Message-----
From: Yair Ben-Meir [mailto:yair.ben-meir@sundaysky.com] 
Sent: 16 July 2008 14:08
To: users@cxf.apache.org
Subject: RE: getting the ip address in the action method

Thanks David,
Let me start by saying I consider myself to be a Java specialist, after
programming in it for years and teaching courses about it. So I was
amazed
to find out about this class "ThreadLocal" which is in core Java, and I
(and
my colleagues here in the office) haven't heard about it, ever. I love
to
learn something new always and this was really surprising... so, thanks
about that.

Back to our subject, this is a good solution for me, but I wonder, is
that
the elegant solution? Isn't there a way using CXF to do something like
that?

Thanks
Yair



-----Original Message-----
From: David Soroko [mailto:David.Soroko@betfair.com] 
Sent: Wednesday, July 16, 2008 3:42 PM
To: users@cxf.apache.org
Subject: RE: getting the ip address in the action method

How about a thread local?

-----Original Message-----
From: Yair Ben-Meir [mailto:yair.ben-meir@sundaysky.com] 
Sent: 16 July 2008 13:36
To: users@cxf.apache.org
Subject: getting the ip address in the action method

Hi

I have a class implementing an interface annotated as a web service. In
the
method of this class, I handle the request.

Now, I want to use the IP of the client in the logic of the method. So I
wrote an interceptor that gets the IP:

public class IpCxfInterceptor extends AbstractPhaseInterceptor<Message>

{

      @Override

      public void handleMessage(Message message) throws Fault

      {

            HttpServletRequest request =

 

            (HttpServletRequest)
message.get(AbstractHTTPDestination.HTTP_REQUEST);

            String clientAddress = null;

            if (null != request)

            {

                  clientAddress = request.getRemoteAddr();

            }

            System.out.println("IP="

                + clientAddress);

      }

      

      public IpCxfInterceptor()

      {

            super(Phase.RECIEVE);

      }

}

 

The problem is - how can I pass the IP to the method? The method's
arguments
are according to the WSDL, I can't change them and add "ip" or something
like that.

 

Thanks

Yair

 

 


________________________________________________________________________
In order to protect our email recipients, Betfair Group use SkyScan from

MessageLabs to scan all Incoming and Outgoing mail for viruses.

________________________________________________________________________


________________________________________________________________________
In order to protect our email recipients, Betfair Group use SkyScan from 
MessageLabs to scan all Incoming and Outgoing mail for viruses.

________________________________________________________________________

RE: getting the ip address in the action method

Posted by Yair Ben-Meir <ya...@sundaysky.com>.
Thanks David,
Let me start by saying I consider myself to be a Java specialist, after
programming in it for years and teaching courses about it. So I was amazed
to find out about this class "ThreadLocal" which is in core Java, and I (and
my colleagues here in the office) haven't heard about it, ever. I love to
learn something new always and this was really surprising... so, thanks
about that.

Back to our subject, this is a good solution for me, but I wonder, is that
the elegant solution? Isn't there a way using CXF to do something like that?

Thanks
Yair



-----Original Message-----
From: David Soroko [mailto:David.Soroko@betfair.com] 
Sent: Wednesday, July 16, 2008 3:42 PM
To: users@cxf.apache.org
Subject: RE: getting the ip address in the action method

How about a thread local?

-----Original Message-----
From: Yair Ben-Meir [mailto:yair.ben-meir@sundaysky.com] 
Sent: 16 July 2008 13:36
To: users@cxf.apache.org
Subject: getting the ip address in the action method

Hi

I have a class implementing an interface annotated as a web service. In
the
method of this class, I handle the request.

Now, I want to use the IP of the client in the logic of the method. So I
wrote an interceptor that gets the IP:

public class IpCxfInterceptor extends AbstractPhaseInterceptor<Message>

{

      @Override

      public void handleMessage(Message message) throws Fault

      {

            HttpServletRequest request =

 

            (HttpServletRequest)
message.get(AbstractHTTPDestination.HTTP_REQUEST);

            String clientAddress = null;

            if (null != request)

            {

                  clientAddress = request.getRemoteAddr();

            }

            System.out.println("IP="

                + clientAddress);

      }

      

      public IpCxfInterceptor()

      {

            super(Phase.RECIEVE);

      }

}

 

The problem is - how can I pass the IP to the method? The method's
arguments
are according to the WSDL, I can't change them and add "ip" or something
like that.

 

Thanks

Yair

 

 


________________________________________________________________________
In order to protect our email recipients, Betfair Group use SkyScan from 
MessageLabs to scan all Incoming and Outgoing mail for viruses.

________________________________________________________________________


RE: getting the ip address in the action method

Posted by David Soroko <Da...@betfair.com>.
How about a thread local?

-----Original Message-----
From: Yair Ben-Meir [mailto:yair.ben-meir@sundaysky.com] 
Sent: 16 July 2008 13:36
To: users@cxf.apache.org
Subject: getting the ip address in the action method

Hi

I have a class implementing an interface annotated as a web service. In
the
method of this class, I handle the request.

Now, I want to use the IP of the client in the logic of the method. So I
wrote an interceptor that gets the IP:

public class IpCxfInterceptor extends AbstractPhaseInterceptor<Message>

{

      @Override

      public void handleMessage(Message message) throws Fault

      {

            HttpServletRequest request =

 

            (HttpServletRequest)
message.get(AbstractHTTPDestination.HTTP_REQUEST);

            String clientAddress = null;

            if (null != request)

            {

                  clientAddress = request.getRemoteAddr();

            }

            System.out.println("IP="

                + clientAddress);

      }

      

      public IpCxfInterceptor()

      {

            super(Phase.RECIEVE);

      }

}

 

The problem is - how can I pass the IP to the method? The method's
arguments
are according to the WSDL, I can't change them and add "ip" or something
like that.

 

Thanks

Yair

 

 


________________________________________________________________________
In order to protect our email recipients, Betfair Group use SkyScan from 
MessageLabs to scan all Incoming and Outgoing mail for viruses.

________________________________________________________________________

RE: getting the ip address in the action method

Posted by Yair Ben-Meir <ya...@sundaysky.com>.
Thanks! This works like a charm.
I m still happy I've learnt about ThreadLocal today...

-----Original Message-----
From: Ian Roberts [mailto:i.roberts@dcs.shef.ac.uk] 
Sent: Wednesday, July 16, 2008 4:09 PM
To: users@cxf.apache.org
Subject: Re: getting the ip address in the action method

Yair Ben-Meir wrote:
> The problem is - how can I pass the IP to the method? The method's
arguments
> are according to the WSDL, I can't change them and add "ip" or something
> like that.

Store the IP address as a property of the Message in your interceptor:

message.put("my.package.ClientIP", ipAddress);

In your implementation class, add a

@Resource
private WebServiceContext wsCtx;

and you should then be able to access the message property through that:

wsCtx.getMessageContext().get("my.package.ClientIP")

Ian

-- 
Ian Roberts               | Department of Computer Science
i.roberts@dcs.shef.ac.uk  | University of Sheffield, UK


Re: getting the ip address in the action method

Posted by Ian Roberts <i....@dcs.shef.ac.uk>.
Yair Ben-Meir wrote:
> The problem is - how can I pass the IP to the method? The method's arguments
> are according to the WSDL, I can't change them and add "ip" or something
> like that.

Store the IP address as a property of the Message in your interceptor:

message.put("my.package.ClientIP", ipAddress);

In your implementation class, add a

@Resource
private WebServiceContext wsCtx;

and you should then be able to access the message property through that:

wsCtx.getMessageContext().get("my.package.ClientIP")

Ian

-- 
Ian Roberts               | Department of Computer Science
i.roberts@dcs.shef.ac.uk  | University of Sheffield, UK

Re: getting the ip address in the action method

Posted by Ulhas Bhole <ul...@iona.com>.
I guess you mean you need access to the ip address in the implementation 
of the method. If you are using JAX-WS frontend you can add a custom 
header in the message and inject webServiceContext into your service 
implementation. once you are inside your method implementation you can 
retrive your custom header from the webServiceContext which has the IP 
address.

Here is a like to how to add out-of-band headers.

http://cxf.apache.org/faq.html#FAQ-HowcanIaddsoapheaderstotherequest%252Fresponse%253F

Look at "How can I add soap headers to the request/response?" section 
point 4.

-- Ulhas



Yair Ben-Meir wrote:
> Hi
>
> I have a class implementing an interface annotated as a web service. In the
> method of this class, I handle the request.
>
> Now, I want to use the IP of the client in the logic of the method. So I
> wrote an interceptor that gets the IP:
>
> public class IpCxfInterceptor extends AbstractPhaseInterceptor<Message>
>
> {
>
>       @Override
>
>       public void handleMessage(Message message) throws Fault
>
>       {
>
>             HttpServletRequest request =
>
>  
>
>             (HttpServletRequest)
> message.get(AbstractHTTPDestination.HTTP_REQUEST);
>
>             String clientAddress = null;
>
>             if (null != request)
>
>             {
>
>                   clientAddress = request.getRemoteAddr();
>
>             }
>
>             System.out.println("IP="
>
>                 + clientAddress);
>
>       }
>
>       
>
>       public IpCxfInterceptor()
>
>       {
>
>             super(Phase.RECIEVE);
>
>       }
>
> }
>
>  
>
> The problem is - how can I pass the IP to the method? The method's arguments
> are according to the WSDL, I can't change them and add "ip" or something
> like that.
>
>  
>
> Thanks
>
> Yair
>
>  
>
>  
>
>
>   

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland