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.HashMap;
020 import java.util.Iterator;
021 import java.util.Map;
022
023 import org.apache.camel.impl.DefaultMessage;
024
025 import org.jivesoftware.smack.packet.Message;
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 } else {
048 return "XmppMessage: " + getBody();
049 }
050 }
051
052 @Override
053 public XmppExchange getExchange() {
054 return (XmppExchange)super.getExchange();
055 }
056
057 /**
058 * Returns the underlying XMPP message
059 *
060 * @return the underlying XMPP message
061 */
062 public Message getXmppMessage() {
063 return xmppMessage;
064 }
065
066 public void setXmppMessage(Message xmppMessage) {
067 this.xmppMessage = xmppMessage;
068 }
069
070 public Object getHeader(String name) {
071 return xmppMessage.getProperty(name);
072 }
073
074 @Override
075 public void setHeader(String name, Object value) {
076 if (value == null) {
077 xmppMessage.deleteProperty(name);
078 } else {
079 xmppMessage.setProperty(name, value);
080 }
081 }
082
083 @Override
084 public Map<String, Object> getHeaders() {
085 Map<String, Object> answer = new HashMap<String, Object>();
086 Iterator iter = xmppMessage.getPropertyNames();
087 while (iter.hasNext()) {
088 String name = (String)iter.next();
089 answer.put(name, xmppMessage.getProperty(name));
090 }
091 return answer;
092 }
093
094 @Override
095 public XmppMessage newInstance() {
096 return new XmppMessage();
097 }
098
099 @Override
100 protected Object createBody() {
101 if (xmppMessage != null) {
102 return getExchange().getBinding().extractBodyFromXmpp(getExchange(), xmppMessage);
103 }
104 return null;
105 }
106 }