RSS Feed for This PostCurrent Article

Axis: Adding Client IP to SOAP Message Header

Here is the piece of code that I used to add the client IP address to my incoming SOAP messages.

import org.apache.axis.AxisFault;
import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.axis.transport.http.HTTPConstants;
import org.apache.axis.handlers.BasicHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.http.HttpServletRequest;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.Name;
public class ClientIPHandler extends BasicHandler {
    protected final Log log = LogFactory.getLog(ClientIPHandler.class);
    public ClientIPHandler() {"Creating client ip handler");
    public void invoke(MessageContext msgContext) throws AxisFault {
        Message requestMessage = msgContext.getRequestMessage();
        //Message responseMessage = new Message(requestMessage.getSOAPEnvelope());
        HttpServletRequest req = (HttpServletRequest) msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
        try {
            String clientIP = req.getRemoteAddr();
  "Client IP is : " + clientIP);
            SOAPEnvelope envelope = requestMessage.getSOAPEnvelope();
            SOAPHeaderElement header = requestMessage.getSOAPHeader().addHeaderElement(envelope.createName("AuthenticationInfo"));
            SOAPElement node = header.addChildElement("Client-IP");
            //responseMessage.getSOAPHeader().addChildElement("Client-IP", clientIP);
        } catch (SOAPException e) {

The above is a Axis message handler.

If your Web Service is session aware, you can retrieve the client IP from the message context.

String remoteAddress = (String)msgCtx.getProperty("REMOTE_ADDR"):



Accessing Client IP and Transport Session Related Data from MessageContext

Trackback URL

Sorry, comments for this entry are closed at this time.