Search in sources :

Example 1 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project camel by apache.

the class CxfPayloadConverter method convertTo.

@SuppressWarnings("unchecked")
@FallbackConverter
public static <T> T convertTo(Class<T> type, Exchange exchange, Object value, TypeConverterRegistry registry) {
    // CxfPayloads from other types
    if (type.isAssignableFrom(CxfPayload.class)) {
        try {
            if (!value.getClass().isArray()) {
                Source src = null;
                // directly
                if (value instanceof InputStream) {
                    src = new StreamSource((InputStream) value);
                } else if (value instanceof Reader) {
                    src = new StreamSource((Reader) value);
                } else if (value instanceof String) {
                    src = new StreamSource(new StringReader((String) value));
                } else if (value instanceof Node) {
                    src = new DOMSource((Node) value);
                } else if (value instanceof Source) {
                    src = (Source) value;
                }
                if (src == null) {
                    // assuming staxsource is preferred, otherwise use the
                    // one preferred
                    TypeConverter tc = registry.lookup(javax.xml.transform.stax.StAXSource.class, value.getClass());
                    if (tc == null) {
                        tc = registry.lookup(Source.class, value.getClass());
                    }
                    if (tc != null) {
                        src = tc.convertTo(Source.class, exchange, value);
                    }
                }
                if (src != null) {
                    return (T) sourceToCxfPayload(src, exchange);
                }
            }
            TypeConverter tc = registry.lookup(NodeList.class, value.getClass());
            if (tc != null) {
                NodeList nodeList = tc.convertTo(NodeList.class, exchange, value);
                return (T) nodeListToCxfPayload(nodeList, exchange);
            }
            tc = registry.lookup(Document.class, value.getClass());
            if (tc != null) {
                Document document = tc.convertTo(Document.class, exchange, value);
                return (T) documentToCxfPayload(document, exchange);
            }
            // maybe we can convert via an InputStream
            CxfPayload<?> p;
            p = convertVia(InputStream.class, exchange, value, registry);
            if (p != null) {
                return (T) p;
            }
            // String is the converter of last resort
            p = convertVia(String.class, exchange, value, registry);
            if (p != null) {
                return (T) p;
            }
        } catch (RuntimeCamelException e) {
        // the internal conversion to XML can throw an exception if the content is not XML
        // ignore this and return Void.TYPE to indicate that we cannot convert this
        }
        // no we could not do it currently
        return (T) Void.TYPE;
    }
    // Convert a CxfPayload into something else
    if (CxfPayload.class.isAssignableFrom(value.getClass())) {
        CxfPayload<?> payload = (CxfPayload<?>) value;
        int size = payload.getBodySources().size();
        if (size == 1) {
            if (type.isAssignableFrom(Document.class)) {
                Source s = payload.getBodySources().get(0);
                Document d;
                try {
                    d = StaxUtils.read(s);
                } catch (XMLStreamException e) {
                    throw new RuntimeException(e);
                }
                return type.cast(d);
            }
            // CAMEL-8410 Just make sure we get the Source object directly from the payload body source
            Source s = payload.getBodySources().get(0);
            if (type.isInstance(s)) {
                return type.cast(s);
            }
            TypeConverter tc = registry.lookup(type, XMLStreamReader.class);
            if (tc != null && (s instanceof StaxSource || s instanceof StAXSource)) {
                XMLStreamReader r = (s instanceof StAXSource) ? ((StAXSource) s).getXMLStreamReader() : ((StaxSource) s).getXMLStreamReader();
                if (payload.getNsMap() != null) {
                    r = new DelegatingXMLStreamReader(r, payload.getNsMap());
                }
                return tc.convertTo(type, exchange, r);
            }
            tc = registry.lookup(type, Source.class);
            if (tc != null) {
                XMLStreamReader r = null;
                if (payload.getNsMap() != null) {
                    if (s instanceof StaxSource) {
                        r = ((StaxSource) s).getXMLStreamReader();
                    } else if (s instanceof StAXSource) {
                        r = ((StAXSource) s).getXMLStreamReader();
                    }
                    if (r != null) {
                        s = new StAXSource(new DelegatingXMLStreamReader(r, payload.getNsMap()));
                    }
                }
                return tc.convertTo(type, exchange, s);
            }
        }
        TypeConverter tc = registry.lookup(type, NodeList.class);
        if (tc != null) {
            Object result = tc.convertTo(type, exchange, cxfPayloadToNodeList((CxfPayload<?>) value, exchange));
            if (result == null) {
                // no we could not do it currently, and we just abort the convert here
                return (T) Void.TYPE;
            } else {
                return (T) result;
            }
        }
        // we cannot convert a node list, so we try the first item from the
        // node list
        tc = registry.lookup(type, Node.class);
        if (tc != null) {
            NodeList nodeList = cxfPayloadToNodeList((CxfPayload<?>) value, exchange);
            if (nodeList.getLength() > 0) {
                return tc.convertTo(type, exchange, nodeList.item(0));
            } else {
                // no we could not do it currently
                return (T) Void.TYPE;
            }
        } else {
            if (size == 0) {
                // empty size so we cannot convert
                return (T) Void.TYPE;
            }
        }
    }
    return null;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) CxfPayload(org.apache.camel.component.cxf.CxfPayload) Node(org.w3c.dom.Node) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) StaxSource(org.apache.cxf.staxutils.StaxSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StAXSource(javax.xml.transform.stax.StAXSource) StringReader(java.io.StringReader) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) NodeList(org.w3c.dom.NodeList) StAXSource(javax.xml.transform.stax.StAXSource) TypeConverter(org.apache.camel.TypeConverter) XMLStreamException(javax.xml.stream.XMLStreamException) StaxSource(org.apache.cxf.staxutils.StaxSource) RuntimeCamelException(org.apache.camel.RuntimeCamelException) FallbackConverter(org.apache.camel.FallbackConverter)

Example 2 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project camel by apache.

the class DataInInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    DepthXMLStreamReader xmlReader = getXMLStreamReader(message);
    try {
        // put the payload source as a document
        Document doc = StaxUtils.read(xmlReader);
        message.setContent(Source.class, new DOMSource(doc));
    } catch (XMLStreamException e) {
        throw new Fault(new org.apache.cxf.common.i18n.Message("XMLSTREAM_EXCEPTION", JUL_LOG), e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamException(javax.xml.stream.XMLStreamException) Message(org.apache.cxf.message.Message) Fault(org.apache.cxf.interceptor.Fault) DepthXMLStreamReader(org.apache.cxf.staxutils.DepthXMLStreamReader) Document(org.w3c.dom.Document)

Example 3 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project camel by apache.

the class RawMessageWSDLGetOutInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    Document doc = (Document) message.get(RawMessageWSDLGetInterceptor.DOCUMENT_HOLDER);
    if (doc == null) {
        return;
    }
    message.remove(RawMessageWSDLGetInterceptor.DOCUMENT_HOLDER);
    OutputStream out = message.getContent(OutputStream.class);
    String enc = null;
    try {
        enc = doc.getXmlEncoding();
    } catch (Exception ex) {
    //ignore - not dom level 3
    }
    if (enc == null) {
        enc = "utf-8";
    }
    XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(out, enc);
    try {
        StaxUtils.writeNode(doc, writer, true);
        writer.flush();
    } catch (XMLStreamException e) {
        throw new Fault(e);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStream(java.io.OutputStream) Fault(org.apache.cxf.interceptor.Fault) Document(org.w3c.dom.Document) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 4 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project camel by apache.

the class XMLTokenExpressionIteratorInvalidXMLTest method invokeAndVerify.

private void invokeAndVerify(Iterator<?> tokenizer, boolean error) throws IOException, XMLStreamException {
    Exception exp = null;
    try {
        tokenizer.next();
        tokenizer.next();
    } catch (Exception e) {
        exp = e;
    } finally {
        ((Closeable) tokenizer).close();
    }
    if (error) {
        assertNotNull("the error expected", exp);
    } else {
        assertNull("no error expected", exp);
    }
}
Also used : Closeable(java.io.Closeable) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Example 5 with XMLStreamException

use of javax.xml.stream.XMLStreamException in project camel by apache.

the class StaxConverter method createXMLInputFactory.

public static XMLInputFactory createXMLInputFactory(boolean nsAware) {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    setProperty(factory, XMLInputFactory.IS_NAMESPACE_AWARE, nsAware);
    setProperty(factory, XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
    setProperty(factory, XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
    setProperty(factory, XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    factory.setXMLResolver(new XMLResolver() {

        public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException {
            throw new XMLStreamException("Reading external entities is disabled");
        }
    });
    if (isWoodstox(factory)) {
        // just log a debug as we are good then
        LOG.debug("Created Woodstox XMLInputFactory: {}", factory);
    } else {
        // log a hint that woodstock may be a better factory to use
        LOG.info("Created XMLInputFactory: {}. DOMSource/DOMResult may have issues with {}. We suggest using Woodstox.", factory, factory);
    }
    return factory;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLResolver(javax.xml.stream.XMLResolver) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

XMLStreamException (javax.xml.stream.XMLStreamException)274 IOException (java.io.IOException)75 XMLStreamReader (javax.xml.stream.XMLStreamReader)74 XMLInputFactory (javax.xml.stream.XMLInputFactory)60 InputStream (java.io.InputStream)43 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)34 XMLEvent (javax.xml.stream.events.XMLEvent)24 StringReader (java.io.StringReader)22 JAXBException (javax.xml.bind.JAXBException)22 XMLEventReader (javax.xml.stream.XMLEventReader)19 ModelNode (org.jboss.dmr.ModelNode)18 ArrayList (java.util.ArrayList)16 XMLExtendedStreamReader (org.jboss.staxmapper.XMLExtendedStreamReader)15 ModelElement (org.wildfly.extension.picketlink.common.model.ModelElement)15 SAXException (org.xml.sax.SAXException)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 StringWriter (java.io.StringWriter)12 Unmarshaller (javax.xml.bind.Unmarshaller)11 Characters (javax.xml.stream.events.Characters)11