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

org.apache.camel.component.cxf.common.CxfPayload Maven / Gradle / Ivy

There is a newer version: 4.8.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.common;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.XMLConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.Element;

import org.apache.camel.RuntimeCamelException;
import org.apache.camel.util.ObjectHelper;
import org.apache.cxf.staxutils.StaxUtils;

/**
 * CxfMessage body type when {@link DataFormat#PAYLOAD} is used.
 */
public class CxfPayload {

    private List body;
    private List headers;
    private Map nsMap;

    public CxfPayload(List headers, List body, Map nsMap) {
        this.headers = headers;
        this.body = body;
        this.nsMap = nsMap;
    }

    public CxfPayload(List headers, List body) {
        this.headers = headers;
        this.body = new ArrayList<>(body.size());
        for (Element el : body) {
            this.body.add(new DOMSource(el));
        }
    }

    /**
     * Get the body as a List of DOM elements. This will cause the Body to be fully read and parsed.
     *
     * @return
     */
    public List getBody() {
        return new AbstractList() {
            @Override
            public boolean add(Element e) {
                return body.add(new DOMSource(e));
            }

            @Override
            public Element set(int index, Element element) {
                Source s = body.set(index, new DOMSource(element));
                try {
                    return StaxUtils.read(s).getDocumentElement();
                } catch (XMLStreamException e) {
                    throw new RuntimeCamelException("Problem converting content to Element", e);
                }
            }

            @Override
            public void add(int index, Element element) {
                body.add(index, new DOMSource(element));
            }

            @Override
            public Element remove(int index) {
                Source s = body.remove(index);
                try {
                    return StaxUtils.read(s).getDocumentElement();
                } catch (XMLStreamException e) {
                    throw new RuntimeCamelException("Problem converting content to Element", e);
                }
            }

            public Element get(int index) {
                Source s = body.get(index);
                try {
                    Element el = StaxUtils.read(s).getDocumentElement();
                    addNamespace(el, nsMap);
                    body.set(index, new DOMSource(el));
                    return el;
                } catch (Exception ex) {
                    throw new RuntimeCamelException("Problem converting content to Element", ex);
                }
            }

            public int size() {
                return body.size();
            }
        };
    }

    protected static void addNamespace(Element element, Map nsMap) {
        if (nsMap != null) {
            for (Map.Entry entry : nsMap.entrySet()) {
                String ns = entry.getKey();
                // We should not override the namespace setting of the element
                if (XMLConstants.XMLNS_ATTRIBUTE.equals(ns)) {
                    if (ObjectHelper.isEmpty(element.getAttribute(XMLConstants.XMLNS_ATTRIBUTE))) {
                        element.setAttribute(ns, entry.getValue());
                    }
                } else {
                    if (ObjectHelper.isEmpty(element.getAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + ns))) {
                        element.setAttribute(XMLConstants.XMLNS_ATTRIBUTE + ":" + ns, entry.getValue());
                    }
                }
            }
        }
    }

    /**
     * Gets the body as a List of source objects. If possible, the Source objects will likely be StaxSource or similar
     * that allows streaming. If you plan to modify or use the Source, be careful that the result is still usable by the
     * Camel runtime.
     *
     * @return
     */
    public List getBodySources() {
        return body;
    }

    /*
     * set the body back with cached stream source so
     * CxfPayload is re-readable
     */
    public void setBodySources(List body) {
        this.body = body;
    }

    public List getHeaders() {
        return headers;
    }

    public Map getNsMap() {
        return nsMap;
    }

    @Override
    public String toString() {
        // do not load or print the payload body etc as we do not want to load that into memory etc
        return super.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy