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 022 /** 023 * The default implementation of {@link Message} 024 * 025 * @version $Revision: 563607 $ 026 */ 027 public class DefaultMessage extends MessageSupport { 028 private Map<String, Object> headers; 029 030 @Override 031 public String toString() { 032 return "Message: " + getBody(); 033 } 034 035 public Object getHeader(String name) { 036 return getHeaders().get(name); 037 } 038 039 public <T> T getHeader(String name, Class<T> type) { 040 Object value = getHeader(name); 041 return getExchange().getContext().getTypeConverter().convertTo(type, value); 042 } 043 044 public void setHeader(String name, Object value) { 045 if (headers == null) { 046 headers = createHeaders(); 047 } 048 headers.put(name, value); 049 } 050 051 public Map<String, Object> getHeaders() { 052 if (headers == null) { 053 headers = createHeaders(); 054 } 055 return headers; 056 } 057 058 public void setHeaders(Map<String, Object> headers) { 059 this.headers = headers; 060 } 061 062 public DefaultMessage newInstance() { 063 return new DefaultMessage(); 064 } 065 066 /** 067 * A factory method to lazily create the headers to make it easy to create 068 * efficient Message implementations which only construct and populate the 069 * Map on demand 070 * 071 * @return return a newly constructed Map possibly containing headers from 072 * the underlying inbound transport 073 */ 074 protected Map<String, Object> createHeaders() { 075 HashMap<String, Object> map = new HashMap<String, Object>(); 076 populateInitialHeaders(map); 077 return map; 078 } 079 080 /** 081 * A strategy method populate the initial set of headers on an inbound 082 * message from an underlying binding 083 * 084 * @param map is the empty header map to populate 085 */ 086 protected void populateInitialHeaders(Map<String, Object> map) { 087 } 088 }