001
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
035 public class LDAPUtil {
036
037 public static Object getAttributeObject(
038 Attributes attributes, Properties properties, String key)
039 throws NamingException {
040
041 String id = properties.getProperty(key);
042
043 return getAttributeObject(attributes, id);
044 }
045
046 public static Object getAttributeObject(
047 Attributes attributes, Properties properties, String key,
048 Object defaultValue)
049 throws NamingException {
050
051 String id = properties.getProperty(key);
052
053 return getAttributeObject(attributes, id, defaultValue);
054 }
055
056 public static Object getAttributeObject(Attributes attributes, String id)
057 throws NamingException {
058
059 return getAttributeObject(attributes, id, null);
060 }
061
062 public static Object getAttributeObject(
063 Attributes attributes, String id, Object defaultValue)
064 throws NamingException {
065
066 Attribute attribute = attributes.get(id);
067
068 if (attribute == null) {
069 return defaultValue;
070 }
071
072 Object object = attribute.get();
073
074 if (object == null) {
075 return defaultValue;
076 }
077
078 return object;
079 }
080
081 public static String getAttributeString(
082 Attributes attributes, Properties properties, String key)
083 throws NamingException {
084
085 String id = properties.getProperty(key);
086
087 return getAttributeString(attributes, id);
088 }
089
090 public static String getAttributeString(
091 Attributes attributes, Properties properties, String key,
092 String defaultValue)
093 throws NamingException {
094
095 String id = properties.getProperty(key);
096
097 return getAttributeString(attributes, id, defaultValue);
098 }
099
100 public static String getAttributeString(Attributes attributes, String id)
101 throws NamingException {
102
103 return getAttributeString(attributes, id, StringPool.BLANK);
104 }
105
106 public static String getAttributeString(
107 Attributes attributes, String id, String defaultValue)
108 throws NamingException {
109
110 Attribute attribute = attributes.get(id);
111
112 if (attribute == null) {
113 return defaultValue;
114 }
115
116 Object object = attribute.get();
117
118 if (object == null) {
119 return defaultValue;
120 }
121
122 return object.toString();
123 }
124
125 public static String getFullProviderURL(String baseURL, String baseDN) {
126 return baseURL + StringPool.SLASH + baseDN;
127 }
128
129 public static Date parseDate(String date) throws Exception {
130 String format = "yyyyMMddHHmmss";
131
132 if (date.endsWith("Z")) {
133 if (date.indexOf(CharPool.PERIOD) != -1) {
134 format = "yyyyMMddHHmmss.S'Z'";
135 }
136 else {
137 format = "yyyyMMddHHmmss'Z'";
138 }
139 }
140 else if ((date.indexOf(CharPool.DASH) != -1) ||
141 (date.indexOf(CharPool.PLUS) != -1)) {
142
143 if (date.indexOf(CharPool.PERIOD) != -1) {
144 format = "yyyyMMddHHmmss.SZ";
145 }
146 else {
147 format = "yyyyMMddHHmmssZ";
148 }
149 }
150 else if (date.indexOf(CharPool.PERIOD) != -1) {
151 format = "yyyyMMddHHmmss.S";
152 }
153
154 DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
155 format);
156
157 return dateFormat.parse(date);
158 }
159
160 }