001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.jbi;
019    
020    import org.apache.camel.Message;
021    import org.apache.camel.impl.DefaultMessage;
022    
023    import javax.jbi.messaging.NormalizedMessage;
024    import java.util.Iterator;
025    import java.util.Map;
026    
027    /**
028     * A JBI {@link Message} which provides access to the underlying JBI features such as {@link #getNormalizedMessage()}
029     *
030     * @version $Revision: 522517 $
031     */
032    public class JbiMessage extends DefaultMessage {
033        private NormalizedMessage normalizedMessage;
034    
035        public JbiMessage() {
036        }
037    
038        public JbiMessage(NormalizedMessage normalizedMessage) {
039            this.normalizedMessage = normalizedMessage;
040        }
041    
042        @Override
043        public String toString() {
044            if (normalizedMessage != null) {
045                return "JbiMessage: " + normalizedMessage;
046            }
047            else {
048                return "JbiMessage: " + getBody();
049            }
050        }
051    
052        @Override
053        public JbiExchange getExchange() {
054            return (JbiExchange) super.getExchange();
055        }
056    
057        /**
058         * Returns the underlying JBI message
059         *
060         * @return the underlying JBI message
061         */
062        public NormalizedMessage getNormalizedMessage() {
063            return normalizedMessage;
064        }
065    
066        public void setNormalizedMessage(NormalizedMessage normalizedMessage) {
067            this.normalizedMessage = normalizedMessage;
068        }
069    
070        public Object getHeader(String name) {
071            Object answer = null;
072            if (normalizedMessage != null) {
073                answer = normalizedMessage.getProperty(name);
074            }
075            if (answer == null) {
076                answer = super.getHeader(name);
077            }
078            return answer;
079        }
080    
081        @Override
082        public JbiMessage newInstance() {
083            return new JbiMessage();
084        }
085    
086        @Override
087        protected Object createBody() {
088            if (normalizedMessage != null) {
089                return getExchange().getBinding().extractBodyFromJbi(getExchange(), normalizedMessage);
090            }
091            return null;
092        }
093    
094        @Override
095        protected void populateInitialHeaders(Map<String, Object> map) {
096            if (normalizedMessage != null) {
097                Iterator iter = normalizedMessage.getPropertyNames().iterator();
098                while (iter.hasNext()) {
099                    String name = iter.next().toString();
100                    Object value = normalizedMessage.getProperty(name);
101                    map.put(name, value);
102                }
103            }
104        }
105    }