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.xmpp;
018    
019    import java.util.Map;
020    
021    import org.apache.camel.impl.DefaultMessage;
022    import org.apache.camel.util.ExchangeHelper;
023    import org.jivesoftware.smack.packet.Message;
024    
025    /**
026     * Represents a {@link org.apache.camel.Message} for working with XMPP
027     *
028     * @version 
029     */
030    public class XmppMessage extends DefaultMessage {
031        private Message xmppMessage;
032    
033        public XmppMessage() {
034            this(new Message());
035        }
036    
037        public XmppMessage(Message jmsMessage) {
038            this.xmppMessage = jmsMessage;
039        }
040    
041        @Override
042        public String toString() {
043            if (xmppMessage != null) {
044                return "XmppMessage: " + xmppMessage;
045            } else {
046                return "XmppMessage: " + getBody();
047            }
048        }
049    
050        /**
051         * Returns the underlying XMPP message
052         */
053        public Message getXmppMessage() {
054            return xmppMessage;
055        }
056    
057        public void setXmppMessage(Message xmppMessage) {
058            this.xmppMessage = xmppMessage;
059        }
060        
061        @Override
062        public XmppMessage newInstance() {
063            return new XmppMessage();
064        }
065    
066        @Override
067        protected Object createBody() {
068            if (xmppMessage != null) {
069                XmppBinding binding = ExchangeHelper.getBinding(getExchange(), XmppBinding.class);
070                if (binding != null) {
071                    return binding.extractBodyFromXmpp(getExchange(), xmppMessage);
072                }
073            }
074            return null;
075        }
076        
077        @Override
078        protected void populateInitialHeaders(Map<String, Object> map) {
079            if (xmppMessage != null) {
080                XmppBinding binding = ExchangeHelper.getBinding(getExchange(), XmppBinding.class);
081                if (binding != null) {
082                    map.putAll(binding.extractHeadersFromXmpp(xmppMessage, getExchange()));
083                }
084            }
085        }
086    }