1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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 javax.naming.NamingException;
23  import javax.naming.directory.Attribute;
24  import javax.naming.directory.Attributes;
25  
26  /**
27   * <a href="LDAPUtil.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Toma Bedolla
30   * @author Michael Young
31   */
32  public class LDAPUtil {
33  
34      public static String getAttributeValue(Attributes attrs, String id)
35          throws NamingException {
36  
37          return getAttributeValue(attrs, id, StringPool.BLANK);
38      }
39  
40      public static String getAttributeValue(
41              Attributes attrs, String id, String defaultValue)
42          throws NamingException {
43  
44          try {
45              Attribute attr = attrs.get(id);
46  
47              Object obj = attr.get();
48  
49              return obj.toString();
50          }
51          catch (NullPointerException npe) {
52              return defaultValue;
53          }
54      }
55  
56      public static String getFullProviderURL(String baseURL, String baseDN) {
57          return baseURL + StringPool.SLASH + baseDN;
58      }
59  
60      public static String[] splitFullName(String fullName) {
61          String firstName = StringPool.BLANK;
62          String lastName = StringPool.BLANK;
63          String middleName = StringPool.BLANK;
64  
65          if (Validator.isNotNull(fullName)) {
66              String[] name = StringUtil.split(fullName, " ");
67  
68              firstName = name[0];
69              lastName = name[name.length - 1];
70              middleName = StringPool.BLANK;
71  
72              if (name.length > 2) {
73                  for (int i = 1; i < name.length - 1; i++) {
74                      if (Validator.isNull(name[i].trim())) {
75                          continue;
76                      }
77  
78                      if (i != 1) {
79                          middleName += " ";
80                      }
81  
82                      middleName += name[i].trim();
83                  }
84              }
85          }
86          else {
87              firstName = GetterUtil.getString(firstName, lastName);
88              lastName = firstName;
89          }
90  
91          return new String[] {firstName, middleName, lastName};
92      }
93  
94  }