4 Replies Latest reply on Nov 9, 2006 7:06 PM by Thomas Diesler

    getting ip address from webservice

    Iván Párraga García Newbie

      Hi,

      I've developed and deployed some webservices using ws4ee in JBoss 4.0.3. Now I need to log some data. I specially need to save ip addresses from clients accessing my webservices.

      Since that the implementation of a webservice is just a "normal" class, it's not clear to me how to access to the client request info from the code.

      Any suggestion wellcome.

      Thanks,

      Iván

        • 1. Re: getting ip address from webservice
          sappenin Novice

          You need to create an implementation of the javax.xml.rpc.handler.GenericHandler class, and intercept the web-services call before it makes it to your SEI.

          So, create a handler class (separate from your SEI interface and impl). Makes sure this handler class extends GenericHandler (see above for correct package). You'll need to override the function "public QName[] getHeaders()" (because it's abstract in GenericHandler) so that it returns the QNames of the services you want "handled".

          Mine looks like this (this is just the QName of each endpoint):

          public class ExampleGenericHandler extends GenericHandler
          {
          
          @Override
          public QName[] getHeaders()
          {
          String NAMESPACE = "http://com.example";
          QName SERVICE_NAME = new QName(NAMESPACE, "MySEI_JSEService");
          
          QName[] qNames = {SERVICE_NAME};
          return qNames;
          }
          }
          


          Next, in your webservices.xml descriptor that is deployed with your webservice, you'll want to include the handler definition inside of the port-component, like so:

          
          <webservices
          ...
          
           <webservice-description>
          <webservice-description-name>...</webservice-description-name>
           <wsdl-file>..._JSEService.wsdl</wsdl-file>
           ...
           <port-component>
           <port-component-name>PortComponent</port-component-name>
           <wsdl-port>ExamplePort</wsdl-port>
           <service-endpoint-interface>com.example.exampleInterface</service-endpoint-interface>
           <service-impl-bean>
           <servlet-link>Serlvet</servlet-link>
           </service-impl-bean>
           <handler>
           <handler-name>ExampleHanlder</handler-name>
           <handler-class>com.example.ExampleHandler</handler-class>
           </handler>
           </port-component>
           </webservice-description>
           </webservices>
          


          If you already have a functional web-services SEI working, then you should already have everything in place to implement the above (including a webservices.xml file).

          Finally, in your GenericHandler class, you can do something like this:

          
          /* (non-Javadoc)
           * @see javax.xml.rpc.handler.GenericHandler#handleRequest(javax.xml.rpc.handler.MessageContext)
           */
           @Override
           public boolean handleRequest(MessageContext context)
           {
           //Output the IP address of the caller.
           String ip = (String) context.getProperty("remoteaddr");
           log.error("Caller IP: " + ip);
          
           return super.handleRequest(context);
           }
          


          For more info about GenericHandlers, see: http://labs.jboss.com/portal/jbossws/user-guide/en/html/headers-handlers.html#handlers

          Also, this post was helpful if you want to try to determine what all the properties can be found in MessageContext: http://www.jboss.com/index.html?module=bb&op=viewtopic&t=62299

          • 3. Re: getting ip address from webservice
            Romeu Figueira Newbie

            What if one doesn't have that property listed (remoteaddr?)

            public boolean handleRequest(MessageContext messageContext)
             {
             String IP = (String) messageContext.getProperty("remoteaddr");
            
             System.out.println("Remote IP: " + IP);
            
             Iterator propertyNames = messageContext.getPropertyNames( );
             while ( propertyNames.hasNext( ) )
             {
             String keyName = (String) propertyNames.next( );
             System.out.println("Key: " + keyName + " Val: " + messageContext.getProperty( keyName ).toString() );
             }
            ....
            continues
            ....
            


            The first one gives me null, and listing the properties shows no "remoteaddr".

            2006-10-31 11:57:34,784 INFO [STDOUT] Remote IP: null
            2006-10-31 11:57:34,784 INFO [STDOUT] Key: javax.xml.ws.servlet.request Val:org.apache.catalina.connector.RequestFacade@12e2fba
            2006-10-31 11:57:34,784 INFO [STDOUT] Key: javax.xml.ws.servlet.context Val:org.jboss.ws.server.ServletEndpointContextImpl@11fc6b2
            2006-10-31 11:57:34,784 INFO [STDOUT] Key: javax.xml.ws.servlet.response Val:org.apache.catalina.connector.ResponseFacade@31c43f
            2006-10-31 11:57:34,784 INFO [STDOUT] Key: javax.xml.ws.servlet.session Val:org.apache.catalina.session.StandardSessionFacade@12dd1b8
            


            Is there anything more to configure or am I missing something else in my calls?

            Soft: Jboss AS 4.0.5, JBossWS 1.0.3 GA, Server Windows and HP-UX

            • 4. Re: getting ip address from webservice
              Thomas Diesler Master

              javax.xml.ws.servlet.request should get you at the information you want.