Search in sources :

Example 1 with W3CDOMStreamReader

use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.

the class SoapHeaderInterceptor method handleMessage.

public void handleMessage(Message m) throws Fault {
    SoapMessage message = (SoapMessage) m;
    SoapVersion soapVersion = message.getVersion();
    Exchange exchange = message.getExchange();
    MessageContentsList parameters = MessageContentsList.getContentsList(message);
    if (null == parameters) {
        parameters = new MessageContentsList();
    }
    BindingOperationInfo bop = exchange.getBindingOperationInfo();
    if (null == bop) {
        return;
    }
    if (bop.isUnwrapped()) {
        bop = bop.getWrappedOperation();
    }
    boolean client = isRequestor(message);
    BindingMessageInfo bmi = client ? bop.getOutput() : bop.getInput();
    if (bmi == null) {
        // one way operation.
        return;
    }
    List<SoapHeaderInfo> headers = bmi.getExtensors(SoapHeaderInfo.class);
    if (headers == null || headers.isEmpty()) {
        return;
    }
    boolean supportsNode = this.supportsDataReader(message, Node.class);
    Service service = ServiceModelUtil.getService(message.getExchange());
    Schema schema = null;
    final boolean schemaValidationEnabled = ServiceUtils.isSchemaValidationEnabled(SchemaValidationType.IN, message);
    if (schemaValidationEnabled) {
        schema = EndpointReferenceUtils.getSchema(service.getServiceInfos().get(0), message.getExchange().getBus());
    }
    for (SoapHeaderInfo header : headers) {
        MessagePartInfo mpi = header.getPart();
        try {
            if (schemaValidationEnabled && schema != null) {
                validateHeader(message, mpi, schema);
            }
        } catch (Fault f) {
            if (!isRequestor(message)) {
                f.setFaultCode(Fault.FAULT_CODE_CLIENT);
            }
            throw f;
        }
        if (mpi.getTypeClass() != null) {
            Header param = findHeader(message, mpi);
            Object object = null;
            if (param != null) {
                message.getHeaders().remove(param);
                if (param.getDataBinding() == null) {
                    Node source = (Node) param.getObject();
                    if (source instanceof Element) {
                        // need to remove these attributes as they
                        // would cause validation failures
                        Element el = (Element) source;
                        el.removeAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameMustUnderstand());
                        el.removeAttributeNS(soapVersion.getNamespace(), soapVersion.getAttrNameRole());
                    }
                    if (supportsNode) {
                        object = getNodeDataReader(message).read(mpi, source);
                    } else {
                        W3CDOMStreamReader reader = new W3CDOMStreamReader((Element) source);
                        try {
                            // advance into the first tag
                            reader.nextTag();
                        } catch (XMLStreamException e) {
                        // ignore
                        }
                        object = getDataReader(message, XMLStreamReader.class).read(mpi, reader);
                    }
                } else {
                    object = param.getObject();
                }
            }
            parameters.put(mpi, object);
        }
    }
    if (!parameters.isEmpty()) {
        message.setContent(List.class, parameters);
    }
}
Also used : BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) MessageContentsList(org.apache.cxf.message.MessageContentsList) SoapHeaderInfo(org.apache.cxf.binding.soap.model.SoapHeaderInfo) Schema(javax.xml.validation.Schema) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Service(org.apache.cxf.service.Service) Fault(org.apache.cxf.interceptor.Fault) MessagePartInfo(org.apache.cxf.service.model.MessagePartInfo) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SoapVersion(org.apache.cxf.binding.soap.SoapVersion) Exchange(org.apache.cxf.message.Exchange) BindingMessageInfo(org.apache.cxf.service.model.BindingMessageInfo) Header(org.apache.cxf.headers.Header) XMLStreamException(javax.xml.stream.XMLStreamException) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader)

Example 2 with W3CDOMStreamReader

use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.

the class MessageModeInInterceptor method doFromSoapMessage.

private void doFromSoapMessage(Message message, Object sm) {
    SOAPMessage m = (SOAPMessage) sm;
    MessageContentsList list = (MessageContentsList) message.getContent(List.class);
    if (list == null) {
        list = new MessageContentsList();
        message.setContent(List.class, list);
    }
    Object o = m;
    if (StreamSource.class.isAssignableFrom(type)) {
        try {
            try (CachedOutputStream out = new CachedOutputStream()) {
                XMLStreamWriter xsw = StaxUtils.createXMLStreamWriter(out);
                StaxUtils.copy(new DOMSource(m.getSOAPPart()), xsw);
                xsw.close();
                o = new StreamSource(out.getInputStream());
            }
        } catch (Exception e) {
            throw new Fault(e);
        }
    } else if (SAXSource.class.isAssignableFrom(type)) {
        o = new StaxSource(new W3CDOMStreamReader(m.getSOAPPart()));
    } else if (Source.class.isAssignableFrom(type)) {
        o = new DOMSource(m.getSOAPPart());
    }
    list.set(0, o);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) MessageContentsList(org.apache.cxf.message.MessageContentsList) StreamSource(javax.xml.transform.stream.StreamSource) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) SAXSource(javax.xml.transform.sax.SAXSource) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StaxSource(org.apache.cxf.staxutils.StaxSource) MessageContentsList(org.apache.cxf.message.MessageContentsList) List(java.util.List)

Example 3 with W3CDOMStreamReader

use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.

the class LogicalMessageImpl method setPayload.

public void setPayload(Source s) {
    Message message = msgContext.getWrappedMessage();
    Service.Mode mode = (Service.Mode) msgContext.getWrappedMessage().getContextualProperty(Service.Mode.class.getName());
    SOAPMessage m = message.getContent(SOAPMessage.class);
    if (m != null && !MessageUtils.isOutbound(message)) {
        try {
            SAAJUtils.getBody(m).removeContents();
            W3CDOMStreamWriter writer = new W3CDOMStreamWriter(SAAJUtils.getBody(m));
            StaxUtils.copy(s, writer);
            writer.flush();
            writer.close();
            if (mode == Service.Mode.MESSAGE) {
                s = new DOMSource(m.getSOAPPart());
            } else {
                s = new DOMSource(SAAJUtils.getBody(m).getFirstChild());
            }
            W3CDOMStreamReader r = new W3CDOMStreamReader(DOMUtils.getFirstElement(SAAJUtils.getBody(m)));
            message.setContent(XMLStreamReader.class, r);
        } catch (Exception e) {
            throw new Fault(e);
        }
    } else if (mode != null) {
        if (message instanceof SoapMessage) {
            if (mode == Service.Mode.MESSAGE) {
                try {
                    // REVISIT: should try to use the original SOAPMessage
                    // instead of creating a new empty one.
                    SOAPMessage msg = initSOAPMessage(null);
                    write(s, SAAJUtils.getBody(msg));
                    s = new DOMSource(msg.getSOAPPart());
                } catch (Exception e) {
                    throw new Fault(e);
                }
            }
        } else if (message instanceof XMLMessage && message.getContent(DataSource.class) != null) {
            throw new Fault(new org.apache.cxf.common.i18n.Message("GETPAYLOAD_OF_DATASOURCE_NOT_VALID_XMLHTTPBINDING", LOG));
        }
    } else {
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(s);
        msgContext.getWrappedMessage().setContent(XMLStreamReader.class, reader);
    }
    msgContext.getWrappedMessage().setContent(Source.class, s);
}
Also used : W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) XMLMessage(org.apache.cxf.message.XMLMessage) DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) LogicalMessage(javax.xml.ws.LogicalMessage) Message(org.apache.cxf.message.Message) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) SOAPMessage(javax.xml.soap.SOAPMessage) XMLMessage(org.apache.cxf.message.XMLMessage) Service(javax.xml.ws.Service) Fault(org.apache.cxf.interceptor.Fault) SOAPMessage(javax.xml.soap.SOAPMessage) SOAPException(javax.xml.soap.SOAPException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) WebServiceException(javax.xml.ws.WebServiceException) SoapMessage(org.apache.cxf.binding.soap.SoapMessage) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader)

Example 4 with W3CDOMStreamReader

use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.

the class XMLStreamDataReader method read.

public DOMSource read(XMLStreamReader reader) {
    // performance reasons
    try {
        XMLStreamReader reader2 = reader;
        if (reader2 instanceof DepthXMLStreamReader) {
            reader2 = ((DepthXMLStreamReader) reader2).getReader();
        }
        if (reader2 instanceof W3CDOMStreamReader) {
            W3CDOMStreamReader domreader = (W3CDOMStreamReader) reader2;
            DOMSource o = new DOMSource(domreader.getCurrentElement());
            domreader.consumeFrame();
            return o;
        }
        Document document = StaxUtils.read(reader);
        if (reader.hasNext()) {
            // need to actually consume the END_ELEMENT
            reader.next();
        }
        return new DOMSource(document);
    } catch (XMLStreamException e) {
        throw new Fault("COULD_NOT_READ_XML_STREAM_CAUSED_BY", LOG, e, e.getMessage());
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader) Fault(org.apache.cxf.interceptor.Fault) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) Document(org.w3c.dom.Document)

Example 5 with W3CDOMStreamReader

use of org.apache.cxf.staxutils.W3CDOMStreamReader in project cxf by apache.

the class AegisElementDataReader method read.

public Object read(Element input, AegisType desiredType) throws Exception {
    W3CDOMStreamReader sreader = new W3CDOMStreamReader(input);
    // advance into the first tag
    sreader.nextTag();
    return reader.read(sreader, desiredType);
}
Also used : W3CDOMStreamReader(org.apache.cxf.staxutils.W3CDOMStreamReader)

Aggregations

W3CDOMStreamReader (org.apache.cxf.staxutils.W3CDOMStreamReader)13 Element (org.w3c.dom.Element)6 XMLStreamException (javax.xml.stream.XMLStreamException)5 XMLStreamReader (javax.xml.stream.XMLStreamReader)5 Document (org.w3c.dom.Document)5 Fault (org.apache.cxf.interceptor.Fault)4 InputStreamReader (java.io.InputStreamReader)3 DOMSource (javax.xml.transform.dom.DOMSource)3 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)3 Message (org.apache.cxf.message.Message)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 JAXBException (javax.xml.bind.JAXBException)2 QName (javax.xml.namespace.QName)2 SOAPMessage (javax.xml.soap.SOAPMessage)2 MessageContentsList (org.apache.cxf.message.MessageContentsList)2 Service (org.apache.cxf.service.Service)2 BindingOperationInfo (org.apache.cxf.service.model.BindingOperationInfo)2 MessagePartInfo (org.apache.cxf.service.model.MessagePartInfo)2 Node (org.w3c.dom.Node)2