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