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