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 java.util.HashMap;
020 import java.util.Map;
021 import java.util.Set;
022
023 import javax.activation.DataHandler;
024
025 import org.apache.camel.Exchange;
026 import org.apache.camel.Message;
027
028 /**
029 * The default implementation of {@link Message}
030 *
031 * @version $Revision: 712697 $
032 */
033 public class DefaultMessage extends MessageSupport {
034 private Map<String, Object> headers;
035 private Map<String, DataHandler> attachments;
036
037 @Override
038 public String toString() {
039 return "Message: " + getBody();
040 }
041
042 public Object getHeader(String name) {
043 return getHeaders().get(name);
044 }
045
046 public <T> T getHeader(String name, Class<T> type) {
047 Object value = getHeader(name);
048 Exchange e = getExchange();
049 return e.getContext().getTypeConverter().convertTo(type, e, value);
050 }
051
052 public void setHeader(String name, Object value) {
053 if (headers == null) {
054 headers = createHeaders();
055 }
056 headers.put(name, value);
057 }
058
059 public Object removeHeader(String name) {
060 if (headers != null) {
061 return headers.remove(name);
062 } else {
063 return null;
064 }
065 }
066
067 public Map<String, Object> getHeaders() {
068 if (headers == null) {
069 headers = createHeaders();
070 }
071 return headers;
072 }
073
074 public void setHeaders(Map<String, Object> headers) {
075 this.headers = headers;
076 }
077
078 public DefaultMessage newInstance() {
079 return new DefaultMessage();
080 }
081
082 /**
083 * A factory method to lazily create the headers to make it easy to create
084 * efficient Message implementations which only construct and populate the
085 * Map on demand
086 *
087 * @return return a newly constructed Map possibly containing headers from
088 * the underlying inbound transport
089 */
090 protected Map<String, Object> createHeaders() {
091 HashMap<String, Object> map = new HashMap<String, Object>();
092 populateInitialHeaders(map);
093 return map;
094 }
095
096 /**
097 * A strategy method populate the initial set of headers on an inbound
098 * message from an underlying binding
099 *
100 * @param map is the empty header map to populate
101 */
102 protected void populateInitialHeaders(Map<String, Object> map) {
103 }
104
105 /**
106 * A factory method to lazily create the attachments to make it easy to
107 * create efficient Message implementations which only construct and
108 * populate the Map on demand
109 *
110 * @return return a newly constructed Map
111 */
112 protected Map<String, DataHandler> createAttachments() {
113 HashMap<String, DataHandler> map = new HashMap<String, DataHandler>();
114 populateInitialAttachments(map);
115 return map;
116 }
117
118 /**
119 * A strategy method populate the initial set of attachments on an inbound
120 * message from an underlying binding
121 *
122 * @param map is the empty attachment map to populate
123 */
124 protected void populateInitialAttachments(Map<String, DataHandler> map) {
125 }
126
127 public void addAttachment(String id, DataHandler content) {
128 if (attachments == null) {
129 attachments = createAttachments();
130 }
131 attachments.put(id, content);
132 }
133
134 public DataHandler getAttachment(String id) {
135 return getAttachments().get(id);
136 }
137
138 public Set<String> getAttachmentNames() {
139 if (attachments == null) {
140 attachments = createAttachments();
141 }
142 return attachments.keySet();
143 }
144
145 public void removeAttachment(String id) {
146 if (attachments != null && attachments.containsKey(id)) {
147 attachments.remove(id);
148 }
149 }
150
151 public Map<String, DataHandler> getAttachments() {
152 if (attachments == null) {
153 attachments = createAttachments();
154 }
155 return attachments;
156 }
157
158 public void setAttachments(Map<String, DataHandler> attachments) {
159 this.attachments = attachments;
160 }
161
162 public boolean hasAttachments() {
163 if (attachments == null) {
164 attachments = createAttachments();
165 }
166 return this.attachments != null && this.attachments.size() > 0;
167 }
168
169 /**
170 * Returns true if the headers have been mutated in some way
171 */
172 protected boolean hasPopulatedHeaders() {
173 return headers != null;
174 }
175
176 }