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/05/15 08:58:08 UTC

svn commit: r656522 - in /incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera: RequestUrlTemplate.java SocialApiProvider.java SocialRouteManager.java

Author: doll
Date: Wed May 14 23:58:08 2008
New Revision: 656522

URL: http://svn.apache.org/viewvc?rev=656522&view=rev
Log:
SHINDIG-268
Patch from David Primmer. Some cleanup for the restful code.


Added:
    incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/RequestUrlTemplate.java
    incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/SocialRouteManager.java
Modified:
    incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/SocialApiProvider.java

Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/RequestUrlTemplate.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/RequestUrlTemplate.java?rev=656522&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/RequestUrlTemplate.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/RequestUrlTemplate.java Wed May 14 23:58:08 2008
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.shindig.social.abdera;
+
+/**
+ * The RequestUrlTemplate enum standardizes the names and descriptions of the
+ * URL templates as defined in the RESTful API spec.
+ *
+ * For example, "/people/{uid}/@all" is roughly translated in English to read
+ * "Profiles of Connections of User" and is the referred to in the code as
+ * PROFILES_OF_CONNECTIONS_OF_USER. The descriptions can be customized by an
+ * implementer and used as the titles for feeds.
+ *
+ * TODO: Add ResourceBundle functions.
+ */
+
+public enum RequestUrlTemplate {
+  // People
+  PROFILES_OF_CONNECTIONS_OF_USER("Profiles of Connections of User",
+      "people/:uid/:groupid", "/people/{uid}/{groupid}"),
+  PROFILES_OF_FRIENDS_OF_USER("Profiles of Friends of User",
+      "people/:uid/@friends", "/people/{uid}/@friends"),
+  CONNECTIONS_OF_USER("Connections of User",
+      "people/:uid/@all", "/people/{uid}/@all"),
+  PROFILE_OF_CONNECTION_OF_USER("Profile of Connection of User",
+      "people/:uid/@all/:pid", "/people/{uid}/@all/{pid}"),
+  PROFILE_OF_USER("Profile of User",
+      "people/:uid/@self", "/people/{uid}/@self"),
+  // Activities
+  ACTIVITIES_OF_USER("Activities of User",
+      "activities/:uid/@self", "/activities/{uid}/@self"),
+  ACTIVITIES_OF_FRIENDS_OF_USER("Activities of Friends of User",
+      "activities/:uid/@friends", "/activities/{uid}/@friends"),
+  ACTIVITIES_OF_GROUP_OF_USER("Activities of Group of User",
+      "activities/:uid/:gid", "/activities/{uid}/{gid}"),
+  ACTIVITY_OF_USER("Activity of User",
+      "activities/:uid/@self/:aid", "/activities/{uid}/@self/{aid}"),
+  // AppData
+  APPDATA_OF_APP_OF_USER("AppData of App of User",
+      "appdata/:uid/@self/:aid", "/appdata/{uid}/@self/{aid}"),
+  APPDATA_OF_FRIENDS_OF_USER("AppData of Friends of User",
+      "appdata/:uid/@friends/:aid", "/appdata/{uid}/@friends/{aid}");
+
+  private String description;
+  private String routePattern;
+  private String urlTemplate;
+
+  private RequestUrlTemplate(String description, String routePattern,
+      String urlTemplate) {
+    this.description = description;
+    this.routePattern = routePattern;
+    this.urlTemplate = urlTemplate;
+  }
+
+  @Override
+  public String toString() {
+    return description;
+  }
+
+  public String getRoutePattern() {
+    return routePattern;
+  }
+
+  public String getUrlTemplate() {
+    return urlTemplate;
+  }
+
+  public static RequestUrlTemplate getValue(String value) {
+    return valueOf(value.replaceAll(" ", "_").toUpperCase());
+  }
+}

Modified: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/SocialApiProvider.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/SocialApiProvider.java?rev=656522&r1=656521&r2=656522&view=diff
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/SocialApiProvider.java (original)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/SocialApiProvider.java Wed May 14 23:58:08 2008
@@ -17,12 +17,12 @@
  */
 package org.apache.shindig.social.abdera;
 
+import org.apache.shindig.social.abdera.json.JSONFilter;
+
 import com.google.inject.Inject;
 import com.google.inject.Provider;
-import org.apache.shindig.social.abdera.json.JSONFilter;
 import org.apache.abdera.protocol.server.TargetType;
 import org.apache.abdera.protocol.server.impl.DefaultProvider;
-import org.apache.abdera.protocol.server.impl.RouteManager;
 
 public class SocialApiProvider extends DefaultProvider {
   //TODO why is this hardcoded here. can't this be from servletContext?
@@ -33,47 +33,6 @@
   private Provider<FriendsServiceAdapter> friendsAdapterProvider;
   private Provider<DataServiceAdapter> dataAdapterProvider;
   private Provider<ActivityAdapter> activityAdapterProvider;
-  
-  /**
-   * The CollectionAdapter enum standardizes the names and descriptions of the
-   * URL templates as defined in the RESTful API spec. Each unique template has
-   * a corresponding CollectionAdapter. For example, "/people/{uid}/@all" is
-   * roughly translated in English to read "Profiles of Connections of User" and
-   * is the referred to in the code as PROFILES_OF_CONNECTIONS_OF_USER. The
-   * descriptions can be customized by an implementer and used as the titles for
-   * feeds.
-   * TODO: Add ResourceBundle functions.
-   */
-  public enum CollectionAdapter {
-    //People
-    PROFILES_OF_CONNECTIONS_OF_USER("Profiles of Connections of User"),
-    PROFILES_OF_FRIENDS_OF_USER("Profiles of Friends of User"),
-    CONNECTIONS_OF_USER("Connections of User"),
-    PROFILE_OF_CONNECTION_OF_USER("Profile of Connection of User"),
-    PROFILE_OF_USER("Profile of User"),
-    //Activities
-    ACTIVITIES_OF_USER("Activities of User"),
-    ACTIVITIES_OF_FRIENDS_OF_USER("Activities of Friends of User"),
-    ACTIVITIES_OF_GROUP_OF_USER("Activities of Group of User"),
-    ACTIVITY_OF_USER("Activity of User"),
-    //AppData
-    APPDATA_OF_APP_OF_USER("AppData of App of User"),
-    APPDATA_OF_FRIENDS_OF_USER("AppData of Friends of User");
-
-    private String description;
-
-    private CollectionAdapter(String description) {
-      this.description = description;
-    }
-
-    public String toString() {
-      return description;
-    }
-
-    public static CollectionAdapter getValue(String value) {
-      return valueOf(value.replaceAll(" ", "_").toUpperCase());
-    }
-  }
 
   @Inject
   public void setAdapters(
@@ -87,7 +46,7 @@
     this.activitiesAdapterProvider = activitiesAdapterProvider;
     this.dataAdapterProvider = dataAdapterProvider;
     this.activityAdapterProvider = activityAdapterProvider;
-    }
+  }
 
   /**
    * CollectionAdapters are provided via Guice and the RouteManager wires
@@ -97,14 +56,7 @@
    * People and Activities Adapters that allows them to be multi-purpose, but
    * this will need to change.
    *
-   * TODO: Fully implement all routes in the 0.8 RESTful API spec.
-   * They are specified here, but only some produce output from the server.
-   * Currently implemented:
-   * /people/{uid}/@all
-   * /people/{uid}/@all/{pid}
-   * /people/{uid}/@self
-   * /activities/{uid}/@self/{aid}
-   * /activities/{uid}/@self
+   * TODO: Implement the group urls.
    */
   public void initialize() {
     PeopleServiceAdapter peopleAdapter = peopleAdapterProvider.get();
@@ -116,81 +68,34 @@
 
     // Add the RouteManager that parses incoming and builds outgoing URLs
     // {uid} is assumed to be a deterministic GUID for the service
-    routeManager = new RouteManager()
+    routeManager = new SocialRouteManager(BASE)
         // People
-
-        // Collection of all people connected to user {uid}
-        // /people/{uid}/@all
-        // Currently, Shindig only has friends, so @all == @friends
-        .addRoute(CollectionAdapter.CONNECTIONS_OF_USER.toString(),
-            BASE + "people/:uid/@all",
+        .addRoute(RequestUrlTemplate.CONNECTIONS_OF_USER,
             TargetType.TYPE_COLLECTION, friendsAdapter)
-
-        // Collection of all friends of user {uid}; equal to @all
-        // /people/{uid}/@friends
-        .addRoute(CollectionAdapter.PROFILES_OF_FRIENDS_OF_USER.toString(),
-            BASE + "people/:uid/@friends",
+        .addRoute(RequestUrlTemplate.PROFILES_OF_FRIENDS_OF_USER,
             TargetType.TYPE_COLLECTION, friendsAdapter)
-
-        // Collection of all people connected to user {uid} in group {groupid}
-        // /people/{uid}/{groupid}
-        // TODO: Shindig does not support groups yet
-        .addRoute(CollectionAdapter.PROFILES_OF_CONNECTIONS_OF_USER.toString(),
-            BASE + "people/:uid/:groupid",
+        .addRoute(RequestUrlTemplate.PROFILES_OF_CONNECTIONS_OF_USER,
             TargetType.TYPE_COLLECTION, null)
-
-        // Individual person record. /people/{uid}/@all/{pid}
-        .addRoute(CollectionAdapter.PROFILE_OF_CONNECTION_OF_USER.toString(),
-            BASE + "people/:uid/@all/:pid",
+        .addRoute(RequestUrlTemplate.PROFILE_OF_CONNECTION_OF_USER,
             TargetType.TYPE_ENTRY, friendsAdapter)
-
-        // Self Profile record for user {uid} /people/{uid}/@self
-        .addRoute(CollectionAdapter.PROFILE_OF_USER.toString(),
-            BASE + "people/:uid/@self",
+        .addRoute(RequestUrlTemplate.PROFILE_OF_USER,
             TargetType.TYPE_ENTRY, peopleAdapter)
 
-
-        // Activities
-
-        // Collection of activities for given user /activities/{uid}/@self
-        .addRoute(CollectionAdapter.ACTIVITIES_OF_USER.toString(),
-            BASE + "activities/:uid/@self",
+         // Activities
+        .addRoute(RequestUrlTemplate.ACTIVITIES_OF_USER,
             TargetType.TYPE_COLLECTION, activitiesAdapter)
-
-        // Collection of activities for friends of the given user {uid}
-        // /activities/{uid}/@friends
-        .addRoute(CollectionAdapter.ACTIVITIES_OF_FRIENDS_OF_USER.toString(),
-            BASE + "activities/:uid/@friends",
+        .addRoute(RequestUrlTemplate.ACTIVITIES_OF_FRIENDS_OF_USER,
             TargetType.TYPE_COLLECTION, activitiesAdapter)
-
-        // Collection of activities for people in group {groupid}
-        // belonging to given user {uid} -- /activities/{uid}/{groupid}
-        // TODO: Shindig does not support groups yet
-        .addRoute(CollectionAdapter.ACTIVITIES_OF_GROUP_OF_USER.toString(),
-            BASE + "activities/:uid/:groupid",
+        .addRoute(RequestUrlTemplate.ACTIVITIES_OF_GROUP_OF_USER,
             TargetType.TYPE_COLLECTION, null)
-
-        // Individual activity resource; usually discovered from collection
-        // /activities/{uid}/@self/{aid}
-        .addRoute(CollectionAdapter.ACTIVITY_OF_USER.toString(),
-            BASE + "activities/:uid/@self/:aid",
+        .addRoute(RequestUrlTemplate.ACTIVITY_OF_USER,
             TargetType.TYPE_ENTRY, activityAdapter)
 
-
-        // AppData
-
-        // Individual App Data record for a given user+app, consisting primarily
-        // of a bag of key/value pairs. -- /appdata/{uid}/@self/{aid}
-        .addRoute(CollectionAdapter.APPDATA_OF_APP_OF_USER.toString(),
-            BASE + "appdata/:uid/@self/:aid",
+         // AppData
+        .addRoute(RequestUrlTemplate.APPDATA_OF_APP_OF_USER,
             TargetType.TYPE_ENTRY, dataAdapter)
-
-        // Collection of App Data records for friends of {uid}
-        // /appdata/{uid}/@friends/{aid}
-        .addRoute(CollectionAdapter.APPDATA_OF_FRIENDS_OF_USER.toString(),
-            BASE + "appdata/:uid/@friends/:aid",
+        .addRoute(RequestUrlTemplate.APPDATA_OF_FRIENDS_OF_USER,
             TargetType.TYPE_COLLECTION, dataAdapter)
-
         ;
 
     addFilter(new JSONFilter());

Added: incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/SocialRouteManager.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/SocialRouteManager.java?rev=656522&view=auto
==============================================================================
--- incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/SocialRouteManager.java (added)
+++ incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/abdera/SocialRouteManager.java Wed May 14 23:58:08 2008
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+package org.apache.shindig.social.abdera;
+
+import org.apache.abdera.protocol.server.CollectionAdapter;
+import org.apache.abdera.protocol.server.TargetType;
+import org.apache.abdera.protocol.server.impl.RouteManager;
+
+
+public class SocialRouteManager extends RouteManager {
+  private String base;
+
+  /**
+   * @param base Should be the same as RequestContext.getContextPath()
+   */
+  public SocialRouteManager(String base) {
+    this.base = base;
+  }
+
+  /**
+   * This extension of the addRoute from the parent allows a RequestUrlTemplate
+   * to be passed in instead of a name and pattern. This is just a convenience
+   * method to clean up the code. The parent method maps routes to types and
+   * adapters.
+   *
+   * @param template RequestUrlTemplate enum should contain names and patterns.
+   * @param type TargetType
+   * @param collectionAdapter CollectionAdapter
+   * @return addRoute from the parent RouteManager
+   */
+  public SocialRouteManager addRoute(RequestUrlTemplate template,
+      TargetType type, CollectionAdapter collectionAdapter) {
+    return (SocialRouteManager) addRoute(template.toString(),
+        base + template.getRoutePattern(), type, collectionAdapter);
+  }
+}