All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.camel.component.cxf.converter.CxfPayloadConverter Maven / Gradle / Ivy

There is a newer version: 3.17.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.camel.component.cxf.converter;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.apache.camel.Converter;
import org.apache.camel.Exchange;
import org.apache.camel.FallbackConverter;
import org.apache.camel.TypeConverter;
import org.apache.camel.component.cxf.CxfPayload;
import org.apache.camel.spi.TypeConverterRegistry;
import org.apache.cxf.staxutils.StaxUtils;

@Converter
public final class CxfPayloadConverter {

    private CxfPayloadConverter() {
        // Helper class
    }

    @Converter
    public static  CxfPayload documentToCxfPayload(Document doc, Exchange exchange) {
        return elementToCxfPayload(doc.getDocumentElement(), exchange);
    }

    @Converter
    public static  CxfPayload elementToCxfPayload(Element element, Exchange exchange) {
        List headers = new ArrayList();
        List body = new ArrayList();
        body.add(element);
        return new CxfPayload(headers, body);
    }

    @Converter
    public static  CxfPayload nodeListToCxfPayload(NodeList nodeList, Exchange exchange) {
        List headers = new ArrayList();
        List body = new ArrayList();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            // add all nodes to the body that are elements
            if (Element.class.isAssignableFrom(node.getClass())) {
                body.add((Element) node);
            }
        }
        return new CxfPayload(headers, body);
    }
    
    @Converter
    public static  CxfPayload sourceToCxfPayload(Source src, Exchange exchange) {
        List headers = new ArrayList();
        List body = new ArrayList();
        body.add(src);
        return new CxfPayload(headers, body, null);
    }

    @Converter
    public static  NodeList cxfPayloadToNodeList(CxfPayload payload, Exchange exchange) {
        return new NodeListWrapper(payload.getBody());
    }
    
    @Converter
    public static  Node cxfPayLoadToNode(CxfPayload payload, Exchange exchange) {
        List payloadBodyElements = payload.getBody();
        
        if (payloadBodyElements.size() > 0) {
            return payloadBodyElements.get(0);
        }
        return null;
    }
    
    @Converter
    public static  Source cxfPayLoadToSource(CxfPayload payload, Exchange exchange) {
        List payloadBody = payload.getBodySources();
        
        if (payloadBody.size() > 0) {
            return payloadBody.get(0);
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    @FallbackConverter
    public static  T convertTo(Class type, Exchange exchange, Object value, TypeConverterRegistry registry) {
        // use fallback type converter, so we can probably convert into
        // CxfPayloads from other types
        if (type.isAssignableFrom(CxfPayload.class)) {
            if (!value.getClass().isArray()) {
                TypeConverter tc = registry.lookup(Source.class, value.getClass());
                if (tc != null) {
                    Source src = tc.convertTo(Source.class, exchange, value);
                    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;
            }
            // 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;
            
            if (payload.getBodySources().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);
                    }
                    payload.getBodySources().set(0, new DOMSource(d.getDocumentElement()));
                    return type.cast(d);
                }
                TypeConverter tc = registry.lookup(type, Source.class);
                if (tc != null) {
                    Source s = payload.getBodySources().get(0);
                    if (type.isInstance(s)) {
                        return type.cast(s);
                    }
                    if ((s instanceof StreamSource
                        || s instanceof SAXSource) 
                        && !type.isAssignableFrom(Document.class)
                        && !type.isAssignableFrom(Source.class)) {
                        //non-reproducible sources, we need to convert to DOMSource first
                        //or the payload will get wiped out
                        Document d;
                        try {
                            d = StaxUtils.read(s);
                        } catch (XMLStreamException e) {
                            throw new RuntimeException(e);
                        }
                        s = new DOMSource(d.getDocumentElement());
                        payload.getBodySources().set(0, s);
                    }
                    
                    T t = tc.convertTo(type, s);
                    if (t instanceof Document) {
                        payload.getBodySources().set(0, new DOMSource(((Document)t).getDocumentElement()));
                    } else if (t instanceof Source) {
                        payload.getBodySources().set(0, (Source)t);
                    }
                    return t;
                }                
            }
            TypeConverter tc = registry.lookup(type, NodeList.class);
            if (tc != null) { 
                Object result = tc.convertTo(type, 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, nodeList.item(0));
                } else {
                    // no we could not do it currently
                    return (T) Void.TYPE;
                }
            }
        }
        return null;
    }

    private static  CxfPayload convertVia(Class via, Exchange exchange, Object value, TypeConverterRegistry registry) {
        TypeConverter tc = registry.lookup(via, value.getClass());
        if (tc != null) {
            TypeConverter tc1 = registry.lookup(Document.class, via);
            if (tc1 != null) {
                V is = tc.convertTo(via, exchange, value);
                Document document = tc1.convertTo(Document.class, exchange, is);
                return documentToCxfPayload(document, exchange);
            }
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy