001    /**
002     * Copyright (c) 2000-2010 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.StringPool;
018    
019    import java.util.Properties;
020    
021    import javax.naming.NamingException;
022    import javax.naming.directory.Attribute;
023    import javax.naming.directory.Attributes;
024    
025    /**
026     * @author Toma Bedolla
027     * @author Michael Young
028     * @author Brian Wing Shun Chan
029     */
030    public class LDAPUtil {
031    
032            public static String getAttributeValue(
033                            Attributes attributes, Properties properties, String key)
034                    throws NamingException {
035    
036                    String id = properties.getProperty(key);
037    
038                    return getAttributeValue(attributes, id);
039            }
040    
041            public static String getAttributeValue(
042                            Attributes attributes, Properties properties, String key,
043                            String defaultValue)
044                    throws NamingException {
045    
046                    String id = properties.getProperty(key);
047    
048                    return getAttributeValue(attributes, id, defaultValue);
049            }
050    
051            public static String getAttributeValue(Attributes attributes, String id)
052                    throws NamingException {
053    
054                    return getAttributeValue(attributes, id, StringPool.BLANK);
055            }
056    
057            public static String getAttributeValue(
058                            Attributes attributes, String id, String defaultValue)
059                    throws NamingException {
060    
061                    try {
062                            Attribute attribute = attributes.get(id);
063    
064                            Object obj = attribute.get();
065    
066                            return obj.toString();
067                    }
068                    catch (NullPointerException npe) {
069                            return defaultValue;
070                    }
071            }
072    
073            public static String getFullProviderURL(String baseURL, String baseDN) {
074                    return baseURL + StringPool.SLASH + baseDN;
075            }
076    
077    }