Author: doll
Date: Wed Jul 23 15:28:18 2008
New Revision: 679215

URL: http://svn.apache.org/viewvc?rev=679215&view=rev
Log:
Moved the invalid key checking from the DataService into the AppDataHandler so 
that all service impls can benefit from the common code.


Modified:
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/AppDataHandler.java
    
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/sample/service/JsonDbOpensocialService.java

Modified: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/AppDataHandler.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/AppDataHandler.java?rev=679215&r1=679214&r2=679215&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/AppDataHandler.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/opensocial/service/AppDataHandler.java
 Wed Jul 23 15:28:18 2008
@@ -17,12 +17,15 @@
  */
 package org.apache.shindig.social.opensocial.service;
 
+import org.apache.shindig.common.util.ImmediateFuture;
+import org.apache.shindig.social.ResponseError;
 import org.apache.shindig.social.ResponseItem;
 import org.apache.shindig.social.opensocial.spi.AppDataService;
 
 import com.google.inject.Inject;
 
 import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.Future;
 
 public class AppDataHandler extends DataRequestHandler {
@@ -85,9 +88,16 @@
   protected Future<? extends ResponseItem> handlePost(RequestItem request) {
     request.parseUrlWithTemplate(APP_DATA_PATH);
 
+    Map<String, String> values = request.getPostData(HashMap.class);
+    for (String key : values.keySet()) {
+      if (!isValidKey(key)) {
+        return ImmediateFuture.newInstance(new 
ResponseItem<Object>(ResponseError.BAD_REQUEST,
+            "One or more of the app data keys are invalid: " + key, null));
+      }
+    }
+
     return service.updatePersonData(request.getUser(), request.getGroup(),
-        request.getAppId(), request.getFields(), 
request.getPostData(HashMap.class),
-        request.getToken());
+        request.getAppId(), request.getFields(), values, request.getToken());
   }
 
   /**
@@ -105,5 +115,32 @@
         request.getAppId(), request.getFields(), request.getToken());
   }
 
+  /**
+   * Determines whether the input is a valid key. Valid keys match the regular
+   * expression [\w\-\.]+. The logic is not done using java.util.regex.* as
+   * that is 20X slower.
+   *
+   * @param key the key to validate.
+   * @return true if the key is a valid appdata key, false otherwise.
+   */
+  public static boolean isValidKey(String key) {
+    if (key == null || key.length() == 0) {
+      return false;
+    }
+    for (int i = 0; i < key.length(); ++i) {
+      char c = key.charAt(i);
+      if ((c >= 'a' && c <= 'z') ||
+          (c >= 'A' && c <= 'Z') ||
+          (c >= '0' && c <= '9') ||
+          (c == '-') ||
+          (c == '_') ||
+          (c == '.')) {
+        continue;
+      }
+      return false;
+    }
+    return true;
+  }
+
 }
 

Modified: 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/sample/service/JsonDbOpensocialService.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/sample/service/JsonDbOpensocialService.java?rev=679215&r1=679214&r2=679215&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/sample/service/JsonDbOpensocialService.java
 (original)
+++ 
incubator/shindig/trunk/java/social-api/src/main/java/org/apache/shindig/social/sample/service/JsonDbOpensocialService.java
 Wed Jul 23 15:28:18 2008
@@ -341,13 +341,6 @@
     // TODO: According to rest, yes there is. If a field is in the param list 
but not in the map
     // that means it is a delete
 
-    for (String key : values.keySet()) {
-      if (!isValidKey(key)) {
-        return ImmediateFuture.newInstance(new 
ResponseItem<Object>(ResponseError.BAD_REQUEST,
-            "The person data key had invalid characters", null));
-      }
-    }
-
     try {
       JSONObject personData = 
db.getJSONObject(DATA_TABLE).getJSONObject(userId.getUserId(token));
       if (personData == null) {
@@ -366,33 +359,7 @@
   }
 
   /**
-   * Determines whether the input is a valid key. Valid keys match the regular
-   * expression [\w\-\.]+. The logic is not done using java.util.regex.* as
-   * that is 20X slower.
-   *
-   * @param key the key to validate.
-   * @return true if the key is a valid appdata key, false otherwise.
-   */
-  public static boolean isValidKey(String key) {
-    if (key == null || key.length() == 0) {
-      return false;
-    }
-    for (int i = 0; i < key.length(); ++i) {
-      char c = key.charAt(i);
-      if ((c >= 'a' && c <= 'z')
-          || (c >= 'A' && c <= 'Z')
-          || (c >= '0' && c <= '9')
-          || (c == '-')
-          || (c == '_') || (c == '.')) {
-        continue;
-      }
-      return false;
-    }
-    return true;
-  }
-
-  /**
-   * Get the set of user id's from a user and group.
+   * Get the set of user id's from a user and group
    */
   private Set<String> getIdSet(UserId user, GroupId group, SecurityToken token)
       throws JSONException {


Reply via email to