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.jivesoftware.smack.Chat;
024    import org.jivesoftware.smack.ChatManager;
025    import org.jivesoftware.smack.MessageListener;
026    import org.jivesoftware.smack.XMPPConnection;
027    import org.jivesoftware.smack.XMPPException;
028    import org.jivesoftware.smack.packet.Message;
029    import org.slf4j.Logger;
030    import org.slf4j.LoggerFactory;
031    
032    /**
033     * @version 
034     */
035    public class XmppPrivateChatProducer extends DefaultProducer {
036        private static final transient Logger LOG = LoggerFactory.getLogger(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            if (LOG.isDebugEnabled()) {
048                LOG.debug("Creating XmppPrivateChatProducer to participant " + participant);
049            }
050        }
051    
052        public void process(Exchange exchange) {
053            try {
054                // make sure we are connected
055                if (!connection.isConnected()) {
056                    if (LOG.isDebugEnabled()) {
057                        LOG.debug("Reconnecting to: " + XmppEndpoint.getConnectionMessage(connection));
058                    }
059                    connection.connect();
060                }
061            } catch (XMPPException e) {
062                throw new RuntimeExchangeException("Cannot connect to: "
063                        + XmppEndpoint.getConnectionMessage(connection), exchange, e);
064            }
065    
066            ChatManager chatManager = connection.getChatManager();
067    
068            if (LOG.isTraceEnabled()) {
069                LOG.trace("Looking for existing chat instance with thread ID " + endpoint.getChatId());
070            }
071            Chat chat = chatManager.getThreadChat(endpoint.getChatId());
072            if (chat == null) {
073                if (LOG.isTraceEnabled()) {
074                    LOG.trace("Creating new chat instance with thread ID " + endpoint.getChatId());
075                }
076                chat = chatManager.createChat(getParticipant(), endpoint.getChatId(), new MessageListener() {
077                    public void processMessage(Chat chat, Message message) {
078                        // not here to do conversation
079                        if (LOG.isDebugEnabled()) {
080                            LOG.debug("Received and discarding message from " + getParticipant() + " : " + message.getBody());
081                        }
082                    }
083                });
084            }
085    
086            Message message = null;
087            try {
088                message = new Message();
089                message.setTo(getParticipant());
090                message.setThread(endpoint.getChatId());
091                message.setType(Message.Type.normal);
092    
093                endpoint.getBinding().populateXmppMessage(message, exchange);
094    
095                if (LOG.isDebugEnabled()) {
096                    LOG.debug("Sending XMPP message to " + endpoint.getParticipant() + " from " + endpoint.getUser() + " : " + message.getBody());
097                }
098                chat.sendMessage(message);
099            } catch (XMPPException xmppe) {
100                throw new RuntimeExchangeException("Cannot send XMPP message: to " + endpoint.getParticipant() + " from " + endpoint.getUser() + " : " + message
101                        + " to: " + XmppEndpoint.getConnectionMessage(connection), exchange, xmppe);
102            } catch (Exception e) {
103                throw new RuntimeExchangeException("Cannot send XMPP message to " + endpoint.getParticipant() + " from " + endpoint.getUser() + " : " + message
104                        + " to: " + XmppEndpoint.getConnectionMessage(connection), exchange, e);
105            }
106        }
107        
108        @Override
109        protected void doStart() throws Exception {
110            if (connection == null) {
111                connection = endpoint.createConnection();
112            }
113            super.doStart();
114        }
115    
116        @Override
117        protected void doStop() throws Exception {
118            if (connection != null && connection.isConnected()) {
119                connection.disconnect();
120            }
121            super.doStop();
122        }
123    
124        // Properties
125        // -------------------------------------------------------------------------
126    
127        public String getParticipant() {
128            return participant;
129        }
130    }