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.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: 46968 $ 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 @SuppressWarnings({"unchecked" }) 049 public <T> T getBody(Class<T> type) { 050 return getBody(type, getBody()); 051 } 052 053 protected <T> T getBody(Class<T> type, Object body) { 054 Exchange e = getExchange(); 055 if (e != null) { 056 CamelContext camelContext = e.getContext(); 057 if (camelContext != null) { 058 TypeConverter converter = camelContext.getTypeConverter(); 059 T answer = converter.convertTo(type, e, body); 060 if (answer == null) { 061 // lets first try converting the message itself first 062 // as for some types like InputStream v Reader its more efficient to do the transformation 063 // from the Message itself as its got efficient implementations of them, before trying the 064 // payload 065 answer = converter.convertTo(type, this); 066 } 067 return answer; 068 } 069 } 070 return (T)getBody(); 071 } 072 073 public void setBody(Object body) { 074 this.body = body; 075 } 076 077 public <T> void setBody(Object value, Class<T> type) { 078 Exchange e = getExchange(); 079 if (e != null) { 080 T v = e.getContext().getTypeConverter().convertTo(type, e, value); 081 if (v != null) { 082 value = v; 083 } 084 } 085 setBody(value); 086 } 087 088 public Message copy() { 089 Message answer = newInstance(); 090 answer.copyFrom(this); 091 return answer; 092 } 093 094 public void copyFrom(Message that) { 095 setMessageId(that.getMessageId()); 096 setBody(that.getBody()); 097 getHeaders().putAll(that.getHeaders()); 098 getAttachments().putAll(that.getAttachments()); 099 } 100 101 public Exchange getExchange() { 102 return exchange; 103 } 104 105 public void setExchange(Exchange exchange) { 106 this.exchange = exchange; 107 } 108 109 /** 110 * Returns a new instance 111 */ 112 public abstract Message newInstance(); 113 114 /** 115 * A factory method to allow a provider to lazily create the message body 116 * for inbound messages from other sources 117 * 118 * @return the value of the message body or null if there is no value 119 * available 120 */ 121 protected Object createBody() { 122 return null; 123 } 124 125 public String getMessageId() { 126 if (messageId == null) { 127 messageId = createMessageId(); 128 } 129 return this.messageId; 130 } 131 132 public void setMessageId(String messageId) { 133 this.messageId = messageId; 134 } 135 136 /** 137 * Lets allow implementations to auto-create a messageId 138 */ 139 protected String createMessageId() { 140 return DEFALT_ID_GENERATOR.generateId(); 141 } 142 }