You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/02/12 11:02:27 UTC

svn commit: r743674 - in /camel/trunk/components/camel-xmpp/src: main/java/org/apache/camel/component/xmpp/ test/java/org/apache/camel/component/xmpp/

Author: davsclaus
Date: Thu Feb 12 10:02:26 2009
New Revision: 743674

URL: http://svn.apache.org/viewvc?rev=743674&view=rev
Log:
CAMEL-505: xmpp endpoint can now be created spring bean style.

Added:
    camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/GoogleTalkEndpointTest.java   (contents, props changed)
      - copied, changed from r743658, camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/GoogleTalkTest.java
Modified:
    camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/RuntimeXmppException.java
    camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppBinding.java
    camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppComponent.java
    camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java

Modified: camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/RuntimeXmppException.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/RuntimeXmppException.java?rev=743674&r1=743673&r2=743674&view=diff
==============================================================================
--- camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/RuntimeXmppException.java (original)
+++ camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/RuntimeXmppException.java Thu Feb 12 10:02:26 2009
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.xmpp;
 
+import org.apache.camel.RuntimeCamelException;
 import org.jivesoftware.smack.XMPPException;
 
 /**
@@ -23,12 +24,12 @@
  *
  * @version $Revision:520964 $
  */
-public class RuntimeXmppException extends RuntimeException {
-    private static final long serialVersionUID = -2141493732308871761L;
+public class RuntimeXmppException extends RuntimeCamelException {
 
     public RuntimeXmppException(XMPPException cause) {
         super(cause);
     }
+
     public RuntimeXmppException(String message, XMPPException cause) {
         super(message, cause);
     }

Modified: camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppBinding.java?rev=743674&r1=743673&r2=743674&view=diff
==============================================================================
--- camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppBinding.java (original)
+++ camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppBinding.java Thu Feb 12 10:02:26 2009
@@ -23,6 +23,7 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultHeaderFilterStrategy;
 import org.apache.camel.spi.HeaderFilterStrategy;
+import org.apache.camel.util.ObjectHelper;
 import org.jivesoftware.smack.packet.Message;
 
 /**
@@ -40,6 +41,7 @@
     }
 
     public XmppBinding(HeaderFilterStrategy headerFilterStrategy) {
+        ObjectHelper.notNull(headerFilterStrategy, "headerFilterStrategy");
         this.headerFilterStrategy = headerFilterStrategy;
     }
 
@@ -53,12 +55,11 @@
         for (Map.Entry<String, Object> entry : entries) {
             String name = entry.getKey();
             Object value = entry.getValue();
-            // BUG?
-            if (headerFilterStrategy != null
-                    && !headerFilterStrategy.applyFilterToCamelHeaders(name, value)) {
+            if (!headerFilterStrategy.applyFilterToCamelHeaders(name, value)) {
                 message.setProperty(name, value);
             }
         }
+        
         String id = exchange.getExchangeId();
         if (id != null) {
             message.setProperty("exchangeId", id);
@@ -78,11 +79,11 @@
         for (String name : xmppMessage.getPropertyNames()) {
             Object value = xmppMessage.getProperty(name);
 
-            if (headerFilterStrategy != null
-                    && !headerFilterStrategy.applyFilterToExternalHeaders(name, value)) {
+            if (!headerFilterStrategy.applyFilterToExternalHeaders(name, value)) {
                 answer.put(name, value);
             }
         }
+
         return answer;
     }
 }

Modified: camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppComponent.java?rev=743674&r1=743673&r2=743674&view=diff
==============================================================================
--- camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppComponent.java (original)
+++ camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppComponent.java Thu Feb 12 10:02:26 2009
@@ -19,33 +19,13 @@
 import java.net.URI;
 import java.util.Map;
 
-import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
-import org.apache.camel.HeaderFilterStrategyAware;
 import org.apache.camel.impl.DefaultComponent;
-import org.apache.camel.impl.DefaultHeaderFilterStrategy;
-import org.apache.camel.spi.HeaderFilterStrategy;
 
 /**
  * @version $Revision:520964 $
  */
-public class XmppComponent extends DefaultComponent implements HeaderFilterStrategyAware {
-
-    private HeaderFilterStrategy headerFilterStrategy = new DefaultHeaderFilterStrategy();
-
-    public XmppComponent() {
-    }
-
-    public XmppComponent(CamelContext context) {
-        super(context);
-    }
-
-    /**
-     * Static builder method
-     */
-    public static XmppComponent xmppComponent() {
-        return new XmppComponent();
-    }
+public class XmppComponent extends DefaultComponent {
 
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
@@ -68,15 +48,8 @@
                 endpoint.setParticipant(remainingPath);
             }
         }
+        
         return endpoint;
     }
 
-    public HeaderFilterStrategy getHeaderFilterStrategy() {
-        return headerFilterStrategy;
-    }
-
-    public void setHeaderFilterStrategy(HeaderFilterStrategy strategy) {
-        headerFilterStrategy = strategy;
-        
-    }
 }

Modified: camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java?rev=743674&r1=743673&r2=743674&view=diff
==============================================================================
--- camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java (original)
+++ camel/trunk/components/camel-xmpp/src/main/java/org/apache/camel/component/xmpp/XmppEndpoint.java Thu Feb 12 10:02:26 2009
@@ -18,13 +18,15 @@
 
 import java.util.Iterator;
 
-import org.apache.camel.CamelException;
 import org.apache.camel.Consumer;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
+import org.apache.camel.HeaderFilterStrategyAware;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.impl.DefaultEndpoint;
+import org.apache.camel.impl.DefaultHeaderFilterStrategy;
+import org.apache.camel.spi.HeaderFilterStrategy;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.jivesoftware.smack.AccountManager;
@@ -39,8 +41,9 @@
  *
  * @version $Revision:520964 $
  */
-public class XmppEndpoint extends DefaultEndpoint {
+public class XmppEndpoint extends DefaultEndpoint implements HeaderFilterStrategyAware {
     private static final transient Log LOG = LogFactory.getLog(XmppEndpoint.class);
+    private HeaderFilterStrategy headerFilterStrategy = new DefaultHeaderFilterStrategy();
     private XmppBinding binding;
     private XMPPConnection connection;
     private String host;
@@ -55,9 +58,11 @@
     private String nickname;
     private String serviceName;
 
+    public XmppEndpoint() {
+    }
+
     public XmppEndpoint(String uri, XmppComponent component) {
         super(uri, component);
-        binding = new XmppBinding(component.getHeaderFilterStrategy());
     }
 
     public XmppEndpoint(String endpointUri) {
@@ -69,8 +74,7 @@
             return createGroupChatProducer();
         } else {
             if (participant == null) {
-                throw new IllegalArgumentException("No room or participant configured on this endpoint: "
-                                                   + this);
+                throw new IllegalArgumentException("No room or participant configured on this endpoint: " + this);
             }
             return createPrivateChatProducer(participant);
         }
@@ -97,11 +101,20 @@
         return new XmppExchange(this, getExchangePattern(), getBinding(), message);
     }
 
+    @Override
+    protected String createEndpointUri() {
+        return "xmpp://" + host + ":" + port + "/" + participant + "?serviceName=" + serviceName;
+    }
+
+    public boolean isSingleton() {
+        return true;
+    }
+    
     // Properties
     // -------------------------------------------------------------------------
     public XmppBinding getBinding() {
         if (binding == null) {
-            binding = new XmppBinding();
+            binding = new XmppBinding(headerFilterStrategy);
         }
         return binding;
     }
@@ -213,6 +226,14 @@
         this.connection = connection;
     }
 
+    public HeaderFilterStrategy getHeaderFilterStrategy() {
+        return headerFilterStrategy;
+    }
+
+    public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
+        this.headerFilterStrategy = headerFilterStrategy;
+    }
+
     // Implementation methods
     // -------------------------------------------------------------------------
     protected XMPPConnection createConnection() throws XMPPException {
@@ -252,6 +273,7 @@
 
             // presence is not needed to be sent after login
         }
+
         return connection;
     }
 
@@ -259,7 +281,7 @@
      * 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 {
+    public String resolveRoom() throws XMPPException {
         if (room == null) {
             throw new IllegalArgumentException("room is not specified");
         }
@@ -271,17 +293,12 @@
         XMPPConnection conn = getConnection();
         Iterator<String> iterator = MultiUserChat.getServiceNames(conn).iterator();
         if (!iterator.hasNext()) {
-            throw new CamelException("Cannot find Multi User Chat service");
+            throw new XMPPException("Cannot find Multi User Chat service");
         }
         String chatServer = iterator.next();
-        if (LOG.isInfoEnabled()) {
-            LOG.info("Detected chat server: " + chatServer);
-        }
+        LOG.info("Detected chat server: " + chatServer);
 
         return room + "@" + chatServer;
     }
 
-    public boolean isSingleton() {
-        return true;
-    }
 }

Copied: camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/GoogleTalkEndpointTest.java (from r743658, camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/GoogleTalkTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/GoogleTalkEndpointTest.java?p2=camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/GoogleTalkEndpointTest.java&p1=camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/GoogleTalkTest.java&r1=743658&r2=743674&rev=743674&view=diff
==============================================================================
--- camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/GoogleTalkTest.java (original)
+++ camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/GoogleTalkEndpointTest.java Thu Feb 12 10:02:26 2009
@@ -16,32 +16,28 @@
  */
 package org.apache.camel.component.xmpp;
 
-import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.mock.MockEndpoint;
 
-public class GoogleTalkTest extends ContextTestSupport {
-    // a disabled test... before enabling you must fill in your own gmail credentials in the route below
-    public void xtestSendToGTalk() throws Exception {
-        MockEndpoint result = getMockEndpoint("mock:result");
-        template.sendBody("direct:start", "Hi!");
-        result.assertIsSatisfied();
-    }
-    
-    // get around junit warning
-    public void testNothing() throws Exception {        
-    }
+public class GoogleTalkEndpointTest extends GoogleTalkTest {
 
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
-            public void configure() {
-                // START SNIPPET: e1
-                // send a message from fromuser@gmail.com to touser@gmail.com
+            public void configure() throws Exception {
+                XmppEndpoint endpoint = new XmppEndpoint();
+                endpoint.setCamelContext(context);
+                endpoint.setHost("talk.google.com");
+                endpoint.setPort(5222);
+                endpoint.setUser("user");
+                endpoint.setPassword("secret");
+                endpoint.setServiceName("gmail.com");
+                endpoint.setParticipant("touser@gmail.com");
+
+                context.addEndpoint("talk", endpoint);
+
                 from("direct:start").
-                    to("xmpp://talk.google.com:5222/touser@gmail.com?serviceName=gmail.com&user=fromuser&password=secret").
+                    to("talk").
                     to("mock:result");
-                // END SNIPPET: e1
             }
         };
     }
-}
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/GoogleTalkEndpointTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/GoogleTalkEndpointTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: camel/trunk/components/camel-xmpp/src/test/java/org/apache/camel/component/xmpp/GoogleTalkEndpointTest.java
------------------------------------------------------------------------------
    svn:mergeinfo =