IBM Support

How to detect the IP address of the client of a JAX-RPC WebService

Question & Answer


Question

This technote explains how you can obtain the IP address of the managed client that invokes the Web Service, after generating a Java Bean Skeleton Web Service from a WSDL file using IBM® WebSphere® Application Server 6.1 and JAX-RPC

Answer

DISCLAIMER
All source code and/or binaries attached to this document are referred to here as "the Program". IBM is not providing program services of any kind for the Program. IBM is providing the Program on an "AS IS" basis without warranty of any kind. IBM WILL NOT BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF IBM, OR ITS RESELLER, HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

There are two possibilities to obtain the IP address of the client:


    A. Add a servlet filter that gets access to the

    javax.servlet.ServletRequest object.

    B. Implement javax.xml.rpc.server.ServiceLifecycle so that you can get access to: javax.xml.rpc.server.ServletEndpointContext


A: Add a Servlet Filter

1. Open the web.xml file of the Dynamic Web project where you have already generated the Java Bean Skeleton Web Service.

2. Select the Filters tab

3. Click on the Add button in the Filters section




5. Define the name of the class to be generated:



6. Let it implement: javax.servlet.Filter as the dialog suggests:

7. In the Servlet Mapping section of the Filters tab of the web.xml file, add the Filter Mapping to Servlet by selecting the router servlet that was generated by the Web Services wizard and choose as Dispatcher type: REQUEST:


8. Implement the filter as follows:


package filters;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;

public class MyFilter implements Filter {

public MyFilter() {
super();
}

public void init(FilterConfig arg0) throws ServletException {}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                  throws IOException, ServletException {
System.out.println("Remote Address: "+request.getRemoteAddr());
chain.doFilter(request, response);
}

public void destroy() {}

}
Note that the line: chain.doFilter(request, response); is required so that the Web Services is actually invoked after the servlet filter is processed.

When a Web client invokes the web service, the IP address of the client will be written to the server console.

B. Implement javax.xml.rpc.server.ServiceLifecycle

1. Have the web service implementation class implement the interface:

javax.xml.rpc.server.ServiceLifecycle


2. Add a field defined as follows:


private javax.xml.rpc.server.ServletEndpointContext ctx;


3. Implement the init method to store the ServletEndpointContext :


public void init(Object arg0) throws ServiceException {
ctx=(javax.xml.rpc.server.ServletEndpointContext)arg0;
}


4. Implement a method like the following:


public java.lang.String getRemoteAddr() throws java.rmi.RemoteException {
javax.servlet.http.HttpServletRequest request = (javax.servlet.http.HttpServletRequest)ctx.getMessageContext().getProperty(
com.ibm.ws.webservices.engine.transport.http.HTTPConstants.MC_HTTP_SERVLETREQUEST);
return = request.getRemoteAddr();
}


[{"Product":{"code":"SSRTLW","label":"Rational Application Developer for WebSphere Software"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Web Services Development","Platform":[{"code":"PF033","label":"Windows"},{"code":"PF016","label":"Linux"}],"Version":"7.0;7.0.0.1;7.0.0.2;7.0.0.3;7.0.0.4;7.0.0.5;7.0.0.6;7.0.0.7","Edition":"","Line of Business":{"code":"LOB45","label":"Automation"}},{"Product":{"code":"SSYK2S","label":"Rational Software Architect Designer"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Web Services Development","Platform":[{"code":"PF016","label":"Linux"},{"code":"PF033","label":"Windows"}],"Version":"7.0;7.0.0.1;7.0.0.2;7.0.0.3;7.0.0.4;7.0.0.5;7.0.0.6;7.0.0.7","Edition":"","Line of Business":{"code":"LOB45","label":"Automation"}}]

Document Information

Modified date:
10 September 2020

UID

swg21304368