001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.ldap;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    
021    import java.text.DateFormat;
022    
023    import java.util.Date;
024    import java.util.Properties;
025    
026    import javax.naming.NamingException;
027    import javax.naming.directory.Attribute;
028    import javax.naming.directory.Attributes;
029    
030    /**
031     * @author Toma Bedolla
032     * @author Michael Young
033     * @author Brian Wing Shun Chan
034     */
035    public class LDAPUtil {
036    
037            public static String getAttributeValue(
038                            Attributes attributes, Properties properties, String key)
039                    throws NamingException {
040    
041                    String id = properties.getProperty(key);
042    
043                    return getAttributeValue(attributes, id);
044            }
045    
046            public static String getAttributeValue(
047                            Attributes attributes, Properties properties, String key,
048                            String defaultValue)
049                    throws NamingException {
050    
051                    String id = properties.getProperty(key);
052    
053                    return getAttributeValue(attributes, id, defaultValue);
054            }
055    
056            public static String getAttributeValue(Attributes attributes, String id)
057                    throws NamingException {
058    
059                    return getAttributeValue(attributes, id, StringPool.BLANK);
060            }
061    
062            public static String getAttributeValue(
063                            Attributes attributes, String id, String defaultValue)
064                    throws NamingException {
065    
066                    try {
067                            Attribute attribute = attributes.get(id);
068    
069                            Object obj = attribute.get();
070    
071                            return obj.toString();
072                    }
073                    catch (NullPointerException npe) {
074                            return defaultValue;
075                    }
076            }
077    
078            public static Date parseDate(String date) throws Exception {
079                    String format = "yyyyMMddHHmmss";
080    
081                    if (date.endsWith("Z")) {
082                            if (date.indexOf(CharPool.PERIOD) != -1) {
083                                    format = "yyyyMMddHHmmss.S'Z'";
084                            }
085                            else {
086                                    format = "yyyyMMddHHmmss'Z'";
087                            }
088                    }
089                    else if ((date.indexOf(CharPool.DASH) != -1) ||
090                                     (date.indexOf(CharPool.PLUS) != -1)) {
091    
092                            if (date.indexOf(CharPool.PERIOD) != -1) {
093                                    format = "yyyyMMddHHmmss.SZ";
094                            }
095                            else {
096                                    format = "yyyyMMddHHmmssZ";
097                            }
098                    }
099                    else if (date.indexOf(CharPool.PERIOD) != -1) {
100                            format = "yyyyMMddHHmmss.S";
101                    }
102    
103                    DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
104                            format);
105    
106                    return dateFormat.parse(date);
107            }
108    
109            public static String getFullProviderURL(String baseURL, String baseDN) {
110                    return baseURL + StringPool.SLASH + baseDN;
111            }
112    
113    }