You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ja...@apache.org on 2008/12/09 17:48:27 UTC

svn commit: r724775 - in /activemq/camel/trunk/components/camel-rss/src: main/java/org/apache/camel/component/rss/ test/java/org/apache/camel/component/rss/

Author: janstey
Date: Tue Dec  9 08:48:27 2008
New Revision: 724775

URL: http://svn.apache.org/viewvc?rev=724775&view=rev
Log:
CAMEL-1158 - Add ability to merge multiple feeds into a single feed.

Added:
    activemq/camel/trunk/components/camel-rss/src/main/java/org/apache/camel/component/rss/AggregateRssFeedCollection.java   (with props)
    activemq/camel/trunk/components/camel-rss/src/main/java/org/apache/camel/component/rss/AggregateRssFeedStrategy.java   (with props)
    activemq/camel/trunk/components/camel-rss/src/test/java/org/apache/camel/component/rss/RssCustomAggregatorTest.java   (with props)

Added: activemq/camel/trunk/components/camel-rss/src/main/java/org/apache/camel/component/rss/AggregateRssFeedCollection.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-rss/src/main/java/org/apache/camel/component/rss/AggregateRssFeedCollection.java?rev=724775&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-rss/src/main/java/org/apache/camel/component/rss/AggregateRssFeedCollection.java (added)
+++ activemq/camel/trunk/components/camel-rss/src/main/java/org/apache/camel/component/rss/AggregateRssFeedCollection.java Tue Dec  9 08:48:27 2008
@@ -0,0 +1,33 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.component.rss;
+
+import com.sun.syndication.feed.synd.SyndFeed;
+import org.apache.camel.Exchange;
+import org.apache.camel.Expression;
+import org.apache.camel.processor.aggregate.DefaultAggregationCollection;
+
+public class AggregateRssFeedCollection extends DefaultAggregationCollection {
+    public AggregateRssFeedCollection() {
+        super(new Expression() {
+            public Object evaluate(Exchange exchange) {
+                return exchange.getIn().getBody() instanceof SyndFeed;
+            }
+        }, new AggregateRssFeedStrategy());
+    }
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-rss/src/main/java/org/apache/camel/component/rss/AggregateRssFeedCollection.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-rss/src/main/java/org/apache/camel/component/rss/AggregateRssFeedStrategy.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-rss/src/main/java/org/apache/camel/component/rss/AggregateRssFeedStrategy.java?rev=724775&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-rss/src/main/java/org/apache/camel/component/rss/AggregateRssFeedStrategy.java (added)
+++ activemq/camel/trunk/components/camel-rss/src/main/java/org/apache/camel/component/rss/AggregateRssFeedStrategy.java Tue Dec  9 08:48:27 2008
@@ -0,0 +1,48 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.component.rss;
+
+import com.sun.syndication.feed.synd.SyndFeed;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.processor.aggregate.AggregationStrategy;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class AggregateRssFeedStrategy implements AggregationStrategy {
+    protected final transient Log LOG = LogFactory.getLog(AggregateRssFeedStrategy.class);    
+    
+    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
+        SyndFeed oldFeed = oldExchange.getIn().getBody(SyndFeed.class);
+        SyndFeed newFeed = newExchange.getIn().getBody(SyndFeed.class);
+        if (oldFeed != null && newFeed != null) {                
+            List oldEntries = oldFeed.getEntries();                  
+            List newEntries = newFeed.getEntries();
+            List mergedList = new ArrayList(oldEntries.size() + newEntries.size());
+            mergedList.addAll(oldEntries);
+            mergedList.addAll(newEntries);
+            oldFeed.setEntries(mergedList);    
+        } else {
+            LOG.debug("Could not merge exchanges. One body was null.");
+        }
+        return oldExchange;
+    }
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-rss/src/main/java/org/apache/camel/component/rss/AggregateRssFeedStrategy.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-rss/src/test/java/org/apache/camel/component/rss/RssCustomAggregatorTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-rss/src/test/java/org/apache/camel/component/rss/RssCustomAggregatorTest.java?rev=724775&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-rss/src/test/java/org/apache/camel/component/rss/RssCustomAggregatorTest.java (added)
+++ activemq/camel/trunk/components/camel-rss/src/test/java/org/apache/camel/component/rss/RssCustomAggregatorTest.java Tue Dec  9 08:48:27 2008
@@ -0,0 +1,93 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.rss;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Arrays;
+import java.util.Collections;
+
+import javax.naming.Context;
+
+import com.sun.syndication.feed.synd.SyndFeed;
+
+import org.apache.camel.Body;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.builder.ExpressionBuilder;
+import org.apache.camel.builder.PredicateBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.MyAggregationStrategy;
+import org.apache.camel.processor.aggregate.AggregationCollection;
+import org.apache.camel.util.jndi.JndiContext;
+
+public class RssCustomAggregatorTest extends ContextTestSupport {
+
+    public void testMergingListOfEntries() throws Exception { 
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.assertIsSatisfied();
+
+        Exchange exchange = mock.getExchanges().get(0);
+        Message in = exchange.getIn();
+        assertNotNull(in);
+        assertTrue(in.getBody() instanceof SyndFeed);
+        assertTrue(in.getHeader(RssEndpoint.HEADER_RSS_FEED) instanceof SyndFeed);
+
+        SyndFeed body = in.getBody(SyndFeed.class);
+        assertEquals(20, body.getEntries().size());
+    }
+    
+    @Override
+    protected void setUp() throws Exception {        
+        super.setUp();   
+        copy("src/test/data/rss20.xml", "target/rss20.xml");                
+    }
+    
+    private void copy(String source, String destination) throws IOException {
+        BufferedReader input =  new BufferedReader(new FileReader(new File(source)));
+        BufferedWriter output = new BufferedWriter(new FileWriter(new File(destination)));
+ 
+        String line = null; 
+        while (( line = input.readLine()) != null) {
+            output.write(line);
+        }
+        input.close();
+        output.close();       
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("rss:file:src/test/data/rss20.xml?sortEntries=true&consumer.delay=50").to("seda:temp");
+                from("rss:file:target/rss20.xml?sortEntries=true&consumer.delay=50").to("seda:temp");
+                
+                from("seda:temp").aggregate(new AggregateRssFeedCollection()).batchTimeout(5000L).to("mock:result");
+            }
+        };
+    }
+}

Propchange: activemq/camel/trunk/components/camel-rss/src/test/java/org/apache/camel/component/rss/RssCustomAggregatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native