001    /**
002     * Copyright (c) 2000-2013 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.exception.PortalException;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.text.DateFormat;
025    
026    import java.util.Date;
027    import java.util.Properties;
028    import java.util.regex.Pattern;
029    
030    import javax.naming.NamingException;
031    import javax.naming.directory.Attribute;
032    import javax.naming.directory.Attributes;
033    
034    /**
035     * @author Toma Bedolla
036     * @author Michael Young
037     * @author Brian Wing Shun Chan
038     * @author James Lefeu
039     */
040    public class LDAPUtil {
041    
042            public static Object getAttributeObject(
043                            Attributes attributes, Properties properties, String key)
044                    throws NamingException {
045    
046                    String id = properties.getProperty(key);
047    
048                    return getAttributeObject(attributes, id);
049            }
050    
051            public static Object getAttributeObject(
052                            Attributes attributes, Properties properties, String key,
053                            Object defaultValue)
054                    throws NamingException {
055    
056                    String id = properties.getProperty(key);
057    
058                    return getAttributeObject(attributes, id, defaultValue);
059            }
060    
061            public static Object getAttributeObject(Attributes attributes, String id)
062                    throws NamingException {
063    
064                    return getAttributeObject(attributes, id, null);
065            }
066    
067            public static Object getAttributeObject(
068                            Attributes attributes, String id, Object defaultValue)
069                    throws NamingException {
070    
071                    if (Validator.isNull(id)) {
072                            return defaultValue;
073                    }
074    
075                    Attribute attribute = attributes.get(id);
076    
077                    if (attribute == null) {
078                            return defaultValue;
079                    }
080    
081                    Object object = attribute.get();
082    
083                    if (object == null) {
084                            return defaultValue;
085                    }
086    
087                    return object;
088            }
089    
090            public static String getAttributeString(
091                            Attributes attributes, Properties properties, String key)
092                    throws NamingException {
093    
094                    String id = properties.getProperty(key);
095    
096                    return getAttributeString(attributes, id);
097            }
098    
099            public static String getAttributeString(
100                            Attributes attributes, Properties properties, String key,
101                            String defaultValue)
102                    throws NamingException {
103    
104                    String id = properties.getProperty(key);
105    
106                    return getAttributeString(attributes, id, defaultValue);
107            }
108    
109            public static String getAttributeString(Attributes attributes, String id)
110                    throws NamingException {
111    
112                    return getAttributeString(attributes, id, StringPool.BLANK);
113            }
114    
115            public static String getAttributeString(
116                            Attributes attributes, String id, String defaultValue)
117                    throws NamingException {
118    
119                    if (Validator.isNull(id)) {
120                            return defaultValue;
121                    }
122    
123                    Attribute attribute = attributes.get(id);
124    
125                    if (attribute == null) {
126                            return defaultValue;
127                    }
128    
129                    Object object = attribute.get();
130    
131                    if (object == null) {
132                            return defaultValue;
133                    }
134    
135                    return object.toString();
136            }
137    
138            public static String[] getAttributeStringArray(
139                            Attributes attributes, Properties properties, String key)
140                    throws NamingException {
141    
142                    String id = properties.getProperty(key);
143    
144                    return getAttributeStringArray(attributes, id);
145            }
146    
147            public static String[] getAttributeStringArray(
148                            Attributes attributes, String id)
149                    throws NamingException {
150    
151                    if (Validator.isNull(id)) {
152                            return null;
153                    }
154    
155                    Attribute attribute = attributes.get(id);
156    
157                    if (attribute == null) {
158                            return new String[0];
159                    }
160    
161                    int size = attribute.size();
162    
163                    if (size == 0) {
164                            return null;
165                    }
166    
167                    String[] array = new String[size];
168    
169                    for (int i = 0; i < size; i++) {
170                            Object object = attribute.get(i);
171    
172                            if (object == null) {
173                                    array[i] = StringPool.BLANK;
174                            }
175                            else {
176                                    array[i] = object.toString();
177                            }
178                    }
179    
180                    return array;
181            }
182    
183            public static String getFullProviderURL(String baseURL, String baseDN) {
184                    return baseURL + StringPool.SLASH + baseDN;
185            }
186    
187            public static boolean isValidFilter(String filter) {
188                    if (Validator.isNull(filter)) {
189                            return true;
190                    }
191    
192                    filter = filter.trim();
193    
194                    if (filter.equals(StringPool.STAR)) {
195                            return true;
196                    }
197    
198                    filter = StringUtil.replace(filter, StringPool.SPACE, StringPool.BLANK);
199    
200                    if (!filter.startsWith(StringPool.OPEN_PARENTHESIS) ||
201                            !filter.endsWith(StringPool.CLOSE_PARENTHESIS)) {
202    
203                            return false;
204                    }
205    
206                    int count = 0;
207    
208                    for (int i = 0; i < filter.length(); i++) {
209                            char c = filter.charAt(i);
210    
211                            if (c == CharPool.CLOSE_PARENTHESIS) {
212                                    count--;
213                            }
214                            else if (c == CharPool.OPEN_PARENTHESIS) {
215                                    count++;
216                            }
217    
218                            if (count < 0) {
219                                    return false;
220                            }
221                    }
222    
223                    if (count > 0) {
224                            return false;
225                    }
226    
227                    // Cannot have two filter types in a sequence
228    
229                    if (Pattern.matches(".*[~<>]*=[~<>]*=.*", filter)) {
230                            return false;
231                    }
232    
233                    // Cannot have a filter type after an opening parenthesis
234    
235                    if (Pattern.matches("\\([~<>]*=.*", filter)) {
236                            return false;
237                    }
238    
239                    // Cannot have an attribute without a filter type or extensible
240    
241                    if (Pattern.matches("\\([^~<>=]*\\)", filter)) {
242                            return false;
243                    }
244    
245                    if (Pattern.matches(".*[^~<>=]*[~<>]*=\\)", filter)) {
246                            return false;
247                    }
248    
249                    return true;
250            }
251    
252            public static Date parseDate(String date) throws Exception {
253                    String format = "yyyyMMddHHmmss";
254    
255                    if (date.endsWith("Z")) {
256                            if (date.indexOf(CharPool.PERIOD) != -1) {
257                                    format = "yyyyMMddHHmmss.S'Z'";
258                            }
259                            else {
260                                    format = "yyyyMMddHHmmss'Z'";
261                            }
262                    }
263                    else if ((date.indexOf(CharPool.DASH) != -1) ||
264                                     (date.indexOf(CharPool.PLUS) != -1)) {
265    
266                            if (date.indexOf(CharPool.PERIOD) != -1) {
267                                    format = "yyyyMMddHHmmss.SZ";
268                            }
269                            else {
270                                    format = "yyyyMMddHHmmssZ";
271                            }
272                    }
273                    else if (date.indexOf(CharPool.PERIOD) != -1) {
274                            format = "yyyyMMddHHmmss.S";
275                    }
276    
277                    DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
278                            format);
279    
280                    return dateFormat.parse(date);
281            }
282    
283            public static void validateFilter(String filter) throws PortalException {
284                    if (!isValidFilter(filter)) {
285                            throw new LDAPFilterException("Invalid filter " + filter);
286                    }
287            }
288    
289    }