Friday, October 31, 2008

Get Remote IP Address In Axis SOAP Server

Obtain remote IP from ServletRequest (Axis 1.0 only)

import javax.servlet.http.HttpServletRequest;

import org.apache.axis.MessageContext;

import org.apache.axis.transport.http.HTTPConstants;

public static String getSOAPRemoteIP() {

MessageContext context = MessageContext.getCurrentContext();

String address = null;

if(context!=null &&

context.containsProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST))

{

HttpServletRequest servletReq = (HttpServletRequest)

context.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);

if (servletReq != null)

address = servletReq.getRemoteAddr();

}

return address;

}


3 comments:

Anonymous said...

Hi, it this works for axis2 to ??
because i tried but it does not work, if you can have a way to help please..

Hakan ISIK said...

Thanks for the comment , i did not try it with axis2.0. if anybody else try in axis2.0 , that will be appreciated.

Anonymous said...

works like a charm in axis2.

MessageContext context = MessageContext.getCurrentMessageContext();
HttpServletRequest servletReq = (HttpServletRequest) context.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);

System.out.println(BomenApp.now("yyyy.MM.dd '-' hh:mm:ss ") + "Request from client " + servletReq.getRemoteAddr() );