Coverage Report - org.apache.camel.impl.DefaultMessage
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMessage
89% 
100% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.impl;
 19  
 
 20  
 import org.apache.camel.Message;
 21  
 
 22  
 import java.util.HashMap;
 23  
 import java.util.Map;
 24  
 
 25  
 /**
 26  
  * The default implementation of {@link Message}
 27  
  *
 28  
  * @version $Revision: 540969 $
 29  
  */
 30  836
 public class DefaultMessage extends MessageSupport {
 31  
     private Map<String, Object> headers;
 32  
 
 33  
     @Override
 34  
     public String toString() {
 35  84
         return "Message: " + getBody();
 36  
     }
 37  
 
 38  
     public Object getHeader(String name) {
 39  57
         return getHeaders().get(name);
 40  
     }
 41  
 
 42  
     public <T> T getHeader(String name, Class<T> type) {
 43  12
         Object value = getHeader(name);
 44  12
         return getExchange().getContext().getTypeConverter().convertTo(type, value);
 45  
     }
 46  
 
 47  
     public void setHeader(String name, Object value) {
 48  70
         if (headers == null) {
 49  52
             headers = createHeaders();
 50  
         }
 51  70
         headers.put(name, value);
 52  70
     }
 53  
 
 54  
     public Map<String, Object> getHeaders() {
 55  106
         if (headers == null) {
 56  31
             headers = createHeaders();
 57  
         }
 58  106
         return headers;
 59  
     }
 60  
 
 61  
     public void setHeaders(Map<String, Object> headers) {
 62  0
         this.headers = headers;
 63  0
     }
 64  
 
 65  
     public DefaultMessage newInstance() {
 66  24
         return new DefaultMessage();
 67  
     }
 68  
 
 69  
     /**
 70  
      * A factory method to lazily create the headers to make it easy to create efficient Message implementations
 71  
      * which only construct and populate the Map on demand
 72  
      *
 73  
      * @return return a newly constructed Map possibly containing headers from the underlying inbound transport
 74  
      */
 75  
     protected Map<String, Object> createHeaders() {
 76  83
         HashMap<String, Object> map = new HashMap<String, Object>();
 77  83
         populateInitialHeaders(map);
 78  83
         return map;
 79  
     }
 80  
 
 81  
     /**
 82  
      * A strategy method populate the initial set of headers on an inbound message from an underlying binding
 83  
      *
 84  
      * @param map is the empty header map to populate
 85  
      */
 86  
     protected void populateInitialHeaders(Map<String, Object> map) {
 87  83
     }
 88  
 }