javaweb-servicesservletsjax-rpcweblogic9.x

HttpServletRequest.getRemoteAddr() implementation on Weblogic 9.2.3 using JAX-RPC


Salut,

I am new in Weblogic WebService implementation. Trying to get client's remote address from SOAP message using JAX-RPC. Need to get client's remote IP from SOAP message header. Tried many solutions according BEA/Oracle documentation. WebService java file:

@WebService
public class clientsIPWs {
    @Control
    private SoapMessageControl smc;

    @WebMethod
    @WebResult(name="return")
    public String getClientAddress() {
        return smc.getRemoteAddress();
    }

}

SoapMessageControl.java

@ControlInterface
public interface SoapMessageControl {

    public String getRemoteAddress();

}

SoapMessageControlImpl.java

@ControlImplementation
public class SoapMessageControlImpl implements SoapMessageControl, Serializable {
    private static final long serialVersionUID = 1L;

    @Context
    private SOAPMessageContext smc;

    @WebMethod()
    public String getRemoteAddress() {
        HttpServletRequest request = 
            (HttpServletRequest) smc.getProperty("transport.http.servletRequest");
        return request.getRemoteAddr();
    }
}

In the last one tried many variations with @Context like MessageContext / WebServiceContext (this one is deprecated), but everytime in testing received such exception:

Failed to invoke end component Failed to invoke method java.lang.NullPointerException at controls.SoapMessageControlImpl.getRemoteAddress(SoapMessageControlImpl.java:33) at controls.SoapMessageControlBean.getRemoteAddress(SoapMessageControlBean.java:119) at services.CustProdWs.getClientAddress(CustProdWs.java:63) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at weblogic.wsee.component.pojo.JavaClassComponent.invoke(JavaClassComponent.java:99) at weblogic.wsee.ws.dispatch.server.ComponentHandler.handleRequest(ComponentHandler.java:64) at weblogic.wsee.handler.HandlerIterator.handleRequest(HandlerIterator.java:127) at weblogic.wsee.ws.dispatch.server.ServerDispatcher.dispatch(ServerDispatcher.java:85) at weblogic.wsee.ws.WsSkel.invoke(WsSkel.java:80) at weblogic.wsee.server.servlet.SoapProcessor.handlePost(SoapProcessor.java:66) at weblogic.wsee.server.servlet.SoapProcessor.process(SoapProcessor.java:44) at weblogic.wsee.server.servlet.BaseWSServlet$AuthorizedInvoke.run(BaseWSServlet.java:181) at weblogic.wsee.server.servlet.BaseWSServlet.service(BaseWSServlet.java:92) at javax.servlet.http.HttpServlet.service(HttpServlet.java:856) at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227) at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125) at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283) at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175) at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3244) at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121) at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2010) at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1916) at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1366) at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209) at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)

In google almost every solution uses JAX-WS that is not available for me in this framework. Thanks.


Solution

  • This solution worked for me:

    Getting the HttpServletRequest/Response Object

    If your Web Service uses HTTP as its transport protocol, you can use the weblogic.wsee.connection.transport.servlet.HttpTransportUtils API to get the javax.servlet.http.HttpServletRequest and javax.servlet.http.HttpServletResponse objects from the JAX-RPC ServletEndpointContext object, as shown in the following example (relevant code shown in bold and explained after the example):

     package examples.webservices.http_transport_utils;
     import javax.xml.rpc.server.ServiceLifecycle;
    import javax.xml.rpc.server.ServletEndpointContext;
    import javax.xml.rpc.ServiceException;
     import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     import javax.jws.WebMethod;
    import javax.jws.WebService;
     import weblogic.jws.WLHttpTransport;
     import weblogic.wsee.connection.transport.servlet.HttpTransportUtils;
     @WebService(name="HttpTransportUtilsPortType",
                serviceName="HttpTransportUtilsService",
                targetNamespace="http://example.org")
     @WLHttpTransport(contextPath="servlet", serviceUri="HttpTransportUtils",
                     portName="HttpTransportUtilsPort")
     public class HttpTransportUtilsImpl implements ServiceLifecycle {
       private ServletEndpointContext wsctx = null;
       public void init(Object context) throws ServiceException {
        System.out.println("ServletEndpointContext inited...");
        wsctx = (ServletEndpointContext)context;
      }
       public void destroy() {
        System.out.println("ServletEndpointContext destroyed...");
        wsctx = null;
      }
       @WebMethod()
      public String getServletRequestAndResponse() {
         HttpServletRequest request =
           HttpTransportUtils.getHttpServletRequest(wsctx.getMessageContext());
        HttpServletResponse response =
           HttpTransportUtils.getHttpServletResponse(wsctx.getMessageContext());
         System.out.println("HttpTransportUtils API used successfully.");
        return "HttpTransportUtils API used successfully";
       }
     }