Author: ivol37 at gmail.com
Date: Wed Feb  9 14:11:28 2011
New Revision: 770

Log:
[AMDATU-286] Fixed adding properties to result of REST call to retrieve user 
information

Added:
   
trunk/amdatu-libraries/utilities/src/main/java/org/amdatu/libraries/utilities/xml/
   
trunk/amdatu-libraries/utilities/src/main/java/org/amdatu/libraries/utilities/xml/StringMapAdapter.java
   
trunk/amdatu-libraries/utilities/src/main/java/org/amdatu/libraries/utilities/xml/StringMapEntryType.java
   
trunk/amdatu-libraries/utilities/src/main/java/org/amdatu/libraries/utilities/xml/StringMapType.java
Modified:
   
trunk/amdatu-authorization/useradmin-rest/src/main/java/org/amdatu/authorization/useradmin/rest/bean/RoleBean.java
   
trunk/amdatu-authorization/useradmin-rest/src/main/java/org/amdatu/authorization/useradmin/rest/service/ResourceBase.java

Modified: 
trunk/amdatu-authorization/useradmin-rest/src/main/java/org/amdatu/authorization/useradmin/rest/bean/RoleBean.java
==============================================================================
--- 
trunk/amdatu-authorization/useradmin-rest/src/main/java/org/amdatu/authorization/useradmin/rest/bean/RoleBean.java
  (original)
+++ 
trunk/amdatu-authorization/useradmin-rest/src/main/java/org/amdatu/authorization/useradmin/rest/bean/RoleBean.java
  Wed Feb  9 14:11:28 2011
@@ -18,7 +18,11 @@
 
 import java.net.URISyntaxException;
 import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.ws.rs.core.UriBuilder;
 import javax.xml.bind.annotation.XmlAccessType;
@@ -26,8 +30,10 @@
 import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
 
 import org.amdatu.libraries.utilities.rest.AtomSyndicationLink;
+import org.amdatu.libraries.utilities.xml.StringMapAdapter;
 import org.apache.wink.common.internal.uri.UriEncoder;
 import org.osgi.service.useradmin.Group;
 import org.osgi.service.useradmin.Role;
@@ -56,6 +62,10 @@
     @XmlElement(name = "basicMembers")
     private List<RoleBean> m_basicMembers;
 
+    @XmlJavaTypeAdapter(StringMapAdapter.class)
+    @XmlElement(name = "properties")
+    Map<String, String> m_properties = new HashMap<String, String>();
+
     public RoleBean() {
     }
 
@@ -116,12 +126,20 @@
         m_basicMembers = basicMembers;
     }
 
+    public Map<String, String> getProperties() {
+        return m_properties;
+    }
+
+    public void setProperties(Map<String, String> properties) {
+        m_properties = properties;
+    }
+
     public static RoleBean fromRole(Role role, UserAdmin userAdmin) throws 
URISyntaxException {
         return fromRole(role, userAdmin, true);
     }
-    
+
     private static String encode(String name) {
-               return UriEncoder.encodeString(name);
+        return UriEncoder.encodeString(name);
     }
 
     public static RoleBean fromRole(Role role, UserAdmin userAdmin, boolean 
loadMembers) throws URISyntaxException {
@@ -129,6 +147,7 @@
         String name = role.getName();
         roleBean.setName(name);
         roleBean.setType(role.getType());
+        roleBean.setProperties(toStringMap(role.getProperties()));
         String href = "/rest/";
         if (role.getType() == Role.USER) {
             String[] impliedRoles = userAdmin.getAuthorization((User) 
role).getRoles();
@@ -162,4 +181,16 @@
         roleBean.setLink(new 
AtomSyndicationLink().setHref(href).setRel("Alternate").setType("application/json"));
         return roleBean;
     }
+
+    @SuppressWarnings("unchecked")
+    private static Map<String, String> toStringMap(Dictionary dic) {
+        Map<String, String> map = new HashMap<String, String>();
+        Enumeration keyEnum = dic.keys();
+        while (keyEnum.hasMoreElements()) {
+            Object key = keyEnum.nextElement();
+            Object value = dic.get(key);
+            map.put(key.toString(), value.toString());
+        }
+        return map;
+    }
 }

Modified: 
trunk/amdatu-authorization/useradmin-rest/src/main/java/org/amdatu/authorization/useradmin/rest/service/ResourceBase.java
==============================================================================
--- 
trunk/amdatu-authorization/useradmin-rest/src/main/java/org/amdatu/authorization/useradmin/rest/service/ResourceBase.java
   (original)
+++ 
trunk/amdatu-authorization/useradmin-rest/src/main/java/org/amdatu/authorization/useradmin/rest/service/ResourceBase.java
   Wed Feb  9 14:11:28 2011
@@ -112,7 +112,7 @@
                     if (roleType == Role.ROLE) {
                         filteredRoles.add(role);
                     } else if (role.getType() == roleType) {
-                        if (filter == null || role.getName().matches("?i:.*" + 
filter + ".*")) {
+                        if (filter == null || role.getName().matches("(?i:.*" 
+ filter + ".*)")) {
                             filteredRoles.add(role);
                         }
                     }

Added: 
trunk/amdatu-libraries/utilities/src/main/java/org/amdatu/libraries/utilities/xml/StringMapAdapter.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-libraries/utilities/src/main/java/org/amdatu/libraries/utilities/xml/StringMapAdapter.java
     Wed Feb  9 14:11:28 2011
@@ -0,0 +1,59 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.libraries.utilities.xml;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.xml.bind.annotation.adapters.XmlAdapter;
+
+/**
+ * This String map adapter is is a helper class to convert a Map<String, 
String> to XML and
+ * visa versa. It can be used by JAX-RS annotated classes to annotate Java 
methods with parameters
+ * or type Map<String, String> such that they can be marshallled/unmarshalled 
to/from XML.
+ * 
+ * @author ivol
+ */
+ at SuppressWarnings("restriction")
+public class StringMapAdapter extends XmlAdapter<StringMapType, Map<String, 
String>> {
+    /**
+     * Marshals a Java object of type Map<String, String> to a StringMapType, 
which can be
+     * transformed using JAXB to XML.
+     */
+    public StringMapType marshal(Map<String, String> map) throws Exception {
+        StringMapType mapType = new StringMapType();
+        for(Entry<String, String> entry : map.entrySet()) {
+            StringMapEntryType mapEntryType = new StringMapEntryType();
+            mapEntryType.key = entry.getKey();
+            mapEntryType.value = entry.getValue();
+            mapType.entry.add(mapEntryType);
+        }
+        return mapType;
+    }
+
+    /**
+     * Unmarshals a Java object of type Map<String, String> from a 
StringMapType.
+     */
+    public Map<String, String> unmarshal(StringMapType mapType) throws 
Exception {
+        HashMap<String, String> hashMap = new HashMap<String, String>();
+        for(StringMapEntryType myEntryType : mapType.entry) {
+            hashMap.put(myEntryType.key, myEntryType.value);
+        }
+        return hashMap;
+    }
+}

Added: 
trunk/amdatu-libraries/utilities/src/main/java/org/amdatu/libraries/utilities/xml/StringMapEntryType.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-libraries/utilities/src/main/java/org/amdatu/libraries/utilities/xml/StringMapEntryType.java
   Wed Feb  9 14:11:28 2011
@@ -0,0 +1,40 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.libraries.utilities.xml;
+
+import javax.xml.bind.annotation.XmlAttribute;
+
+/**
+ * This class represents a single entry in a Java object of type Map<String, 
String>, used by
+ * StringMapAdapter.
+ * 
+ * @author ivol
+ */
+ at SuppressWarnings("restriction")
+public class StringMapEntryType {
+    /**
+     * The key under which the entry is stored.
+     */
+    @XmlAttribute(name = "key")
+    public String key;
+
+    /**
+     * The value of the entry.
+     */
+    @XmlAttribute(name = "value")
+    public String value;
+}

Added: 
trunk/amdatu-libraries/utilities/src/main/java/org/amdatu/libraries/utilities/xml/StringMapType.java
==============================================================================
--- (empty file)
+++ 
trunk/amdatu-libraries/utilities/src/main/java/org/amdatu/libraries/utilities/xml/StringMapType.java
        Wed Feb  9 14:11:28 2011
@@ -0,0 +1,37 @@
+/*
+    Copyright (C) 2010 Amdatu.org
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.amdatu.libraries.utilities.xml;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.bind.annotation.XmlElement;
+
+/**
+ * This class represents the entries in a Java object of type Map<String, 
String>, used by
+ * StringMapAdapter.
+ * 
+ * @author ivol
+ */
+ at SuppressWarnings("restriction")
+public class StringMapType {
+    /**
+     * The entries in the map.
+     */
+    @XmlElement(name = "entries")
+    public List<StringMapEntryType> entry = new 
ArrayList<StringMapEntryType>();
+}

Reply via email to