camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r682466 - in /activemq/camel/trunk/components/camel-xmpp/src: main/java/org/apache/camel/component/xmpp/ test/java/org/apache/camel/component/xmpp/
Date Mon, 04 Aug 2008 19:05:42 GMT
Author: davsclaus
Date: Mon Aug  4 12:05:41 2008
New Revision: 682466

URL: http://svn.apache.org/viewvc?rev=682466&view=rev
Log:
CAMEL-757: Applied patch with thanks to Vadim.

Modified:
    activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppBinding.java
    activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppConsumer.java
    activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java
    activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppExchange.java
    activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppGroupChatProducer.java
    activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppMessage.java
    activemq/camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteTest.java

Modified: activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppBinding.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppBinding.java?rev=682466&r1=682465&r2=682466&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppBinding.java
(original)
+++ activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppBinding.java
Mon Aug  4 12:05:41 2008
@@ -20,7 +20,6 @@
 import java.util.Set;
 
 import org.apache.camel.Exchange;
-
 import org.jivesoftware.smack.packet.Message;
 
 /**
@@ -30,6 +29,7 @@
  * @version $Revision$
  */
 public class XmppBinding {
+
     /**
      * Populates the given XMPP message from the inbound exchange
      */
@@ -52,9 +52,6 @@
 
     /**
      * Extracts the body from the XMPP message
-     *
-     * @param exchange
-     * @param message
      */
     public Object extractBodyFromXmpp(XmppExchange exchange, Message message) {
         return message.getBody();

Modified: activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppConsumer.java?rev=682466&r1=682465&r2=682466&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppConsumer.java
(original)
+++ activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppConsumer.java
Mon Aug  4 12:05:41 2008
@@ -16,17 +16,17 @@
  */
 package org.apache.camel.component.xmpp;
 
-import java.util.Iterator;
-
 import org.apache.camel.Processor;
 import org.apache.camel.impl.DefaultConsumer;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-
+import org.jivesoftware.smack.Chat;
 import org.jivesoftware.smack.PacketListener;
+import org.jivesoftware.smack.SmackConfiguration;
 import org.jivesoftware.smack.packet.Message;
 import org.jivesoftware.smack.packet.Packet;
-import org.jivesoftware.smack.packet.RosterPacket;
+import org.jivesoftware.smackx.muc.DiscussionHistory;
+import org.jivesoftware.smackx.muc.MultiUserChat;
 
 /**
  * A {@link org.apache.camel.Consumer Consumer} which listens to XMPP packets
@@ -36,6 +36,8 @@
 public class XmppConsumer extends DefaultConsumer<XmppExchange> implements PacketListener
{
     private static final transient Log LOG = LogFactory.getLog(XmppConsumer.class);
     private final XmppEndpoint endpoint;
+    private Chat privateChat;
+    private MultiUserChat muc;
 
     public XmppConsumer(XmppEndpoint endpoint, Processor processor) {
         super(endpoint, processor);
@@ -44,46 +46,43 @@
 
     @Override
     protected void doStart() throws Exception {
+        if (endpoint.getRoom() == null) {
+        	privateChat = endpoint.getConnection().createChat(endpoint.getParticipant());
+	        privateChat.addMessageListener(this);
+	        LOG.info("Open chat to " + privateChat.getParticipant());
+        } else {
+        	muc = new MultiUserChat(endpoint.getConnection(), endpoint.resolveRoom());
+        	muc.addMessageListener(this);
+        	DiscussionHistory history = new DiscussionHistory();
+        	history.setMaxChars(0); // we do not want any historical messages
+        	muc.join(endpoint.getNickname(), null, history, SmackConfiguration.getPacketReplyTimeout());
+        	LOG.info("Joined room: " + muc.getRoom());
+        }
         super.doStart();
-        endpoint.getConnection().addPacketListener(this, endpoint.getFilter());
     }
 
     @Override
     protected void doStop() throws Exception {
-        endpoint.getConnection().removePacketListener(this);
         super.doStop();
+        if (muc != null) {
+        	muc.leave();
+        	muc = null;
+        }
     }
 
     public void processPacket(Packet packet) {
+        Message message = (Message)packet;
 
-        if (packet instanceof Message) {
-            Message message = (Message)packet;
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("<<<< message: " + message.getBody());
-            }
-            XmppExchange exchange = endpoint.createExchange(message);
-            try {
-                getProcessor().process(exchange);
-            } catch (Exception e) {
-                // TODO: what should we do when a processing failure occurs??
-                e.printStackTrace();
-            }
-        } else if (packet instanceof RosterPacket) {
-            // TODO: what to do with a RosterPacket other than debug logging it
-            RosterPacket rosterPacket = (RosterPacket)packet;
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Roster packet with : " + rosterPacket.getRosterItemCount() + "
item(s)");
-                Iterator rosterItems = rosterPacket.getRosterItems();
-                while (rosterItems.hasNext()) {
-                    Object item = rosterItems.next();
-                    LOG.debug("Roster item: " + item);
-                }
-            }
-        } else {
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("<<<< ignored packet: " + packet);
-            }
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Recieved XMPP message: " + message.getBody());
+        }
 
+        XmppExchange exchange = endpoint.createExchange(message);
+        try {
+            getProcessor().process(exchange);
+        } catch (Exception e) {
+        	LOG.error("Error while processing message", e);
         }
     }
+
 }

Modified: activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java?rev=682466&r1=682465&r2=682466&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java
(original)
+++ activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java
Mon Aug  4 12:05:41 2008
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.component.xmpp;
 
+import java.util.Iterator;
+
+import org.apache.camel.CamelException;
 import org.apache.camel.Consumer;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.Processor;
@@ -26,9 +29,8 @@
 import org.jivesoftware.smack.AccountManager;
 import org.jivesoftware.smack.XMPPConnection;
 import org.jivesoftware.smack.XMPPException;
-import org.jivesoftware.smack.filter.PacketFilter;
 import org.jivesoftware.smack.packet.Message;
-import org.jivesoftware.smack.packet.Presence;
+import org.jivesoftware.smackx.muc.MultiUserChat;
 
 /**
  * A XMPP Endpoint
@@ -45,7 +47,6 @@
     private String password;
     private String resource = "Camel";
     private boolean login = true;
-    private PacketFilter filter;
     private boolean createAccount;
     private String room;
     private String participant;
@@ -61,7 +62,7 @@
 
     public Producer<XmppExchange> createProducer() throws Exception {
         if (room != null) {
-            return createGroupChatProducer(room);
+            return createGroupChatProducer();
         } else {
             if (participant == null) {
                 throw new IllegalArgumentException("No room or participant configured on
this endpoint: " + this);
@@ -70,8 +71,8 @@
         }
     }
 
-    public Producer<XmppExchange> createGroupChatProducer(String room) throws Exception
{
-        return new XmppGroupChatProducer(this, room);
+    public Producer<XmppExchange> createGroupChatProducer() throws Exception {
+        return new XmppGroupChatProducer(this);
     }
 
     public Producer<XmppExchange> createPrivateChatProducer(String participant) throws
Exception {
@@ -79,7 +80,7 @@
     }
 
     public Consumer<XmppExchange> createConsumer(Processor processor) throws Exception
{
-        return new XmppConsumer(this, processor);
+    		return new XmppConsumer(this, processor);
     }
 
     @Override
@@ -156,14 +157,6 @@
         this.login = login;
     }
 
-    public PacketFilter getFilter() {
-        return filter;
-    }
-
-    public void setFilter(PacketFilter filter) {
-        this.filter = filter;
-    }
-
     public boolean isCreateAccount() {
         return createAccount;
     }
@@ -189,7 +182,7 @@
     }
     
     public String getNickname() {
-        return nickname;
+        return nickname != null ? nickname : getUser();
     }
     
     public void setNickname(String nickname) {
@@ -241,6 +234,32 @@
         }
         return connection;
     }
+    
+    /*
+     * If there is no "@" symbol in the room, find the chat service JID and return fully
+     * qualified JID for the room as room@conference.server.domain
+     */
+    public String resolveRoom() throws XMPPException, CamelException {
+    	if (room == null) {
+    		throw new IllegalArgumentException("room is not specified");
+        }
+
+        if (room.indexOf('@', 0) != -1) {
+    		return room;
+        }
+
+        XMPPConnection conn = getConnection();
+    	Iterator<String> iterator = MultiUserChat.getServiceNames(conn).iterator();
+    	if (!iterator.hasNext()) {
+    		throw new CamelException("Can not find Multi User Chat service");
+        }
+        String chatServer = iterator.next();
+    	if (LOG.isInfoEnabled()) {
+    		LOG.info("Detected chat server: " + chatServer);
+        }
+
+        return room + "@" + chatServer;
+    }
 
     public boolean isSingleton() {
         return true;

Modified: activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppExchange.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppExchange.java?rev=682466&r1=682465&r2=682466&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppExchange.java
(original)
+++ activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppExchange.java
Mon Aug  4 12:05:41 2008
@@ -46,7 +46,6 @@
         this.binding = binding;
     }
 
-    @Override
     public XmppMessage getIn() {
         return (XmppMessage) super.getIn();
     }

Modified: activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppGroupChatProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppGroupChatProducer.java?rev=682466&r1=682465&r2=682466&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppGroupChatProducer.java
(original)
+++ activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppGroupChatProducer.java
Mon Aug  4 12:05:41 2008
@@ -16,12 +16,15 @@
  */
 package org.apache.camel.component.xmpp;
 
+import org.apache.camel.CamelException;
 import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.jivesoftware.smack.SmackConfiguration;
 import org.jivesoftware.smack.XMPPException;
 import org.jivesoftware.smack.packet.Message;
+import org.jivesoftware.smackx.muc.DiscussionHistory;
 import org.jivesoftware.smackx.muc.MultiUserChat;
 
 /**
@@ -33,10 +36,10 @@
     private final String room;
     private MultiUserChat chat;
 
-    public XmppGroupChatProducer(XmppEndpoint endpoint, String room) {
+    public XmppGroupChatProducer(XmppEndpoint endpoint) throws XMPPException, CamelException
{
         super(endpoint);
         this.endpoint = endpoint;
-        this.room = room;
+        this.room = endpoint.resolveRoom();
         if (room == null) {
             throw new IllegalArgumentException("No room property specified");
         }
@@ -50,7 +53,7 @@
 
         endpoint.getBinding().populateXmppMessage(message, exchange);
         if (LOG.isDebugEnabled()) {
-            LOG.debug(">>>> message: " + message.getBody());
+            LOG.debug("Sending XMPP message: " + message.getBody());
         }
         try {
             chat.sendMessage(message);
@@ -61,12 +64,13 @@
 
     @Override
     protected void doStart() throws Exception {
-        super.doStart();
         if (chat == null) {
             chat = new MultiUserChat(endpoint.getConnection(), room);
-            String nickname = this.endpoint.getNickname();
-            chat.join(nickname != null ? nickname : this.endpoint.getUser());
+            DiscussionHistory history = new DiscussionHistory();
+            history.setMaxChars(0); // we do not want any historical messages
+            chat.join(this.endpoint.getNickname(), null, history, SmackConfiguration.getPacketReplyTimeout());
         }
+        super.doStart();
     }
 
     @Override

Modified: activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppMessage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppMessage.java?rev=682466&r1=682465&r2=682466&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppMessage.java
(original)
+++ activemq/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppMessage.java
Mon Aug  4 12:05:41 2008
@@ -56,8 +56,6 @@
 
     /**
      * Returns the underlying XMPP message
-     * 
-     * @return the underlying XMPP message
      */
     public Message getXmppMessage() {
         return xmppMessage;

Modified: activemq/camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteTest.java?rev=682466&r1=682465&r2=682466&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteTest.java
(original)
+++ activemq/camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/XmppRouteTest.java
Mon Aug  4 12:05:41 2008
@@ -41,7 +41,6 @@
  * <p/>
  * You can overload the <b>xmpp.url</b> system property to define the jabber
connection URI
  * to something like <b>xmpp://camel@localhost/?login=false&room=</b>
- *
  * @version $Revision$
  */
 public class XmppRouteTest extends TestCase {
@@ -73,7 +72,7 @@
             //Thread.sleep(100000);
         }
     }
-
+    
     protected static boolean isXmppServerPresent() {
         if (enabled) {
             return true;
@@ -112,16 +111,20 @@
     @Override
     protected void setUp() throws Exception {
         if (isXmppServerPresent()) {
-            String uriPrefx = getUriPrefix();
-            final String uri1 = uriPrefx + "a";
-            final String uri2 = uriPrefx + "b";
+            String uriPrefix = getUriPrefix();
+            final String uri1 = uriPrefix + "&resource=camel-test-from&nickname=came-test-from";
+            final String uri2 = uriPrefix + "&resource=camel-test-to&nickname=came-test-to";
+            final String uri3 = uriPrefix + "&resource=camel-test-from-processor&nickname=came-test-from-processor";
             LOG.info("Using URI " + uri1 + " and " + uri2);
 
+            endpoint = container.getEndpoint(uri1);
+            assertNotNull("No endpoint found!", endpoint);
+
             // lets add some routes
             container.addRoutes(new RouteBuilder() {
                 public void configure() {
                     from(uri1).to(uri2);
-                    from(uri2).process(new Processor() {
+                    from(uri3).process(new Processor() {
                         public void process(Exchange e) {
                             LOG.info("Received exchange: " + e);
                             receivedExchange = (XmppExchange) e;
@@ -130,8 +133,6 @@
                     });
                 }
             });
-            endpoint = container.getEndpoint(uri1);
-            assertNotNull("No endpoint found!", endpoint);
         }
 
         container.start();
@@ -143,7 +144,7 @@
         }
         return System.getProperty("xmpp.url", "xmpp://camel@localhost/?login=false&room=").trim();
     }
-
+    
     @Override
     protected void tearDown() throws Exception {
         client.stop();



Mime
View raw message