Updated Branches:
  refs/heads/api_refactoring 39db3be29 -> 623e7389e

api: Make APIServer backward compatible to accept both uuid and id for pre 3.x 
apis

- Allow both uuid and id in param for pre 3.x apis
- Enforce uuid as param for all >= 3.x apis
- Use regex to better match uuid param

Signed-off-by: Rohit Yadav <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/commit/623e7389
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/tree/623e7389
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/diff/623e7389

Branch: refs/heads/api_refactoring
Commit: 623e7389ef0b2923b7859629cd70406e2e471525
Parents: 39db3be
Author: Rohit Yadav <[email protected]>
Authored: Thu Dec 27 15:24:17 2012 -0800
Committer: Rohit Yadav <[email protected]>
Committed: Thu Dec 27 15:24:17 2012 -0800

----------------------------------------------------------------------
 server/src/com/cloud/api/ApiDispatcher.java |   25 +++++++++++++++++++--
 1 files changed, 22 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cloudstack/blob/623e7389/server/src/com/cloud/api/ApiDispatcher.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/ApiDispatcher.java 
b/server/src/com/cloud/api/ApiDispatcher.java
index b2b9ec2..b81e070 100755
--- a/server/src/com/cloud/api/ApiDispatcher.java
+++ b/server/src/com/cloud/api/ApiDispatcher.java
@@ -540,10 +540,29 @@ public class ApiDispatcher {
             // APITODO: Find and get rid of all hardcoded params in API Cmds 
and service layer
             return -1L;
         }
+        Long internalId = null;
+        // If annotation's empty, the cmd existed before 3.x try conversion to 
long
+        // FIXME: Fails if someone adds since field for any pre 3.x apis
+        boolean isPre3x = annotation.since().isEmpty();
+        // Match against Java's UUID regex to check if input is uuid string
+        boolean isUuid = 
uuid.matches("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$");
+        // Enforce that it's uuid for newly added apis from version 3.x
+        if (!isPre3x && !isUuid)
+            return null;
+        // Allow both uuid and internal id for pre3x apis
+        if (isPre3x && !isUuid) {
+            try {
+                internalId = Long.parseLong(uuid);
+            } catch(NumberFormatException e) {
+                // In case regex failed, and it's still uuid string
+                internalId = null;
+            }
+            if (internalId != null)
+                return internalId;
+        }
         // There may be multiple entities defined on the @EntityReference of a 
Response.class
         // UUID CommandType would expect only one entityType, so use the first 
entityType
         Class<?>[] entities = 
annotation.entityType()[0].getAnnotation(EntityReference.class).value();
-        Long internalId = null;
         // Go through each entity which is an interface to a VO class and get 
a VO object
         // Try to getId() for the object using reflection, break on first 
non-null value
         for (Class<?> entity: entities) {
@@ -662,8 +681,8 @@ public class ApiDispatcher {
                 Long internalId = 
translateUuidToInternalId(paramObj.toString(), annotation);
                 // If id is null, entity with the uuid was not found, throw 
exception
                 if (internalId == null) {
-                    throw new InvalidParameterValueException("No entity with " 
+ field.getName() + "(uuid)="
-                            + paramObj.toString() + " was found in the 
database.");
+                    throw new InvalidParameterValueException("Object entity 
with " + field.getName() + "(uuid)="
+                            + paramObj.toString() + " was not found.");
                 }
                 field.set(cmdObj, internalId);
                 break;

Reply via email to