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.impl;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.InvalidPayloadException;
021    import org.apache.camel.Message;
022    import org.apache.camel.TypeConverter;
023    import org.apache.camel.util.UuidGenerator;
024    
025    /**
026     * A base class for implementation inheritence providing the core
027     * {@link Message} body handling features but letting the derived class deal
028     * with headers.
029     *
030     * Unless a specific provider wishes to do something particularly clever with
031     * headers you probably want to just derive from {@link DefaultMessage}
032     *
033     * @version $Revision: 755890 $
034     */
035    public abstract class MessageSupport implements Message {
036        private static final UuidGenerator DEFALT_ID_GENERATOR = new UuidGenerator();
037        private Exchange exchange;
038        private Object body;
039        private String messageId;
040    
041        public Object getBody() {
042            if (body == null) {
043                body = createBody();
044            }
045            return body;
046        }
047    
048        public <T> T getBody(Class<T> type) {
049            return getBody(type, getBody());
050        }
051    
052        public Object getMandatoryBody() throws InvalidPayloadException {
053            Object answer = getBody();
054            if (answer == null) {
055                throw new InvalidPayloadException(getExchange(), Object.class, this);
056            }
057            return answer;
058        }
059    
060        protected <T> T getBody(Class<T> type, Object body) {
061            Exchange e = getExchange();
062            if (e != null) {
063                TypeConverter converter = e.getContext().getTypeConverter();
064    
065                // lets first try converting the body itself first
066                // as for some types like InputStream v Reader its more efficient to do the transformation
067                // from the body itself as its got efficient implementations of them, before trying the
068                // message
069                T answer = converter.convertTo(type, getExchange(), body);
070                if (answer != null) {
071                    return answer;
072                }
073                // fallback to the message itself
074                answer = converter.convertTo(type, getExchange(), this);
075                if (answer != null) {
076                    return answer;
077                }
078            }
079    
080            // not possible to convert
081            return null;
082        }
083    
084        public <T> T getMandatoryBody(Class<T> type) throws InvalidPayloadException {
085            Exchange e = getExchange();
086            if (e != null) {
087                TypeConverter converter = e.getContext().getTypeConverter();
088                try {
089                    return converter.mandatoryConvertTo(type, e, getBody());
090                } catch (Exception cause) {
091                    throw new InvalidPayloadException(e, type, this, cause);
092                }
093            }
094            throw new InvalidPayloadException(e, type, this);
095        }
096    
097        public void setBody(Object body) {
098            this.body = body;
099        }
100    
101        public <T> void setBody(Object value, Class<T> type) {
102            Exchange e = getExchange();
103            if (e != null) {
104                T v = e.getContext().getTypeConverter().convertTo(type, e, value);
105                if (v != null) {
106                    value = v;
107                }
108            }
109            setBody(value);
110        }
111    
112        public Message copy() {
113            Message answer = newInstance();
114            answer.copyFrom(this);
115            return answer;
116        }
117    
118        public void copyFrom(Message that) {
119            setMessageId(that.getMessageId());
120            setBody(that.getBody());
121            getHeaders().putAll(that.getHeaders());
122            getAttachments().putAll(that.getAttachments());
123        }
124    
125        public Exchange getExchange() {
126            return exchange;
127        }
128    
129        public void setExchange(Exchange exchange) {
130            this.exchange = exchange;
131        }
132    
133        /**
134         * Returns a new instance
135         */
136        public abstract Message newInstance();
137    
138        /**
139         * A factory method to allow a provider to lazily create the message body
140         * for inbound messages from other sources
141         *
142         * @return the value of the message body or null if there is no value
143         *         available
144         */
145        protected Object createBody() {
146            return null;
147        }
148    
149        public String getMessageId() {
150            if (messageId == null) {
151                messageId = createMessageId();
152            }
153            return this.messageId;
154        }
155    
156        public void setMessageId(String messageId) {
157            this.messageId = messageId;
158        }
159    
160        /**
161         * Lets allow implementations to auto-create a messageId
162         */
163        protected String createMessageId() {
164            return DEFALT_ID_GENERATOR.generateId();
165        }
166    }