package at.gv.util.client.pvp.rprofile; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Set; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.namespace.QName; import javax.xml.parsers.ParserConfigurationException; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPFactory; import javax.xml.soap.SOAPHeader; import javax.xml.soap.SOAPMessage; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; import javax.xml.ws.handler.soap.SOAPMessageContext; import at.gv.util.DOMUtils; import at.gv.util.MiscUtil; import at.gv.util.xsd.szr.pvp19.ObjectFactory; import at.gv.util.xsd.szr.pvp19.PvpTokenType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Element; import org.xml.sax.SAXException; public class Pvp19pSoapHandler implements SOAPHandler { private static final String AUTH_NS = "http://schemas.xmlsoap.org/ws/2002/04/secext"; private static final String AUTH_PREFIX="wss"; private PvpTokenType pvpTokenType = null; private final Logger log = LoggerFactory.getLogger(Pvp19pSoapHandler.class); @Override public void close(MessageContext arg0) { } @Override public boolean handleFault(SOAPMessageContext arg0) { return true; } public void configure(PvpTokenType pvpToken) { MiscUtil.assertNotNull(pvpToken, "pvpToken"); this.pvpTokenType = pvpToken; } @Override public boolean handleMessage(SOAPMessageContext smc) { log.trace("Initializing SZR SOAP message handler."); final boolean isOutMessage = ((Boolean) smc.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue(); log.trace("Outbound message: " + isOutMessage); if (pvpTokenType == null) { throw new NullPointerException("Please configure first the PVP token."); } if (!isOutMessage) { return true; } try { final SOAPMessage message = smc.getMessage(); final SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); final SOAPFactory soapFactory = SOAPFactory.newInstance(); // Creating WS-Security header element final SOAPElement wsSecHeaderElm = soapFactory.createElement( "Security", AUTH_PREFIX, AUTH_NS); // serialize pvp token final JAXBContext ctx = JAXBContext.newInstance(PvpTokenType.class); final ObjectFactory of = new ObjectFactory(); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); ctx.createMarshaller().marshal(of.createPvpToken(pvpTokenType), bos); final Element pvpTokenElement = DOMUtils.parseXmlNonValidating(new ByteArrayInputStream(bos.toByteArray())); final SOAPElement pvpToken = soapFactory.createElement(pvpTokenElement); // adding elements wsSecHeaderElm.addChildElement(pvpToken); SOAPHeader header = envelope.getHeader(); if (header == null) header = envelope.addHeader(); header.addChildElement(wsSecHeaderElm); return true; } catch(final SOAPException e) { log.warn("Cannot add WS-Security header.", e); return false; } catch (final JAXBException e) { log.warn("Cannot add WS-Security header.", e); return false; } catch (final ParserConfigurationException e) { log.warn("Cannot add WS-Security header.", e); return false; } catch (final SAXException e) { log.warn("Cannot add WS-Security header.", e); return false; } catch (final IOException e) { log.warn("Cannot add WS-Security header.", e); return false; } } @Override public Set getHeaders() { return null; } }