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.XmlElementRef;
029    import javax.xml.bind.annotation.XmlMixed;
030    import javax.xml.bind.annotation.XmlRootElement;
031    import javax.xml.bind.annotation.XmlTransient;
032    
033    import org.apache.camel.Message;
034    
035    /**
036     * Represents a JAXB2 representation of a Camel {@link Message} - <b>Important</b>: work in progress!
037     *
038     * @version $Revision: 664343 $
039     */
040    @XmlRootElement(name = "message")
041    @XmlAccessorType(value = XmlAccessType.FIELD)
042    public class MessageType {
043        // TODO: XmlElementRef to the abstrac class HeaderType does not work (CAMEL-583)
044        //@XmlElementRef(type = HeaderType.class)
045        @XmlAnyElement(lax = true)
046        @XmlMixed
047        List<HeaderType> headers = new ArrayList<HeaderType>();
048        @XmlAnyElement(lax = true)
049        @XmlMixed
050        private List content = new ArrayList();
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                content = (List)body;
071            } else {
072                content = new ArrayList();
073                content.add(body);
074            }
075        }
076    
077        public List<HeaderType> getHeaders() {
078            return headers;
079        }
080    
081        public void setHeaders(List<HeaderType> headers) {
082            this.headers = headers;
083        }
084    
085        public Map<String, Object> getHeaderMap() {
086            Map<String, Object> answer = new HashMap<String, Object>();
087            for (HeaderType header : headers) {
088                answer.put(header.getName(), header.getValue());
089            }
090            return answer;
091        }
092    
093        /**
094         * Copies the headers and body of this object from the given Camel message
095         *
096         * @param message the Camel message to read the headers and body from
097         */
098        public void copyFrom(Message message) {
099            headers.clear();
100            Set<Map.Entry<String, Object>> entries = message.getHeaders().entrySet();
101            for (Map.Entry<String, Object> entry : entries) {
102                Object value = entry.getValue();
103                if (value != null) {
104                    headers.add(createHeader(entry.getKey(), value));
105                }
106            }
107            setBody(message.getBody());
108        }
109    
110        /**
111         * Copies the headers and body of this object to the given Camel message
112         *
113         * @param message the camel message to overwrite its headers and body
114         */
115        public void copyTo(Message message) {
116            message.setHeaders(getHeaderMap());
117            message.setBody(getBody());
118        }
119    
120        protected HeaderType createHeader(String key, Object value) {
121            if (value instanceof String) {
122                return new StringHeader(key, (String)value);
123            } else if (value instanceof Integer) {
124                return new IntegerHeader(key, (Integer)value);
125            } else if (value instanceof Long) {
126                return new LongHeader(key, (Long)value);
127            } else {
128                // lets convert to a String
129                return new StringHeader(key, value.toString());
130    
131                //return new ObjectHeader(key, value);
132            }
133        }
134    }