001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.converter.jaxb;
018    
019    import java.util.ArrayList;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    import java.util.Set;
024    
025    import javax.xml.bind.annotation.XmlAccessType;
026    import javax.xml.bind.annotation.XmlAccessorType;
027    import javax.xml.bind.annotation.XmlAnyElement;
028    import javax.xml.bind.annotation.XmlMixed;
029    import javax.xml.bind.annotation.XmlRootElement;
030    import javax.xml.bind.annotation.XmlTransient;
031    
032    import org.apache.camel.Message;
033    import org.apache.camel.util.CastUtils;
034    
035    /**
036     * Represents a JAXB2 representation of a Camel {@link Message} - <b>Important</b>: work in progress!
037     *
038     * @version $Revision: 834590 $
039     */
040    @XmlRootElement(name = "message")
041    @XmlAccessorType(value = XmlAccessType.FIELD)
042    public class MessageDefinition {
043        // TODO: XmlElementRef to the abstract class HeaderType does not work (CAMEL-583)
044        //@XmlElementRef(type = HeaderType.class)
045        @XmlAnyElement(lax = true)
046        @XmlMixed
047        List<HeaderDefinition> headers = new ArrayList<HeaderDefinition>();
048        @XmlAnyElement(lax = true)
049        @XmlMixed
050        private List<Object> content = new ArrayList<Object>();
051        @XmlTransient
052        private Object body;
053    
054        public Object getBody() {
055            if (body == null) {
056                if (content != null) {
057                    if (content.size() == 1) {
058                        return content.get(0);
059                    } else {
060                        return content;
061                    }
062                }
063            }
064            return body;
065        }
066    
067        public void setBody(Object body) {
068            this.body = body;
069            if (body instanceof List) {
070                List<Object> cont = CastUtils.cast((List<?>)body);
071                content = cont;
072            } else {
073                content = new ArrayList<Object>();
074                content.add(body);
075            }
076        }
077    
078        public List<HeaderDefinition> getHeaders() {
079            return headers;
080        }
081    
082        public void setHeaders(List<HeaderDefinition> headers) {
083            this.headers = headers;
084        }
085    
086        public Map<String, Object> getHeaderMap() {
087            Map<String, Object> answer = new HashMap<String, Object>();
088            for (HeaderDefinition header : headers) {
089                answer.put(header.getName(), header.getValue());
090            }
091            return answer;
092        }
093    
094        /**
095         * Copies the headers and body of this object from the given Camel message
096         *
097         * @param message the Camel message to read the headers and body from
098         */
099        public void copyFrom(Message message) {
100            headers.clear();
101            Set<Map.Entry<String, Object>> entries = message.getHeaders().entrySet();
102            for (Map.Entry<String, Object> entry : entries) {
103                Object value = entry.getValue();
104                if (value != null) {
105                    headers.add(createHeader(entry.getKey(), value));
106                }
107            }
108            setBody(message.getBody());
109        }
110    
111        /**
112         * Copies the headers and body of this object to the given Camel message
113         *
114         * @param message the camel message to overwrite its headers and body
115         */
116        public void copyTo(Message message) {
117            message.setHeaders(getHeaderMap());
118            message.setBody(getBody());
119        }
120    
121        protected HeaderDefinition createHeader(String key, Object value) {
122            if (value instanceof String) {
123                return new StringHeader(key, (String)value);
124            } else if (value instanceof Integer) {
125                return new IntegerHeader(key, (Integer)value);
126            } else if (value instanceof Long) {
127                return new LongHeader(key, (Long)value);
128            } else {
129                // lets convert to a String
130                return new StringHeader(key, value.toString());
131    
132                //return new ObjectHeader(key, value);
133            }
134        }
135    }