001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.xmpp;
019    
020    import org.apache.camel.Exchange;
021    import org.apache.camel.impl.DefaultProducer;
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    import org.jivesoftware.smack.Chat;
025    import org.jivesoftware.smack.XMPPException;
026    import org.jivesoftware.smack.packet.Message;
027    
028    /**
029     * @version $Revision: 534145 $
030     */
031    public class XmppPrivateChatProducer extends DefaultProducer {
032        private static final transient Log log = LogFactory.getLog(XmppPrivateChatProducer.class);
033        private final XmppEndpoint endpoint;
034        private final String participant;
035        private Chat chat;
036    
037        public XmppPrivateChatProducer(XmppEndpoint endpoint, String participant) {
038            super(endpoint);
039            this.endpoint = endpoint;
040            this.participant = participant;
041            if (participant == null) {
042                throw new IllegalArgumentException("No participant property specified");
043            }
044        }
045    
046        public void process(Exchange exchange) {
047            // TODO it would be nice if we could reuse the message from the exchange
048            Message message = chat.createMessage();
049            message.setTo(participant);
050            message.setFrom(endpoint.getUser());
051            message.setThread(exchange.getExchangeId());
052            message.setType(Message.Type.NORMAL);
053    
054            endpoint.getBinding().populateXmppMessage(message, exchange);
055            if (log.isDebugEnabled()) {
056                log.debug(">>>> message: " + message.getBody());
057            }
058            try {
059                chat.sendMessage(message);
060            }
061            catch (XMPPException e) {
062                throw new RuntimeXmppException(e);
063            }
064        }
065    
066        @Override
067        protected void doStart() throws Exception {
068            super.doStart();
069            if (chat == null) {
070                chat = endpoint.getConnection().createChat(getParticipant());
071            }
072        }
073    
074        @Override
075        protected void doStop() throws Exception {
076            chat = null;
077            super.doStop();
078        }
079    
080        // Properties
081        //-------------------------------------------------------------------------
082        public Chat getChat() {
083            return chat;
084        }
085    
086        public void setChat(Chat chat) {
087            this.chat = chat;
088        }
089    
090        public String getParticipant() {
091            return participant;
092        }
093    }