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 LOG.debug("Creating XmppPrivateChatProducer to participant {}", participant);
048 }
049
050 public void process(Exchange exchange) {
051 try {
052 // make sure we are connected
053 if (!connection.isConnected()) {
054 if (LOG.isDebugEnabled()) {
055 LOG.debug("Reconnecting to: {}", XmppEndpoint.getConnectionMessage(connection));
056 }
057 connection.connect();
058 }
059 } catch (XMPPException e) {
060 throw new RuntimeExchangeException("Cannot connect to: "
061 + XmppEndpoint.getConnectionMessage(connection), exchange, e);
062 }
063
064 ChatManager chatManager = connection.getChatManager();
065
066 LOG.trace("Looking for existing chat instance with thread ID {}", endpoint.getChatId());
067 Chat chat = chatManager.getThreadChat(endpoint.getChatId());
068 if (chat == null) {
069 LOG.trace("Creating new chat instance with thread ID {}", endpoint.getChatId());
070 chat = chatManager.createChat(getParticipant(), endpoint.getChatId(), new MessageListener() {
071 public void processMessage(Chat chat, Message message) {
072 // not here to do conversation
073 if (LOG.isDebugEnabled()) {
074 LOG.debug("Received and discarding message from {} : {}", getParticipant(), message.getBody());
075 }
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 {} from {} : {}", new Object[]{endpoint.getParticipant(), 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 @Override
103 protected void doStart() throws Exception {
104 if (connection == null) {
105 connection = endpoint.createConnection();
106 }
107 super.doStart();
108 }
109
110 @Override
111 protected void doStop() throws Exception {
112 if (connection != null && connection.isConnected()) {
113 connection.disconnect();
114 }
115 connection = null;
116 super.doStop();
117 }
118
119 // Properties
120 // -------------------------------------------------------------------------
121
122 public String getParticipant() {
123 return participant;
124 }
125 }