camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [1/2] camel git commit: [CAMEL-9139] Make reading parameter configurable via header in camel-facebook
Date Tue, 15 Sep 2015 19:05:57 GMT
Repository: camel
Updated Branches:
  refs/heads/master 1e8bb13a2 -> ea53dce2d


[CAMEL-9139] Make reading parameter configurable via header in camel-facebook


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e05b103f
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e05b103f
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e05b103f

Branch: refs/heads/master
Commit: e05b103f42461f3c49a9f4053bc217880301dccc
Parents: 1e8bb13
Author: Manuel Holzleitner <manuel.holzleitner@gmail.com>
Authored: Tue Sep 15 11:22:30 2015 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Tue Sep 15 20:56:56 2015 +0200

----------------------------------------------------------------------
 .../component/facebook/FacebookProducer.java    | 15 +++-
 .../facebook/data/FacebookPropertiesHelper.java | 11 ++-
 .../component/facebook/data/ReadingBuilder.java | 14 ++++
 .../FacebookComponentPageIdProducerTest.java    | 76 ++++++++++++++++++++
 4 files changed, 112 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e05b103f/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/FacebookProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/FacebookProducer.java
b/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/FacebookProducer.java
index 14d37f2..800147f 100644
--- a/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/FacebookProducer.java
+++ b/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/FacebookProducer.java
@@ -23,6 +23,7 @@ import java.util.Set;
 import java.util.concurrent.ExecutorService;
 
 import facebook4j.Facebook;
+import facebook4j.Reading;
 import facebook4j.json.DataObjectFactory;
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.CamelContext;
@@ -31,6 +32,7 @@ import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.component.facebook.config.FacebookEndpointConfiguration;
 import org.apache.camel.component.facebook.data.FacebookMethodsType;
 import org.apache.camel.component.facebook.data.FacebookMethodsTypeHelper;
+import org.apache.camel.component.facebook.data.FacebookPropertiesHelper;
 import org.apache.camel.impl.DefaultAsyncProducer;
 import org.apache.camel.spi.ExecutorServiceManager;
 import org.apache.camel.spi.ThreadPoolProfile;
@@ -64,8 +66,10 @@ public class FacebookProducer extends DefaultAsyncProducer {
     public boolean process(final Exchange exchange, final AsyncCallback callback) {
         // properties for method arguments
         final Map<String, Object> properties = new HashMap<String, Object>();
-        getEndpointProperties(endpoint.getConfiguration(), properties);
+
         getExchangeProperties(exchange, properties);
+        FacebookPropertiesHelper.configureReadingProperties(endpoint.getConfiguration(),
properties);
+        getEndpointProperties(endpoint.getConfiguration(), properties);
 
         // decide which method to invoke
         final FacebookMethodsType method = findMethod(exchange, properties);
@@ -123,6 +127,15 @@ public class FacebookProducer extends DefaultAsyncProducer {
         return false;
     }
 
+    private boolean hasReadingParameters(Map<String, Object> properties) {
+        for (String parameterName : properties.keySet()) {
+            if (parameterName.startsWith(FacebookConstants.READING_PREFIX)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     private FacebookMethodsType findMethod(Exchange exchange, Map<String, Object> properties)
{
 
         FacebookMethodsType method = null;

http://git-wip-us.apache.org/repos/asf/camel/blob/e05b103f/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/data/FacebookPropertiesHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/data/FacebookPropertiesHelper.java
b/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/data/FacebookPropertiesHelper.java
index 39505a1..5911705 100644
--- a/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/data/FacebookPropertiesHelper.java
+++ b/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/data/FacebookPropertiesHelper.java
@@ -73,10 +73,15 @@ public final class FacebookPropertiesHelper {
                 // add to an existing reading reference?
                 // NOTE Reading class does not support overwriting properties!!!
                 Reading reading = configuration.getReading();
-                if (reading == null) {
-                    reading = new Reading();
+
+                if (reading != null) {
+                    Reading readingUpdate = new Reading();
+                    ReadingBuilder.setProperties(readingUpdate, readingProperties);
+
+                    reading = ReadingBuilder.merge(reading, readingUpdate);
                 } else {
-                    reading = ReadingBuilder.copy(reading, false);
+                    reading = new Reading();
+                    ReadingBuilder.setProperties(reading, readingProperties);
                 }
                 // set properties
                 ReadingBuilder.setProperties(reading,

http://git-wip-us.apache.org/repos/asf/camel/blob/e05b103f/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/data/ReadingBuilder.java
----------------------------------------------------------------------
diff --git a/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/data/ReadingBuilder.java
b/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/data/ReadingBuilder.java
index 30854e1..50afefb 100644
--- a/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/data/ReadingBuilder.java
+++ b/components/camel-facebook/src/main/java/org/apache/camel/component/facebook/data/ReadingBuilder.java
@@ -20,6 +20,7 @@ import java.lang.reflect.Field;
 import java.util.LinkedHashMap;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Objects;
 
 import facebook4j.Reading;
 
@@ -112,4 +113,17 @@ public final class ReadingBuilder {
         }
     }
 
+    public static Reading merge(Reading reading, Reading readingUpdate) throws NoSuchFieldException,
IllegalAccessException {
+        Reading mergedReading = new Reading();
+
+        Field field = Reading.class.getDeclaredField("parameterMap");
+        field.setAccessible(true);
+        final LinkedHashMap<String, Object> readingParameters = (LinkedHashMap<String,
Object>) field.get(reading);
+        readingParameters.putAll((LinkedHashMap<String, Object>) field.get(readingUpdate));
+        field.setAccessible(false);
+
+        setProperties(mergedReading, readingParameters);
+
+        return mergedReading;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/e05b103f/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentPageIdProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentPageIdProducerTest.java
b/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentPageIdProducerTest.java
new file mode 100644
index 0000000..d099d90
--- /dev/null
+++ b/components/camel-facebook/src/test/java/org/apache/camel/component/facebook/FacebookComponentPageIdProducerTest.java
@@ -0,0 +1,76 @@
+/**
+ * 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
+ * <p/>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p/>
+ * 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.facebook;
+
+import facebook4j.Post;
+import facebook4j.ResponseList;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+public class FacebookComponentPageIdProducerTest extends CamelFacebookTestSupport {
+    public static final String APACHE_FOUNDATION_PAGE_ID = "6538157161";
+
+    public FacebookComponentPageIdProducerTest() throws Exception {
+    }
+
+    long lastTimestamp = -1;
+
+    @Test
+    public void testProducers() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:page");
+        mock.expectedMinimumMessageCount(3);
+        mock.assertIsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() {
+                from("timer:period=20000")
+                    .setHeader("CamelFacebook.reading.limit", constant("10"))
+                    .process(new Processor() {
+                        @Override
+                        public void process(Exchange exchange) throws Exception {
+                            if (lastTimestamp > 0) {
+                                exchange.getIn().setHeader("CamelFacebook.reading.since",
lastTimestamp);
+                            }
+                        }
+                    })
+                    .to("facebook://getPosts?" + getOauthParams() + "&userId=" + APACHE_FOUNDATION_PAGE_ID
+ "&reading.limit=5")
+                    .process(new Processor() {
+                        @Override
+                        public void process(Exchange exchange) throws Exception {
+                            ResponseList<Post> body = (ResponseList<Post>) exchange.getIn().getBody();
+                            log.info("Number of posts received: {}", body.size());
+                            for (Post post : body) {
+                                log.debug(post.toString());
+                            }
+
+                            if (!body.isEmpty()) {
+                                lastTimestamp = body.get(0).getUpdatedTime().getTime();
+                            }
+                        }
+                    })
+                    .to("mock:page");
+            }
+        };
+    }
+
+}


Mime
View raw message