Search in sources :

Example 1 with OutboundXMLSec

use of org.apache.xml.security.stax.ext.OutboundXMLSec in project cxf by apache.

the class XmlSecOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    if (message.getExchange().get(Throwable.class) != null) {
        return;
    }
    OutputStream os = message.getContent(OutputStream.class);
    String encoding = getEncoding(message);
    if (!(encryptRequest || signRequest)) {
        Exception ex = new Exception("Either encryption and/or signature must be enabled");
        throwFault(ex.getMessage(), ex);
    }
    XMLStreamWriter newXMLStreamWriter = null;
    try {
        XMLSecurityProperties properties = new XMLSecurityProperties();
        if (signRequest) {
            configureSignature(message, properties);
        }
        if (encryptRequest) {
            configureEncryption(message, properties);
        }
        OutboundXMLSec outboundXMLSec = XMLSec.getOutboundXMLSec(properties);
        newXMLStreamWriter = outboundXMLSec.processOutMessage(os, encoding);
        message.setContent(XMLStreamWriter.class, newXMLStreamWriter);
    } catch (XMLSecurityException e) {
        throwFault(e.getMessage(), e);
    } catch (Exception e) {
        throwFault(e.getMessage(), e);
    }
    message.put(AbstractOutDatabindingInterceptor.DISABLE_OUTPUTSTREAM_OPTIMIZATION, Boolean.TRUE);
    message.put(StaxOutInterceptor.FORCE_START_DOCUMENT, Boolean.TRUE);
    if (MessageUtils.getContextualBoolean(message, StaxOutInterceptor.FORCE_START_DOCUMENT, false)) {
        try {
            newXMLStreamWriter.writeStartDocument(encoding, "1.0");
        } catch (XMLStreamException e) {
            throw new Fault(e);
        }
        message.removeContent(OutputStream.class);
        message.put(OUTPUT_STREAM_HOLDER, os);
    }
    // Add a final interceptor to write end elements
    message.getInterceptorChain().add(ending);
}
Also used : OutboundXMLSec(org.apache.xml.security.stax.ext.OutboundXMLSec) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStream(java.io.OutputStream) XMLSecurityProperties(org.apache.xml.security.stax.ext.XMLSecurityProperties) Fault(org.apache.cxf.interceptor.Fault) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException) XMLSecurityException(org.apache.xml.security.exceptions.XMLSecurityException)

Aggregations

OutputStream (java.io.OutputStream)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 Fault (org.apache.cxf.interceptor.Fault)1 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)1 XMLSecurityException (org.apache.xml.security.exceptions.XMLSecurityException)1 OutboundXMLSec (org.apache.xml.security.stax.ext.OutboundXMLSec)1 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)1