001    /**
002     * Copyright (c) 2000-2012 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.portal.kernel.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    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.text.DateFormat;
023    
024    import java.util.Date;
025    import java.util.Properties;
026    
027    import javax.naming.NamingException;
028    import javax.naming.directory.Attribute;
029    import javax.naming.directory.Attributes;
030    
031    /**
032     * @author Toma Bedolla
033     * @author Michael Young
034     * @author Brian Wing Shun Chan
035     */
036    public class LDAPUtil {
037    
038            public static Object getAttributeObject(
039                            Attributes attributes, Properties properties, String key)
040                    throws NamingException {
041    
042                    String id = properties.getProperty(key);
043    
044                    return getAttributeObject(attributes, id);
045            }
046    
047            public static Object getAttributeObject(
048                            Attributes attributes, Properties properties, String key,
049                            Object defaultValue)
050                    throws NamingException {
051    
052                    String id = properties.getProperty(key);
053    
054                    return getAttributeObject(attributes, id, defaultValue);
055            }
056    
057            public static Object getAttributeObject(Attributes attributes, String id)
058                    throws NamingException {
059    
060                    return getAttributeObject(attributes, id, null);
061            }
062    
063            public static Object getAttributeObject(
064                            Attributes attributes, String id, Object defaultValue)
065                    throws NamingException {
066    
067                    if (Validator.isNull(id)) {
068                            return defaultValue;
069                    }
070    
071                    Attribute attribute = attributes.get(id);
072    
073                    if (attribute == null) {
074                            return defaultValue;
075                    }
076    
077                    Object object = attribute.get();
078    
079                    if (object == null) {
080                            return defaultValue;
081                    }
082    
083                    return object;
084            }
085    
086            public static String getAttributeString(
087                            Attributes attributes, Properties properties, String key)
088                    throws NamingException {
089    
090                    String id = properties.getProperty(key);
091    
092                    return getAttributeString(attributes, id);
093            }
094    
095            public static String getAttributeString(
096                            Attributes attributes, Properties properties, String key,
097                            String defaultValue)
098                    throws NamingException {
099    
100                    String id = properties.getProperty(key);
101    
102                    return getAttributeString(attributes, id, defaultValue);
103            }
104    
105            public static String getAttributeString(Attributes attributes, String id)
106                    throws NamingException {
107    
108                    return getAttributeString(attributes, id, StringPool.BLANK);
109            }
110    
111            public static String getAttributeString(
112                            Attributes attributes, String id, String defaultValue)
113                    throws NamingException {
114    
115                    if (Validator.isNull(id)) {
116                            return defaultValue;
117                    }
118    
119                    Attribute attribute = attributes.get(id);
120    
121                    if (attribute == null) {
122                            return defaultValue;
123                    }
124    
125                    Object object = attribute.get();
126    
127                    if (object == null) {
128                            return defaultValue;
129                    }
130    
131                    return object.toString();
132            }
133    
134            public static String getFullProviderURL(String baseURL, String baseDN) {
135                    return baseURL + StringPool.SLASH + baseDN;
136            }
137    
138            public static Date parseDate(String date) throws Exception {
139                    String format = "yyyyMMddHHmmss";
140    
141                    if (date.endsWith("Z")) {
142                            if (date.indexOf(CharPool.PERIOD) != -1) {
143                                    format = "yyyyMMddHHmmss.S'Z'";
144                            }
145                            else {
146                                    format = "yyyyMMddHHmmss'Z'";
147                            }
148                    }
149                    else if ((date.indexOf(CharPool.DASH) != -1) ||
150                                     (date.indexOf(CharPool.PLUS) != -1)) {
151    
152                            if (date.indexOf(CharPool.PERIOD) != -1) {
153                                    format = "yyyyMMddHHmmss.SZ";
154                            }
155                            else {
156                                    format = "yyyyMMddHHmmssZ";
157                            }
158                    }
159                    else if (date.indexOf(CharPool.PERIOD) != -1) {
160                            format = "yyyyMMddHHmmss.S";
161                    }
162    
163                    DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
164                            format);
165    
166                    return dateFormat.parse(date);
167            }
168    
169    }