Repository: camel Updated Branches: refs/heads/camel-2.19.x 1bd593b70 -> 7b23f1189
CAMEL-11280: camel-twitter : hard-coded component scheme Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/7b23f118 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/7b23f118 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/7b23f118 Branch: refs/heads/camel-2.19.x Commit: 7b23f11896edeab1fcd6c12bf0d1036a5f8eeabd Parents: 1bd593b Author: lburgazzoli <lburgazz...@gmail.com> Authored: Mon May 15 11:31:13 2017 +0200 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Mon May 15 13:40:22 2017 +0200 ---------------------------------------------------------------------- .../component/twitter/TwitterComponent.java | 18 ++++--- .../twitter/TwitterEndpointDirect.java | 9 ++-- .../component/twitter/TwitterEndpointEvent.java | 6 ++- .../twitter/TwitterEndpointPolling.java | 11 ++-- .../camel/component/twitter/TwitterHelper.java | 56 ++++++++------------ .../component/twitter/data/ConsumerType.java | 2 +- .../component/twitter/data/EndpointType.java | 2 +- .../component/twitter/data/StreamingType.java | 2 +- .../component/twitter/data/TimelineType.java | 2 +- .../component/twitter/data/TrendsType.java | 2 +- .../CamelTwitterConsumerTestSupport.java | 3 +- .../SearchDirectCustomComponentTest.java | 45 ++++++++++++++++ 12 files changed, 102 insertions(+), 56 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/7b23f118/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterComponent.java b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterComponent.java index 291d407..3672501 100644 --- a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterComponent.java +++ b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterComponent.java @@ -18,17 +18,18 @@ package org.apache.camel.component.twitter; import java.util.Map; +import org.apache.camel.CamelContext; import org.apache.camel.ComponentVerifier; import org.apache.camel.Endpoint; import org.apache.camel.VerifiableComponent; -import org.apache.camel.impl.UriEndpointComponent; +import org.apache.camel.impl.DefaultComponent; import org.apache.camel.spi.Metadata; /** * Twitter component */ @Metadata(label = "verifiers", enums = "parameters,connectivity") -public class TwitterComponent extends UriEndpointComponent implements VerifiableComponent { +public class TwitterComponent extends DefaultComponent implements VerifiableComponent { @Metadata(label = "security", secret = true) private String consumerKey; @@ -48,7 +49,10 @@ public class TwitterComponent extends UriEndpointComponent implements Verifiable private Integer httpProxyPort; public TwitterComponent() { - super(TwitterEndpointEvent.class); + } + + public TwitterComponent(CamelContext context) { + super(context); } protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { @@ -74,13 +78,13 @@ public class TwitterComponent extends UriEndpointComponent implements Verifiable switch (properties.getType()) { case POLLING: - endpoint = new TwitterEndpointPolling(uri, this, properties); + endpoint = new TwitterEndpointPolling(uri, remaining, this, properties); break; case EVENT: - endpoint = new TwitterEndpointEvent(uri, this, properties); + endpoint = new TwitterEndpointEvent(uri, remaining, this, properties); break; default: - endpoint = new TwitterEndpointDirect(uri, this, properties); + endpoint = new TwitterEndpointDirect(uri, remaining, this, properties); break; } return endpoint; @@ -175,7 +179,7 @@ public class TwitterComponent extends UriEndpointComponent implements Verifiable } /** - * TODO: document + * Get a verifier for the twitter component. */ public ComponentVerifier getVerifier() { return new TwitterComponentVerifier(this); http://git-wip-us.apache.org/repos/asf/camel/blob/7b23f118/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointDirect.java ---------------------------------------------------------------------- diff --git a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointDirect.java b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointDirect.java index a3f2254..52f5594 100644 --- a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointDirect.java +++ b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointDirect.java @@ -31,19 +31,20 @@ import org.apache.camel.component.twitter.data.EndpointType; * Twitter direct endpoint */ public class TwitterEndpointDirect extends DirectEndpoint implements TwitterEndpoint { + private final String remaining; // only TwitterEndpointPolling is annotated - private TwitterConfiguration properties; - public TwitterEndpointDirect(String uri, TwitterComponent component, TwitterConfiguration properties) { + public TwitterEndpointDirect(String uri, String remaining, TwitterComponent component, TwitterConfiguration properties) { super(uri, component); + this.remaining = remaining; this.properties = properties; } @Override public Consumer createConsumer(Processor processor) throws Exception { - TwitterConsumer twitter4jConsumer = TwitterHelper.createConsumer(this, getEndpointUri()); + TwitterConsumer twitter4jConsumer = TwitterHelper.createConsumer(this, getEndpointUri(), remaining); TwitterConsumerDirect answer = new TwitterConsumerDirect(this, processor, twitter4jConsumer); configureConsumer(answer); return answer; @@ -51,7 +52,7 @@ public class TwitterEndpointDirect extends DirectEndpoint implements TwitterEndp @Override public Producer createProducer() throws Exception { - return TwitterHelper.createProducer(this, getEndpointUri()); + return TwitterHelper.createProducer(this, getEndpointUri(), remaining); } @ManagedAttribute http://git-wip-us.apache.org/repos/asf/camel/blob/7b23f118/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointEvent.java ---------------------------------------------------------------------- diff --git a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointEvent.java b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointEvent.java index 0c49e17..c9edc40 100644 --- a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointEvent.java +++ b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointEvent.java @@ -25,18 +25,20 @@ import org.apache.camel.component.twitter.data.EndpointType; import org.apache.camel.impl.DefaultEndpoint; public class TwitterEndpointEvent extends DefaultEndpoint implements TwitterEndpoint { + private final String remaining; // only TwitterEndpointPolling is annotated private TwitterConfiguration properties; - public TwitterEndpointEvent(String uri, TwitterComponent component, TwitterConfiguration properties) { + public TwitterEndpointEvent(String uri, String remaining, TwitterComponent component, TwitterConfiguration properties) { super(uri, component); + this.remaining = remaining; this.properties = properties; } @Override public Consumer createConsumer(Processor processor) throws Exception { - TwitterConsumer twitter4jConsumer = TwitterHelper.createConsumer(this, getEndpointUri()); + TwitterConsumer twitter4jConsumer = TwitterHelper.createConsumer(this, getEndpointUri(), remaining); return new TwitterConsumerEvent(this, processor, twitter4jConsumer); } http://git-wip-us.apache.org/repos/asf/camel/blob/7b23f118/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointPolling.java ---------------------------------------------------------------------- diff --git a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointPolling.java b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointPolling.java index 0cad28f..255757e 100644 --- a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointPolling.java +++ b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterEndpointPolling.java @@ -34,6 +34,7 @@ import org.apache.camel.spi.UriParam; @ManagedResource(description = "Managed Twitter Endpoint") @UriEndpoint(firstVersion = "2.10.0", scheme = "twitter", title = "Twitter", syntax = "twitter:kind", consumerClass = TwitterConsumer.class, label = "api,social") public class TwitterEndpointPolling extends DefaultPollingEndpoint implements TwitterEndpoint { + private final String remaining; @UriParam(optionalPrefix = "consumer.", defaultValue = "" + TwitterConsumerPolling.DEFAULT_CONSUMER_DELAY, label = "consumer,scheduler", description = "Milliseconds before the next poll.") @@ -42,15 +43,17 @@ public class TwitterEndpointPolling extends DefaultPollingEndpoint implements Tw @UriParam private TwitterConfiguration properties; - public TwitterEndpointPolling(String uri, TwitterComponent component, TwitterConfiguration properties) { + public TwitterEndpointPolling(String uri, String remaining, TwitterComponent component, TwitterConfiguration properties) { super(uri, component); - setDelay(delay); // reconfigure the default delay + this.remaining = remaining; this.properties = properties; + + setDelay(delay); // reconfigure the default delay } @Override public Consumer createConsumer(Processor processor) throws Exception { - TwitterConsumer twitter4jConsumer = TwitterHelper.createConsumer(this, getEndpointUri()); + TwitterConsumer twitter4jConsumer = TwitterHelper.createConsumer(this, getEndpointUri(), remaining); // update the pulling lastID with sinceId twitter4jConsumer.setLastId(properties.getSinceId()); TwitterConsumerPolling tc = new TwitterConsumerPolling(this, processor, twitter4jConsumer); @@ -60,7 +63,7 @@ public class TwitterEndpointPolling extends DefaultPollingEndpoint implements Tw @Override public Producer createProducer() throws Exception { - return TwitterHelper.createProducer(this, getEndpointUri()); + return TwitterHelper.createProducer(this, getEndpointUri(), remaining); } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/7b23f118/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterHelper.java ---------------------------------------------------------------------- diff --git a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterHelper.java b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterHelper.java index 9266a5a..81fefa9 100644 --- a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterHelper.java +++ b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/TwitterHelper.java @@ -16,8 +16,6 @@ */ package org.apache.camel.component.twitter; -import java.util.regex.Pattern; - import org.apache.camel.Exchange; import org.apache.camel.Message; import org.apache.camel.component.twitter.consumer.TwitterConsumer; @@ -40,9 +38,6 @@ import org.apache.camel.component.twitter.producer.UserProducer; import twitter4j.User; public final class TwitterHelper { - private static final Pattern TWITTER_SCHEMA_PATTERN = Pattern.compile("twitter:(//)*"); - private static final Pattern TWITTER_OPTIONS_PATTERN = Pattern.compile("\\?.*"); - private TwitterHelper() { } @@ -63,11 +58,11 @@ public final class TwitterHelper { message.setHeader(TwitterConstants.TWITTER_USER_ROLE + index, role); } - public static TwitterConsumer createConsumer(TwitterEndpoint te, String uri) throws IllegalArgumentException { - String[] uriSplit = splitUri(uri); + public static TwitterConsumer createConsumer(TwitterEndpoint te, String uri, String remaining) throws IllegalArgumentException { + String[] tokens = remaining.split("/"); - if (uriSplit.length > 0) { - switch (ConsumerType.fromUri(uriSplit[0])) { + if (tokens.length > 0) { + switch (ConsumerType.fromString(tokens[0])) { case DIRECTMESSAGE: return new DirectMessageConsumer(te); case SEARCH: @@ -79,20 +74,22 @@ public final class TwitterHelper { return new SearchConsumer(te); } case STREAMING: - switch (StreamingType.fromUri(uriSplit[1])) { - case SAMPLE: - return new SampleStreamingConsumer(te); - case FILTER: - return new FilterStreamingConsumer(te); - case USER: - return new UserStreamingConsumer(te); - default: - break; + if (tokens.length > 1) { + switch (StreamingType.fromString(tokens[1])) { + case SAMPLE: + return new SampleStreamingConsumer(te); + case FILTER: + return new FilterStreamingConsumer(te); + case USER: + return new UserStreamingConsumer(te); + default: + break; + } } break; case TIMELINE: - if (uriSplit.length > 1) { - switch (TimelineType.fromUri(uriSplit[1])) { + if (tokens.length > 1) { + switch (TimelineType.fromString(tokens[1])) { case HOME: return new HomeConsumer(te); case MENTIONS: @@ -119,11 +116,11 @@ public final class TwitterHelper { + ". A consumer type was not provided (or an incorrect pairing was used)."); } - public static TwitterProducer createProducer(TwitterEndpoint te, String uri) throws IllegalArgumentException { - String[] uriSplit = splitUri(uri); + public static TwitterProducer createProducer(TwitterEndpoint te, String uri, String remaining) throws IllegalArgumentException { + String[] tokens = remaining.split("/"); - if (uriSplit.length > 0) { - switch (ConsumerType.fromUri(uriSplit[0])) { + if (tokens.length > 0) { + switch (ConsumerType.fromString(tokens[0])) { case DIRECTMESSAGE: if (te.getProperties().getUser() == null || te.getProperties().getUser().trim().isEmpty()) { throw new IllegalArgumentException( @@ -132,8 +129,8 @@ public final class TwitterHelper { return new DirectMessageProducer(te); } case TIMELINE: - if (uriSplit.length > 1) { - switch (TimelineType.fromUri(uriSplit[1])) { + if (tokens.length > 1) { + switch (TimelineType.fromString(tokens[1])) { case USER: return new UserProducer(te); default: @@ -153,13 +150,6 @@ public final class TwitterHelper { + ". A producer type was not provided (or an incorrect pairing was used)."); } - private static String[] splitUri(String uri) { - uri = TWITTER_SCHEMA_PATTERN.matcher(uri).replaceAll(""); - uri = TWITTER_OPTIONS_PATTERN.matcher(uri).replaceAll(""); - - return uri.split("/"); - } - public static <T extends Enum<T>> T enumFromString(T[] values, String uri, T defaultValue) { for (int i = values.length - 1; i >= 0; i--) { if (values[i].name().equalsIgnoreCase(uri)) { http://git-wip-us.apache.org/repos/asf/camel/blob/7b23f118/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/ConsumerType.java ---------------------------------------------------------------------- diff --git a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/ConsumerType.java b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/ConsumerType.java index 721182d..c1a8acc 100644 --- a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/ConsumerType.java +++ b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/ConsumerType.java @@ -23,7 +23,7 @@ public enum ConsumerType { private static final ConsumerType[] VALUES = values(); - public static ConsumerType fromUri(String uri) { + public static ConsumerType fromString(String uri) { return TwitterHelper.enumFromString(VALUES, uri, ConsumerType.UNKNOWN); } } http://git-wip-us.apache.org/repos/asf/camel/blob/7b23f118/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/EndpointType.java ---------------------------------------------------------------------- diff --git a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/EndpointType.java b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/EndpointType.java index 40738e0..4dca73c 100644 --- a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/EndpointType.java +++ b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/EndpointType.java @@ -23,7 +23,7 @@ public enum EndpointType { private static final EndpointType[] VALUES = values(); - public static EndpointType fromUri(String uri) { + public static EndpointType fromString(String uri) { return TwitterHelper.enumFromString(VALUES, uri, EndpointType.DIRECT); } } http://git-wip-us.apache.org/repos/asf/camel/blob/7b23f118/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/StreamingType.java ---------------------------------------------------------------------- diff --git a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/StreamingType.java b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/StreamingType.java index 02d8f00..859256c 100644 --- a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/StreamingType.java +++ b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/StreamingType.java @@ -23,7 +23,7 @@ public enum StreamingType { private static final StreamingType[] VALUES = values(); - public static StreamingType fromUri(String uri) { + public static StreamingType fromString(String uri) { return TwitterHelper.enumFromString(VALUES, uri, StreamingType.UNKNOWN); } } http://git-wip-us.apache.org/repos/asf/camel/blob/7b23f118/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/TimelineType.java ---------------------------------------------------------------------- diff --git a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/TimelineType.java b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/TimelineType.java index 9b24574..8e86b6c 100644 --- a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/TimelineType.java +++ b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/TimelineType.java @@ -23,7 +23,7 @@ public enum TimelineType { private static final TimelineType[] VALUES = values(); - public static TimelineType fromUri(String uri) { + public static TimelineType fromString(String uri) { return TwitterHelper.enumFromString(VALUES, uri, TimelineType.UNKNOWN); } } http://git-wip-us.apache.org/repos/asf/camel/blob/7b23f118/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/TrendsType.java ---------------------------------------------------------------------- diff --git a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/TrendsType.java b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/TrendsType.java index 11608c6..2c4f49a 100644 --- a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/TrendsType.java +++ b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/data/TrendsType.java @@ -23,7 +23,7 @@ public enum TrendsType { private static final TrendsType[] VALUES = values(); - public static TrendsType fromUri(String uri) { + public static TrendsType fromString(String uri) { return TwitterHelper.enumFromString(VALUES, uri, TrendsType.UNKNOWN); } } http://git-wip-us.apache.org/repos/asf/camel/blob/7b23f118/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/CamelTwitterConsumerTestSupport.java ---------------------------------------------------------------------- diff --git a/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/CamelTwitterConsumerTestSupport.java b/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/CamelTwitterConsumerTestSupport.java index 08ed4c6..43cebf4 100644 --- a/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/CamelTwitterConsumerTestSupport.java +++ b/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/CamelTwitterConsumerTestSupport.java @@ -40,7 +40,8 @@ public abstract class CamelTwitterConsumerTestSupport extends CamelTwitterTestSu } } - + + @Override protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { public void configure() { http://git-wip-us.apache.org/repos/asf/camel/blob/7b23f118/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/SearchDirectCustomComponentTest.java ---------------------------------------------------------------------- diff --git a/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/SearchDirectCustomComponentTest.java b/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/SearchDirectCustomComponentTest.java new file mode 100644 index 0000000..ab8dc04 --- /dev/null +++ b/components/camel-twitter/src/test/java/org/apache/camel/component/twitter/SearchDirectCustomComponentTest.java @@ -0,0 +1,45 @@ +/** + * 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.twitter; + +import org.apache.camel.impl.JndiRegistry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * consumes tweets + */ +public class SearchDirectCustomComponentTest extends CamelTwitterConsumerTestSupport { + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry registry = super.createRegistry(); + registry.bind("my-twitter", new TwitterComponent()); + + return registry; + } + + @Override + protected String getUri() { + return "my-twitter://search?type=direct&keywords=java&"; + } + + @Override + protected Logger getLogger() { + return LoggerFactory.getLogger(SearchDirectCustomComponentTest.class); + } +}