1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util.ldap;
21  
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.kernel.util.Validator;
26  
27  import javax.naming.NamingException;
28  import javax.naming.directory.Attribute;
29  import javax.naming.directory.Attributes;
30  
31  /**
32   * <a href="LDAPUtil.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Toma Bedolla
35   * @author Michael Young
36   *
37   */
38  public class LDAPUtil {
39  
40      public static String getAttributeValue(Attributes attrs, String id)
41          throws NamingException {
42  
43          return getAttributeValue(attrs, id, StringPool.BLANK);
44      }
45  
46      public static String getAttributeValue(
47              Attributes attrs, String id, String defaultValue)
48          throws NamingException {
49  
50          try {
51              Attribute attr = attrs.get(id);
52  
53              Object obj = attr.get();
54  
55              return obj.toString();
56          }
57          catch (NullPointerException npe) {
58              return defaultValue;
59          }
60      }
61  
62      public static String getFullProviderURL(String baseURL, String baseDN) {
63          return baseURL + StringPool.SLASH + baseDN;
64      }
65  
66      public static String[] splitFullName(String fullName) {
67          String firstName = StringPool.BLANK;
68          String lastName = StringPool.BLANK;
69          String middleName = StringPool.BLANK;
70  
71          if (Validator.isNotNull(fullName)) {
72              String[] name = StringUtil.split(fullName, " ");
73  
74              firstName = name[0];
75              lastName = name[name.length - 1];
76              middleName = StringPool.BLANK;
77  
78              if (name.length > 2) {
79                  for (int i = 1; i < name.length - 1; i++) {
80                      if (Validator.isNull(name[i].trim())) {
81                          continue;
82                      }
83  
84                      if (i != 1) {
85                          middleName += " ";
86                      }
87  
88                      middleName += name[i].trim();
89                  }
90              }
91          }
92          else {
93              firstName = GetterUtil.getString(firstName, lastName);
94              lastName = firstName;
95          }
96  
97          return new String[] {firstName, middleName, lastName};
98      }
99  
100 }