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.xmpp;
019    
020    import org.apache.camel.impl.DefaultMessage;
021    import org.jivesoftware.smack.packet.Message;
022    
023    import java.util.HashMap;
024    import java.util.Iterator;
025    import java.util.Map;
026    
027    /**
028     * Represents a {@link org.apache.camel.Message} for working with XMPP
029     *
030     * @version $Revision:520964 $
031     */
032    public class XmppMessage extends DefaultMessage {
033        private Message xmppMessage;
034    
035        public XmppMessage() {
036            this(new Message());
037        }
038    
039        public XmppMessage(Message jmsMessage) {
040            this.xmppMessage = jmsMessage;
041        }
042    
043        @Override
044        public String toString() {
045            if (xmppMessage != null) {
046                return "XmppMessage: " + xmppMessage;
047            }
048            else {
049                return "XmppMessage: " + getBody();
050            }
051        }
052    
053        @Override
054        public XmppExchange getExchange() {
055            return (XmppExchange) super.getExchange();
056        }
057    
058        /**
059         * Returns the underlying XMPP message
060         *
061         * @return the underlying XMPP message
062         */
063        public Message getXmppMessage() {
064            return xmppMessage;
065        }
066    
067        public void setXmppMessage(Message xmppMessage) {
068            this.xmppMessage = xmppMessage;
069        }
070    
071        public Object getHeader(String name) {
072            return xmppMessage.getProperty(name);
073        }
074    
075        @Override
076        public void setHeader(String name, Object value) {
077            if (value == null) {
078                xmppMessage.deleteProperty(name);
079            }
080            else {
081                xmppMessage.setProperty(name, value);
082            }
083        }
084    
085        @Override
086        public Map<String, Object> getHeaders() {
087            Map<String, Object> answer = new HashMap<String, Object>();
088            Iterator iter = xmppMessage.getPropertyNames();
089            while (iter.hasNext()) {
090                String name = (String) iter.next();
091                answer.put(name, xmppMessage.getProperty(name));
092            }
093            return answer;
094        }
095    
096        @Override
097        public XmppMessage newInstance() {
098            return new XmppMessage();
099        }
100    
101        @Override
102        protected Object createBody() {
103            if (xmppMessage != null) {
104                return getExchange().getBinding().extractBodyFromXmpp(getExchange(), xmppMessage);
105            }
106            return null;
107        }
108    }