SOAPHandler JAX-WS WebService中的MessageContext

时间:2014-07-30 09:37:18

标签: java web-services soap jax-ws

我有一个JAX-WS 2.2 WebService,我必须获取与之通信的每个客户端的IP地址。我写了一个SOAP协议处理程序,但我看不到地址,因为处理程序不包含这些信息,并且使用mimeheaders我也看不到这些信息。我的处理程序的代码如下:

public class AddressHandler implements SOAPHandler<SOAPMessageContext> {

private void takeIPAddress(SOAPMessageContext context) {

    try {
        SOAPMessage original = context.getMessage();
        MimeHeaders mimeheaders = original.getMimeHeaders();
        MimeHeader mimeheader = null;

        Iterator<?> iter = mimeheaders.getAllHeaders();

        for (; iter.hasNext();) {
            mimeheader = (MimeHeader) iter.next();

            System.out.println("name=" + mimeheader.getName() + ", value="
                    + mimeheader.getValue());
        }


     } catch (Exception e) {
         e.printStackTrace();
     }


}

@Override
public void close(MessageContext arg0) {
    // TODO Auto-generated method stub

}

@Override
public boolean handleFault(SOAPMessageContext arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean handleMessage(SOAPMessageContext context) {
    takeIPAddress(context);
    return true;
}

@Override
public Set<QName> getHeaders() {
    // TODO Auto-generated method stub
    return null;
}
}

现在我看到可以使用以下代码查看地址:

SOAPMessageContext jaxwsContext = (SOAPMessageContext)wsContext.getMessageContext();
HttpServletRequest request = HttpServletRequest)jaxwsContext.get(SOAPMessageContext.SERVLET_REQUEST);
String ipAddress = request.getRemoteAddr();

但我无法正确导入HttpServletRequest类。你有什么想法吗?

更新

感谢A Nyar Thar,我已经看到存在另一种获取地址的方法,我已经在我的代码中实现了这个,现在是:

private void takeIPAddress(SOAPMessageContext context) {

    HttpExchange exchange = (HttpExchange)context.get("com.sun.xml.ws.http.exchange");
    InetSocketAddress remoteAddress = exchange.getRemoteAddress();
    String remoteHost = remoteAddress.getHostName();

    System.out.println(remoteHost);     

}

但代码执行会产生此错误(其中第39行是我执行exchange.getRemoteAddress()的地方):

java.lang.NullPointerException
at server.AddressHandler.takeIPAddress(AddressHandler.java:39)
at server.AddressHandler.handleMessage(AddressHandler.java:80)
at server.AddressHandler.handleMessage(AddressHandler.java:1)
at com.sun.xml.internal.ws.handler.HandlerProcessor.callHandleMessage(HandlerProcessor.java:282)
at com.sun.xml.internal.ws.handler.HandlerProcessor.callHandlersRequest(HandlerProcessor.java:125)
at com.sun.xml.internal.ws.handler.ServerSOAPHandlerTube.callHandlersOnRequest(ServerSOAPHandlerTube.java:123)
at com.sun.xml.internal.ws.handler.HandlerTube.processRequest(HandlerTube.java:105)
at com.sun.xml.internal.ws.api.pipe.Fiber.__doRun(Fiber.java:626)
at com.sun.xml.internal.ws.api.pipe.Fiber._doRun(Fiber.java:585)
at com.sun.xml.internal.ws.api.pipe.Fiber.doRun(Fiber.java:570)
at com.sun.xml.internal.ws.api.pipe.Fiber.runSync(Fiber.java:467)
at com.sun.xml.internal.ws.server.WSEndpointImpl$2.process(WSEndpointImpl.java:299)
at com.sun.xml.internal.ws.transport.http.HttpAdapter$HttpToolkit.handle(HttpAdapter.java:593)
at com.sun.xml.internal.ws.transport.http.HttpAdapter.handle(HttpAdapter.java:244)
at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handleExchange(WSHttpHandler.java:95)
at com.sun.xml.internal.ws.transport.http.server.WSHttpHandler.handle(WSHttpHandler.java:80)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:80)
at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:677)
at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:77)
at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:649)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)

我认为真正的问题是我不知道如何从我的类AddressHandler中获取WebServiceContext。你有想法吗?

1 个答案:

答案 0 :(得分:4)

对于基于JAX-WS的Web服务,您可以从javax.xml.ws.spi.http.HttpExchange访问远程主机信息,可以基于JAX-WS版本访问

  • JAX-WS 2.1

    SOAPMessageContext soapContext = (SOAPMessageContext)wsContext.getMessageContext();
    HttpExchange exchange = (HttpExchange)soapContext.get(JAXWSProperties.HTTP_EXCHANGE);
    
  • JAX-WS 2.2

    SOAPMessageContext soapContext = (SOAPMessageContext)wsContext.getMessageContext();
    HttpExchange exchange = (HttpExchange)soapContext.get("com.sun.xml.ws.http.exchange");
    

请注意,wsContext.getMessageContext()将返回MessageContext。如果你想要它,不要像{1}那样投射到SOAPMessageContext

MessageContext msgContext = wsContext.getMessageContext();

最后,您可以访问远程地址信息

InetSocketAddress remoteAddress = exchange.getRemoteAddress();
String remoteHost = remoteAddress.getHostName();