mardi 2 juin 2015

JAX-WS: SoapHandler not getting Invoked

so I'm trying to consume a WSSE secured (usernametoken) webservice, I created a SoapHandler but I'm not seeing it getting invoked (I actually stuffed the Handler with breakpoints and its not stopping there), aside of course from the fact that I'm getting an Soap Error (see below). Any idea where I'm messing up?

  • In CommonConstants I just pasted the whole WSSE header from SOAPUI
  • In IntegrationBean I'm (in theory) binding the handler to the wsi generated proxy and invoking the secured service
  • In WSSEHandler I'm doing all the black magic. Capturing the SOAP headers and appending the WSSE one. This is the place that's filled with breakpoints that are not getting hit.

SOAP Fault

 javax.xml.ws.soap.SOAPFaultException: No username available

CommonConstants.java

public static String WSSE_USENAME_TOKEN_HEADER = "<wsse:Security xmlns:wsse=\"http://ift.tt/LRW8Ij\" xmlns:wsu=\"http://ift.tt/Hm2joJ\">\n"
        + "         <wsse:UsernameToken>\n"
        + "            <wsse:Username>**USERNAME**</wsse:Username>\n"
        + "            <wsse:Password Type=\"http://ift.tt/1aTA7XU\">**PASSWORD**</wsse:Password>\n"
        + "         </wsse:UsernameToken>\n"
        + "</wsse:Security>";

IntegrationBean.java

public String testMethod() throws Exception {
    String result = "";
    CisChannelPort cisChannel = new ChannelService().getCisChannelPort();

    Binding binding = ((BindingProvider) cisChannel).getBinding();

    List<Handler> handlerList = binding.getHandlerChain();
    handlerList.add(new WSSEHandler());
    binding.setHandlerChain(handlerList);

    try {
        List<Channel> response = cisChannel.getallChannels(null).getChannels().getChannel();

        for (Channel c : response) {
            result += c.getNameChannel() + " -- ";
            LOG.info(PACKAGE + "Found Channel: " + c.getNameChannel());
        }

    } catch (Exception ex) {
        LOG.info(PACKAGE + "Error consumiendo el servicio");
        LOG.error(PACKAGE + ex.getMessage());
        throw new Exception("Error consumiendo el servicio");
    }

    return result;
}

WSSEHandler.java

public class WSSEHandler implements SOAPHandler<SOAPMessageContext> {

private static final String PACKAGE = "[co.com.tigo.test.integration.ejb.impl.WSSEHandler] ";

public WSSEHandler() {

}

@Override
public Set<QName> getHeaders() {
    return Collections.emptySet();
}

@Override
public boolean handleMessage(SOAPMessageContext context) {
    CommonConstants.LOG.info(PACKAGE + "Begin HandleMessage");
    Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (outboundProperty) {
        CommonConstants.LOG.info(PACKAGE + "Outbound Message Detected");
        try {
            addSecurityHeader(context);
        } catch (Exception ex) {
            CommonConstants.LOG.info(PACKAGE + "Error while setting WSSE Headers");
            CommonConstants.LOG.error(PACKAGE + ex.getClass().getCanonicalName() + " - " + ex.getMessage());
            return false;
        }

    }
    return true;
}

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

@Override
public void close(MessageContext context) {

}

private void addSecurityHeader(SOAPMessageContext messageContext) throws SOAPException, SAXException, IOException {
    LOG.info(PACKAGE + "Adding Security Header");
    SOAPHeader header = messageContext.getMessage().getSOAPPart().getEnvelope().getHeader();
    if (header == null) {
        header = messageContext.getMessage().getSOAPPart().getEnvelope().addHeader();
    }

    DOMParser parser = new DOMParser();
    parser.parse(new InputSource(new java.io.StringReader(CommonConstants.WSSE_USENAME_TOKEN_HEADER)));
    Node doc = (Node) parser.getDocument();
    header.appendChild(doc);

}

}

Aucun commentaire:

Enregistrer un commentaire