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.impl.DefaultProducer;
021 import org.apache.commons.logging.Log;
022 import org.apache.commons.logging.LogFactory;
023 import org.jivesoftware.smack.Chat;
024 import org.jivesoftware.smack.XMPPException;
025 import org.jivesoftware.smack.packet.Message;
026
027 /**
028 * @version $Revision: 563665 $
029 */
030 public class XmppPrivateChatProducer extends DefaultProducer {
031 private static final transient Log LOG = LogFactory.getLog(XmppPrivateChatProducer.class);
032 private final XmppEndpoint endpoint;
033 private final String participant;
034 private Chat chat;
035
036 public XmppPrivateChatProducer(XmppEndpoint endpoint, String participant) {
037 super(endpoint);
038 this.endpoint = endpoint;
039 this.participant = participant;
040 if (participant == null) {
041 throw new IllegalArgumentException("No participant property specified");
042 }
043 }
044
045 public void process(Exchange exchange) {
046 // TODO it would be nice if we could reuse the message from the exchange
047 Message message = chat.createMessage();
048 message.setTo(participant);
049 message.setFrom(endpoint.getUser());
050 message.setThread(exchange.getExchangeId());
051 message.setType(Message.Type.NORMAL);
052
053 endpoint.getBinding().populateXmppMessage(message, exchange);
054 if (LOG.isDebugEnabled()) {
055 LOG.debug(">>>> message: " + message.getBody());
056 }
057 try {
058 chat.sendMessage(message);
059 } catch (XMPPException e) {
060 throw new RuntimeXmppException(e);
061 }
062 }
063
064 @Override
065 protected void doStart() throws Exception {
066 super.doStart();
067 if (chat == null) {
068 chat = endpoint.getConnection().createChat(getParticipant());
069 }
070 }
071
072 @Override
073 protected void doStop() throws Exception {
074 chat = null;
075 super.doStop();
076 }
077
078 // Properties
079 // -------------------------------------------------------------------------
080 public Chat getChat() {
081 return chat;
082 }
083
084 public void setChat(Chat chat) {
085 this.chat = chat;
086 }
087
088 public String getParticipant() {
089 return participant;
090 }
091 }