CAMEL-6493 - add support for relationship API
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/bca34300 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/bca34300 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/bca34300 Branch: refs/heads/master Commit: bca3430097bc3752d6549f8e03ee44d659060b93 Parents: 6f32fa2 Author: Jonathan Anstey <jans...@gmail.com> Authored: Wed Jul 10 00:04:50 2013 -0230 Committer: Jonathan Anstey <jans...@gmail.com> Committed: Wed Jul 10 00:04:50 2013 -0230 ---------------------------------------------------------------------- .../camel/component/yammer/YammerEndpoint.java | 2 + .../component/yammer/YammerFunctionType.java | 3 +- .../YammerRelationshipPollingConsumer.java | 91 ++++++++++++++++++++ .../component/yammer/model/Relationships.java | 59 +++++++++++++ .../yammer/YammerRelationshipRouteTest.java | 58 +++++++++++++ .../src/test/resources/relationships.json | 28 ++++++ 6 files changed, 240 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/bca34300/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerEndpoint.java b/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerEndpoint.java index 6e392a9..e48dc3d 100644 --- a/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerEndpoint.java +++ b/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerEndpoint.java @@ -59,6 +59,8 @@ public class YammerEndpoint extends ScheduledPollEndpoint { case USERS: case CURRENT: return new YammerUserPollingConsumer(this, processor); + case RELATIONSHIPS: + return new YammerRelationshipPollingConsumer(this, processor); default: throw new Exception(String.format("%s is not a valid Yammer function type.", config.getFunction())); } http://git-wip-us.apache.org/repos/asf/camel/blob/bca34300/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerFunctionType.java ---------------------------------------------------------------------- diff --git a/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerFunctionType.java b/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerFunctionType.java index 2735c93..649bff5 100644 --- a/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerFunctionType.java +++ b/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerFunctionType.java @@ -18,7 +18,8 @@ package org.apache.camel.component.yammer; public enum YammerFunctionType { MESSAGES, MY_FEED, ALGO, FOLLOWING, SENT, PRIVATE, - USERS, CURRENT; + USERS, CURRENT, + RELATIONSHIPS; public static YammerFunctionType fromUri(String uri) { for (YammerFunctionType endpointType : YammerFunctionType.values()) { http://git-wip-us.apache.org/repos/asf/camel/blob/bca34300/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerRelationshipPollingConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerRelationshipPollingConsumer.java b/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerRelationshipPollingConsumer.java new file mode 100644 index 0000000..6ef5ce5 --- /dev/null +++ b/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/YammerRelationshipPollingConsumer.java @@ -0,0 +1,91 @@ +/** + * 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.yammer; + +import java.util.concurrent.TimeUnit; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.component.yammer.model.Relationships; +import org.apache.camel.impl.ScheduledPollConsumer; +import org.codehaus.jackson.map.ObjectMapper; + +/** + * A Yammer consumer that periodically polls relationships from Yammer's relationship API. + */ +public class YammerRelationshipPollingConsumer extends ScheduledPollConsumer { + private final YammerEndpoint endpoint; + private final String apiUrl; + + public YammerRelationshipPollingConsumer(YammerEndpoint endpoint, Processor processor) throws Exception { + super(endpoint, processor); + this.endpoint = endpoint; + + long delay = endpoint.getConfig().getDelay(); + setDelay(delay); + setTimeUnit(TimeUnit.MILLISECONDS); + apiUrl = getApiUrl(); + } + + private String getApiUrl() throws Exception { + StringBuilder url = new StringBuilder(); + + String function = endpoint.getConfig().getFunction(); + switch (YammerFunctionType.fromUri(function)) { + case RELATIONSHIPS: + url.append(YammerConstants.YAMMER_BASE_API_URL); + url.append(function); + url.append(".json"); + break; + default: + throw new Exception(String.format("%s is not a valid Yammer relationship function type.", function)); + } + + return url.toString(); + } + + + @Override + protected int poll() throws Exception { + Exchange exchange = endpoint.createExchange(); + + try { + String jsonBody = endpoint.getConfig().getRequestor(apiUrl).send(); + + if (!endpoint.getConfig().isUseJson()) { + ObjectMapper jsonMapper = new ObjectMapper(); + Relationships relationships = jsonMapper.readValue(jsonBody, Relationships.class); + + exchange.getIn().setBody(relationships); + } else { + exchange.getIn().setBody(jsonBody); + } + + // send message to next processor in the route + getProcessor().process(exchange); + + return 1; // number of messages polled + } finally { + // log exception if an exception occurred and was not handled + if (exchange.getException() != null) { + getExceptionHandler().handleException("Error processing exchange", exchange, exchange.getException()); + } + } + } + + +} http://git-wip-us.apache.org/repos/asf/camel/blob/bca34300/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/model/Relationships.java ---------------------------------------------------------------------- diff --git a/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/model/Relationships.java b/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/model/Relationships.java new file mode 100644 index 0000000..17e6d00 --- /dev/null +++ b/components/camel-yammer/src/main/java/org/apache/camel/component/yammer/model/Relationships.java @@ -0,0 +1,59 @@ +/** + * 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.yammer.model; + +import java.util.List; + +import org.codehaus.jackson.annotate.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public class Relationships { + + private List<User> subordinates; + private List<User> superiors; + private List<User> colleagues; + + public List<User> getSubordinates() { + return subordinates; + } + + public void setSubordinates(List<User> subordinates) { + this.subordinates = subordinates; + } + + public List<User> getSuperiors() { + return superiors; + } + + public void setSuperiors(List<User> superiors) { + this.superiors = superiors; + } + + public List<User> getColleagues() { + return colleagues; + } + + public void setColleagues(List<User> colleagues) { + this.colleagues = colleagues; + } + + @Override + public String toString() { + return "Relationships [subordinates=" + subordinates + ", superiors=" + superiors + ", colleagues=" + colleagues + "]"; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/bca34300/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerRelationshipRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerRelationshipRouteTest.java b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerRelationshipRouteTest.java new file mode 100644 index 0000000..baaa876 --- /dev/null +++ b/components/camel-yammer/src/test/java/org/apache/camel/component/yammer/YammerRelationshipRouteTest.java @@ -0,0 +1,58 @@ +/** + * 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.yammer; + + +import java.util.List; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.yammer.model.Relationships; +import org.junit.Test; + +public class YammerRelationshipRouteTest extends YammerComponentTestSupport { + + @SuppressWarnings("unchecked") + @Test + public void testConsumeAllUsers() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMinimumMessageCount(1); + assertMockEndpointsSatisfied(); + + Exchange exchange = mock.getExchanges().get(0); + Relationships relationships = exchange.getIn().getBody(Relationships.class); + + assertEquals(1, relationships.getSuperiors().size()); + assertEquals("Joe Camel", relationships.getSuperiors().get(0).getFullName()); + } + + @Override + protected String jsonFile() { + return "/relationships.json"; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + // using dummy keys here since we are mocking out calls to yammer.com with static json; in a real app, please use your own keys! + from("yammer:relationships?consumerKey=aConsumerKey&consumerSecret=aConsumerSecretKey&accessToken=aAccessToken").to("mock:result"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/bca34300/components/camel-yammer/src/test/resources/relationships.json ---------------------------------------------------------------------- diff --git a/components/camel-yammer/src/test/resources/relationships.json b/components/camel-yammer/src/test/resources/relationships.json new file mode 100644 index 0000000..da7cbd9 --- /dev/null +++ b/components/camel-yammer/src/test/resources/relationships.json @@ -0,0 +1,28 @@ +{ + "subordinates":[ + + ], + "superiors":[ + { + "mugshot_url":"", + "stats":{ + "updates":24, + "following":6, + "followers":80 + }, + "state":"active", + "url":"https://www.yammer.com/api/v1/users/123456", + "job_title":"", + "type":"user", + "mugshot_url_template":"", + "web_url":"https://www.yammer.com/redhat.com/users/jcamel", + "full_name":"Joe Camel", + "id":143117, + "activated_at":"2010/01/21 16:24:25 +0000", + "name":"jcamel" + } + ], + "colleagues":[ + + ] +} \ No newline at end of file