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.GetterUtil;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.util.Validator;
21  
22  import java.util.Properties;
23  
24  import javax.naming.NamingException;
25  import javax.naming.directory.Attribute;
26  import javax.naming.directory.Attributes;
27  
28  /**
29   * <a href="LDAPUtil.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Toma Bedolla
32   * @author Michael Young
33   * @author Brian Wing Shun Chan
34   */
35  public class LDAPUtil {
36  
37      public static String getAttributeValue(
38              Attributes attributes, Properties properties, String key)
39          throws NamingException {
40  
41          String id = properties.getProperty(key);
42  
43          return getAttributeValue(attributes, id);
44      }
45  
46      public static String getAttributeValue(
47              Attributes attributes, Properties properties, String key,
48              String defaultValue)
49          throws NamingException {
50  
51          String id = properties.getProperty(key);
52  
53          return getAttributeValue(attributes, id, defaultValue);
54      }
55  
56      public static String getAttributeValue(Attributes attributes, String id)
57          throws NamingException {
58  
59          return getAttributeValue(attributes, id, StringPool.BLANK);
60      }
61  
62      public static String getAttributeValue(
63              Attributes attributes, String id, String defaultValue)
64          throws NamingException {
65  
66          try {
67              Attribute attribute = attributes.get(id);
68  
69              Object obj = attribute.get();
70  
71              return obj.toString();
72          }
73          catch (NullPointerException npe) {
74              return defaultValue;
75          }
76      }
77  
78      public static String getFullProviderURL(String baseURL, String baseDN) {
79          return baseURL + StringPool.SLASH + baseDN;
80      }
81  
82      public static String[] splitFullName(String fullName) {
83          String firstName = StringPool.BLANK;
84          String lastName = StringPool.BLANK;
85          String middleName = StringPool.BLANK;
86  
87          if (Validator.isNotNull(fullName)) {
88              String[] name = StringUtil.split(fullName, " ");
89  
90              firstName = name[0];
91              lastName = name[name.length - 1];
92              middleName = StringPool.BLANK;
93  
94              if (name.length > 2) {
95                  for (int i = 1; i < name.length - 1; i++) {
96                      if (Validator.isNull(name[i].trim())) {
97                          continue;
98                      }
99  
100                     if (i != 1) {
101                         middleName += " ";
102                     }
103 
104                     middleName += name[i].trim();
105                 }
106             }
107         }
108         else {
109             firstName = GetterUtil.getString(firstName, lastName);
110             lastName = firstName;
111         }
112 
113         return new String[] {firstName, middleName, lastName};
114     }
115 
116 }