Author: [email protected]
Date: Tue Feb 15 15:56:49 2011
New Revision: 792

Log:
[AMDATU-309] Improved error handling when tenant cannot be resolved

Modified:
   
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/CassandraAppDataServiceStore.java
   
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/TenantHostnameDispatchExtenderFilter.java

Modified: 
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/CassandraAppDataServiceStore.java
==============================================================================
--- 
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/CassandraAppDataServiceStore.java
   (original)
+++ 
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/persistence/CassandraAppDataServiceStore.java
   Tue Feb 15 15:56:49 2011
@@ -38,6 +38,7 @@
 import org.osgi.service.log.LogService;
 import org.osgi.service.useradmin.Role;
 import org.osgi.service.useradmin.User;
+import org.osgi.service.useradmin.UserAdmin;
 
 import com.google.common.collect.Maps;
 
@@ -50,109 +51,113 @@
  * @author ivol
  */
 public class CassandraAppDataServiceStore implements AppDataService {
-       // Services injected by the dependency manager
+    // Services injected by the dependency manager
+    private volatile LogService m_logService;
 
-       private volatile LogService m_logService;
-
-       public void start() {
-               m_logService.log(LogService.LOG_INFO, getClass().getName() + " 
service started");
-       }
-
-       @SuppressWarnings("unchecked")
-       public Future<Void> updatePersonData(UserId userId, GroupId groupId, 
String appId, Set<String> fields,
-                       Map<String, String> values, SecurityToken token) throws 
ProtocolException {
-               try {
-                       User user = getUser(userId, token);
-                       if (user != null) {
-                               // Convert the String Map to a byte array and 
write it to UserAdmin
-                               try {
-                                       byte[] bytesValue = 
ConversionUtil.objectToByteArray(values);
-                                       
user.getProperties().put(OpenSocialConstants.APP_DATA, bytesValue);
-                                       return 
ImmediateFuture.newInstance(null);
-                               } catch (IOException e) {
-                                       throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
-                                                       "Could not update 
AppData for user with id '" + userId.getUserId() + "'.", e);
-                               }
-                       }
-               } catch (InvalidSyntaxException e) {
-                       throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not 
update person data", e);
-               }
-               
-               // If no user is known, ignore this update person data request
-               return ImmediateFuture.newInstance(null);
-       }
-
-       @SuppressWarnings("unchecked")
-       public Future<DataCollection> getPersonData(Set<UserId> userIds, 
GroupId groupId, String appId, Set<String> fields,
-                       SecurityToken token) throws ProtocolException {
-               Map<String, Map<String, String>> idToData = Maps.newHashMap();
-               try {
-                       for (UserId userId : userIds) {
-                               User user = getUser(userId, token);
-                               if (user != null) {
-                                       byte[] bytesValue = (byte[]) 
user.getProperties().get(OpenSocialConstants.APP_DATA);
-                                       if (bytesValue != null) {
-                                               try {
-                                                       Map<String, String> 
values = (Map<String, String>) ConversionUtil.byteArrayToObject(bytesValue);
-                                                       
idToData.put(userId.getUserId(token), values);
-                                               } catch (ClassNotFoundException 
e) {
-                                                       throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
-                                                                       "Could 
not retrieve AppData for user with id '" + userId.getUserId() + "'.", e);
-                                               } catch (IOException e) {
-                                                       throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
-                                                                       "Could 
not retrieve AppData for user with id '" + userId.getUserId() + "'.", e);
-                                               }
-                                       } else {
-                                               
idToData.put(userId.getUserId(token), new HashMap<String, String>());
-                                       }
-                               }
-                       }
-               } catch (InvalidSyntaxException e) {
-                       throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not get 
person data", e);
-               }
-               return ImmediateFuture.newInstance(new 
DataCollection(idToData));
-       }
-
-       @SuppressWarnings("unchecked")
-       public Future<Void> deletePersonData(UserId userId, GroupId groupId, 
String appId, Set<String> fields,
-                       SecurityToken token) throws ProtocolException {
-               try {
-                       User user = getUser(userId, token);
-                       if (user != null) {
-                               // Convert the String Map to a byte array and 
write it to UserAdmin
-                               try {
-                                       byte[] bytesValue = (byte[]) 
user.getProperties().get(OpenSocialConstants.APP_DATA);
-                                       Map<String, String> values = 
(Map<String, String>) ConversionUtil.byteArrayToObject(bytesValue);
-                                       for (String field: fields) {
-                                               values.remove(field);
-                                       }
-                                       
user.getProperties().put(OpenSocialConstants.APP_DATA, 
ConversionUtil.objectToByteArray(values));
-                                       return 
ImmediateFuture.newInstance(null);
-                               } catch (IOException e) {
-                                       throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
-                                                       "Could not delete 
AppData for user with id '" + userId.getUserId() + "'.", e);
-                               } catch (ClassNotFoundException e) {
-                                       throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
-                                                       "Could not delete 
AppData for user with id '" + userId.getUserId() + "'.", e);
-                               }
-                       } 
-               } catch (InvalidSyntaxException e) {
-                       throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "User with id 
'" + userId.getUserId()
-                                       + "' not found", e);
-               }
-
-               throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "User with id 
'" + userId.getUserId()
-                               + "' not found");
-       }
-
-       private User getUser(UserId userId, SecurityToken token) throws 
InvalidSyntaxException {
-               String id = userId.getUserId(token);
-               if (!"".equals(userId.getUserId(token))) {
-                       Role user = 
TenantHostnameDispatchExtenderFilter.getUserAdmin().getRole(id);
-                       if (user != null) {
-                               return (User) user;
-                       }
-               }
-               return null;
-       }
+    public void start() {
+        m_logService.log(LogService.LOG_INFO, getClass().getName() + " service 
started");
+    }
+
+    @SuppressWarnings("unchecked")
+    public Future<Void> updatePersonData(UserId userId, GroupId groupId, 
String appId, Set<String> fields,
+        Map<String, String> values, SecurityToken token) throws 
ProtocolException {
+        try {
+            User user = getUser(userId, token);
+            if (user != null) {
+                // Convert the String Map to a byte array and write it to 
UserAdmin
+                try {
+                    byte[] bytesValue = 
ConversionUtil.objectToByteArray(values);
+                    user.getProperties().put(OpenSocialConstants.APP_DATA, 
bytesValue);
+                    return ImmediateFuture.newInstance(null);
+                } catch (IOException e) {
+                    throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+                        "Could not update AppData for user with id '" + 
userId.getUserId() + "'.", e);
+                }
+            }
+        } catch (InvalidSyntaxException e) {
+            throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not 
update person data", e);
+        }
+
+        // If no user is known, ignore this update person data request
+        return ImmediateFuture.newInstance(null);
+    }
+
+    @SuppressWarnings("unchecked")
+    public Future<DataCollection> getPersonData(Set<UserId> userIds, GroupId 
groupId, String appId, Set<String> fields,
+        SecurityToken token) throws ProtocolException {
+        Map<String, Map<String, String>> idToData = Maps.newHashMap();
+        try {
+            for (UserId userId : userIds) {
+                User user = getUser(userId, token);
+                if (user != null) {
+                    byte[] bytesValue = (byte[]) 
user.getProperties().get(OpenSocialConstants.APP_DATA);
+                    if (bytesValue != null) {
+                        try {
+                            Map<String, String> values = (Map<String, String>) 
ConversionUtil.byteArrayToObject(bytesValue);
+                            idToData.put(userId.getUserId(token), values);
+                        } catch (ClassNotFoundException e) {
+                            throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+                                "Could not retrieve AppData for user with id 
'" + userId.getUserId() + "'.", e);
+                        } catch (IOException e) {
+                            throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+                                "Could not retrieve AppData for user with id 
'" + userId.getUserId() + "'.", e);
+                        }
+                    } else {
+                        idToData.put(userId.getUserId(token), new 
HashMap<String, String>());
+                    }
+                }
+            }
+        } catch (InvalidSyntaxException e) {
+            throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not get 
person data", e);
+        }
+        return ImmediateFuture.newInstance(new DataCollection(idToData));
+    }
+
+    @SuppressWarnings("unchecked")
+    public Future<Void> deletePersonData(UserId userId, GroupId groupId, 
String appId, Set<String> fields,
+        SecurityToken token) throws ProtocolException {
+        try {
+            User user = getUser(userId, token);
+            if (user != null) {
+                // Convert the String Map to a byte array and write it to 
UserAdmin
+                try {
+                    byte[] bytesValue = (byte[]) 
user.getProperties().get(OpenSocialConstants.APP_DATA);
+                    Map<String, String> values = (Map<String, String>) 
ConversionUtil.byteArrayToObject(bytesValue);
+                    for (String field: fields) {
+                        values.remove(field);
+                    }
+                    user.getProperties().put(OpenSocialConstants.APP_DATA, 
ConversionUtil.objectToByteArray(values));
+                    return ImmediateFuture.newInstance(null);
+                } catch (IOException e) {
+                    throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+                        "Could not delete AppData for user with id '" + 
userId.getUserId() + "'.", e);
+                } catch (ClassNotFoundException e) {
+                    throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
+                        "Could not delete AppData for user with id '" + 
userId.getUserId() + "'.", e);
+                }
+            }
+        } catch (InvalidSyntaxException e) {
+            throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "User with id 
'" + userId.getUserId()
+                + "' not found", e);
+        }
+
+        throw new 
ProtocolException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "User with id 
'" + userId.getUserId()
+            + "' not found");
+    }
+
+    private User getUser(UserId userId, SecurityToken token) throws 
InvalidSyntaxException {
+        String id = userId.getUserId(token);
+        if (!"".equals(userId.getUserId(token))) {
+            UserAdmin userAdmin = 
TenantHostnameDispatchExtenderFilter.getUserAdmin();
+            if (userAdmin == null) {
+                m_logService.log(LogService.LOG_ERROR, "UserAdmin could not be 
resolved for the current tenant, no users can be retrieved");
+                return null;
+            }
+            Role user = userAdmin.getRole(id);
+            if (user != null) {
+                return (User) user;
+            }
+        }
+        return null;
+    }
 }

Modified: 
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/TenantHostnameDispatchExtenderFilter.java
==============================================================================
--- 
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/TenantHostnameDispatchExtenderFilter.java
       (original)
+++ 
trunk/amdatu-opensocial/shindig/src/main/java/org/amdatu/opensocial/shindig/service/TenantHostnameDispatchExtenderFilter.java
       Tue Feb 15 15:56:49 2011
@@ -70,7 +70,7 @@
         Tenant tenant = getTenant();
         ServiceReference[] refs = 
m_bundleContext.getAllServiceReferences(UserAdmin.class.getName(), null);
         for (ServiceReference ref : refs) {
-            if 
(tenant.getId().equals(ref.getProperty(Tenant.TENANT_ID_SERVICEPROPERTY))) {
+            if (tenant != null && 
tenant.getId().equals(ref.getProperty(Tenant.TENANT_ID_SERVICEPROPERTY))) {
                 return (UserAdmin) m_bundleContext.getService(ref);
             }
         }
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to