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.Exchange;
020    import org.apache.camel.RuntimeExchangeException;
021    import org.apache.camel.impl.DefaultProducer;
022    import org.apache.camel.util.ObjectHelper;
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    import org.jivesoftware.smack.Chat;
026    import org.jivesoftware.smack.ChatManager;
027    import org.jivesoftware.smack.MessageListener;
028    import org.jivesoftware.smack.XMPPConnection;
029    import org.jivesoftware.smack.XMPPException;
030    import org.jivesoftware.smack.packet.Message;
031    
032    /**
033     * @version $Revision: 756358 $
034     */
035    public class XmppPrivateChatProducer extends DefaultProducer {
036        private static final transient Log LOG = LogFactory.getLog(XmppPrivateChatProducer.class);
037        private final XmppEndpoint endpoint;
038        private XMPPConnection connection;
039        private final String participant;
040    
041        public XmppPrivateChatProducer(XmppEndpoint endpoint, String participant) {
042            super(endpoint);
043            this.endpoint = endpoint;
044            this.participant = participant;
045            ObjectHelper.notEmpty(participant, "participant");
046        }
047    
048        public void process(Exchange exchange) {
049            String threadId = exchange.getExchangeId();
050    
051            try {
052                if (connection == null) {
053                    connection = endpoint.createConnection();
054                }
055    
056                // make sure we are connected
057                if (!connection.isConnected()) {
058                    if (LOG.isDebugEnabled()) {
059                        LOG.debug("Reconnecting to: " + XmppEndpoint.getConnectionMessage(connection));
060                    }
061                    connection.connect();
062                }
063            } catch (XMPPException e) {
064                throw new RuntimeExchangeException("Cannot connect to: "
065                        + XmppEndpoint.getConnectionMessage(connection), exchange, e);
066            }
067    
068            ChatManager chatManager = connection.getChatManager();
069            Chat chat = chatManager.getThreadChat(threadId);
070            if (chat == null) {
071                chat = chatManager.createChat(getParticipant(), threadId, new MessageListener() {
072                    public void processMessage(Chat chat, Message message) {
073                        // not here to do conversation
074                    }
075                });
076            }
077    
078            Message message = null;
079            try {
080                message = new Message();
081                message.setTo(participant);
082                message.setThread(threadId);
083                message.setType(Message.Type.normal);
084    
085                endpoint.getBinding().populateXmppMessage(message, exchange);
086    
087                if (LOG.isDebugEnabled()) {
088                    LOG.debug("Sending XMPP message: " + message.getBody());
089                }
090                chat.sendMessage(message);
091            } catch (XMPPException e) {
092                throw new RuntimeExchangeException("Cannot send XMPP message: " + message
093                        + " to: " + XmppEndpoint.getConnectionMessage(connection), exchange, e);
094            }
095        }
096    
097        @Override
098        protected void doStop() throws Exception {
099            if (connection != null) {
100                if (LOG.isDebugEnabled()) {
101                    LOG.debug("Disconnecting from: " + XmppEndpoint.getConnectionMessage(connection));
102                }
103                connection.disconnect();
104                connection = null;
105            }
106            super.doStop();
107        }
108    
109        // Properties
110        // -------------------------------------------------------------------------
111    
112        public String getParticipant() {
113            return participant;
114        }
115    }