HttpServletRequest.getRemoteAddr() 在 Weblogic 9.2.3 上使用 JAX-RPC 实现

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

敬礼,

我是 Weblogic WebService 实现的新手。尝试使用 JAX-RPC 从 SOAP 消息中获取客户端的远程地址。需要从 SOAP 消息头中获取客户端的远程 IP。根据 BEA/Oracle 文档尝试了许多解决方案。 Web 服务 java 文件:

@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();
    }
}

在最后一个中尝试了很多使用@Context 的变体,比如 MessageContext / WebServiceContext(这个已被弃用),但每次在测试中都会收到这样的异常:

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)

在 google 中,几乎每个解决方案都使用 JAX-WS,但我无法在此框架中使用它。 谢谢。

这个解决方案对我有用:

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";
   }
 }