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.CharPool;
18  import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
19  import com.liferay.portal.kernel.util.StringPool;
20  
21  import java.text.DateFormat;
22  
23  import java.util.Date;
24  
25  import javax.naming.NamingException;
26  import javax.naming.directory.Attribute;
27  import javax.naming.directory.Attributes;
28  
29  /**
30   * <a href="LDAPUtil.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Toma Bedolla
33   * @author Michael Young
34   */
35  public class LDAPUtil {
36  
37      public static String getAttributeValue(Attributes attrs, String id)
38          throws NamingException {
39  
40          return getAttributeValue(attrs, id, StringPool.BLANK);
41      }
42  
43      public static String getAttributeValue(
44              Attributes attrs, String id, String defaultValue)
45          throws NamingException {
46  
47          try {
48              Attribute attr = attrs.get(id);
49  
50              Object obj = attr.get();
51  
52              return obj.toString();
53          }
54          catch (NullPointerException npe) {
55              return defaultValue;
56          }
57      }
58  
59      public static String getFullProviderURL(String baseURL, String baseDN) {
60          return baseURL + StringPool.SLASH + baseDN;
61      }
62  
63      public static Date parseDate(String date) throws Exception {
64          String format = "yyyyMMddHHmmss";
65  
66          if (date.endsWith("Z")) {
67              if (date.indexOf(CharPool.PERIOD) != -1) {
68                  format = "yyyyMMddHHmmss.S'Z'";
69              }
70              else {
71                  format = "yyyyMMddHHmmss'Z'";
72              }
73          }
74          else if ((date.indexOf(CharPool.DASH) != -1) ||
75                   (date.indexOf(CharPool.PLUS) != -1)) {
76  
77              if (date.indexOf(CharPool.PERIOD) != -1) {
78                  format = "yyyyMMddHHmmss.SZ";
79              }
80              else {
81                  format = "yyyyMMddHHmmssZ";
82              }
83          }
84          else if (date.indexOf(CharPool.PERIOD) != -1) {
85              format = "yyyyMMddHHmmss.S";
86          }
87  
88          DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
89              format);
90  
91          return dateFormat.parse(date);
92      }
93  
94  }