- // 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));
+ private static String getBaseDnFromHostname() {
+ String hostname;
+ try {
+ hostname = InetAddress.getLocalHost().getHostName();
+ } catch (UnknownHostException e) {
+ hostname = "localhost.localdomain";
+ }
+ int dotIdx = hostname.indexOf('.');
+ if (dotIdx >= 0) {
+ String domain = hostname.substring(dotIdx + 1, hostname.length());
+ String bDn = ("." + domain).replaceAll("\\.", ",dc=");
+ bDn = bDn.substring(1, bDn.length());
+ return bDn;
+ } else {
+ return "dc=" + hostname;
+ }
+ }
+
+ /**
+ * Hash the base DN in order to have a deterministic string to be used as a cn
+ * for the underlying user directory.
+ */
+ public static String baseDnHash(Dictionary<String, Object> properties) {
+ String bDn = (String) properties.get(baseDn.name());
+ if (bDn == null)
+ throw new UserDirectoryException("No baseDn in " + properties);
+ return DigestUtils.sha1str(bDn);