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: 37863 $
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        /* (non-Javadoc)
126         * @see org.apache.camel.Message#addAttachment(java.lang.String, javax.activation.DataHandler)
127         */
128        public void addAttachment(String id, DataHandler content) {
129            if (attachments == null) {
130                attachments = createAttachments();
131            }
132            attachments.put(id, content);
133        }
134    
135        /* (non-Javadoc)
136         * @see org.apache.camel.Message#getAttachment(java.lang.String)
137         */
138        public DataHandler getAttachment(String id) {
139            return getAttachments().get(id);
140        }
141    
142        /* (non-Javadoc)
143         * @see org.apache.camel.Message#getAttachmentNames()
144         */
145        public Set<String> getAttachmentNames() {
146            if (attachments == null) {
147                attachments = createAttachments();
148            }
149            return attachments.keySet();
150        }
151    
152        /* (non-Javadoc)
153         * @see org.apache.camel.Message#removeAttachment(java.lang.String)
154         */
155        public void removeAttachment(String id) {
156            if (attachments != null && attachments.containsKey(id)) {
157                attachments.remove(id);
158            }
159        }
160    
161        /* (non-Javadoc)
162         * @see org.apache.camel.Message#getAttachments()
163         */
164        public Map<String, DataHandler> getAttachments() {
165            if (attachments == null) {
166                attachments = createAttachments();
167            }
168            return attachments;
169        }
170    
171        /* (non-Javadoc)
172         * @see org.apache.camel.Message#setAttachments(java.util.Map)
173         */
174        public void setAttachments(Map<String, DataHandler> attachments) {
175            this.attachments = attachments;
176        }
177    }