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