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.packet.Message;
029    import org.jivesoftware.smack.packet.Packet;
030    import org.jivesoftware.smackx.muc.DiscussionHistory;
031    import org.jivesoftware.smackx.muc.MultiUserChat;
032    
033    /**
034     * A {@link org.apache.camel.Consumer Consumer} which listens to XMPP packets
035     *
036     * @version $Revision: 759035 $
037     */
038    public class XmppConsumer extends DefaultConsumer<XmppExchange> implements PacketListener, MessageListener {
039        private static final transient Log LOG = LogFactory.getLog(XmppConsumer.class);
040        private final XmppEndpoint endpoint;
041        private MultiUserChat muc;
042        private XMPPConnection connection;
043    
044        public XmppConsumer(XmppEndpoint endpoint, Processor processor) {
045            super(endpoint, processor);
046            this.endpoint = endpoint;
047        }
048    
049        @Override
050        protected void doStart() throws Exception {
051            connection = endpoint.createConnection();
052    
053            if (endpoint.getRoom() == null) {
054                Chat privateChat = connection.getChatManager().createChat(endpoint.getParticipant(), this);
055                if (LOG.isInfoEnabled()) {
056                    LOG.info("Open private chat to: " + privateChat.getParticipant());
057                }
058            } else {
059                muc = new MultiUserChat(connection, endpoint.resolveRoom(connection));
060                muc.addMessageListener(this);
061                DiscussionHistory history = new DiscussionHistory();
062                history.setMaxChars(0); // we do not want any historical messages
063    
064                muc.join(endpoint.getNickname(), null, history, SmackConfiguration.getPacketReplyTimeout());
065                if (LOG.isInfoEnabled()) {
066                    LOG.info("Joined room: " + muc.getRoom() + " as: " + endpoint.getNickname());
067                }
068            }
069    
070            super.doStart();
071        }
072    
073        @Override
074        protected void doStop() throws Exception {
075            super.doStop();
076            if (muc != null) {
077                if (LOG.isInfoEnabled()) {
078                    LOG.info("Leaving room: " + muc.getRoom());
079                }
080                muc.removeMessageListener(this);
081                muc.leave();
082                muc = null;
083            }
084            if (connection != null) {
085                if (LOG.isDebugEnabled()) {
086                    LOG.debug("Disconnecting from: " + XmppEndpoint.getConnectionMessage(connection));
087                }
088                connection.disconnect();
089                connection = null;
090            }
091        }
092    
093        public void processPacket(Packet packet) {
094            Message message = (Message)packet;
095    
096            if (LOG.isDebugEnabled()) {
097                LOG.debug("Recieved XMPP message: " + message.getBody());
098            }
099    
100            XmppExchange exchange = endpoint.createExchange(message);
101            try {
102                getProcessor().process(exchange);
103                if (muc != null) {
104                    // must invoke nextMessage to consume the response from the server
105                    // otherwise the client local queue will fill up (CAMEL-1467)
106                    muc.nextMessage();
107                }
108            } catch (Exception e) {
109                LOG.error("Error while processing XMPP message", e);
110            }
111        }
112    
113        public void processMessage(Chat chat, Message message) {
114            processPacket(message);
115        }
116    
117    }