1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.ldap;
16  
17  import com.liferay.portal.kernel.util.StringPool;
18  
19  import java.util.Properties;
20  
21  import javax.naming.NamingException;
22  import javax.naming.directory.Attribute;
23  import javax.naming.directory.Attributes;
24  
25  /**
26   * <a href="LDAPUtil.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Toma Bedolla
29   * @author Michael Young
30   * @author Brian Wing Shun Chan
31   */
32  public class LDAPUtil {
33  
34      public static String getAttributeValue(
35              Attributes attributes, Properties properties, String key)
36          throws NamingException {
37  
38          String id = properties.getProperty(key);
39  
40          return getAttributeValue(attributes, id);
41      }
42  
43      public static String getAttributeValue(
44              Attributes attributes, Properties properties, String key,
45              String defaultValue)
46          throws NamingException {
47  
48          String id = properties.getProperty(key);
49  
50          return getAttributeValue(attributes, id, defaultValue);
51      }
52  
53      public static String getAttributeValue(Attributes attributes, String id)
54          throws NamingException {
55  
56          return getAttributeValue(attributes, id, StringPool.BLANK);
57      }
58  
59      public static String getAttributeValue(
60              Attributes attributes, String id, String defaultValue)
61          throws NamingException {
62  
63          try {
64              Attribute attribute = attributes.get(id);
65  
66              Object obj = attribute.get();
67  
68              return obj.toString();
69          }
70          catch (NullPointerException npe) {
71              return defaultValue;
72          }
73      }
74  
75      public static String getFullProviderURL(String baseURL, String baseDN) {
76          return baseURL + StringPool.SLASH + baseDN;
77      }
78  
79  }