Search in sources :

Example 46 with SOAPException

use of javax.xml.soap.SOAPException 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 47 with SOAPException

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

the class HandlerTestImpl method createSOAPFaultException.

private SOAPFaultException createSOAPFaultException(String faultString) {
    try {
        SOAPFault fault = SOAPFactory.newInstance().createFault();
        fault.setFaultString(faultString);
        SAAJUtils.setFaultCode(fault, new QName("http://cxf.apache.org/faultcode", "Server"));
        return new SOAPFaultException(fault);
    } catch (SOAPException e) {
    // do nothing
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) SOAPException(javax.xml.soap.SOAPException) SOAPFault(javax.xml.soap.SOAPFault) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException)

Example 48 with SOAPException

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

the class DocLitBareCodeFirstServiceImpl method greetMe.

public GreetMeResponse greetMe(GreetMeRequest gmr) {
    if ("fault".equals(gmr.getName())) {
        try {
            SOAPFactory factory = SOAPFactory.newInstance();
            SOAPFault fault = factory.createFault("this is a fault string!", new QName("http://foo", "FooCode"));
            fault.setFaultActor("mr.actor");
            fault.addDetail().addChildElement("test").addTextNode("TestText");
            throw new SOAPFaultException(fault);
        } catch (SOAPException ex) {
            throw new WebServiceException(ex);
        }
    } else if ("emptyfault".equals(gmr.getName())) {
        throw new RuntimeException("Empty!");
    }
    GreetMeResponse resp = new GreetMeResponse();
    resp.setName(gmr.getName());
    return resp;
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) QName(javax.xml.namespace.QName) SOAPException(javax.xml.soap.SOAPException) SOAPFault(javax.xml.soap.SOAPFault) SOAPFaultException(javax.xml.ws.soap.SOAPFaultException) SOAPFactory(javax.xml.soap.SOAPFactory)

Example 49 with SOAPException

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

the class AbstractModifyRequestInterceptor method handleMessage.

public void handleMessage(SoapMessage mc) throws Fault {
    SOAPMessage saaj = mc.getContent(SOAPMessage.class);
    try {
        Iterator<?> secHeadersIterator = SAAJUtils.getHeader(saaj).getChildElements(SEC_HEADER);
        if (secHeadersIterator.hasNext()) {
            SOAPHeaderElement securityHeader = (SOAPHeaderElement) secHeadersIterator.next();
            modifySecurityHeader(securityHeader);
        }
        modifySOAPBody(SAAJUtils.getBody(saaj));
    } catch (SOAPException ex) {
        throw new Fault(ex);
    }
}
Also used : SOAPHeaderElement(javax.xml.soap.SOAPHeaderElement) SOAPException(javax.xml.soap.SOAPException) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage)

Example 50 with SOAPException

use of javax.xml.soap.SOAPException in project Payara by payara.

the class BaseAuthConfig method getName.

private static Name getName(SOAPMessage message) {
    Name rvalue = null;
    SOAPPart soap = message.getSOAPPart();
    if (soap != null) {
        try {
            SOAPEnvelope envelope = soap.getEnvelope();
            if (envelope != null) {
                SOAPBody body = envelope.getBody();
                if (body != null) {
                    Iterator it = body.getChildElements();
                    while (it.hasNext()) {
                        Object o = it.next();
                        if (o instanceof SOAPElement) {
                            rvalue = ((SOAPElement) o).getElementName();
                            break;
                        }
                    }
                }
            }
        } catch (SOAPException se) {
            if (logger.isLoggable(Level.FINE)) {
                logger.log(Level.FINE, "WSS: Unable to get SOAP envelope", se);
            }
        }
    }
    return rvalue;
}
Also used : SOAPBody(javax.xml.soap.SOAPBody) SOAPException(javax.xml.soap.SOAPException) SOAPPart(javax.xml.soap.SOAPPart) Iterator(java.util.Iterator) SOAPElement(javax.xml.soap.SOAPElement) SOAPEnvelope(javax.xml.soap.SOAPEnvelope) Name(javax.xml.soap.Name) QName(javax.xml.namespace.QName)

Aggregations

SOAPException (javax.xml.soap.SOAPException)225 SOAPMessage (javax.xml.soap.SOAPMessage)135 SOAPElement (javax.xml.soap.SOAPElement)71 SOAPBody (javax.xml.soap.SOAPBody)62 WebServiceException (javax.xml.ws.WebServiceException)57 IOException (java.io.IOException)53 Element (org.w3c.dom.Element)36 QName (javax.xml.namespace.QName)33 SOAPBodyElement (javax.xml.soap.SOAPBodyElement)33 SOAPFaultException (javax.xml.ws.soap.SOAPFaultException)28 SOAPFault (javax.xml.soap.SOAPFault)27 SOAPEnvelope (javax.xml.soap.SOAPEnvelope)25 SAML2Exception (com.sun.identity.saml2.common.SAML2Exception)24 Node (org.w3c.dom.Node)23 SOAPFactory (javax.xml.soap.SOAPFactory)22 SOAPMessageContext (javax.xml.ws.handler.soap.SOAPMessageContext)20 SOAPHeader (javax.xml.soap.SOAPHeader)19 SOAPPart (javax.xml.soap.SOAPPart)19 Iterator (java.util.Iterator)17 Fault (org.apache.cxf.interceptor.Fault)16