Search in sources :

Example 81 with Fault

use of org.apache.cxf.interceptor.Fault in project cxf by apache.

the class WrappedOutInterceptor method handleMessage.

public void handleMessage(Message message) {
    BindingOperationInfo bop = message.getExchange().getBindingOperationInfo();
    if (bop != null && bop.isUnwrapped()) {
        XMLStreamWriter xmlWriter = message.getContent(XMLStreamWriter.class);
        MessageInfo messageInfo;
        if (isRequestor(message)) {
            messageInfo = bop.getWrappedOperation().getOperationInfo().getInput();
        } else {
            messageInfo = bop.getWrappedOperation().getOperationInfo().getOutput();
        }
        QName name = messageInfo.getFirstMessagePart().getConcreteName();
        try {
            String pfx = null;
            Service service = message.getExchange().getService();
            if (service.getDataBinding().getDeclaredNamespaceMappings() != null) {
                pfx = service.getDataBinding().getDeclaredNamespaceMappings().get(name.getNamespaceURI());
            }
            if (pfx == null) {
                pfx = StaxUtils.getUniquePrefix(xmlWriter, name.getNamespaceURI(), false);
            }
            xmlWriter.setPrefix(pfx, name.getNamespaceURI());
            xmlWriter.writeStartElement(pfx, name.getLocalPart(), name.getNamespaceURI());
            if (StringUtils.isEmpty(pfx)) {
                xmlWriter.writeDefaultNamespace(name.getNamespaceURI());
            } else {
                xmlWriter.writeNamespace(pfx, name.getNamespaceURI());
            }
        } catch (XMLStreamException e) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("STAX_WRITE_EXC", BUNDLE), e);
        }
        // Add a final interceptor to write end element
        message.getInterceptorChain().add(ending);
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) XMLStreamException(javax.xml.stream.XMLStreamException) Message(org.apache.cxf.message.Message) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) QName(javax.xml.namespace.QName) Service(org.apache.cxf.service.Service) Fault(org.apache.cxf.interceptor.Fault) MessageInfo(org.apache.cxf.service.model.MessageInfo)

Example 82 with Fault

use of org.apache.cxf.interceptor.Fault in project cxf by apache.

the class CertConstraintsInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    final CertConstraints certConstraints = (CertConstraints) message.getContextualProperty(CertConstraints.class.getName());
    if (certConstraints == null) {
        return;
    }
    if (isRequestor(message)) {
        try {
            String scheme = (String) message.get("http.scheme");
            if ("https".equals(scheme)) {
                final MessageTrustDecider orig = message.get(MessageTrustDecider.class);
                MessageTrustDecider trust = new HttpsMessageTrustDecider(certConstraints, orig);
                message.put(MessageTrustDecider.class, trust);
            } else {
                throw new UntrustedURLConnectionIOException("TLS is not in use");
            }
        } catch (UntrustedURLConnectionIOException ex) {
            throw new Fault(ex);
        }
    } else {
        try {
            TLSSessionInfo tlsInfo = message.get(TLSSessionInfo.class);
            final Certificate[] certs = tlsInfo.getPeerCertificates();
            if (certs == null || certs.length == 0) {
                throw new UntrustedURLConnectionIOException("No client certificates were found");
            }
            X509Certificate[] x509Certs = (X509Certificate[]) certs;
            if (!certConstraints.matches(x509Certs[0])) {
                throw new UntrustedURLConnectionIOException("The client certificate does not match the defined cert constraints");
            }
        } catch (UntrustedURLConnectionIOException ex) {
            throw new Fault(ex);
        }
    }
}
Also used : UntrustedURLConnectionIOException(org.apache.cxf.transport.http.UntrustedURLConnectionIOException) Fault(org.apache.cxf.interceptor.Fault) MessageTrustDecider(org.apache.cxf.transport.http.MessageTrustDecider) TLSSessionInfo(org.apache.cxf.security.transport.TLSSessionInfo) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 83 with Fault

use of org.apache.cxf.interceptor.Fault in project cxf by apache.

the class AbstractRMInterceptorTest method testHandleMessageSequenceFaultNoBinding.

@Test
public void testHandleMessageSequenceFaultNoBinding() {
    RMInterceptor interceptor = new RMInterceptor();
    Message message = control.createMock(Message.class);
    SequenceFault sf = control.createMock(SequenceFault.class);
    interceptor.setSequenceFault(sf);
    Exchange ex = control.createMock(Exchange.class);
    EasyMock.expect(message.getExchange()).andReturn(ex).anyTimes();
    Endpoint e = control.createMock(Endpoint.class);
    EasyMock.expect(ex.getEndpoint()).andReturn(e);
    EasyMock.expect(e.getBinding()).andReturn(null);
    control.replay();
    try {
        interceptor.handleMessage(message);
        fail("Expected Fault not thrown.");
    } catch (Fault f) {
        assertSame(sf, f.getCause());
    }
}
Also used : Exchange(org.apache.cxf.message.Exchange) Message(org.apache.cxf.message.Message) Endpoint(org.apache.cxf.endpoint.Endpoint) Fault(org.apache.cxf.interceptor.Fault) Test(org.junit.Test)

Example 84 with Fault

use of org.apache.cxf.interceptor.Fault in project cxf by apache.

the class AbstractRMInterceptorTest method testHandleMessageSequenceFault.

@Test
public void testHandleMessageSequenceFault() {
    RMInterceptor interceptor = new RMInterceptor();
    Message message = control.createMock(Message.class);
    SequenceFault sf = control.createMock(SequenceFault.class);
    interceptor.setSequenceFault(sf);
    Exchange ex = control.createMock(Exchange.class);
    EasyMock.expect(message.getExchange()).andReturn(ex).anyTimes();
    Endpoint e = control.createMock(Endpoint.class);
    EasyMock.expect(ex.getEndpoint()).andReturn(e);
    Binding b = control.createMock(Binding.class);
    EasyMock.expect(e.getBinding()).andReturn(b);
    RMManager mgr = control.createMock(RMManager.class);
    interceptor.setManager(mgr);
    BindingFaultFactory bff = control.createMock(BindingFaultFactory.class);
    EasyMock.expect(mgr.getBindingFaultFactory(b)).andReturn(bff);
    Fault fault = control.createMock(Fault.class);
    EasyMock.expect(bff.createFault(sf, message)).andReturn(fault);
    EasyMock.expect(bff.toString(fault)).andReturn("f");
    control.replay();
    try {
        interceptor.handleMessage(message);
        fail("Expected Fault not thrown.");
    } catch (Fault f) {
        assertSame(f, fault);
    }
}
Also used : Exchange(org.apache.cxf.message.Exchange) Binding(org.apache.cxf.binding.Binding) Message(org.apache.cxf.message.Message) Endpoint(org.apache.cxf.endpoint.Endpoint) Fault(org.apache.cxf.interceptor.Fault) Test(org.junit.Test)

Example 85 with Fault

use of org.apache.cxf.interceptor.Fault in project cxf by apache.

the class BareInInterceptor method handleMessage.

public void handleMessage(Message message) {
    if (isGET(message) && message.getContent(List.class) != null) {
        LOG.fine("BareInInterceptor skipped in HTTP GET method");
        return;
    }
    DepthXMLStreamReader xmlReader = getXMLStreamReader(message);
    Exchange exchange = message.getExchange();
    DataReader<XMLStreamReader> dr = getDataReader(message);
    MessageContentsList parameters = new MessageContentsList();
    Endpoint ep = exchange.getEndpoint();
    BindingOperationInfo bop = exchange.getBindingOperationInfo();
    ServiceInfo si = ep.getEndpointInfo().getService();
    BindingMessageInfo msgInfo = null;
    boolean client = isRequestor(message);
    Collection<OperationInfo> ops = null;
    if (bop == null) {
        ops = new ArrayList<>();
        ops.addAll(si.getInterface().getOperations());
        if (xmlReader.getEventType() == XMLStreamConstants.END_ELEMENT && !client) {
            // TO DO : check duplicate operation with no input
            for (OperationInfo op : ops) {
                MessageInfo bmsg = op.getInput();
                if (bmsg.getMessagePartsNumber() == 0) {
                    BindingOperationInfo boi = ep.getEndpointInfo().getBinding().getOperation(op);
                    exchange.put(BindingOperationInfo.class, boi);
                    exchange.setOneWay(op.isOneWay());
                }
            }
        }
    } else {
        getMessageInfo(message, bop);
        if (client) {
            msgInfo = bop.getOutput();
        } else {
            msgInfo = bop.getInput();
        }
    }
    int paramNum = 0;
    while (StaxUtils.toNextElement(xmlReader)) {
        QName elName = xmlReader.getName();
        final MessagePartInfo p;
        if (msgInfo != null && msgInfo.getMessageParts() != null) {
            assert msgInfo.getMessageParts().size() > paramNum;
            p = msgInfo.getMessageParts().get(paramNum);
        } else {
            p = findMessagePart(exchange, ops, elName, client, paramNum, message);
        }
        if (p == null) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("NO_PART_FOUND", LOG, elName), Fault.FAULT_CODE_CLIENT);
        }
        final Object o;
        try {
            o = dr.read(p, xmlReader);
        } catch (Fault fault) {
            if (!isRequestor(message)) {
                fault.setFaultCode(Fault.FAULT_CODE_CLIENT);
            }
            throw fault;
        }
        if (o != null) {
            parameters.put(p, o);
        }
        paramNum++;
    }
    if (!parameters.isEmpty()) {
        message.setContent(List.class, parameters);
    }
}
Also used : OperationInfo(org.apache.cxf.service.model.OperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) XMLStreamReader(javax.xml.stream.XMLStreamReader) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) MessageContentsList(org.apache.cxf.message.MessageContentsList) QName(javax.xml.namespace.QName) Fault(org.apache.cxf.interceptor.Fault) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) Endpoint(org.apache.cxf.endpoint.Endpoint) MessageInfo(org.apache.cxf.service.model.MessageInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) Exchange(org.apache.cxf.message.Exchange) ServiceInfo(org.apache.cxf.service.model.ServiceInfo) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) Endpoint(org.apache.cxf.endpoint.Endpoint)

Aggregations

Fault (org.apache.cxf.interceptor.Fault)283 IOException (java.io.IOException)74 QName (javax.xml.namespace.QName)56 Message (org.apache.cxf.message.Message)52 XMLStreamException (javax.xml.stream.XMLStreamException)50 Element (org.w3c.dom.Element)42 Message (org.apache.cxf.common.i18n.Message)34 Exchange (org.apache.cxf.message.Exchange)30 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)30 SOAPException (javax.xml.soap.SOAPException)28 InputStream (java.io.InputStream)27 ArrayList (java.util.ArrayList)27 XMLStreamReader (javax.xml.stream.XMLStreamReader)26 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)26 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)25 Test (org.junit.Test)24 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)23 List (java.util.List)21 SOAPMessage (javax.xml.soap.SOAPMessage)21 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)21