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.CamelContext;
020 import org.apache.camel.Exchange;
021 import org.apache.camel.Message;
022 import org.apache.camel.NoTypeConversionAvailableException;
023 import org.apache.camel.TypeConverter;
024 import org.apache.camel.impl.converter.DefaultTypeConverter;
025 import org.apache.camel.util.UuidGenerator;
026
027 /**
028 * A base class for implementation inheritence providing the core
029 * {@link Message} body handling features but letting the derived class deal
030 * with headers.
031 *
032 * Unless a specific provider wishes to do something particularly clever with
033 * headers you probably want to just derive from {@link DefaultMessage}
034 *
035 * @version $Revision: 749962 $
036 */
037 public abstract class MessageSupport implements Message {
038 private static final UuidGenerator DEFALT_ID_GENERATOR = new UuidGenerator();
039 private Exchange exchange;
040 private Object body;
041 private String messageId;
042
043 public Object getBody() {
044 if (body == null) {
045 body = createBody();
046 }
047 return body;
048 }
049
050 @SuppressWarnings({"unchecked" })
051 public <T> T getBody(Class<T> type) {
052 return getBody(type, getBody());
053 }
054
055 protected <T> T getBody(Class<T> type, Object body) {
056 Exchange e = getExchange();
057 if (e != null) {
058 CamelContext camelContext = e.getContext();
059 if (camelContext != null) {
060 boolean tryConvert = true;
061 TypeConverter converter = camelContext.getTypeConverter();
062 // if its the default type converter then use a performance shortcut to check if it can convert it
063 // this is faster than getting throwing and catching NoTypeConversionAvailableException
064 // the StreamCachingInterceptor will attempt to convert the payload to a StremCache for caching purpose
065 // so we get invoked on each node the exchange passes. So this is a little performance optimization
066 // to avoid the excessive exception handling
067 if (body != null && converter instanceof DefaultTypeConverter) {
068 DefaultTypeConverter defaultTypeConverter = (DefaultTypeConverter) converter;
069 // we can only check if there is no converter meaning we have tried to convert it beforehand
070 // and then knows for sure there is no converter possible
071 tryConvert = !defaultTypeConverter.hasNoConverterFor(type, body.getClass());
072 }
073 if (tryConvert) {
074 try {
075 // lets first try converting the body itself first
076 // as for some types like InputStream v Reader its more efficient to do the transformation
077 // from the body itself as its got efficient implementations of them, before trying the
078 // message
079 return converter.convertTo(type, e, body);
080 } catch (NoTypeConversionAvailableException ex) {
081 // ignore
082 }
083 }
084 // fallback to the message itself
085 return converter.convertTo(type, this);
086 }
087 }
088 return (T)getBody();
089 }
090
091 public void setBody(Object body) {
092 this.body = body;
093 }
094
095 public <T> void setBody(Object value, Class<T> type) {
096 Exchange e = getExchange();
097 if (e != null) {
098 T v = e.getContext().getTypeConverter().convertTo(type, e, value);
099 if (v != null) {
100 value = v;
101 }
102 }
103 setBody(value);
104 }
105
106 public Message copy() {
107 Message answer = newInstance();
108 answer.copyFrom(this);
109 return answer;
110 }
111
112 public void copyFrom(Message that) {
113 setMessageId(that.getMessageId());
114 setBody(that.getBody());
115 getHeaders().putAll(that.getHeaders());
116 getAttachments().putAll(that.getAttachments());
117 }
118
119 public Exchange getExchange() {
120 return exchange;
121 }
122
123 public void setExchange(Exchange exchange) {
124 this.exchange = exchange;
125 }
126
127 /**
128 * Returns a new instance
129 */
130 public abstract Message newInstance();
131
132 /**
133 * A factory method to allow a provider to lazily create the message body
134 * for inbound messages from other sources
135 *
136 * @return the value of the message body or null if there is no value
137 * available
138 */
139 protected Object createBody() {
140 return null;
141 }
142
143 public String getMessageId() {
144 if (messageId == null) {
145 messageId = createMessageId();
146 }
147 return this.messageId;
148 }
149
150 public void setMessageId(String messageId) {
151 this.messageId = messageId;
152 }
153
154 /**
155 * Lets allow implementations to auto-create a messageId
156 */
157 protected String createMessageId() {
158 return DEFALT_ID_GENERATOR.generateId();
159 }
160 }