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: 808253 $
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 LOG.debug("Creating XmppPrivateChatProducer to participant " + participant);
047 }
048
049 public void process(Exchange exchange) {
050 try {
051 if (connection == null) {
052 connection = endpoint.createConnection();
053 }
054
055 // make sure we are connected
056 if (!connection.isConnected()) {
057 if (LOG.isDebugEnabled()) {
058 LOG.debug("Reconnecting to: " + XmppEndpoint.getConnectionMessage(connection));
059 }
060 connection.connect();
061 }
062 } catch (XMPPException e) {
063 throw new RuntimeExchangeException("Cannot connect to: "
064 + XmppEndpoint.getConnectionMessage(connection), exchange, e);
065 }
066
067 ChatManager chatManager = connection.getChatManager();
068 LOG.debug("Looking for existing chat instance with thread ID " + endpoint.getChatId());
069 Chat chat = chatManager.getThreadChat(endpoint.getChatId());
070 if (chat == null) {
071 LOG.debug("Creating new chat instance with thread ID " + endpoint.getChatId());
072 chat = chatManager.createChat(getParticipant(), endpoint.getChatId(), new MessageListener() {
073 public void processMessage(Chat chat, Message message) {
074 // not here to do conversation
075 LOG.debug("Received and discarding message from " + getParticipant() + " : " + message.getBody());
076 }
077 });
078 }
079
080 Message message = null;
081 try {
082 message = new Message();
083 message.setTo(getParticipant());
084 message.setThread(endpoint.getChatId());
085 message.setType(Message.Type.normal);
086
087 endpoint.getBinding().populateXmppMessage(message, exchange);
088
089 if (LOG.isDebugEnabled()) {
090 LOG.debug("Sending XMPP message to " + endpoint.getParticipant() + " from " + endpoint.getUser() + " : " + message.getBody());
091 }
092 chat.sendMessage(message);
093 } catch (XMPPException xmppe) {
094 throw new RuntimeExchangeException("Cannot send XMPP message: to " + endpoint.getParticipant() + " from " + endpoint.getUser() + " : " + message
095 + " to: " + XmppEndpoint.getConnectionMessage(connection), exchange, xmppe);
096 } catch (Exception e) {
097 throw new RuntimeExchangeException("Cannot send XMPP message to " + endpoint.getParticipant() + " from " + endpoint.getUser() + " : " + message
098 + " to: " + XmppEndpoint.getConnectionMessage(connection), exchange, e);
099 }
100 }
101
102 // Properties
103 // -------------------------------------------------------------------------
104
105 public String getParticipant() {
106 return participant;
107 }
108 }