SoapHandler not called after WS operation is executed
Asked Answered
B

1

2

I have a servlet that also consumes SOAP WS (in doGet). I want to see SOAP envelope (or anything else in soap message). I adddd this code to my servlet class:

class ClientHandlerResolver implements HandlerResolver {
    public List<Handler> getHandlerChain(PortInfo port_info) {
        List<Handler> hchain = new ArrayList<Handler>();
        hchain.add(new TestHandler()); //  soap handler defined in TestHandler.java dumps messages to stdout
        return hchain;
    }
}

I a separate class, i have my TestHandler:

public class TestHandler implements SOAPHandler<SOAPMessageContext> {
private static final String LoggerName = "ClientSideLogger";
private Logger logger;
private final boolean log_p = true; // set to false to turn off 

public TestHandler() { 
logger = Logger.getLogger(LoggerName);
 }

public boolean handleMessage(SOAPMessageContext ctx) {
if (log_p) 
    logger.info("Test::handleMessage"); 

// Is this an outbound message, i.e., a request?
Boolean request_p = (Boolean)
      ctx.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

// Manipulate the SOAP only if it's a request
  if (request_p) {
    try {
        SOAPMessage msg = ctx.getMessage(); 
        msg.writeTo(System.out);

    }
    catch(SOAPException e) 
    { System.err.println(e); 
    }
    catch(IOException e) 
    { System.err.println(e); 
    }
}
  return true; // continue down the chain

}

public boolean handleFault(SOAPMessageContext ctx) {
  return true; 

}

public Set<QName> getHeaders() { 
if (log_p)
    logger.info("getHeaders");
return null;

}

public void close(MessageContext messageContext) { 
if (log_p)
    logger.info("close");
}

}

Nothing is printed out when i make WS request. I don't even know if handler is called.

How to see printouts defined in Handler?

Bud answered 16/2, 2011 at 16:22 Comment(5)
You haven't shown how you tie your handler resolver into service invocation. Do you use something like javax.xml.ws.Service.setHandlerResolver?Stomatal
Hm, i don't have anything like that. Where should i put it, as a parameter to my webservice call?Bud
What does the code look like for invoking the service? Do you use the Service class?Stomatal
I just create MyService_ServiceLocator and MyService_Port objects and then call my operations on MyService_Port object like p.getApples(); All my code for WS client was generated from WSDL in Eclipse.Bud
Aha, i found out that i should call setHandlerResolver() on my service object. Problem is there is no such method in my generated Eclipse generated classes.Bud
S
2

I'm not sure what Eclipse is generating for you, but here's one way to do it. Assume MyService is the interface generated from the WSDL:

URL wsdlURL = new URL("...");
QName serviceName = new QName("...", "...");
Service service = Service.create(wsdlURL, serviceName);
HandlerResolver handlerResolver = new ClientHandlerResolver();
service.setHandlerResolver(handlerResolver);
MyService myService = service.getPort(MyService.class);
// invoke methods (operations) on myService
Stomatal answered 17/2, 2011 at 18:10 Comment(1)
Eclipse generated code for Axis1 didn't allow me to setHandlerResolver. Then i used wsiimport tool from CLI (generates JAX-WS RI 2.1.6 in JDK 6 code) and now i am able to use handlers.Bud

© 2022 - 2024 — McMap. All rights reserved.