Search in sources :

Example 86 with Fault

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

the class DocLiteralInInterceptor method validatePart.

private void validatePart(MessagePartInfo p, QName elName, Message m) {
    if (p == null) {
        throw new Fault(new org.apache.cxf.common.i18n.Message("NO_PART_FOUND", LOG, elName), Fault.FAULT_CODE_CLIENT);
    }
    boolean synth = false;
    if (p.getMessageInfo() != null && p.getMessageInfo().getOperation() != null) {
        OperationInfo op = p.getMessageInfo().getOperation();
        Boolean b = (Boolean) op.getProperty("operation.is.synthetic");
        if (b != null) {
            synth = b;
        }
    }
    if (MessageUtils.getContextualBoolean(m, "soap.no.validate.parts", false)) {
        // something like a Provider service or similar that is forcing a
        // doc/lit/bare on an endpoint that may not really be doc/lit/bare.
        // we need to just let these through per spec so the endpoint
        // can process it
        synth = true;
    }
    if (synth) {
        return;
    }
    if (p.isElement()) {
        if (p.getConcreteName() != null && !elName.equals(p.getConcreteName()) && !synth) {
            throw new Fault("UNEXPECTED_ELEMENT", LOG, null, elName, p.getConcreteName());
        }
    } else {
        if (!(elName.equals(p.getName()) || elName.equals(p.getConcreteName())) && !synth) {
            throw new Fault("UNEXPECTED_ELEMENT", LOG, null, elName, p.getConcreteName());
        }
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) Fault(org.apache.cxf.interceptor.Fault)

Example 87 with Fault

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

the class DocLiteralInInterceptor method handleMessage.

public void handleMessage(Message message) {
    if (isGET(message) && message.getContent(List.class) != null) {
        LOG.fine("DocLiteralInInterceptor skipped in HTTP GET method");
        return;
    }
    DepthXMLStreamReader xmlReader = getXMLStreamReader(message);
    MessageContentsList parameters = new MessageContentsList();
    Exchange exchange = message.getExchange();
    BindingOperationInfo bop = exchange.getBindingOperationInfo();
    boolean client = isRequestor(message);
    // operation anymore, just return
    if (bop != null && !StaxUtils.toNextElement(xmlReader)) {
        // body may be empty for partial response to decoupled request
        return;
    }
    Service service = ServiceModelUtil.getService(message.getExchange());
    bop = getBindingOperationInfo(xmlReader, exchange, bop, client);
    boolean forceDocLitBare = false;
    if (bop != null && bop.getBinding() != null) {
        forceDocLitBare = Boolean.TRUE.equals(bop.getBinding().getService().getProperty("soap.force.doclit.bare"));
    }
    DataReader<XMLStreamReader> dr = getDataReader(message);
    try {
        if (!forceDocLitBare && bop != null && bop.isUnwrappedCapable()) {
            ServiceInfo si = bop.getBinding().getService();
            // Wrapped case
            MessageInfo msgInfo = setMessage(message, bop, client, si);
            setDataReaderValidation(service, message, dr);
            // Determine if we should keep the parameters wrapper
            if (shouldWrapParameters(msgInfo, message)) {
                QName startQName = xmlReader.getName();
                MessagePartInfo mpi = msgInfo.getFirstMessagePart();
                if (!mpi.getConcreteName().equals(startQName)) {
                    throw new Fault("UNEXPECTED_WRAPPER_ELEMENT", LOG, null, startQName, mpi.getConcreteName());
                }
                Object wrappedObject = dr.read(mpi, xmlReader);
                parameters.put(mpi, wrappedObject);
            } else {
                // Unwrap each part individually if we don't have a wrapper
                bop = bop.getUnwrappedOperation();
                msgInfo = setMessage(message, bop, client, si);
                List<MessagePartInfo> messageParts = msgInfo.getMessageParts();
                Iterator<MessagePartInfo> itr = messageParts.iterator();
                // stuck
                if (xmlReader.getEventType() == XMLStreamConstants.START_ELEMENT) {
                    StaxUtils.nextEvent(xmlReader);
                }
                // loop through each child element
                getPara(xmlReader, dr, parameters, itr, message);
            }
        } else {
            // Bare style
            BindingMessageInfo msgInfo = null;
            Endpoint ep = exchange.getEndpoint();
            ServiceInfo si = ep.getEndpointInfo().getService();
            if (bop != null) {
                // for xml binding or client side
                if (client) {
                    msgInfo = bop.getOutput();
                } else {
                    msgInfo = bop.getInput();
                    if (bop.getOutput() == null) {
                        exchange.setOneWay(true);
                    }
                }
                if (msgInfo == null) {
                    return;
                }
                setMessage(message, bop, client, si, msgInfo.getMessageInfo());
            }
            final Collection<OperationInfo> operations = new ArrayList<>(si.getInterface().getOperations());
            if (xmlReader == null || !StaxUtils.toNextElement(xmlReader)) {
                // empty input
                getBindingOperationForEmptyBody(operations, ep, exchange);
                return;
            }
            setDataReaderValidation(service, message, dr);
            int paramNum = 0;
            do {
                QName elName = xmlReader.getName();
                MessagePartInfo p;
                if (!client && msgInfo != null && msgInfo.getMessageParts() != null && msgInfo.getMessageParts().isEmpty()) {
                    // no input messagePartInfo
                    return;
                }
                if (msgInfo != null && msgInfo.getMessageParts() != null && msgInfo.getMessageParts().size() > 0) {
                    if (msgInfo.getMessageParts().size() > paramNum) {
                        p = msgInfo.getMessageParts().get(paramNum);
                    } else {
                        p = null;
                    }
                } else {
                    p = findMessagePart(exchange, operations, elName, client, paramNum, message);
                }
                if (!forceDocLitBare) {
                    // Make sure the elName found on the wire is actually OK for
                    // the purpose we need it
                    validatePart(p, elName, message);
                }
                final Object o = dr.read(p, xmlReader);
                if (forceDocLitBare && parameters.isEmpty()) {
                    // webservice provider does not need to ensure size
                    parameters.add(o);
                } else {
                    parameters.put(p, o);
                }
                paramNum++;
                if (message.getContent(XMLStreamReader.class) == null || o == xmlReader) {
                    xmlReader = null;
                }
            } while (xmlReader != null && StaxUtils.toNextElement(xmlReader));
        }
        message.setContent(List.class, parameters);
    } catch (Fault f) {
        if (!isRequestor(message)) {
            f.setFaultCode(Fault.FAULT_CODE_CLIENT);
        }
        throw f;
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) OperationInfo(org.apache.cxf.service.model.OperationInfo) 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) ArrayList(java.util.ArrayList) Service(org.apache.cxf.service.Service) 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)

Example 88 with Fault

use of org.apache.cxf.interceptor.Fault 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 89 with Fault

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

the class InMessageRecorder method handleMessage.

public void handleMessage(Message message) throws Fault {
    try (InputStream is = message.getContent(InputStream.class)) {
        if (is != null) {
            byte[] b = new byte[is.available()];
            is.read(b);
            inbound.add(b);
            if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("inbound: " + new String(b));
            }
            message.setContent(InputStream.class, new ByteArrayInputStream(b));
        }
    } catch (Exception ex) {
        throw new Fault(ex);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Fault(org.apache.cxf.interceptor.Fault)

Example 90 with Fault

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

the class JavascriptGetInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    String method = (String) message.get(Message.HTTP_REQUEST_METHOD);
    String query = (String) message.get(Message.QUERY_STRING);
    if (!"GET".equals(method) || StringUtils.isEmpty(query)) {
        return;
    }
    String baseUri = (String) message.get(Message.REQUEST_URL);
    final URI uri;
    try {
        uri = URI.create(baseUri);
    } catch (IllegalArgumentException iae) {
        // invalid URI, ignore and continue
        return;
    }
    Map<String, String> map = UrlUtils.parseQueryString(query);
    if (isRecognizedQuery(map, uri, message.getExchange().getEndpoint().getEndpointInfo())) {
        try {
            Conduit c = message.getExchange().getDestination().getBackChannel(message);
            Message mout = new MessageImpl();
            mout.setExchange(message.getExchange());
            message.getExchange().setOutMessage(mout);
            mout.put(Message.CONTENT_TYPE, "application/javascript;charset=UTF-8");
            c.prepare(mout);
            OutputStream os = mout.getContent(OutputStream.class);
            writeResponse(uri, map, os, message.getExchange().getEndpoint());
        } catch (IOException ioe) {
            throw new Fault(ioe);
        }
    }
}
Also used : Message(org.apache.cxf.message.Message) Conduit(org.apache.cxf.transport.Conduit) OutputStream(java.io.OutputStream) Fault(org.apache.cxf.interceptor.Fault) IOException(java.io.IOException) URI(java.net.URI) MessageImpl(org.apache.cxf.message.MessageImpl)

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