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