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