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 org.apache.camel.Processor;
020    import org.apache.camel.impl.DefaultConsumer;
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    import org.jivesoftware.smack.Chat;
024    import org.jivesoftware.smack.MessageListener;
025    import org.jivesoftware.smack.PacketListener;
026    import org.jivesoftware.smack.SmackConfiguration;
027    import org.jivesoftware.smack.XMPPConnection;
028    import org.jivesoftware.smack.filter.AndFilter;
029    import org.jivesoftware.smack.filter.PacketTypeFilter;
030    import org.jivesoftware.smack.filter.ToContainsFilter;
031    import org.jivesoftware.smack.packet.Message;
032    import org.jivesoftware.smack.packet.Packet;
033    import org.jivesoftware.smack.packet.Presence;
034    import org.jivesoftware.smackx.muc.DiscussionHistory;
035    import org.jivesoftware.smackx.muc.MultiUserChat;
036    
037    /**
038     * A {@link org.apache.camel.Consumer Consumer} which listens to XMPP packets
039     *
040     * @version $Revision: 783640 $
041     */
042    public class XmppConsumer extends DefaultConsumer<XmppExchange> implements PacketListener, MessageListener {
043        private static final transient Log LOG = LogFactory.getLog(XmppConsumer.class);
044        private final XmppEndpoint endpoint;
045        private MultiUserChat muc;
046        private XMPPConnection connection;
047    
048        public XmppConsumer(XmppEndpoint endpoint, Processor processor) {
049            super(endpoint, processor);
050            this.endpoint = endpoint;
051        }
052    
053        @Override
054        protected void doStart() throws Exception {
055            connection = endpoint.createConnection();
056    
057            if (endpoint.getRoom() == null) {
058                Chat privateChat = connection.getChatManager().createChat(endpoint.getParticipant(), this);
059                if (LOG.isInfoEnabled()) {
060                    LOG.info("Open private chat to: " + privateChat.getParticipant());
061                }
062            } else {
063                // add the presence packet listener to the connection so we only get packets that concers us
064                // we must add the listener before creating the muc
065                final ToContainsFilter toFilter = new ToContainsFilter(endpoint.getParticipant());
066                final AndFilter packetFilter = new AndFilter(new PacketTypeFilter(Presence.class), toFilter);
067                connection.addPacketListener(this, packetFilter);
068    
069                muc = new MultiUserChat(connection, endpoint.resolveRoom(connection));
070                muc.addMessageListener(this);
071                DiscussionHistory history = new DiscussionHistory();
072                history.setMaxChars(0); // we do not want any historical messages
073    
074                muc.join(endpoint.getNickname(), null, history, SmackConfiguration.getPacketReplyTimeout());
075                if (LOG.isInfoEnabled()) {
076                    LOG.info("Joined room: " + muc.getRoom() + " as: " + endpoint.getNickname());
077                }
078            }
079    
080            super.doStart();
081        }
082    
083        @Override
084        protected void doStop() throws Exception {
085            super.doStop();
086            if (muc != null) {
087                if (LOG.isInfoEnabled()) {
088                    LOG.info("Leaving room: " + muc.getRoom());
089                }
090                muc.removeMessageListener(this);
091                muc.leave();
092                muc = null;
093            }
094            if (connection != null) {
095                if (LOG.isDebugEnabled()) {
096                    LOG.debug("Disconnecting from: " + XmppEndpoint.getConnectionMessage(connection));
097                }
098                connection.disconnect();
099                connection = null;
100            }
101        }
102    
103        public void processPacket(Packet packet) {
104            if (packet instanceof Message) {
105                processMessage(null, (Message) packet);
106            }
107        }
108    
109        public void processMessage(Chat chat, Message message) {
110            if (LOG.isDebugEnabled()) {
111                LOG.debug("Recieved XMPP message: " + message.getBody());
112            }
113    
114            XmppExchange exchange = endpoint.createExchange(message);
115            try {
116                getProcessor().process(exchange);
117            } catch (Exception e) {
118                exchange.setException(e);
119            }
120        }
121    
122    }