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