You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by do...@apache.org on 2008/06/03 22:43:04 UTC

svn commit: r662879 - in /incubator/shindig/trunk: ./ java/social-api/ java/social-api/src/main/java/org/apache/shindig/social/abdera/ java/social-api/src/main/java/org/apache/shindig/social/opensocial/model/ java/social-api/src/test/java/org/apache/sh...

Author: doll
Date: Tue Jun  3 13:43:03 2008
New Revision: 662879

URL: http://svn.apache.org/viewvc?rev=662879&view=rev
Log:
Patch from Vasu Nori. Adds some preliminary code for creating new activities with abdera. This needs some work but it gets us going in the right direction. Includes test.


Modified:
    incubator/shindig/trunk/java/social-api/pom.xml
    incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/AbstractSocialEntityCollectionAdapter.java
    incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/ActivityAdapter.java
    incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/model/Activity.java
    incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/model/MediaItem.java
    incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/AbstractLargeRestfulTests.java
    incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulAtomActivityTest.java
    incubator/shindig/trunk/pom.xml

Modified: incubator/shindig/trunk/java/social-api/pom.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/pom.xml?rev=662879&r1=662878&r2=662879&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/pom.xml (original)
+++ incubator/shindig/trunk/java/social-api/pom.xml Tue Jun  3 13:43:03 2008
@@ -65,7 +65,7 @@
     </dependency>
     <dependency>
       <groupId>com.google.code.google-collections</groupId>
-      <artifactId>google-collect</artifactId>     
+      <artifactId>google-collect</artifactId>
     </dependency>
     <dependency>
       <groupId>org.apache.abdera</groupId>
@@ -84,6 +84,10 @@
       <groupId>commons-betwixt</groupId>
     </dependency>
     <dependency>
+      <artifactId>commons-collections</artifactId>
+      <groupId>commons-collections</groupId>
+    </dependency>
+    <dependency>
       <groupId>org.apache.abdera</groupId>
       <artifactId>abdera-extensions-main</artifactId>
     </dependency>

Modified: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/AbstractSocialEntityCollectionAdapter.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/AbstractSocialEntityCollectionAdapter.java?rev=662879&r1=662878&r2=662879&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/AbstractSocialEntityCollectionAdapter.java (original)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/AbstractSocialEntityCollectionAdapter.java Tue Jun  3 13:43:03 2008
@@ -38,6 +38,7 @@
 import org.apache.abdera.model.Content;
 import org.apache.abdera.model.Entry;
 import org.apache.abdera.model.Feed;
+import org.apache.abdera.model.Link;
 import org.apache.abdera.model.Person;
 import org.apache.abdera.protocol.server.RequestContext;
 import org.apache.abdera.protocol.server.ResponseContext;
@@ -315,6 +316,35 @@
     return feed;
   }
 
+  protected void addEditLinkToEntry(Entry entry) throws Exception {
+    if (getEditUriFromEntry(entry) == null) {
+      entry.addLink(entry.getId().toString(), "edit");
+    }
+  }
+
+  protected String getEditUriFromEntry(Entry entry) throws Exception {
+    String editUri = null;
+    List<Link> editLinks = entry.getLinks("edit");
+    if (editLinks != null) {
+      for (Link link : editLinks) {
+        // if there is more than one edit link, we should not automatically
+        // assume that it's always going to point to an Atom document
+        // representation.
+        if (link.getMimeType() != null) {
+          if (link.getMimeType().match("application/atom+xml")) {
+            editUri = link.getResolvedHref().toString();
+            break;
+          }
+        } else {
+          // edit link with no type attribute is the right one to use
+          editUri = link.getResolvedHref().toString();
+          break;
+        }
+      }
+    }
+   return editUri;
+  }
+   
   /**
    * Unimplemented HTTP methods
    */
@@ -325,11 +355,6 @@
   }
 
   @Override
-  public ResponseContext postEntry(RequestContext request) {
-    return null;
-  }
-
-  @Override
   public ResponseContext putEntry(RequestContext request) {
     return null;
   }

Modified: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/ActivityAdapter.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/ActivityAdapter.java?rev=662879&r1=662878&r2=662879&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/ActivityAdapter.java (original)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/ActivityAdapter.java Tue Jun  3 13:43:03 2008
@@ -20,7 +20,6 @@
 import org.apache.shindig.common.SecurityToken;
 import org.apache.shindig.social.opensocial.ActivitiesService;
 import org.apache.shindig.social.opensocial.model.Activity;
-
 import com.google.common.collect.Lists;
 import com.google.inject.Inject;
 import org.apache.abdera.i18n.iri.IRI;
@@ -30,20 +29,23 @@
 import org.apache.abdera.protocol.server.RequestContext;
 import org.apache.abdera.protocol.server.context.ResponseContextException;
 
+import org.apache.commons.betwixt.io.BeanReader;
+import org.xml.sax.SAXException;
+
+import java.beans.IntrospectionException;
+import java.io.IOException;
+import java.io.StringReader;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
-import java.util.logging.Logger;
 
 /**
  * This Collection is backed by a set of Activity entities
  */
 public class ActivityAdapter extends
     AbstractSocialEntityCollectionAdapter<Activity> {
-  private static Logger logger = Logger
-      .getLogger(ActivityAdapter.class.getName());
 
-  private ActivitiesService activitiesService;
+  private final ActivitiesService activitiesService;
 
   /**
    * The Adapter needs Activities, People and Groups.
@@ -135,6 +137,7 @@
    * @param activityObj The object that the entry is based on.
    * @throws ResponseContextException
    */
+  @Override
   protected void addOptionalEntryDetails(RequestContext request, Entry entry,
       IRI feedIri, Activity activityObj) throws ResponseContextException {
     String link = getLink(activityObj, feedIri, request);
@@ -193,12 +196,75 @@
     return activitiesService.getActivities(ids, authToken).getResponse();
   }
 
+  /**
+   * When an entry is POSTed to a collection, purpose is to add that entry
+   * to the collection.
+   * 
+   * Currently, the following method only handles POST to the following
+   * collection
+   *           /activities/:uid/@self
+   */
   @Override
   public Activity postEntry(String title, IRI id, String summary, Date updated,
       List<Person> authors, Content content, RequestContext request)
       throws ResponseContextException {
-    // TODO: Implement
-    return null;
+
+    // handle Atom/XML. TODO handle Json
+
+    /* 
+     * To extract Activity obj from the posted Entry, content element is 
+     * used. To make this work, content element should contain everything
+     * we need to create the Activity object correctly. If there are 
+     * some fields in other atom: elements, copy them into the content element.
+     * i.e., implement "reverse hoisting" logic here
+     */ 
+    // TODO is userId field in content element (correspodns to Activity.userId)
+    // supposed to be filled in by the caller
+    // OR is it to be picked up from the ":uid" param on URL
+    // assuming that it is already filled by the caller.
+    
+    String contentXml = content.getValue();
+    BeanReader reader = new BeanReader();
+    Activity postedActivity = null;
+    try {
+      reader.registerBeanClass("activity", Activity.class);
+      StringReader rd = new StringReader(contentXml);
+      postedActivity = (Activity)reader.parse(rd);
+    } catch (IntrospectionException e) {
+      throw new ResponseContextException(null, e);
+    } catch (IOException e) {
+      throw new ResponseContextException(null, e);
+    } catch (SAXException e) {
+      throw new ResponseContextException(null, e);
+    }
+
+    /*
+     * in Atom/Xml, the posted entry is returned to the original caller
+     * with the following fields being "potentially" different from
+     * what the caller sent:
+     *   1. atom:id  server can optionally assign a new id to the entry
+     *               and return it to the caller. This may not be reqd
+     *   2. editUri link should be added to the entry if required.
+     *           refer to atom spec on all you need to know about this field.
+     *      included the following methods in baseclass to help with this
+     *       . addEditLinkToEntry(Entry entry)
+     *       . getEditUriFromEntry(Entry entry)
+     *       
+     *    TODO figure out WHERE the postedEntry doc is being constructed in
+     *    Abdera. thats where the logic to add editUri should be included.
+     *      
+     */ 
+    
+    // add this to list of activities of the user
+    String uid = request.getTarget().getParameter("uid");
+    SecurityToken authToken = getSecurityToken(request, uid);
+    
+    // the following modifies postedActivity - which is then returned to 
+    // the caller. 
+    // TODO the following method should be modified (or new method needed) 
+    // to return the postedActivity 
+    activitiesService.createActivity(uid, postedActivity, authToken);
+    return postedActivity;
   }
 
   @Override

Modified: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/model/Activity.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/model/Activity.java?rev=662879&r1=662878&r2=662879&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/model/Activity.java (original)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/model/Activity.java Tue Jun  3 13:43:03 2008
@@ -51,7 +51,7 @@
 
     @Override
     public String toString() {
-      return this.jsonString;
+      return jsonString;
     }
   }
 
@@ -74,6 +74,9 @@
   private String url;
   private String userId;
 
+  public Activity() {
+  }
+
   public Activity(String id, String userId) {
     this.id = id;
     this.userId = userId;
@@ -222,4 +225,5 @@
   public void setUserId(String userId) {
     this.userId = userId;
   }
+
 }

Modified: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/model/MediaItem.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/model/MediaItem.java?rev=662879&r1=662878&r2=662879&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/model/MediaItem.java (original)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/model/MediaItem.java Tue Jun  3 13:43:03 2008
@@ -62,6 +62,9 @@
     }
   }
 
+  public MediaItem() {
+  }
+
   public MediaItem(String mimeType, Type type, String url) {
     this.mimeType = mimeType;
     this.type = type;

Modified: incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/AbstractLargeRestfulTests.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/AbstractLargeRestfulTests.java?rev=662879&r1=662878&r2=662879&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/AbstractLargeRestfulTests.java (original)
+++ incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/AbstractLargeRestfulTests.java Tue Jun  3 13:43:03 2008
@@ -22,6 +22,7 @@
 import com.google.common.collect.Maps;
 import org.apache.abdera.Abdera;
 import org.apache.abdera.model.Base;
+import org.apache.abdera.model.Entry;
 import org.apache.abdera.protocol.Response;
 import org.apache.abdera.protocol.client.AbderaClient;
 import org.apache.abdera.protocol.client.ClientResponse;
@@ -167,4 +168,8 @@
     return columns;
   }
 
+  protected Entry getNewEntry() {
+    return abdera.getFactory().newEntry();
+  }
+
 }

Modified: incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulAtomActivityTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulAtomActivityTest.java?rev=662879&r1=662878&r2=662879&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulAtomActivityTest.java (original)
+++ incubator/shindig/trunk/java/social-api/src/test/java/org/apache/shindig/social/abdera/RestfulAtomActivityTest.java Tue Jun  3 13:43:03 2008
@@ -25,10 +25,16 @@
 import org.apache.abdera.model.Feed;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
 import org.junit.Test;
 
+import java.util.Date;
+import java.util.List;
+
 public class RestfulAtomActivityTest extends AbstractLargeRestfulTests {
-  private Activity activity = SocialApiTestsGuiceModule
+
+  private final Activity activity = SocialApiTestsGuiceModule
       .MockXmlStateFileFetcher.johnActivity;
 
   @Test
@@ -79,4 +85,76 @@
     assertEquals(activity.getUserId(), feed.getAuthor().getName().toString());
     assertEquals(title, feed.getTitle());
   }
+
+  @Test
+  public void testCreateActivity() throws Exception {
+    Entry entry = getNewEntry();
+    String feedUrl = BASEURL +  "/activities/jane.doe/@self?format=atom";
+    entry.setBaseUri(feedUrl);
+    entry.setUpdated(new Date());
+    entry.addAuthor("yours truly");
+    entry.setTitle("New Activity");
+    String newActivityId = "activity_new_1";
+    entry.setId("urn:guid:" + newActivityId);
+    entry.setContent("<activity>" +
+        "<body>this is the new activity I just posted</body>" +
+        "<id>activity_new_1</id>" +
+        "<streamTitle>jane's stream title</streamTitle>" +
+        "<title>Jane just posted a new activity</title>" +
+        "<updated>Wed May 27 17:59:35 PDT 2008</updated>" +
+        "<userId>jane.doe</userId>" +
+        "</activity>");
+
+    // TODO Betwixt is not handling List<MediaItem>. needs debugging
+    // so the following is not included in the content above
+    
+        /*
+        "<mediaItems>" +
+        "  <MediaItem>" +
+        "    <mimeType>image/jpeg</mimeType>" +
+        "    <type>" +
+        "       <declaringClass>" +
+        "          org.apache.shindig.social.opensocial.model.MediaItem$Type" +
+        "       </declaringClass>" +
+        "    </type>" +
+        "      <url>" +
+        "        http://animals.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/animals/images/primary/black-spider-monkey.jpg" +
+        "      </url>" +
+        "  </MediaItem>" +
+        "  <MediaItem>" +
+        "    <mimeType>image/jpeg</mimeType>" +
+        "    <type>" +
+        "      <declaringClass>" +
+        "         org.apache.shindig.social.opensocial.model.MediaItem$Type" +
+        "      </declaringClass>" +
+        "    </type>" +
+        "    <url>" +
+        "      http://image.guardian.co.uk/sys-images/Guardian/Pix/gallery/2002/01/03/monkey300.jpg</url>" +
+        "  </MediaItem>" +
+        "</mediaItems>" +
+        */
+
+    resp = client.post(feedUrl, entry);
+    checkForGoodAtomResponse(resp);
+    Entry postedEntry = (Entry)resp.getDocument().getRoot();
+    assertEquals(entry.getId().toString(), postedEntry.getId().toString());
+
+    // get the newly posted entry
+    resp = client.get(BASEURL + "/activities/jane.doe/@self/" + 
+        newActivityId + "?format=atom");
+    checkForGoodAtomResponse(resp);
+    Document<Entry> doc = resp.getDocument();
+    assertEquals(entry.getId().toString(), postedEntry.getId().toString());
+    
+    // get feed and make sure the new entry is in it
+    resp = client.get(BASEURL + "/activities/jane.doe/@self?format=atom");
+    checkForGoodAtomResponse(resp);
+    Feed feed = (Feed) resp.getDocument().getRoot();
+    List<Entry> entries = feed.getEntries();
+    boolean found = false;
+    for (Entry e : entries) {
+      found = e.getId().toString().endsWith(newActivityId);
+    }
+    assertTrue(found);
+  }
 }

Modified: incubator/shindig/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/pom.xml?rev=662879&r1=662878&r2=662879&view=diff
==============================================================================
--- incubator/shindig/trunk/pom.xml (original)
+++ incubator/shindig/trunk/pom.xml Tue Jun  3 13:43:03 2008
@@ -808,6 +808,11 @@
         <version>0.8</version>
       </dependency>
       <dependency>
+        <artifactId>commons-collections</artifactId>
+        <groupId>commons-collections</groupId>
+        <version>3.2.1</version>
+      </dependency>
+      <dependency>
         <groupId>org.mortbay.jetty</groupId>
         <artifactId>jetty</artifactId>
         <version>6.1.9</version>