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.component.jbi;
018    
019    import org.apache.camel.Message;
020    import org.apache.camel.impl.DefaultMessage;
021    
022    import javax.jbi.messaging.NormalizedMessage;
023    import java.util.Iterator;
024    import java.util.Map;
025    
026    /**
027     * A JBI {@link Message} which provides access to the underlying JBI features such as {@link #getNormalizedMessage()}
028     *
029     * @version $Revision: 563665 $
030     */
031    public class JbiMessage extends DefaultMessage {
032        private NormalizedMessage normalizedMessage;
033    
034        public JbiMessage() {
035        }
036    
037        public JbiMessage(NormalizedMessage normalizedMessage) {
038            this.normalizedMessage = normalizedMessage;
039        }
040    
041        @Override
042        public String toString() {
043            if (normalizedMessage != null) {
044                return "JbiMessage: " + normalizedMessage;
045            }
046            else {
047                return "JbiMessage: " + getBody();
048            }
049        }
050    
051        @Override
052        public JbiExchange getExchange() {
053            return (JbiExchange) super.getExchange();
054        }
055    
056        /**
057         * Returns the underlying JBI message
058         *
059         * @return the underlying JBI message
060         */
061        public NormalizedMessage getNormalizedMessage() {
062            return normalizedMessage;
063        }
064    
065        public void setNormalizedMessage(NormalizedMessage normalizedMessage) {
066            this.normalizedMessage = normalizedMessage;
067        }
068    
069        public Object getHeader(String name) {
070            Object answer = null;
071            if (normalizedMessage != null) {
072                answer = normalizedMessage.getProperty(name);
073            }
074            if (answer == null) {
075                answer = super.getHeader(name);
076            }
077            return answer;
078        }
079    
080        @Override
081        public JbiMessage newInstance() {
082            return new JbiMessage();
083        }
084    
085        @Override
086        protected Object createBody() {
087            if (normalizedMessage != null) {
088                return getExchange().getBinding().extractBodyFromJbi(getExchange(), normalizedMessage);
089            }
090            return null;
091        }
092    
093        @Override
094        protected void populateInitialHeaders(Map<String, Object> map) {
095            if (normalizedMessage != null) {
096                Iterator iter = normalizedMessage.getPropertyNames().iterator();
097                while (iter.hasNext()) {
098                    String name = iter.next().toString();
099                    Object value = normalizedMessage.getProperty(name);
100                    map.put(name, value);
101                }
102            }
103        }
104    }