Improve properties
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 16 Sep 2015 11:14:21 +0000 (11:14 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 16 Sep 2015 11:14:21 +0000 (11:14 +0000)
Reduce visibility of some classes.

git-svn-id: https://svn.argeo.org/commons/trunk@8411 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeUserAdmin.java
org.argeo.security.core/src/org/argeo/osgi/useradmin/AbstractUserDirectory.java
org.argeo.security.core/src/org/argeo/osgi/useradmin/DirectoryGroup.java
org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifAuthorization.java
org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifGroup.java
org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifName.java
org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifUserAdmin.java
org.argeo.security.core/src/org/argeo/osgi/useradmin/LdifWriter.java
org.argeo.security.core/src/org/argeo/osgi/useradmin/UserAdminConf.java [new file with mode: 0644]
org.argeo.security.core/src/org/argeo/osgi/useradmin/UserAdminProps.java [deleted file]

index d19324f2518766ac49523da3dc50f70300ccdbd5..416ecef6ba9942413511b595b504c0f5e449c691 100644 (file)
@@ -24,7 +24,7 @@ import org.apache.commons.logging.LogFactory;
 import org.argeo.cms.CmsException;
 import org.argeo.cms.KernelHeader;
 import org.argeo.osgi.useradmin.UserDirectory;
-import org.argeo.osgi.useradmin.UserAdminProps;
+import org.argeo.osgi.useradmin.UserAdminConf;
 import org.argeo.osgi.useradmin.LdapUserAdmin;
 import org.argeo.osgi.useradmin.LdifUserAdmin;
 import org.argeo.osgi.useradmin.UserDirectoryException;
@@ -89,7 +89,7 @@ public class NodeUserAdmin implements UserAdmin {
                                throw new CmsException(
                                                "Cannot interpret " + uri + " as an uri", e);
                        }
-                       Dictionary<String, ?> properties = UserAdminProps.uriAsProperties(u
+                       Dictionary<String, ?> properties = UserAdminConf.uriAsProperties(u
                                        .toString());
                        UserDirectory businessRoles;
                        if (u.getScheme().startsWith("ldap")) {
@@ -120,9 +120,9 @@ public class NodeUserAdmin implements UserAdmin {
                        nodeRolesUri = nodeRolesFile.toURI().toString();
                }
 
-               Dictionary<String, ?> nodeRolesProperties = UserAdminProps
+               Dictionary<String, ?> nodeRolesProperties = UserAdminConf
                                .uriAsProperties(nodeRolesUri);
-               if (!nodeRolesProperties.get(UserAdminProps.baseDn.property())
+               if (!nodeRolesProperties.get(UserAdminConf.baseDn.property())
                                .equals(baseNodeRoleDn)) {
                        throw new CmsException("Invalid base dn for node roles");
                        // TODO deal with "mounted" roles with a different baseDN
@@ -147,7 +147,7 @@ public class NodeUserAdmin implements UserAdmin {
                        if (userAdmins.get(name) instanceof UserDirectory) {
                                UserDirectory userDirectory = (UserDirectory) userAdmins
                                                .get(name);
-                               String uri = UserAdminProps.propertiesAsUri(
+                               String uri = UserAdminConf.propertiesAsUri(
                                                userDirectory.getProperties()).toString();
                                res.put(uri, "");
                        } else {
index 18cb5ece251b1e81fc4b990dc0b5bbbee67d81a8..1d2e72759b479151ddf032d8a8f9a1ac19ef5bd2 100644 (file)
@@ -72,7 +72,7 @@ abstract class AbstractUserDirectory implements UserAdmin, UserDirectory {
                // TODO make a copy?
                this.properties = properties;
 
-               String uriStr = UserAdminProps.uri.getValue(properties);
+               String uriStr = UserAdminConf.uri.getValue(properties);
                if (uriStr == null)
                        uri = null;
                else
@@ -82,16 +82,16 @@ abstract class AbstractUserDirectory implements UserAdmin, UserDirectory {
                                throw new UserDirectoryException("Badly formatted URI", e);
                        }
 
-               baseDn = UserAdminProps.baseDn.getValue(properties).toString();
-               String isReadOnly = UserAdminProps.readOnly.getValue(properties);
+               baseDn = UserAdminConf.baseDn.getValue(properties).toString();
+               String isReadOnly = UserAdminConf.readOnly.getValue(properties);
                if (isReadOnly == null)
                        this.isReadOnly = readOnlyDefault(uri);
                else
                        this.isReadOnly = new Boolean(isReadOnly);
 
-               this.userObjectClass = UserAdminProps.userObjectClass
+               this.userObjectClass = UserAdminConf.userObjectClass
                                .getValue(properties);
-               this.groupObjectClass = UserAdminProps.groupObjectClass
+               this.groupObjectClass = UserAdminConf.groupObjectClass
                                .getValue(properties);
        }
 
index bb64c26e1f0dd920f85202e523721f716d5561e9..9653948ac169aa8ab00788082b6b649504a80ff2 100644 (file)
@@ -6,6 +6,6 @@ import javax.naming.ldap.LdapName;
 
 import org.osgi.service.useradmin.Group;
 
-public interface DirectoryGroup extends Group, DirectoryUser {
+interface DirectoryGroup extends Group, DirectoryUser {
        List<LdapName> getMemberNames();
 }
index 147001b0a0701a4ad95c8e996f2aeaebdec48719..db549e30ec4064f274385821097f6cf2fecaec6b 100644 (file)
@@ -9,7 +9,7 @@ import org.osgi.service.useradmin.Authorization;
 import org.osgi.service.useradmin.Role;
 import org.osgi.service.useradmin.User;
 
-public class LdifAuthorization implements Authorization {
+class LdifAuthorization implements Authorization {
        private final String name;
        private final String displayName;
        private final List<String> allRoles;
index cf65784e09b5ef0ce822ab3776b9ef85f9aad607..1f7bf3760b46534df0e3c9239521029c20fbc8a7 100644 (file)
@@ -10,7 +10,7 @@ import javax.naming.ldap.LdapName;
 
 import org.osgi.service.useradmin.Role;
 
-public class LdifGroup extends LdifUser implements DirectoryGroup {
+class LdifGroup extends LdifUser implements DirectoryGroup {
        private final String memberAttributeId;
 
        public LdifGroup(AbstractUserDirectory userAdmin, LdapName dn,
index 0534267b3503787439600bef4d8cbd2f2deb499e..38f3eaca3f8c1d44a31af40d7c5bce16afc4d935 100644 (file)
@@ -13,14 +13,13 @@ public enum LdifName {
        // Object classes
        inetOrgPerson, organizationalPerson, person, groupOfNames, top;
 
-       public final static String LDAP_PREFIX = "ldap:";
+       public final static String PREFIX = "ldap:";
 
        public String property() {
-               return LDAP_PREFIX + name();
+               return PREFIX + name();
        }
 
        public static LdifName local(String property) {
-               String local = property.substring(LDAP_PREFIX.length());
-               return LdifName.valueOf(local);
+               return LdifName.valueOf(property.substring(PREFIX.length()));
        }
 }
index fa4526ac6a6084b20464964831bf8b8bdefaeee6..ee7128a45108e879530f880b92f8453b9a583853 100644 (file)
@@ -48,8 +48,8 @@ public class LdifUserAdmin extends AbstractUserDirectory {
 
        private static Dictionary<String, Object> fromUri(String uri, String baseDn) {
                Hashtable<String, Object> res = new Hashtable<String, Object>();
-               res.put(UserAdminProps.uri.property(), uri);
-               res.put(UserAdminProps.baseDn.property(), baseDn);
+               res.put(UserAdminConf.uri.property(), uri);
+               res.put(UserAdminConf.baseDn.property(), baseDn);
                return res;
        }
 
index 7aad5c488d78afca3f3121e99d5d7717d6f66d0f..001d78d8b13a97e2f86a905dac9828a1fcc67679 100644 (file)
@@ -14,7 +14,7 @@ import javax.naming.ldap.LdapName;
 import org.apache.commons.codec.binary.Base64;
 
 /** Basic LDIF writer */
-public class LdifWriter {
+class LdifWriter {
        private final Writer writer;
 
        public LdifWriter(OutputStream out) {
diff --git a/org.argeo.security.core/src/org/argeo/osgi/useradmin/UserAdminConf.java b/org.argeo.security.core/src/org/argeo/osgi/useradmin/UserAdminConf.java
new file mode 100644 (file)
index 0000000..e7d0e0d
--- /dev/null
@@ -0,0 +1,195 @@
+package org.argeo.osgi.useradmin;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URLDecoder;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+import javax.naming.Context;
+
+public enum UserAdminConf {
+       /** Base DN */
+       baseDn("dc=example,dc=com"),
+
+       /** URI of the underlying resource */
+       uri("ldap://localhost:10389"),
+
+       /** User objectClass */
+       userObjectClass("inetOrgPerson"),
+
+       /** Relative base DN for users */
+       userBase("ou=users"),
+
+       /** Groups objectClass */
+       groupObjectClass("groupOfNames"),
+
+       /** Relative base DN for users */
+       groupBase("ou=groups"),
+
+       /** Read-only source */
+       readOnly(null);
+
+       public final static String PREFIX = "argeo.useradmin";
+
+       /** The default value. */
+       private Object def;
+
+       UserAdminConf(Object def) {
+               this.def = def;
+       }
+
+       public Object getDefault() {
+               return def;
+       }
+
+       public String property() {
+               return getPrefix() + name();
+       }
+
+       public String getPrefix() {
+               return PREFIX;
+       }
+
+       public String getValue(Dictionary<String, ?> properties) {
+               Object res = getRawValue(properties);
+               if (res == null)
+                       return null;
+               return res.toString();
+       }
+
+       @SuppressWarnings("unchecked")
+       public <T> T getRawValue(Dictionary<String, ?> properties) {
+               Object res = properties.get(property());
+               if (res == null)
+                       res = getDefault();
+               return (T) res;
+       }
+
+       public static UserAdminConf local(String property) {
+               return UserAdminConf.valueOf(property.substring(PREFIX.length()));
+       }
+
+       /** Hides host and credentials. */
+       public static URI propertiesAsUri(Dictionary<String, ?> properties) {
+               StringBuilder query = new StringBuilder();
+
+               boolean first = true;
+               for (Enumeration<String> keys = properties.keys(); keys
+                               .hasMoreElements();) {
+                       String key = keys.nextElement();
+                       if (key.startsWith(PREFIX) && !key.equals(baseDn.property())
+                                       && !key.equals(uri.property())) {
+                               if (first)
+                                       first = false;
+                               else
+                                       query.append('&');
+                               query.append(local(key).name());
+                               query.append('=').append(properties.get(key).toString());
+                       }
+               }
+
+               String bDn = (String) properties.get(baseDn.property());
+               try {
+                       return new URI(null, null, bDn != null ? '/' + bDn : null,
+                                       query.length() != 0 ? query.toString() : null, null);
+               } catch (URISyntaxException e) {
+                       throw new UserDirectoryException(
+                                       "Cannot create URI from properties", e);
+               }
+       }
+
+       public static Dictionary<String, ?> uriAsProperties(String uriStr) {
+               try {
+                       Hashtable<String, Object> res = new Hashtable<String, Object>();
+                       URI u = new URI(uriStr);
+                       String scheme = u.getScheme();
+                       String path = u.getPath();
+                       String bDn = path.substring(path.lastIndexOf('/') + 1,
+                                       path.length());
+                       if (bDn.endsWith(".ldif"))
+                               bDn = bDn.substring(0, bDn.length() - ".ldif".length());
+
+                       String principal = null;
+                       String credentials = null;
+                       if (scheme != null)
+                               if (scheme.equals("ldap") || scheme.equals("ldaps")) {
+                                       // TODO additional checks
+                                       String[] userInfo = u.getUserInfo().split(":");
+                                       principal = userInfo.length > 0 ? userInfo[0] : null;
+                                       credentials = userInfo.length > 1 ? userInfo[1] : null;
+                               } else if (scheme.equals("file")) {
+                               } else
+                                       throw new UserDirectoryException("Unsupported scheme "
+                                                       + scheme);
+                       Map<String, List<String>> query = splitQuery(u.getQuery());
+                       for (String key : query.keySet()) {
+                               UserAdminConf ldapProp = UserAdminConf.valueOf(key);
+                               List<String> values = query.get(key);
+                               if (values.size() == 1) {
+                                       res.put(ldapProp.property(), values.get(0));
+                               } else {
+                                       throw new UserDirectoryException(
+                                                       "Only single values are supported");
+                               }
+                       }
+                       res.put(baseDn.property(), bDn);
+                       if (principal != null)
+                               res.put(Context.SECURITY_PRINCIPAL, principal);
+                       if (credentials != null)
+                               res.put(Context.SECURITY_CREDENTIALS, credentials);
+                       if (scheme != null) {
+                               URI bareUri = new URI(scheme, null, u.getHost(), u.getPort(),
+                                               scheme.equals("file") ? u.getPath() : null, null, null);
+                               res.put(uri.property(), bareUri.toString());
+                       }
+                       return res;
+               } catch (Exception e) {
+                       throw new UserDirectoryException("Cannot convert " + uri
+                                       + " to properties", e);
+               }
+       }
+
+       private static Map<String, List<String>> splitQuery(String query)
+                       throws UnsupportedEncodingException {
+               final Map<String, List<String>> query_pairs = new LinkedHashMap<String, List<String>>();
+               if (query == null)
+                       return query_pairs;
+               final String[] pairs = query.split("&");
+               for (String pair : pairs) {
+                       final int idx = pair.indexOf("=");
+                       final String key = idx > 0 ? URLDecoder.decode(
+                                       pair.substring(0, idx), "UTF-8") : pair;
+                       if (!query_pairs.containsKey(key)) {
+                               query_pairs.put(key, new LinkedList<String>());
+                       }
+                       final String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder
+                                       .decode(pair.substring(idx + 1), "UTF-8") : null;
+                       query_pairs.get(key).add(value);
+               }
+               return query_pairs;
+       }
+
+       public static void main(String[] args) {
+               Dictionary<String, ?> props = uriAsProperties("ldap://"
+                               + "uid=admin,ou=system:secret@localhost:10389"
+                               + "/dc=example,dc=com"
+                               + "?readOnly=false&userObjectClass=person");
+               System.out.println(props);
+               System.out.println(propertiesAsUri(props));
+
+               System.out
+                               .println(uriAsProperties("file://some/dir/dc=example,dc=com.ldif"));
+
+               props = uriAsProperties("/dc=example,dc=com.ldif?readOnly=true"
+                               + "&userBase=ou=CoWorkers,ou=People&groupBase=ou=Roles");
+               System.out.println(props);
+               System.out.println(propertiesAsUri(props));
+       }
+}
diff --git a/org.argeo.security.core/src/org/argeo/osgi/useradmin/UserAdminProps.java b/org.argeo.security.core/src/org/argeo/osgi/useradmin/UserAdminProps.java
deleted file mode 100644 (file)
index 0fe556c..0000000
+++ /dev/null
@@ -1,191 +0,0 @@
-package org.argeo.osgi.useradmin;
-
-import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URLDecoder;
-import java.util.Dictionary;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.LinkedHashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-
-import javax.naming.Context;
-
-public enum UserAdminProps {
-       /** Base DN */
-       baseDn("dc=example,dc=com"),
-
-       /** URI of the underlying resource */
-       uri("ldap://localhost:10389"),
-
-       /** User objectClass */
-       userObjectClass("inetOrgPerson"),
-
-       /** Relative base DN for users */
-       userBase("ou=users"),
-
-       /** Groups objectClass */
-       groupObjectClass("groupOfNames"),
-
-       /** Relative base DN for users */
-       groupBase("ou=groups"),
-
-       /** Read-only source */
-       readOnly(null);
-
-       private static String PREFIX = "argeo.useradmin";
-
-       /** The default value. */
-       private Object def;
-
-       UserAdminProps(Object def) {
-               this.def = def;
-       }
-
-       public Object getDefault() {
-               return def;
-       }
-
-       public String property() {
-               return getPrefix() + name();
-       }
-
-       public String getPrefix() {
-               return PREFIX;
-       }
-
-       public String getValue(Dictionary<String, ?> properties) {
-               Object res = getRawValue(properties);
-               if (res == null)
-                       return null;
-               return res.toString();
-       }
-
-       @SuppressWarnings("unchecked")
-       public <T> T getRawValue(Dictionary<String, ?> properties) {
-               Object res = properties.get(property());
-               if (res == null)
-                       res = getDefault();
-               return (T) res;
-       }
-
-       /** Hides host and credentials. */
-       public static URI propertiesAsUri(Dictionary<String, ?> properties) {
-               StringBuilder query = new StringBuilder();
-
-               boolean first = true;
-               for (Enumeration<String> keys = properties.keys(); keys
-                               .hasMoreElements();) {
-                       String key = keys.nextElement();
-                       if (key.startsWith(PREFIX) && !key.equals(baseDn.property())
-                                       && !key.equals(uri.property())) {
-                               if (first)
-                                       first = false;
-                               else
-                                       query.append('&');
-                               query.append(key.substring(PREFIX.length()));
-                               query.append('=').append(properties.get(key).toString());
-                       }
-               }
-
-               String bDn = (String) properties.get(baseDn.property());
-               try {
-                       return new URI(null, null, bDn != null ? '/' + bDn : null,
-                                       query.length() != 0 ? query.toString() : null, null);
-               } catch (URISyntaxException e) {
-                       throw new UserDirectoryException(
-                                       "Cannot create URI from properties", e);
-               }
-       }
-
-       public static Dictionary<String, ?> uriAsProperties(String uriStr) {
-               try {
-                       Hashtable<String, Object> res = new Hashtable<String, Object>();
-                       URI u = new URI(uriStr);
-                       String scheme = u.getScheme();
-                       String path = u.getPath();
-                       String bDn = path.substring(path.lastIndexOf('/') + 1,
-                                       path.length());
-                       if (bDn.endsWith(".ldif"))
-                               bDn = bDn.substring(0, bDn.length() - ".ldif".length());
-
-                       String principal = null;
-                       String credentials = null;
-                       if (scheme != null)
-                               if (scheme.equals("ldap") || scheme.equals("ldaps")) {
-                                       // TODO additional checks
-                                       String[] userInfo = u.getUserInfo().split(":");
-                                       principal = userInfo.length > 0 ? userInfo[0] : null;
-                                       credentials = userInfo.length > 1 ? userInfo[1] : null;
-                               } else if (scheme.equals("file")) {
-                               } else
-                                       throw new UserDirectoryException("Unsupported scheme "
-                                                       + scheme);
-                       Map<String, List<String>> query = splitQuery(u.getQuery());
-                       for (String key : query.keySet()) {
-                               UserAdminProps ldapProp = UserAdminProps.valueOf(key);
-                               List<String> values = query.get(key);
-                               if (values.size() == 1) {
-                                       res.put(ldapProp.property(), values.get(0));
-                               } else {
-                                       throw new UserDirectoryException(
-                                                       "Only single values are supported");
-                               }
-                       }
-                       res.put(baseDn.property(), bDn);
-                       if (principal != null)
-                               res.put(Context.SECURITY_PRINCIPAL, principal);
-                       if (credentials != null)
-                               res.put(Context.SECURITY_CREDENTIALS, credentials);
-                       if (scheme != null) {
-                               URI bareUri = new URI(scheme, null, u.getHost(), u.getPort(),
-                                               scheme.equals("file") ? u.getPath() : null, null, null);
-                               res.put(uri.property(), bareUri.toString());
-                       }
-                       return res;
-               } catch (Exception e) {
-                       throw new UserDirectoryException("Cannot convert " + uri
-                                       + " to properties", e);
-               }
-       }
-
-       private static Map<String, List<String>> splitQuery(String query)
-                       throws UnsupportedEncodingException {
-               final Map<String, List<String>> query_pairs = new LinkedHashMap<String, List<String>>();
-               if (query == null)
-                       return query_pairs;
-               final String[] pairs = query.split("&");
-               for (String pair : pairs) {
-                       final int idx = pair.indexOf("=");
-                       final String key = idx > 0 ? URLDecoder.decode(
-                                       pair.substring(0, idx), "UTF-8") : pair;
-                       if (!query_pairs.containsKey(key)) {
-                               query_pairs.put(key, new LinkedList<String>());
-                       }
-                       final String value = idx > 0 && pair.length() > idx + 1 ? URLDecoder
-                                       .decode(pair.substring(idx + 1), "UTF-8") : null;
-                       query_pairs.get(key).add(value);
-               }
-               return query_pairs;
-       }
-
-       public static void main(String[] args) {
-               Dictionary<String, ?> props = uriAsProperties("ldap://"
-                               + "uid=admin,ou=system:secret@localhost:10389"
-                               + "/dc=example,dc=com"
-                               + "?readOnly=false&userObjectClass=person");
-               System.out.println(props);
-               System.out.println(propertiesAsUri(props));
-
-               System.out
-                               .println(uriAsProperties("file://some/dir/dc=example,dc=com.ldif"));
-
-               props = uriAsProperties("/dc=example,dc=com.ldif?readOnly=true"
-                               + "&userBase=ou=CoWorkers,ou=People&groupBase=ou=Roles");
-               System.out.println(props);
-               System.out.println(propertiesAsUri(props));
-       }
-}