简体   繁体   English

SOAPHandler JAX-WS WebService中的MessageContext

[英]MessageContext in SOAPHandler JAX-WS WebService

I have a JAX-WS 2.2 WebService and I must take the IP Address of each client that communicate with it. 我有一个JAX-WS 2.2 WebService,我必须获取与之通信的每个客户端的IP地址。 I write a SOAP protocol handler but I can't see the addresses because the handlers doesn't contain this information and using the mimeheaders I can't also see this information. 我写了一个SOAP协议处理程序,但我看不到地址,因为处理程序不包含这些信息,并且使用mimeheaders我也看不到这些信息。 The code of my handler is the follow: 我的处理程序的代码如下:

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

Now I'm seeing that would be possible to see the addresses using the following code: 现在我看到可以使用以下代码查看地址:

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

But I can't import correctly the HttpServletRequest class. 但我无法正确导入HttpServletRequest类。 Do you have any ideas? 你有什么想法?

UPDATE UPDATE

Thanks to A Nyar Thar, I've seen that exists another method to take address and I've implemented this in my code, that now is: 感谢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);     

}

But the code execution create this error (where row 39 is where I do exchange.getRemoteAddress()): 但代码执行会产生此错误(其中第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)

I think that the real problem is that I don't know how take WebServiceContext from my class AddressHandler. 我认为真正的问题是我不知道如何从我的类AddressHandler中获取WebServiceContext。 Do you have ideas? 你有想法吗?

For JAX-WS based webservice, you can access remote host info from javax.xml.ws.spi.http.HttpExchange , that can be accessed based on JAX-WS version, 对于基于JAX-WS的Web服务,您可以从javax.xml.ws.spi.http.HttpExchange访问远程主机信息,可以基于JAX-WS版本访问,

  • JAX-WS 2.1 JAX-WS 2.1

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

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

Note that wsContext.getMessageContext() will return MessageContext . 请注意, wsContext.getMessageContext()将返回MessageContext If you want it, don't cast to SOAPMessageContext , like that, 如果你想要它,不要像那样SOAPMessageContextSOAPMessageContext

MessageContext msgContext = wsContext.getMessageContext();

Finally you can access remote address info, 最后,您可以访问远程地址信息,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM