Author: [email protected]
Date: Tue Aug 16 16:01:40 2011
New Revision: 1327

Log:
[AMDATUOPENSOCIAL-89] Fixed returning a person from a User, even when no 
opensocial properties are available yet.

Modified:
   
trunk/amdatu-opensocial/opensocial-profile/src/main/java/org/amdatu/opensocial/profile/service/PersonServiceImpl.java

Modified: 
trunk/amdatu-opensocial/opensocial-profile/src/main/java/org/amdatu/opensocial/profile/service/PersonServiceImpl.java
==============================================================================
--- 
trunk/amdatu-opensocial/opensocial-profile/src/main/java/org/amdatu/opensocial/profile/service/PersonServiceImpl.java
       (original)
+++ 
trunk/amdatu-opensocial/opensocial-profile/src/main/java/org/amdatu/opensocial/profile/service/PersonServiceImpl.java
       Tue Aug 16 16:01:40 2011
@@ -13,303 +13,285 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.amdatu.opensocial.profile.service;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.Serializable;
-import java.io.UnsupportedEncodingException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
-import java.util.concurrent.Future;
-
-import org.amdatu.opensocial.profile.PersonService;
-import org.apache.shindig.auth.SecurityToken;
-import org.apache.shindig.common.util.ImmediateFuture;
-import org.apache.shindig.protocol.RestfulCollection;
-import org.apache.shindig.social.core.model.NameImpl;
-import org.apache.shindig.social.core.model.PersonImpl;
-import org.apache.shindig.social.opensocial.model.Person;
-import org.apache.shindig.social.opensocial.spi.CollectionOptions;
-import org.apache.shindig.social.opensocial.spi.GroupId;
-import org.apache.shindig.social.opensocial.spi.UserId;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.service.log.LogService;
-import org.osgi.service.useradmin.Role;
-import org.osgi.service.useradmin.User;
-
-/**
- * Implementation of the person service. The person service itself is not 
tenant aware
- * 
- * @author ivol
- */
-public class PersonServiceImpl implements PersonService {
-    // Services injected by the Felix dependency manager
-    private volatile LogService m_logService;
-
-    public Future<RestfulCollection<Person>> getPeople(final Set<UserId> 
userIds, final GroupId groupId,
-            final CollectionOptions collectionOptions, final Set<String> 
fields, final SecurityToken token) {
-        List<Person> friends = new ArrayList<Person>();
-        if (groupId.getType().equals(GroupId.Type.friends)) {
-            // Retrieve friends of the specified user ids
-            for (UserId userId : userIds) {
-                Person person = getPerson(userId.getUserId(token));
-                if (person != null) {
-                    List<Person> personFriends = getFriends(person);
-                    for (Person friend : personFriends) {
-                        friends.add(friend);
-                    }
-                }
-            }
-        }
-        return ImmediateFuture.newInstance(new 
RestfulCollection<Person>(friends));
-    }
-
-    public Future<Person> getPerson(final UserId id, final Set<String> fields, 
final SecurityToken token) {
-        if (!"".equals(id.getUserId(token))) {
-            Person person = getPerson(id.getUserId(token));
-            if (person != null) {
-                return ImmediateFuture.newInstance(person);
-            }
-            else {
-                return null;
-            }
-        }
-        else {
-            // This is an anonymous user, return anonymous Person
-            Person result = new PersonImpl("", "Anonymous user", new 
NameImpl("Anonymous"));
-            return ImmediateFuture.newInstance(result);
-        }
-    }
-
-    public List<Person> getPersons() {
-        m_logService.log(LogService.LOG_DEBUG, "Retrieving all persons");
-        List<Person> result = new ArrayList<Person>();
-        try {
-            // First retrieve all Users
-            Role[] allRoles = 
TenantHostnameDispatchExtenderFilter.getUserAdmin().getRoles(null);
-            for (Role role : allRoles) {
-                if (role.getType() == Role.USER) {
-                    Object person = 
role.getProperties().get(PersonService.OPEN_SOCIAL_PERSON_PROPERTIES_KEY);
-                    if (person != null) {
-                        result.add(personFromBytes((byte[]) person));
-                    }
-                }
-            }
-        }
-        catch (InvalidSyntaxException e) {
-            m_logService.log(LogService.LOG_ERROR, "An error occurred 
retrieving all persons");
-        }
-        catch (ClassNotFoundException e) {
-            m_logService.log(LogService.LOG_ERROR, "An error occurred 
retrieving all persons");
-        }
-        catch (IOException e) {
-            m_logService.log(LogService.LOG_ERROR, "An error occurred 
retrieving all persons");
-        }
-        return result;
-    }
-
-    @SuppressWarnings("unchecked")
-    public void setPerson(final Person person, final User user) {
-        // Since person is not serializable, create a Serializable proxy 
object from it first
-        if (!(person instanceof Serializable)) {
-            m_logService.log(LogService.LOG_ERROR, person.getClass().getName()
-                    + " does not implement java.io.Serializable");
-            return;
-        }
-        try {
-            
user.getProperties().put(PersonService.OPEN_SOCIAL_PERSON_PROPERTIES_KEY, 
personToBytes(person));
-        }
-        catch (IOException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not set person for 
user '" + user.getName() + "'.", e);
-        }
-        catch (ClassNotFoundException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not set person for 
user '" + user.getName() + "'.", e);
-        }
-    }
-
-    public User getUser(final Person person) {
-        return getUser(person.getId());
-    }
-
-    private User getUser(final String personId) {
-        // Retrieved all persons that match the specified profile.
-        try {
-            Role userRole = 
TenantHostnameDispatchExtenderFilter.getUserAdmin().getRole(personId);
-            if (userRole != null && userRole.getType() == Role.USER) {
-                return (User) userRole;
-            }
-            else {
-                return null;
-            }
-        }
-        catch (InvalidSyntaxException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not retrieve user 
for person '" + personId + "'.", e);
-            return null;
-        }
-    }
-
-    public Person getPerson(final User user) {
-        try {
-            byte[] personBytes = (byte[]) 
user.getProperties().get(PersonService.OPEN_SOCIAL_PERSON_PROPERTIES_KEY);
-            if (personBytes != null) {
-                Person person = (Person) fromByteArray(personBytes);
-                return person;
-            }
-        }
-        catch (ClassNotFoundException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not retrieve person 
for user '" + user.getName() + "'", e);
-        }
-        catch (IOException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not retrieve person 
for user '" + user.getName() + "'", e);
-        }
-        return null;
-    }
-
-    @SuppressWarnings("unchecked")
-    public void setFriends(final String[] friends, final User user) {
-        byte[] friendBytes;
-        try {
-            friendBytes = friendsToBytes(friends);
-            
user.getProperties().put(PersonService.OPEN_SOCIAL_FRIENDS_PROPERTIES_KEY, 
friendBytes);
-        }
-        catch (IOException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not set the friends 
for user '" + user.getName() + "'.", e);
-        }
-        catch (ClassNotFoundException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not set the friends 
for user '" + user.getName() + "'.", e);
-        }
-    }
-
-    public List<Person> getFriends(final Person person) {
-        List<Person> friends = new ArrayList<Person>();
-        try {
-            User user = getUser(person);
-            byte[] friendBytes = (byte[]) 
user.getProperties().get(OPEN_SOCIAL_FRIENDS_PROPERTIES_KEY);
-            String[] friendIds = friendsFromBytes(friendBytes);
-            for (String friendId : friendIds) {
-                Person friend = getPerson(friendId);
-                friends.add(friend);
-            }
-        }
-        catch (UnsupportedEncodingException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not retrieve the 
friends for person '" + person.getId()
-                    + "'.", e);
-        }
-        catch (ClassNotFoundException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not retrieve the 
friends for person '" + person.getId()
-                    + "'.", e);
-        }
-        catch (IOException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not retrieve the 
friends for person '" + person.getId()
-                    + "'.", e);
-        }
-        return friends;
-    }
-
-    // Returns the person with the specified id.
-    private Person getPerson(final String id) {
-        User user = null;
-        if (id != null && !"".equals(id)) {
-            user = getUser(id);
-        }
-        if (user != null) {
-            try {
-                byte[] personBytes = (byte[]) 
user.getProperties().get(PersonService.OPEN_SOCIAL_PERSON_PROPERTIES_KEY);
-                if (personBytes != null) {
-                    Person person = (Person) fromByteArray(personBytes);
-                    return person;
-                }
-            }
-            catch (UnsupportedEncodingException e) {
-                m_logService.log(LogService.LOG_ERROR, "Could not retrieve the 
friends for person '" + id + "'.", e);
-            }
-            catch (ClassNotFoundException e) {
-                m_logService.log(LogService.LOG_ERROR, "Could not retrieve the 
friends for person '" + id + "'.", e);
-            }
-            catch (IOException e) {
-                m_logService.log(LogService.LOG_ERROR, "Could not retrieve the 
friends for person '" + id + "'.", e);
-            }
-        }
-        return null;
-
-    }
-
-    // Converts an array of bytes into a Person.
-    private Person personFromBytes(final byte[] personBytes) throws 
ClassNotFoundException, IOException {
-        Person person = (Person) fromByteArray(personBytes);
-        return person;
-    }
-
-    // Converts a person into an array of bytes
-    private byte[] personToBytes(final Person person) throws 
ClassNotFoundException, IOException {
-        byte[] bytes = toByteArray(person);
-        return bytes;
-    }
-
-    // Converts an array of bytes into a String[].
-    private String[] friendsFromBytes(final byte[] friendBytes) throws 
ClassNotFoundException, IOException {
-        String[] friends = (String[]) fromByteArray(friendBytes);
-        return friends;
-    }
-
-    // Converts a String[] into an array of bytes
-    private byte[] friendsToBytes(final String[] friends) throws 
ClassNotFoundException, IOException {
-        byte[] bytes = toByteArray(friends);
-        return bytes;
-    }
-
-    // Converts a serializable Java object to a byte array
-    private byte[] toByteArray(final Object object) throws IOException {
-        ByteArrayOutputStream bos = null;
-        ObjectOutputStream oos = null;
-        try {
-            bos = new ByteArrayOutputStream();
-            oos = new ObjectOutputStream(bos);
-            oos.writeObject(object);
-            oos.flush();
-        }
-        finally {
-            try {
-                if (oos != null) {
-                    oos.close();
-                }
-            }
-            finally {
-                if (bos != null) {
-                    bos.close();
-                }
-            }
-        }
-        return bos.toByteArray();
-    }
-
-    // Converts a persisted byte array to a Java object
-    private Object fromByteArray(final byte[] bytes) throws 
ClassNotFoundException, IOException {
-        ByteArrayInputStream bis = null;
-        ObjectInputStream ois = null;
-        Object data = null;
-        try {
-            bis = new ByteArrayInputStream(bytes);
-            ois = new ObjectInputStream(bis);
-            data = ois.readObject();
-        }
-        finally {
-            try {
-                if (ois != null) {
-                    ois.close();
-                }
-            }
-            finally {
-                if (bis != null) {
-                    bis.close();
-                }
-            }
-        }
-        return data;
-    }
-}
+package org.amdatu.opensocial.profile.service;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.Future;
+
+import org.amdatu.opensocial.profile.PersonService;
+import org.apache.shindig.auth.SecurityToken;
+import org.apache.shindig.common.util.ImmediateFuture;
+import org.apache.shindig.protocol.RestfulCollection;
+import org.apache.shindig.social.core.model.NameImpl;
+import org.apache.shindig.social.core.model.PersonImpl;
+import org.apache.shindig.social.opensocial.model.Person;
+import org.apache.shindig.social.opensocial.spi.CollectionOptions;
+import org.apache.shindig.social.opensocial.spi.GroupId;
+import org.apache.shindig.social.opensocial.spi.UserId;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.service.log.LogService;
+import org.osgi.service.useradmin.Role;
+import org.osgi.service.useradmin.User;
+
+/**
+ * Implementation of the person service. The person service itself is not 
tenant aware
+ * 
+ * @author ivol
+ */
+public class PersonServiceImpl implements PersonService {
+    // Services injected by the Felix dependency manager
+    private volatile LogService m_logService;
+
+    public Future<RestfulCollection<Person>> getPeople(final Set<UserId> 
userIds, final GroupId groupId,
+            final CollectionOptions collectionOptions, final Set<String> 
fields, final SecurityToken token) {
+        List<Person> friends = new ArrayList<Person>();
+        if (groupId.getType().equals(GroupId.Type.friends)) {
+            // Retrieve friends of the specified user ids
+            for (UserId userId : userIds) {
+                Person person = getPerson(userId.getUserId(token));
+                if (person != null) {
+                    List<Person> personFriends = getFriends(person);
+                    for (Person friend : personFriends) {
+                        friends.add(friend);
+                    }
+                }
+            }
+        }
+        return ImmediateFuture.newInstance(new 
RestfulCollection<Person>(friends));
+    }
+
+    public Future<Person> getPerson(final UserId id, final Set<String> fields, 
final SecurityToken token) {
+        if (!"".equals(id.getUserId(token))) {
+            Person person = getPerson(id.getUserId(token));
+            if (person != null) {
+                return ImmediateFuture.newInstance(person);
+            }
+            else {
+                return null;
+            }
+        }
+        else {
+            // This is an anonymous user, return anonymous Person
+            Person result = new PersonImpl("", "Anonymous user", new 
NameImpl("Anonymous"));
+            return ImmediateFuture.newInstance(result);
+        }
+    }
+
+    public List<Person> getPersons() {
+        m_logService.log(LogService.LOG_DEBUG, "Retrieving all persons");
+        List<Person> result = new ArrayList<Person>();
+        try {
+            // First retrieve all Users
+            Role[] allRoles = 
TenantHostnameDispatchExtenderFilter.getUserAdmin().getRoles(null);
+            for (Role role : allRoles) {
+                if (role.getType() == Role.USER) {
+                    result.add(getPerson((User) role));
+                }
+            }
+        }
+        catch (InvalidSyntaxException e) {
+            m_logService.log(LogService.LOG_ERROR, "An error occurred 
retrieving all persons");
+        }
+        return result;
+    }
+
+    @SuppressWarnings("unchecked")
+    public void setPerson(final Person person, final User user) {
+        // Since person is not serializable, create a Serializable proxy 
object from it first
+        if (!(person instanceof Serializable)) {
+            m_logService.log(LogService.LOG_ERROR, person.getClass().getName()
+                    + " does not implement java.io.Serializable");
+            return;
+        }
+        try {
+            
user.getProperties().put(PersonService.OPEN_SOCIAL_PERSON_PROPERTIES_KEY, 
personToBytes(person));
+        }
+        catch (IOException e) {
+            m_logService.log(LogService.LOG_ERROR, "Could not set person for 
user '" + user.getName() + "'.", e);
+        }
+        catch (ClassNotFoundException e) {
+            m_logService.log(LogService.LOG_ERROR, "Could not set person for 
user '" + user.getName() + "'.", e);
+        }
+    }
+
+    public User getUser(final Person person) {
+        return getUser(person.getId());
+    }
+
+    private User getUser(final String personId) {
+        // Retrieved all persons that match the specified profile.
+        try {
+            Role userRole = 
TenantHostnameDispatchExtenderFilter.getUserAdmin().getRole(personId);
+            if (userRole != null && userRole.getType() == Role.USER) {
+                return (User) userRole;
+            }
+            else {
+                return null;
+            }
+        }
+        catch (InvalidSyntaxException e) {
+            m_logService.log(LogService.LOG_ERROR, "Could not retrieve user 
for person '" + personId + "'.", e);
+            return null;
+        }
+    }
+
+    public Person getPerson(final User user) {
+        try {
+            byte[] personBytes = (byte[]) 
user.getProperties().get(PersonService.OPEN_SOCIAL_PERSON_PROPERTIES_KEY);
+            Person person;
+            if (personBytes != null) {
+                person = personFromBytes(personBytes);
+            }
+            else {
+                person = new PersonImpl();
+                person.setId(user.getName());
+            }
+            return person;
+        }
+        catch (ClassNotFoundException e) {
+            m_logService.log(LogService.LOG_ERROR, "Could not retrieve person 
for user '" + user.getName() + "'", e);
+        }
+        catch (IOException e) {
+            m_logService.log(LogService.LOG_ERROR, "Could not retrieve person 
for user '" + user.getName() + "'", e);
+        }
+        return null;
+    }
+
+    @SuppressWarnings("unchecked")
+    public void setFriends(final String[] friends, final User user) {
+        byte[] friendBytes;
+        try {
+            friendBytes = friendsToBytes(friends);
+            
user.getProperties().put(PersonService.OPEN_SOCIAL_FRIENDS_PROPERTIES_KEY, 
friendBytes);
+        }
+        catch (IOException e) {
+            m_logService.log(LogService.LOG_ERROR, "Could not set the friends 
for user '" + user.getName() + "'.", e);
+        }
+        catch (ClassNotFoundException e) {
+            m_logService.log(LogService.LOG_ERROR, "Could not set the friends 
for user '" + user.getName() + "'.", e);
+        }
+    }
+
+    public List<Person> getFriends(final Person person) {
+        List<Person> friends = new ArrayList<Person>();
+        try {
+            User user = getUser(person);
+            byte[] friendBytes = (byte[]) 
user.getProperties().get(OPEN_SOCIAL_FRIENDS_PROPERTIES_KEY);
+            if (friendBytes != null) {
+                String[] friendIds = friendsFromBytes(friendBytes);
+                for (String friendId : friendIds) {
+                    Person friend = getPerson(friendId);
+                    friends.add(friend);
+                }
+            }
+        }
+        catch (UnsupportedEncodingException e) {
+            m_logService.log(LogService.LOG_ERROR, "Could not retrieve the 
friends for person '" + person.getId()
+                    + "'.", e);
+        }
+        catch (ClassNotFoundException e) {
+            m_logService.log(LogService.LOG_ERROR, "Could not retrieve the 
friends for person '" + person.getId()
+                    + "'.", e);
+        }
+        catch (IOException e) {
+            m_logService.log(LogService.LOG_ERROR, "Could not retrieve the 
friends for person '" + person.getId()
+                    + "'.", e);
+        }
+        return friends;
+    }
+
+    // Returns the person with the specified id.
+    private Person getPerson(final String id) {
+        User user = null;
+        if (id != null && !"".equals(id)) {
+            user = getUser(id);
+        }
+        if (user != null) {
+            return getPerson(user);
+        }
+        return null;
+    }
+
+    // Converts an array of bytes into a Person.
+    private Person personFromBytes(final byte[] personBytes) throws 
ClassNotFoundException, IOException {
+        Person person = (Person) fromByteArray(personBytes);
+        return person;
+    }
+
+    // Converts a person into an array of bytes
+    private byte[] personToBytes(final Person person) throws 
ClassNotFoundException, IOException {
+        byte[] bytes = toByteArray(person);
+        return bytes;
+    }
+
+    // Converts an array of bytes into a String[].
+    private String[] friendsFromBytes(final byte[] friendBytes) throws 
ClassNotFoundException, IOException {
+        String[] friends = (String[]) fromByteArray(friendBytes);
+        return friends;
+    }
+
+    // Converts a String[] into an array of bytes
+    private byte[] friendsToBytes(final String[] friends) throws 
ClassNotFoundException, IOException {
+        byte[] bytes = toByteArray(friends);
+        return bytes;
+    }
+
+    // Converts a serializable Java object to a byte array
+    private byte[] toByteArray(final Object object) throws IOException {
+        ByteArrayOutputStream bos = null;
+        ObjectOutputStream oos = null;
+        try {
+            bos = new ByteArrayOutputStream();
+            oos = new ObjectOutputStream(bos);
+            oos.writeObject(object);
+            oos.flush();
+        }
+        finally {
+            try {
+                if (oos != null) {
+                    oos.close();
+                }
+            }
+            finally {
+                if (bos != null) {
+                    bos.close();
+                }
+            }
+        }
+        return bos.toByteArray();
+    }
+
+    // Converts a persisted byte array to a Java object
+    private Object fromByteArray(final byte[] bytes) throws 
ClassNotFoundException, IOException {
+        ByteArrayInputStream bis = null;
+        ObjectInputStream ois = null;
+        Object data = null;
+        try {
+            bis = new ByteArrayInputStream(bytes);
+            ois = new ObjectInputStream(bis);
+            data = ois.readObject();
+        }
+        finally {
+            try {
+                if (ois != null) {
+                    ois.close();
+                }
+            }
+            finally {
+                if (bis != null) {
+                    bis.close();
+                }
+            }
+        }
+        return data;
+    }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits

Reply via email to