7 Replies Latest reply on Aug 21, 2011 11:09 PM by ffang

    How to get the ip address using cxfbc?

    llwang0455

      Hi,all,

      I use the cxfbc to proxy a webservice out of servicemix. In the beans.xml file I use the element of cxfbc such as consumer and provider.

      I know that the processing of webservice is by cxf ,and the jetty first received the request, but I can't get the ip address of the request when using cxfbc,even using the MessageExchange object ...

      I want to know how to get the ip address by cxfbc or cxf or MessageExchange,so I can get which client that are accessing the proxy service in servicemix.

       

      I use servicemix-4.4.0,is anyone who has the same requirement with me ?

      I need your help.

       

      Edited by: llwang0455 on Aug 19, 2011 7:45 AM

        • 1. Re: How to get the ip address using cxfbc?
          ffang

          Hi,

           

          You can add an customer inInterceptor for cxf bc consumer endpoint to do so. something like

           

          public class GrepClientIPInterceptor extends AbstractPhaseInterceptor{

                  

                  public GrepClientIPInterceptor() {

                          super(Phase.RECEIVE);

                  }

           

           

                  public void handleMessage(Message message) throws Fault {

                          HttpServletRequest request = (HttpServletRequest)

          message.get(AbstractHTTPDestination.HTTP_REQUEST);

                          request.getRemoteAddr();// this is the client ip address

                          }

                  }

          }

           

          Freeman

          • 2. Re: How to get the ip address using cxfbc?
            llwang0455

            Thank you for your answer.

            I get it ,using the Interceptor I can get the request before cxfbc consumer,and I have another question , it is that I have many consumer,namely I have many bundles in the deploy folder,then how can I config the same Interceptor to all the cxfbc consumer? Or Could I not change the beans.xml file in the bundle which provides the proxy webservice,and add the Interceptor configuration to all cxfbc consumer?

            I know the description is a lot chaos, sorry about that.Hope your advice.

            • 3. Re: How to get the ip address using cxfbc?
              ffang

              Hi,

               

              You can expose this interceptor as a OSGi service and refer it in all of your cxf bc consumer bundles, but you do need change beans.xml to refer it anyway.

               

              Freeman

              • 4. Re: How to get the ip address using cxfbc?
                llwang0455

                Got it ,thx a lot!

                How can I take the ip address getting from the interceptor to the cxfbc ? Such as saving it to the MessageExchange object? If so,I would get the address in the listener class.

                Do you have any ideas to tell me?

                • 5. Re: How to get the ip address using cxfbc?
                  ffang

                  Hi,

                   

                  You can get the MessageExchange from cxf message like

                  MessageExchange exchange = message

                                      .getContent(MessageExchange.class);

                   

                  then you can save the client ip address as the MessageExchange property.

                   

                  You need change your interceptor phase as Phase.INVOKE, as in this phase the JBI MessageExchange is avaible in cxf message.

                   

                  Hope this helps.

                  Freeman

                  • 6. Re: How to get the ip address using cxfbc?
                    llwang0455

                    Thanks for your answers!

                    Get it,I will try!

                    I have an idea about the problem of not changing the beans.xml file,I need your opinion.

                    It is that if I can modify the source code in cxfbc about the interceptor,then I can put the interceptor as the default interceptor of the cxfbc consumer element .

                    It's just a thought,could I do that as that?

                    • 7. Re: How to get the ip address using cxfbc?
                      ffang

                      Hi,

                       

                      Yeah, you can do it, you need download the src and modify it and then build it again.

                      Add a interceptor should be straightfoward,  just add your interceptor in CxfBcConsumer should be ok.

                       

                      Freeman