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    
023    import org.jivesoftware.smack.packet.Message;
024    
025    /**
026     * Represents a {@link org.apache.camel.Message} for working with XMPP
027     *
028     * @version $Revision:520964 $
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        @Override
051        public XmppExchange getExchange() {
052            return (XmppExchange)super.getExchange();
053        }
054    
055        /**
056         * Returns the underlying XMPP message
057         */
058        public Message getXmppMessage() {
059            return xmppMessage;
060        }
061    
062        public void setXmppMessage(Message xmppMessage) {
063            this.xmppMessage = xmppMessage;
064        }
065        
066        @Override
067        public XmppMessage newInstance() {
068            return new XmppMessage();
069        }
070    
071        @Override
072        protected Object createBody() {
073            if (xmppMessage != null) {
074                return getExchange().getBinding().extractBodyFromXmpp(getExchange(), xmppMessage);
075            }
076            return null;
077        }
078        
079        @Override
080        protected void populateInitialHeaders(Map<String, Object> map) {
081            if (xmppMessage != null) {
082                map.putAll(getExchange().getBinding().extractHeadersFromXmpp(xmppMessage));
083            }
084        }
085    }