Search in sources :

Example 1 with XMLStreamReaderToXMLStreamWriter

use of org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter in project metro-jax-ws by eclipse-ee4j.

the class EPRSDDocumentFilter method filter.

public XMLStreamWriter filter(SDDocument doc, XMLStreamWriter w) throws XMLStreamException, IOException {
    if (!doc.isWSDL()) {
        return w;
    }
    return new XMLStreamWriterFilter(w) {

        // when true, all writer events are filtered out
        private boolean eprExtnFilterON = false;

        private boolean portHasEPR = false;

        // -1 -> outside wsa:epr, 0 -> on wsa:epr start/end , > 0 inside wsa:epr
        private int eprDepth = -1;

        // non null when inside wsdl:service scope
        private String serviceName = null;

        // flag to get service name when on wsdl:service element start
        private boolean onService = false;

        // -1 -> outside wsdl:service, 0 -> on wsdl:service start/end , > 0 inside wsdl:service
        private int serviceDepth = -1;

        // non null when inside wsdl:port scope
        private String portName = null;

        // flag to get port name when on wsdl:port element start
        private boolean onPort = false;

        // -1 -> outside wsdl:port, 0 -> on wsdl:port start/end , > 0 inside wsdl:port
        private int portDepth = -1;

        // when a complete epr is written, endpoint address is used as epr address
        private String portAddress;

        // flag to get endpoint address when on soap:address element start
        private boolean onPortAddress = false;

        private void handleStartElement(String localName, String namespaceURI) throws XMLStreamException {
            resetOnElementFlags();
            if (serviceDepth >= 0) {
                serviceDepth++;
            }
            if (portDepth >= 0) {
                portDepth++;
            }
            if (eprDepth >= 0) {
                eprDepth++;
            }
            if (namespaceURI.equals(WSDLConstants.QNAME_SERVICE.getNamespaceURI()) && localName.equals(WSDLConstants.QNAME_SERVICE.getLocalPart())) {
                onService = true;
                serviceDepth = 0;
            } else if (namespaceURI.equals(WSDLConstants.QNAME_PORT.getNamespaceURI()) && localName.equals(WSDLConstants.QNAME_PORT.getLocalPart())) {
                if (serviceDepth >= 1) {
                    onPort = true;
                    portDepth = 0;
                }
            } else if (namespaceURI.equals(W3CAddressingConstants.WSA_NAMESPACE_NAME) && localName.equals("EndpointReference")) {
                if (serviceDepth >= 1 && portDepth >= 1) {
                    portHasEPR = true;
                    eprDepth = 0;
                }
            } else if ((namespaceURI.equals(WSDLConstants.NS_SOAP_BINDING_ADDRESS.getNamespaceURI()) || namespaceURI.equals(WSDLConstants.NS_SOAP12_BINDING_ADDRESS.getNamespaceURI())) && localName.equals("address") && portDepth == 1) {
                onPortAddress = true;
            }
            WSEndpoint endpoint = getEndpoint(serviceName, portName);
            // if (service.getLocalPart().equals(serviceName) && port.getLocalPart().equals(portName)) {
            if (endpoint != null) {
                if ((eprDepth == 1) && !namespaceURI.equals(W3CAddressingConstants.WSA_NAMESPACE_NAME)) {
                    // epr extension element
                    eprExtnFilterON = true;
                }
            /*
                    if (eprExtnFilterON) {
                        writeEPRExtensions();
                    }
                    */
            }
        }

        private void resetOnElementFlags() {
            if (onService) {
                onService = false;
            }
            if (onPort) {
                onPort = false;
            }
            if (onPortAddress) {
                onPortAddress = false;
            }
        }

        private void writeEPRExtensions(Collection<WSEndpointReference.EPRExtension> eprExtns) throws XMLStreamException {
            if (eprExtns != null) {
                for (WSEndpointReference.EPRExtension e : eprExtns) {
                    XMLStreamReaderToXMLStreamWriter c = new XMLStreamReaderToXMLStreamWriter();
                    XMLStreamReader r = e.readAsXMLStreamReader();
                    c.bridge(r, writer);
                    XMLStreamReaderFactory.recycle(r);
                }
            }
        }

        @Override
        public void writeStartElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
            handleStartElement(localName, namespaceURI);
            if (!eprExtnFilterON) {
                super.writeStartElement(prefix, localName, namespaceURI);
            }
        }

        @Override
        public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
            handleStartElement(localName, namespaceURI);
            if (!eprExtnFilterON) {
                super.writeStartElement(namespaceURI, localName);
            }
        }

        @Override
        public void writeStartElement(String localName) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeStartElement(localName);
            }
        }

        private void handleEndElement() throws XMLStreamException {
            resetOnElementFlags();
            // End of wsdl:port, write complete EPR if not present.
            if (portDepth == 0) {
                if (!portHasEPR && getEndpoint(serviceName, portName) != null) {
                    // write the complete EPR with address.
                    writer.writeStartElement(AddressingVersion.W3C.getPrefix(), "EndpointReference", AddressingVersion.W3C.nsUri);
                    writer.writeNamespace(AddressingVersion.W3C.getPrefix(), AddressingVersion.W3C.nsUri);
                    writer.writeStartElement(AddressingVersion.W3C.getPrefix(), AddressingVersion.W3C.eprType.address, AddressingVersion.W3C.nsUri);
                    writer.writeCharacters(portAddress);
                    writer.writeEndElement();
                    writeEPRExtensions(getEndpoint(serviceName, portName).getEndpointReferenceExtensions());
                    writer.writeEndElement();
                }
            }
            // End of wsa:EndpointReference, write EPR extension elements
            if (eprDepth == 0) {
                if (portHasEPR && getEndpoint(serviceName, portName) != null) {
                    writeEPRExtensions(getEndpoint(serviceName, portName).getEndpointReferenceExtensions());
                }
                eprExtnFilterON = false;
            }
            if (serviceDepth >= 0) {
                serviceDepth--;
            }
            if (portDepth >= 0) {
                portDepth--;
            }
            if (eprDepth >= 0) {
                eprDepth--;
            }
            if (serviceDepth == -1) {
                serviceName = null;
            }
            if (portDepth == -1) {
                portHasEPR = false;
                portAddress = null;
                portName = null;
            }
        }

        @Override
        public void writeEndElement() throws XMLStreamException {
            handleEndElement();
            if (!eprExtnFilterON) {
                super.writeEndElement();
            }
        }

        private void handleAttribute(String localName, String value) {
            if (localName.equals("name")) {
                if (onService) {
                    serviceName = value;
                    onService = false;
                } else if (onPort) {
                    portName = value;
                    onPort = false;
                }
            }
            if (localName.equals("location") && onPortAddress) {
                portAddress = value;
            }
        }

        @Override
        public void writeAttribute(String prefix, String namespaceURI, String localName, String value) throws XMLStreamException {
            handleAttribute(localName, value);
            if (!eprExtnFilterON) {
                super.writeAttribute(prefix, namespaceURI, localName, value);
            }
        }

        @Override
        public void writeAttribute(String namespaceURI, String localName, String value) throws XMLStreamException {
            handleAttribute(localName, value);
            if (!eprExtnFilterON) {
                super.writeAttribute(namespaceURI, localName, value);
            }
        }

        @Override
        public void writeAttribute(String localName, String value) throws XMLStreamException {
            handleAttribute(localName, value);
            if (!eprExtnFilterON) {
                super.writeAttribute(localName, value);
            }
        }

        @Override
        public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeEmptyElement(namespaceURI, localName);
            }
        }

        @Override
        public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeNamespace(prefix, namespaceURI);
            }
        }

        @Override
        public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.setNamespaceContext(context);
            }
        }

        @Override
        public void setDefaultNamespace(String uri) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.setDefaultNamespace(uri);
            }
        }

        @Override
        public void setPrefix(String prefix, String uri) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.setPrefix(prefix, uri);
            }
        }

        @Override
        public void writeProcessingInstruction(String target, String data) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeProcessingInstruction(target, data);
            }
        }

        @Override
        public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeEmptyElement(prefix, localName, namespaceURI);
            }
        }

        @Override
        public void writeCData(String data) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeCData(data);
            }
        }

        @Override
        public void writeCharacters(String text) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeCharacters(text);
            }
        }

        @Override
        public void writeComment(String data) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeComment(data);
            }
        }

        @Override
        public void writeDTD(String dtd) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeDTD(dtd);
            }
        }

        @Override
        public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeDefaultNamespace(namespaceURI);
            }
        }

        @Override
        public void writeEmptyElement(String localName) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeEmptyElement(localName);
            }
        }

        @Override
        public void writeEntityRef(String name) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeEntityRef(name);
            }
        }

        @Override
        public void writeProcessingInstruction(String target) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeProcessingInstruction(target);
            }
        }

        @Override
        public void writeCharacters(char[] text, int start, int len) throws XMLStreamException {
            if (!eprExtnFilterON) {
                super.writeCharacters(text, start, len);
            }
        }
    };
}
Also used : XMLStreamReaderToXMLStreamWriter(org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamWriterFilter(com.sun.xml.ws.util.xml.XMLStreamWriterFilter) NamespaceContext(javax.xml.namespace.NamespaceContext) WSEndpointReference(com.sun.xml.ws.api.addressing.WSEndpointReference) Collection(java.util.Collection)

Example 2 with XMLStreamReaderToXMLStreamWriter

use of org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter in project metro-jax-ws by eclipse-ee4j.

the class StreamMessage method writePayloadTo.

public void writePayloadTo(XMLStreamWriter writer) throws XMLStreamException {
    if (envelopeReader != null)
        readEnvelope(this);
    assert unconsumed();
    if (payloadLocalName == null) {
        // no body
        return;
    }
    if (bodyPrologue != null) {
        writer.writeCharacters(bodyPrologue);
    }
    XMLStreamReaderToXMLStreamWriter conv = new XMLStreamReaderToXMLStreamWriter();
    while (reader.getEventType() != XMLStreamConstants.END_DOCUMENT) {
        String name = reader.getLocalName();
        String nsUri = reader.getNamespaceURI();
        // Check if its not soapenv:Body then move to next ELEMENT
        if (reader.getEventType() == XMLStreamConstants.END_ELEMENT) {
            if (!isBodyElement(name, nsUri)) {
                // closing payload element: store epilogue for further signing, if applicable
                // however if there more than one payloads exist - the last one is stored
                String whiteSpaces = XMLStreamReaderUtil.nextWhiteSpaceContent(reader);
                if (whiteSpaces != null) {
                    this.bodyEpilogue = whiteSpaces;
                    // write it to the message too
                    writer.writeCharacters(whiteSpaces);
                }
            } else {
                // body closed > exit
                break;
            }
        } else {
            // payload opening element: copy payload to writer
            conv.bridge(reader, writer);
        }
    }
    XMLStreamReaderUtil.readRest(reader);
    XMLStreamReaderUtil.close(reader);
    XMLStreamReaderFactory.recycle(reader);
}
Also used : XMLStreamReaderToXMLStreamWriter(org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter)

Example 3 with XMLStreamReaderToXMLStreamWriter

use of org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter in project metro-jax-ws by eclipse-ee4j.

the class SDDocumentImpl method writeTo.

public void writeTo(OutputStream os) throws IOException {
    XMLStreamWriter w = null;
    try {
        // generate the WSDL with utf-8 encoding and XML version 1.0
        w = XMLStreamWriterFactory.create(os, "UTF-8");
        w.writeStartDocument("UTF-8", "1.0");
        new XMLStreamReaderToXMLStreamWriter().bridge(source.read(), w);
        w.writeEndDocument();
    } catch (XMLStreamException e) {
        IOException ioe = new IOException(e.getMessage());
        ioe.initCause(e);
        throw ioe;
    } finally {
        try {
            if (w != null)
                w.close();
        } catch (XMLStreamException e) {
            IOException ioe = new IOException(e.getMessage());
            ioe.initCause(e);
            throw ioe;
        }
    }
}
Also used : XMLStreamReaderToXMLStreamWriter(org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter) XMLStreamReaderToXMLStreamWriter(org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter) IOException(java.io.IOException)

Example 4 with XMLStreamReaderToXMLStreamWriter

use of org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter in project metro-jax-ws by eclipse-ee4j.

the class WSEndpointReference method createBufferFromData.

private static XMLStreamBuffer createBufferFromData(AddressingVersion version, String address, List<Element> referenceParameters, QName service, QName port, QName portType, List<Element> metadata, String wsdlAddress, String wsdlTargetNamespace, @Nullable Collection<EPRExtension> extns, @Nullable Map<QName, String> attributes) {
    StreamWriterBufferCreator writer = new StreamWriterBufferCreator();
    try {
        writer.writeStartDocument();
        writer.writeStartElement(version.getPrefix(), "EndpointReference", version.nsUri);
        writer.writeNamespace(version.getPrefix(), version.nsUri);
        writePartialEPRInfoset(writer, version, address, referenceParameters, service, port, portType, metadata, wsdlAddress, wsdlTargetNamespace, attributes);
        // write extensibility elements in the EPR element
        if (extns != null) {
            for (EPRExtension e : extns) {
                XMLStreamReaderToXMLStreamWriter c = new XMLStreamReaderToXMLStreamWriter();
                XMLStreamReader r = e.readAsXMLStreamReader();
                c.bridge(r, writer);
                XMLStreamReaderFactory.recycle(r);
            }
        }
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
        return writer.getXMLStreamBuffer();
    } catch (XMLStreamException e) {
        throw new WebServiceException(e);
    }
}
Also used : XMLStreamReaderToXMLStreamWriter(org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) WebServiceException(jakarta.xml.ws.WebServiceException) WSEPRExtension(com.sun.xml.ws.addressing.WSEPRExtension) StreamWriterBufferCreator(com.sun.xml.stream.buffer.stax.StreamWriterBufferCreator)

Aggregations

XMLStreamReaderToXMLStreamWriter (org.jvnet.staxex.util.XMLStreamReaderToXMLStreamWriter)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 StreamWriterBufferCreator (com.sun.xml.stream.buffer.stax.StreamWriterBufferCreator)1 WSEPRExtension (com.sun.xml.ws.addressing.WSEPRExtension)1 WSEndpointReference (com.sun.xml.ws.api.addressing.WSEndpointReference)1 XMLStreamWriterFilter (com.sun.xml.ws.util.xml.XMLStreamWriterFilter)1 WebServiceException (jakarta.xml.ws.WebServiceException)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 NamespaceContext (javax.xml.namespace.NamespaceContext)1 XMLStreamException (javax.xml.stream.XMLStreamException)1