Search in sources :

Example 6 with SOAPEnvelope

use of javax.xml.soap.SOAPEnvelope in project ddf by codice.

the class SoapRequestDecoder method decodeRelayState.

public String decodeRelayState(String samlRequest) {
    String relayState = null;
    try {
        SOAPPart soapMessage = SamlProtocol.parseSoapMessage(samlRequest);
        SOAPEnvelope envelope = soapMessage.getEnvelope();
        SOAPHeader header = envelope.getHeader();
        Iterator iterator = header.examineAllHeaderElements();
        while (iterator.hasNext()) {
            SOAPHeaderElement soapHeaderElement = (SOAPHeaderElement) iterator.next();
            if ("RelayState".equals(soapHeaderElement.getLocalName())) {
                relayState = soapHeaderElement.getValue();
                break;
            }
        }
    } catch (XMLStreamException e) {
        throw new IllegalArgumentException("Unable to convert parse SOAP request.");
    } catch (SOAPException e) {
        throw new IllegalArgumentException("Unable to get SOAP envelope.");
    }
    return relayState;
}
Also used : SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) XMLStreamException(javax.xml.stream.XMLStreamException) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) Iterator(java.util.Iterator) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 7 with SOAPEnvelope

use of javax.xml.soap.SOAPEnvelope in project nhin-d by DirectProject.

the class RepositorySOAPHandler method handleMessage.

/**
     * This method handles the incoming and outgoing SOAP-Message. It's an
     * excellent point to manipulate the SOAP.
     * 
     * @param SOAPMessageContext
     *            The SOAPMessageContext object.
     * 
     * @return true if successful handling, false otherwise.
     */
@Override
public boolean handleMessage(SOAPMessageContext context) {
    //Inquire incoming or outgoing message.
    boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    try {
        if (outbound) {
            getHeaderData();
            SOAPMessage msg = ((SOAPMessageContext) context).getMessage();
            // dumpSOAPMessage(msg);
            SOAPPart sp = msg.getSOAPPart();
            // edit Envelope
            SOAPEnvelope env = sp.getEnvelope();
            SOAPHeader sh = env.addHeader();
            @SuppressWarnings("unused") SOAPBody sb = env.getBody();
            if (action != null) {
                QName qname = new QName("http://www.w3.org/2005/08/addressing", "Action");
                SOAPHeaderElement saction = sh.addHeaderElement(qname);
                boolean must = true;
                saction.setMustUnderstand(must);
                saction.setValue(action);
            }
            if (relatesTo != null) {
                QName qname = new QName("http://www.w3.org/2005/08/addressing", "RelatesTo");
                SOAPHeaderElement relates = sh.addHeaderElement(qname);
                relates.setValue(relatesTo);
            }
            if (from != null) {
                QName qname = new QName("http://www.w3.org/2005/08/addressing", "From");
                QName child = new QName("http://www.w3.org/2005/08/addressing", "Address");
                SOAPHeaderElement efrom = sh.addHeaderElement(qname);
                SOAPElement address = efrom.addChildElement(child);
                address.setValue(from);
            }
            if (messageId != null) {
                QName qname = new QName("http://www.w3.org/2005/08/addressing", "MessageID");
                SOAPHeaderElement message = sh.addHeaderElement(qname);
                message.setValue(messageId);
            }
            if (to != null) {
                QName qname = new QName("http://www.w3.org/2005/08/addressing", "To");
                SOAPHeaderElement sto = sh.addHeaderElement(qname);
                sto.setValue(to);
            }
        } else {
        //should not be any inbound
        }
    } catch (Exception e) {
        LOGGER.error("Error handling SOAP message", e);
        return false;
    }
    return true;
}
Also used : SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) QName(javax.xml.namespace.QName) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) IOException(java.io.IOException) SOAPBody(javax.xml.soap.SOAPBody) SOAPMessageContext(javax.xml.ws.handler.soap.SOAPMessageContext) SOAPPart(javax.xml.soap.SOAPPart) SOAPElement(javax.xml.soap.SOAPElement) SOAPHeader(javax.xml.soap.SOAPHeader)

Example 8 with SOAPEnvelope

use of javax.xml.soap.SOAPEnvelope in project cxf by apache.

the class SAAJInInterceptor method handleMessage.

@SuppressWarnings("unchecked")
public void handleMessage(SoapMessage message) throws Fault {
    if (isGET(message)) {
        return;
    }
    Boolean bodySet = (Boolean) message.get(BODY_FILLED_IN);
    if (Boolean.TRUE.equals(bodySet)) {
        return;
    }
    message.put(BODY_FILLED_IN, Boolean.TRUE);
    try {
        SOAPMessage soapMessage = message.getContent(SOAPMessage.class);
        if (soapMessage == null) {
            MessageFactory factory = preInterceptor.getFactory(message);
            soapMessage = factory.createMessage();
            message.setContent(SOAPMessage.class, soapMessage);
        }
        XMLStreamReader xmlReader = message.getContent(XMLStreamReader.class);
        if (xmlReader == null) {
            return;
        }
        final SOAPPart part = soapMessage.getSOAPPart();
        Document node = (Document) message.getContent(Node.class);
        if (node != part && node != null) {
            StaxUtils.copy(node, new SAAJStreamWriter(part));
        } else {
            SOAPEnvelope env = soapMessage.getSOAPPart().getEnvelope();
            if (node == null) {
                adjustPrefixes(env, (String) message.get(ReadHeadersInterceptor.ENVELOPE_PREFIX), (String) message.get(ReadHeadersInterceptor.BODY_PREFIX));
            }
            List<XMLEvent> events = (List<XMLEvent>) message.get(ReadHeadersInterceptor.ENVELOPE_EVENTS);
            applyEvents(events, env);
            SOAPBody body = soapMessage.getSOAPBody();
            events = (List<XMLEvent>) message.get(ReadHeadersInterceptor.BODY_EVENTS);
            applyEvents(events, body);
        }
        message.setContent(Node.class, soapMessage.getSOAPPart());
        Collection<Attachment> atts = message.getAttachments();
        if (atts != null) {
            for (Attachment a : atts) {
                if (a.getDataHandler().getDataSource() instanceof AttachmentDataSource) {
                    try {
                        ((AttachmentDataSource) a.getDataHandler().getDataSource()).cache(message);
                    } catch (IOException e) {
                        throw new Fault(e);
                    }
                }
                AttachmentPart ap = soapMessage.createAttachmentPart(a.getDataHandler());
                Iterator<String> i = a.getHeaderNames();
                while (i != null && i.hasNext()) {
                    String h = i.next();
                    String val = a.getHeader(h);
                    ap.addMimeHeader(h, val);
                }
                if (StringUtils.isEmpty(ap.getContentId())) {
                    ap.setContentId(a.getId());
                }
                soapMessage.addAttachmentPart(ap);
            }
        }
        // replace header element if necessary
        if (message.hasHeaders()) {
            replaceHeaders(soapMessage, message);
        }
        if (soapMessage.getSOAPPart().getEnvelope().getHeader() == null) {
            soapMessage.getSOAPPart().getEnvelope().addHeader();
        }
        // If we have an xmlReader that already is counting the attributes and such
        // then we don't want to rely on the system level defaults in StaxUtils.copy
        // CXF-6173
        boolean secureReader = StaxUtils.isSecureReader(xmlReader, message);
        StaxUtils.copy(xmlReader, new SAAJStreamWriter(soapMessage.getSOAPPart(), soapMessage.getSOAPPart().getEnvelope().getBody()), true, !secureReader);
        DOMSource bodySource = new DOMSource(soapMessage.getSOAPPart().getEnvelope().getBody());
        xmlReader = StaxUtils.createXMLStreamReader(bodySource);
        xmlReader.nextTag();
        // move past body tag
        xmlReader.nextTag();
        message.setContent(XMLStreamReader.class, xmlReader);
    } catch (SOAPException soape) {
        throw new SoapFault(new org.apache.cxf.common.i18n.Message("SOAPHANDLERINTERCEPTOR_EXCEPTION", BUNDLE), soape, message.getVersion().getSender());
    } catch (XMLStreamException e) {
        throw new SoapFault(new org.apache.cxf.common.i18n.Message("SOAPHANDLERINTERCEPTOR_EXCEPTION", BUNDLE), e, message.getVersion().getSender());
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) SoapFault(org.apache.cxf.binding.soap.SoapFault) XMLStreamReader(javax.xml.stream.XMLStreamReader) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) Message(org.apache.cxf.message.Message) SOAPMessage(javax.xml.soap.SOAPMessage) Node(org.w3c.dom.Node) AttachmentDataSource(org.apache.cxf.attachment.AttachmentDataSource) Attachment(org.apache.cxf.message.Attachment) SoapFault(org.apache.cxf.binding.soap.SoapFault) Fault(org.apache.cxf.interceptor.Fault) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) List(java.util.List) ArrayList(java.util.ArrayList) MessageFactory(javax.xml.soap.MessageFactory) AttachmentPart(javax.xml.soap.AttachmentPart) IOException(java.io.IOException) SOAPBody(javax.xml.soap.SOAPBody) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 9 with SOAPEnvelope

use of javax.xml.soap.SOAPEnvelope in project cxf by apache.

the class StaxSerializer method deserialize.

private Node deserialize(Node ctx, XMLStreamReader reader, boolean wrapped) throws XMLEncryptionException {
    Document contextDocument = null;
    if (Node.DOCUMENT_NODE == ctx.getNodeType()) {
        contextDocument = (Document) ctx;
    } else {
        contextDocument = ctx.getOwnerDocument();
    }
    XMLStreamWriter writer = null;
    try {
        if (ctx instanceof SOAPElement) {
            SOAPElement el = (SOAPElement) ctx;
            while (el != null && !(el instanceof SOAPEnvelope)) {
                el = el.getParentElement();
            }
            // cannot load into fragment due to a ClassCastException within SAAJ addChildElement
            // which only checks for Document as parent, not DocumentFragment
            Element element = ctx.getOwnerDocument().createElementNS("dummy", "dummy");
            writer = new SAAJStreamWriter((SOAPEnvelope) el, element);
            return appendNewChild(reader, wrapped, contextDocument, writer, element);
        }
        if (DOMUtils.isJava9SAAJ()) {
            // cannot load into fragment due to a ClassCastException within SAAJ addChildElement
            // which only checks for Document as parent, not DocumentFragment
            Element element = ctx.getOwnerDocument().createElementNS("dummy", "dummy");
            writer = new OverlayW3CDOMStreamWriter(ctx.getOwnerDocument(), element);
            return appendNewChild(reader, wrapped, contextDocument, writer, element);
        }
        // Import to a dummy fragment
        DocumentFragment dummyFragment = contextDocument.createDocumentFragment();
        writer = StaxUtils.createXMLStreamWriter(new DOMResult(dummyFragment));
        StaxUtils.copy(reader, writer);
        if (wrapped) {
            DocumentFragment result = contextDocument.createDocumentFragment();
            Node child = dummyFragment.getFirstChild().getFirstChild();
            if (child != null && child.getNextSibling() == null) {
                return child;
            }
            while (child != null) {
                Node nextChild = child.getNextSibling();
                result.appendChild(child);
                child = nextChild;
            }
            dummyFragment = result;
        }
        return dummyFragment;
    } catch (XMLStreamException ex) {
        throw new XMLEncryptionException(ex);
    }
}
Also used : OverlayW3CDOMStreamWriter(org.apache.cxf.staxutils.OverlayW3CDOMStreamWriter) DOMResult(javax.xml.transform.dom.DOMResult) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SOAPElement(javax.xml.soap.SOAPElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) SOAPElement(javax.xml.soap.SOAPElement) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Document(org.w3c.dom.Document) DocumentFragment(org.w3c.dom.DocumentFragment) SAAJStreamWriter(org.apache.cxf.binding.soap.saaj.SAAJStreamWriter) XMLEncryptionException(org.apache.xml.security.encryption.XMLEncryptionException)

Example 10 with SOAPEnvelope

use of javax.xml.soap.SOAPEnvelope in project cxf by apache.

the class CryptoCoverageChecker method handleMessage.

/**
 * Checks that the WSS4J results refer to the required signed/encrypted
 * elements as defined by the XPath expressions in {@link #xPaths}.
 *
 * @param message
 *            the SOAP message containing the signature
 *
 * @throws SoapFault
 *             if there is an error evaluating an XPath or an element is not
 *             covered by the required cryptographic operation
 */
public void handleMessage(SoapMessage message) throws Fault {
    if (this.xPaths == null || this.xPaths.isEmpty()) {
    // return
    }
    if (message.getContent(SOAPMessage.class) == null) {
        throw new SoapFault("Error obtaining SOAP document", Fault.FAULT_CODE_CLIENT);
    }
    Element documentElement = null;
    try {
        SOAPMessage saajDoc = message.getContent(SOAPMessage.class);
        SOAPEnvelope envelope = saajDoc.getSOAPPart().getEnvelope();
        if (!checkFaults && envelope.getBody().hasFault()) {
            return;
        }
        documentElement = envelope;
        documentElement = (Element) DOMUtils.getDomElement(documentElement);
    } catch (SOAPException e) {
        throw new SoapFault("Error obtaining SOAP document", Fault.FAULT_CODE_CLIENT);
    }
    final Collection<WSDataRef> signed = new HashSet<>();
    final Collection<WSDataRef> encrypted = new HashSet<>();
    List<WSHandlerResult> results = CastUtils.cast((List<?>) message.get(WSHandlerConstants.RECV_RESULTS));
    // Get all encrypted and signed references
    if (results != null) {
        for (WSHandlerResult wshr : results) {
            List<WSSecurityEngineResult> signedResults = wshr.getActionResults().get(WSConstants.SIGN);
            if (signedResults != null) {
                for (WSSecurityEngineResult signedResult : signedResults) {
                    List<WSDataRef> sl = CastUtils.cast((List<?>) signedResult.get(WSSecurityEngineResult.TAG_DATA_REF_URIS));
                    if (sl != null) {
                        if (sl.size() == 1 && sl.get(0).getName().equals(new QName(WSS4JConstants.SIG_NS, WSS4JConstants.SIG_LN))) {
                            // endorsing the signature so don't include
                            continue;
                        }
                        signed.addAll(sl);
                    }
                }
            }
            List<WSSecurityEngineResult> encryptedResults = wshr.getActionResults().get(WSConstants.ENCR);
            if (encryptedResults != null) {
                for (WSSecurityEngineResult encryptedResult : encryptedResults) {
                    List<WSDataRef> el = CastUtils.cast((List<?>) encryptedResult.get(WSSecurityEngineResult.TAG_DATA_REF_URIS));
                    if (el != null) {
                        encrypted.addAll(el);
                    }
                }
            }
        }
    }
    CryptoCoverageUtil.reconcileEncryptedSignedRefs(signed, encrypted);
    // XPathFactory and XPath are not thread-safe so we must recreate them
    // each request.
    final XPathFactory factory = XPathFactory.newInstance();
    final XPath xpath = factory.newXPath();
    if (this.prefixMap != null) {
        xpath.setNamespaceContext(new MapNamespaceContext(this.prefixMap));
    }
    for (XPathExpression xPathExpression : this.xPaths) {
        Collection<WSDataRef> refsToCheck = null;
        switch(xPathExpression.getType()) {
            case SIGNED:
                refsToCheck = signed;
                break;
            case ENCRYPTED:
                refsToCheck = encrypted;
                break;
            default:
                throw new IllegalStateException("Unexpected crypto type: " + xPathExpression.getType());
        }
        try {
            CryptoCoverageUtil.checkCoverage(documentElement, refsToCheck, xpath, Arrays.asList(xPathExpression.getXPath()), xPathExpression.getType(), xPathExpression.getScope());
        } catch (WSSecurityException e) {
            throw new SoapFault("No " + xPathExpression.getType() + " element found matching XPath " + xPathExpression.getXPath(), Fault.FAULT_CODE_CLIENT);
        }
    }
}
Also used : XPath(javax.xml.xpath.XPath) SoapFault(org.apache.cxf.binding.soap.SoapFault) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) MapNamespaceContext(org.apache.cxf.helpers.MapNamespaceContext) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) WSDataRef(org.apache.wss4j.dom.WSDataRef) SOAPMessage(javax.xml.soap.SOAPMessage) WSHandlerResult(org.apache.wss4j.dom.handler.WSHandlerResult) WSSecurityEngineResult(org.apache.wss4j.dom.engine.WSSecurityEngineResult) XPathFactory(javax.xml.xpath.XPathFactory) SOAPException(javax.xml.soap.SOAPException) HashSet(java.util.HashSet)

Aggregations

SOAPEnvelope (javax.xml.soap.SOAPEnvelope)59 SOAPMessage (javax.xml.soap.SOAPMessage)34 SOAPException (javax.xml.soap.SOAPException)31 SOAPBody (javax.xml.soap.SOAPBody)24 SOAPPart (javax.xml.soap.SOAPPart)24 SOAPElement (javax.xml.soap.SOAPElement)22 SOAPHeader (javax.xml.soap.SOAPHeader)16 Name (javax.xml.soap.Name)13 SOAPHeaderElement (javax.xml.soap.SOAPHeaderElement)13 Iterator (java.util.Iterator)12 QName (javax.xml.namespace.QName)12 WebServiceException (javax.xml.ws.WebServiceException)10 ProtocolException (javax.xml.ws.ProtocolException)7 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)6 CoordinationContextType (org.oasis_open.docs.ws_tx.wscoor._2006._06.CoordinationContextType)6 Detail (javax.xml.soap.Detail)5 MessageFactory (javax.xml.soap.MessageFactory)5 SOAPConnection (javax.xml.soap.SOAPConnection)5 SOAPFault (javax.xml.soap.SOAPFault)5 Test (org.junit.Test)5