Search in sources :

Example 76 with SOAPMessage

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

the class LoggingHandler method logToSystemOut.

/*
     * Check the MESSAGE_OUTBOUND_PROPERTY in the context
     * to see if this is an outgoing or incoming message.
     * Write a brief message to the print stream and
     * output the message. The writeTo() method can throw
     * SOAPException or IOException
     */
protected void logToSystemOut(SOAPMessageContext smc) {
    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (outboundProperty.booleanValue()) {
        out.println("\nOutbound message:");
    } else {
        out.println("\nInbound message:");
    }
    SOAPMessage message = smc.getMessage();
    try {
        message.writeTo(out);
        out.println();
    } catch (Exception e) {
        out.println("Exception in handler: " + e);
    }
}
Also used : SOAPMessage(javax.xml.soap.SOAPMessage)

Example 77 with SOAPMessage

use of javax.xml.soap.SOAPMessage 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)

Example 78 with SOAPMessage

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

the class AbstractPolicySecurityTest method runOutInterceptorAndValidate.

protected Document runOutInterceptorAndValidate(SoapMessage msg, Policy policy, AssertionInfoMap aim, List<QName> assertedOutAssertions, List<QName> notAssertedOutAssertions) throws Exception {
    this.getOutInterceptor().handleMessage(msg);
    try {
        aim.checkEffectivePolicy(policy);
    } catch (PolicyException e) {
    // Expected but not relevant
    } finally {
        if (assertedOutAssertions != null) {
            for (QName assertionType : assertedOutAssertions) {
                Collection<AssertionInfo> ais = aim.get(assertionType);
                assertNotNull(ais);
                for (AssertionInfo ai : ais) {
                    checkAssertion(aim, assertionType, ai, true);
                }
            }
        }
        if (notAssertedOutAssertions != null) {
            for (QName assertionType : notAssertedOutAssertions) {
                Collection<AssertionInfo> ais = aim.get(assertionType);
                assertNotNull(ais);
                for (AssertionInfo ai : ais) {
                    checkAssertion(aim, assertionType, ai, false);
                }
            }
        }
    }
    return msg.getContent(SOAPMessage.class).getSOAPPart();
}
Also used : AssertionInfo(org.apache.cxf.ws.policy.AssertionInfo) PolicyException(org.apache.cxf.ws.policy.PolicyException) QName(javax.xml.namespace.QName) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 79 with SOAPMessage

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

the class WSS4JFaultCodeTest method testNoSecurity.

/**
 * Test for WSS4JInInterceptor when it receives a message with no security header.
 */
@Test
public void testNoSecurity() throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");
    SoapMessage msg = getSoapMessageForDom(doc);
    SOAPMessage saajMsg = msg.getContent(SOAPMessage.class);
    doc = saajMsg.getSOAPPart();
    byte[] docbytes = getMessageBytes(doc);
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(docbytes));
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());
    doc = StaxUtils.read(db, reader, false);
    WSS4JInInterceptor inHandler = new WSS4JInInterceptor();
    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);
    inHandler.setProperty(ConfigurationConstants.ACTION, ConfigurationConstants.ENCRYPT);
    inHandler.setProperty(ConfigurationConstants.DEC_PROP_FILE, "insecurity.properties");
    inHandler.setProperty(ConfigurationConstants.PW_CALLBACK_CLASS, TestPwdCallback.class.getName());
    inmsg.put(SecurityConstants.RETURN_SECURITY_ERROR, Boolean.TRUE);
    try {
        inHandler.handleMessage(inmsg);
        fail("Expected failure on an message with no security header");
    } catch (SoapFault fault) {
        assertTrue(fault.getReason().startsWith("An error was discovered processing the <wsse:Security> header"));
        QName faultCode = new QName(WSS4JConstants.WSSE_NS, "InvalidSecurity");
        assertTrue(fault.getFaultCode().equals(faultCode));
    }
}
Also used : SoapFault(org.apache.cxf.binding.soap.SoapFault) XMLStreamReader(javax.xml.stream.XMLStreamReader) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) QName(javax.xml.namespace.QName) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) NullResolver(org.apache.cxf.helpers.DOMUtils.NullResolver) Exchange(org.apache.cxf.message.Exchange) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Example 80 with SOAPMessage

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

the class WSS4JInOutTest method testCustomProcessor.

@Test
public void testCustomProcessor() throws Exception {
    Document doc = readDocument("wsse-request-clean.xml");
    WSS4JOutInterceptor ohandler = new WSS4JOutInterceptor();
    PhaseInterceptor<SoapMessage> handler = ohandler.createEndingInterceptor();
    SoapMessage msg = getSoapMessageForDom(doc);
    msg.put(ConfigurationConstants.ACTION, ConfigurationConstants.SIGNATURE);
    msg.put(ConfigurationConstants.SIG_PROP_FILE, "outsecurity.properties");
    msg.put(ConfigurationConstants.USER, "myalias");
    msg.put("password", "myAliasPassword");
    handler.handleMessage(msg);
    SOAPMessage saajMsg = msg.getContent(SOAPMessage.class);
    doc = saajMsg.getSOAPPart();
    assertValid("//wsse:Security", doc);
    assertValid("//wsse:Security/ds:Signature", doc);
    byte[] docbytes = getMessageBytes(doc);
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(new ByteArrayInputStream(docbytes));
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());
    doc = StaxUtils.read(db, reader, false);
    final Map<String, Object> properties = new HashMap<>();
    properties.put(WSS4JInInterceptor.PROCESSOR_MAP, createCustomProcessorMap());
    WSS4JInInterceptor inHandler = new WSS4JInInterceptor(properties);
    SoapMessage inmsg = new SoapMessage(new MessageImpl());
    Exchange ex = new ExchangeImpl();
    ex.setInMessage(inmsg);
    inmsg.setContent(SOAPMessage.class, saajMsg);
    inHandler.setProperty(ConfigurationConstants.ACTION, WSHandlerConstants.NO_SECURITY);
    inHandler.handleMessage(inmsg);
    List<WSHandlerResult> results = getResults(inmsg);
    assertTrue(results != null && results.size() == 1);
    List<WSSecurityEngineResult> signatureResults = results.get(0).getActionResults().get(WSConstants.SIGN);
    assertTrue(signatureResults == null || signatureResults.isEmpty());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) WSHandlerResult(org.apache.wss4j.dom.handler.WSHandlerResult) WSSecurityEngineResult(org.apache.wss4j.dom.engine.WSSecurityEngineResult) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) NullResolver(org.apache.cxf.helpers.DOMUtils.NullResolver) Exchange(org.apache.cxf.message.Exchange) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) Test(org.junit.Test)

Aggregations

SOAPMessage (javax.xml.soap.SOAPMessage)219 SOAPException (javax.xml.soap.SOAPException)87 SOAPBody (javax.xml.soap.SOAPBody)47 Test (org.junit.Test)46 InputStream (java.io.InputStream)45 QName (javax.xml.namespace.QName)45 Element (org.w3c.dom.Element)44 IOException (java.io.IOException)40 MessageFactory (javax.xml.soap.MessageFactory)40 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)30 SOAPElement (javax.xml.soap.SOAPElement)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26 SAML2Exception (com.sun.identity.saml2.common.SAML2Exception)25 XMLStreamReader (javax.xml.stream.XMLStreamReader)25 Node (org.w3c.dom.Node)24 Document (org.w3c.dom.Document)22 URL (java.net.URL)21 SOAPPart (javax.xml.soap.SOAPPart)21 Exchange (org.apache.cxf.message.Exchange)19 MessageImpl (org.apache.cxf.message.MessageImpl)19