001
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
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[] getAttributeStringArray(
135 Attributes attributes, Properties properties, String key)
136 throws NamingException {
137
138 String id = properties.getProperty(key);
139
140 return getAttributeStringArray(attributes, id);
141 }
142
143 public static String[] getAttributeStringArray(
144 Attributes attributes, String id)
145 throws NamingException {
146
147 if (Validator.isNull(id)) {
148 return null;
149 }
150
151 Attribute attribute = attributes.get(id);
152
153 if (attribute == null) {
154 return null;
155 }
156
157 int size = attribute.size();
158
159 if (size == 0) {
160 return null;
161 }
162
163 String[] array = new String[size];
164
165 for (int i = 0; i < size; i++) {
166 Object object = attribute.get(i);
167
168 if (object == null) {
169 array[i] = StringPool.BLANK;
170 }
171 else {
172 array[i] = object.toString();
173 }
174 }
175
176 return array;
177 }
178
179 public static String getFullProviderURL(String baseURL, String baseDN) {
180 return baseURL + StringPool.SLASH + baseDN;
181 }
182
183 public static Date parseDate(String date) throws Exception {
184 String format = "yyyyMMddHHmmss";
185
186 if (date.endsWith("Z")) {
187 if (date.indexOf(CharPool.PERIOD) != -1) {
188 format = "yyyyMMddHHmmss.S'Z'";
189 }
190 else {
191 format = "yyyyMMddHHmmss'Z'";
192 }
193 }
194 else if ((date.indexOf(CharPool.DASH) != -1) ||
195 (date.indexOf(CharPool.PLUS) != -1)) {
196
197 if (date.indexOf(CharPool.PERIOD) != -1) {
198 format = "yyyyMMddHHmmss.SZ";
199 }
200 else {
201 format = "yyyyMMddHHmmssZ";
202 }
203 }
204 else if (date.indexOf(CharPool.PERIOD) != -1) {
205 format = "yyyyMMddHHmmss.S";
206 }
207
208 DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
209 format);
210
211 return dateFormat.parse(date);
212 }
213
214 }