You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streams.apache.org by mf...@apache.org on 2014/05/27 15:27:53 UTC
[3/7] git commit: Abstracts the updating of the activity object from
a tweet to a reusable activity
Abstracts the updating of the activity object from a tweet to a reusable activity
Project: http://git-wip-us.apache.org/repos/asf/incubator-streams/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-streams/commit/5c19b6ef
Tree: http://git-wip-us.apache.org/repos/asf/incubator-streams/tree/5c19b6ef
Diff: http://git-wip-us.apache.org/repos/asf/incubator-streams/diff/5c19b6ef
Branch: refs/heads/master
Commit: 5c19b6ef3d4f2d5bdb2f2654d31376a2ac9ada7f
Parents: 37967fb
Author: mfranklin <mf...@apache.org>
Authored: Mon May 26 20:59:55 2014 -0400
Committer: mfranklin <mf...@apache.org>
Committed: Mon May 26 20:59:55 2014 -0400
----------------------------------------------------------------------
.../TwitterJsonTweetActivitySerializer.java | 81 +-----------
.../serializer/util/TwitterActivityUtil.java | 127 ++++++++++++++++++-
2 files changed, 126 insertions(+), 82 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/5c19b6ef/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/TwitterJsonTweetActivitySerializer.java
----------------------------------------------------------------------
diff --git a/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/TwitterJsonTweetActivitySerializer.java b/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/TwitterJsonTweetActivitySerializer.java
index a5dc2de..6d019a9 100644
--- a/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/TwitterJsonTweetActivitySerializer.java
+++ b/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/TwitterJsonTweetActivitySerializer.java
@@ -2,27 +2,16 @@ package org.apache.streams.twitter.serializer;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import com.google.common.base.Optional;
-import com.google.common.base.Strings;
-import com.google.common.collect.Lists;
import org.apache.commons.lang.NotImplementedException;
import org.apache.streams.data.ActivitySerializer;
import org.apache.streams.exceptions.ActivitySerializerException;
import org.apache.streams.pojo.json.Activity;
-import org.apache.streams.pojo.json.ActivityObject;
-import org.apache.streams.pojo.json.Actor;
-import org.apache.streams.twitter.Url;
import org.apache.streams.twitter.pojo.Tweet;
-import org.apache.streams.twitter.pojo.User;
import java.io.IOException;
import java.io.Serializable;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
-import static org.apache.streams.data.util.ActivityUtil.ensureExtensions;
import static org.apache.streams.twitter.serializer.util.TwitterActivityUtil.*;
/**
@@ -59,29 +48,7 @@ public class TwitterJsonTweetActivitySerializer implements ActivitySerializer<St
Activity activity = new Activity();
- activity.setActor(buildActor(tweet));
- activity.setVerb("post");
- activity.setId(formatId(activity.getVerb(),
- Optional.fromNullable(
- tweet.getIdStr())
- .or(Optional.of(tweet.getId().toString()))
- .orNull()));
- if(Strings.isNullOrEmpty(activity.getId()))
- throw new ActivitySerializerException("Unable to determine activity id");
- try {
- activity.setPublished(tweet.getCreatedAt());
- } catch( Exception e ) {
- throw new ActivitySerializerException("Unable to determine publishedDate", e);
- }
- activity.setTarget(buildTarget(tweet));
- activity.setProvider(getProvider());
- activity.setTitle("");
- activity.setContent(tweet.getText());
- activity.setUrl("http://twitter.com/" + tweet.getIdStr());
- activity.setLinks(getLinks(tweet));
-
- addTwitterExtension(activity, mapper.convertValue(tweet, ObjectNode.class));
- addLocationExtension(activity, tweet);
+ updateActivity(tweet, activity);
return activity;
}
@@ -89,50 +56,4 @@ public class TwitterJsonTweetActivitySerializer implements ActivitySerializer<St
public List<Activity> deserializeAll(List<String> serializedList) {
return null;
}
-
- public static Actor buildActor(Tweet tweet) {
- Actor actor = new Actor();
- User user = tweet.getUser();
- actor.setId(formatId(
- Optional.fromNullable(
- user.getIdStr())
- .or(Optional.of(user.getId().toString()))
- .orNull()
- ));
- actor.setDisplayName(user.getScreenName());
- if (user.getUrl()!=null){
- actor.setUrl(user.getUrl());
- }
- return actor;
- }
-
- public static List<String> getLinks(Tweet tweet) {
- List<String> links = Lists.newArrayList();
- if( tweet.getEntities().getUrls() != null ) {
- for (Url url : tweet.getEntities().getUrls()) {
- links.add(url.getExpandedUrl());
- }
- }
- else
- System.out.println(" 0 links");
- return links;
- }
-
- public static ActivityObject buildTarget(Tweet tweet) {
- return null;
- }
-
- public static void addLocationExtension(Activity activity, Tweet tweet) {
- Map<String, Object> extensions = ensureExtensions(activity);
- Map<String, Object> location = new HashMap<String, Object>();
- location.put("id", formatId(
- Optional.fromNullable(
- tweet.getIdStr())
- .or(Optional.of(tweet.getId().toString()))
- .orNull()
- ));
- location.put("coordinates", tweet.getCoordinates());
- extensions.put("location", location);
- }
-
}
http://git-wip-us.apache.org/repos/asf/incubator-streams/blob/5c19b6ef/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/util/TwitterActivityUtil.java
----------------------------------------------------------------------
diff --git a/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/util/TwitterActivityUtil.java b/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/util/TwitterActivityUtil.java
index 527b3c6..0b7ea35 100644
--- a/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/util/TwitterActivityUtil.java
+++ b/streams-contrib/streams-provider-twitter/src/main/java/org/apache/streams/twitter/serializer/util/TwitterActivityUtil.java
@@ -19,32 +19,155 @@
package org.apache.streams.twitter.serializer.util;
+import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Joiner;
+import com.google.common.base.Optional;
+import com.google.common.base.Strings;
import com.google.common.collect.Lists;
+import org.apache.streams.exceptions.ActivitySerializerException;
import org.apache.streams.pojo.json.Activity;
+import org.apache.streams.pojo.json.ActivityObject;
+import org.apache.streams.pojo.json.Actor;
import org.apache.streams.pojo.json.Provider;
+import org.apache.streams.twitter.Url;
+import org.apache.streams.twitter.pojo.Tweet;
+import org.apache.streams.twitter.pojo.User;
+import org.apache.streams.twitter.serializer.StreamsTwitterMapper;
+import java.util.HashMap;
+import java.util.List;
import java.util.Map;
+import static org.apache.streams.data.util.ActivityUtil.ensureExtensions;
+
/**
* Provides utilities for working with Activity objects within the context of Twitter
*/
public class TwitterActivityUtil {
+ /**
+ * Updates the given Activity object with the values from the Tweet
+ * @param tweet the object to use as the source
+ * @param activity the target of the updates. Will receive all values from the tweet.
+ * @throws ActivitySerializerException
+ */
+ public static void updateActivity(Tweet tweet, Activity activity) throws ActivitySerializerException {
+ ObjectMapper mapper = StreamsTwitterMapper.getInstance();
+ activity.setActor(buildActor(tweet));
+ activity.setVerb("post");
+ activity.setId(formatId(activity.getVerb(),
+ Optional.fromNullable(
+ tweet.getIdStr())
+ .or(Optional.of(tweet.getId().toString()))
+ .orNull()));
+ if(Strings.isNullOrEmpty(activity.getId()))
+ throw new ActivitySerializerException("Unable to determine activity id");
+ try {
+ activity.setPublished(tweet.getCreatedAt());
+ } catch( Exception e ) {
+ throw new ActivitySerializerException("Unable to determine publishedDate", e);
+ }
+ activity.setTarget(buildTarget(tweet));
+ activity.setProvider(getProvider());
+ activity.setTitle("");
+ activity.setContent(tweet.getText());
+ activity.setUrl("http://twitter.com/" + tweet.getIdStr());
+ activity.setLinks(getLinks(tweet));
+
+ addTwitterExtension(activity, mapper.convertValue(tweet, ObjectNode.class));
+ addLocationExtension(activity, tweet);
+ }
+
+ /**
+ * Builds the activity {@link org.apache.streams.pojo.json.Actor} object from the tweet
+ * @param tweet the object to use as the source
+ * @return a valid Actor populated from the Tweet
+ */
+ public static Actor buildActor(Tweet tweet) {
+ Actor actor = new Actor();
+ User user = tweet.getUser();
+ actor.setId(formatId(
+ Optional.fromNullable(
+ user.getIdStr())
+ .or(Optional.of(user.getId().toString()))
+ .orNull()
+ ));
+ actor.setDisplayName(user.getScreenName());
+ if (user.getUrl()!=null){
+ actor.setUrl(user.getUrl());
+ }
+ return actor;
+ }
+
+ /**
+ * Gets the links from the Twitter event
+ * @param tweet the object to use as the source
+ * @return a list of links corresponding to the expanded URL (no t.co)
+ */
+ public static List<String> getLinks(Tweet tweet) {
+ List<String> links = Lists.newArrayList();
+ if( tweet.getEntities().getUrls() != null ) {
+ for (Url url : tweet.getEntities().getUrls()) {
+ links.add(url.getExpandedUrl());
+ }
+ }
+ else
+ System.out.println(" 0 links");
+ return links;
+ }
+ /**
+ * Builds the {@link org.apache.streams.twitter.pojo.TargetObject} from the tweet
+ * @param tweet the object to use as the source
+ * @return currently returns null for all activities
+ */
+ public static ActivityObject buildTarget(Tweet tweet) {
+ return null;
+ }
+
+ /**
+ * Adds the location extension and populates with teh twitter data
+ * @param activity the Activity object to update
+ * @param tweet the object to use as the source
+ */
+ public static void addLocationExtension(Activity activity, Tweet tweet) {
+ Map<String, Object> extensions = ensureExtensions(activity);
+ Map<String, Object> location = new HashMap<String, Object>();
+ location.put("id", formatId(
+ Optional.fromNullable(
+ tweet.getIdStr())
+ .or(Optional.of(tweet.getId().toString()))
+ .orNull()
+ ));
+ location.put("coordinates", tweet.getCoordinates());
+ extensions.put("location", location);
+ }
+
+ /**
+ * Gets the common twitter {@link org.apache.streams.pojo.json.Provider} object
+ * @return a provider object representing Twitter
+ */
public static Provider getProvider() {
Provider provider = new Provider();
provider.setId("id:providers:twitter");
provider.setDisplayName("Twitter");
return provider;
}
-
+ /**
+ * Adds the given Twitter event to the activity as an extension
+ * @param activity the Activity object to update
+ * @param event the Twitter event to add as the extension
+ */
public static void addTwitterExtension(Activity activity, ObjectNode event) {
Map<String, Object> extensions = org.apache.streams.data.util.ActivityUtil.ensureExtensions(activity);
extensions.put("twitter", event);
}
-
+ /**
+ * Formats the ID to conform with the Apache Streams activity ID convention
+ * @param idparts the parts of the ID to join
+ * @return a valid Activity ID in format "id:twitter:part1:part2:...partN"
+ */
public static String formatId(String... idparts) {
return Joiner.on(":").join(Lists.asList("id:twitter", idparts));
}