1 /*
2 * Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
190 int i = 0;
191 while (true) {
192 try {
193 context = new InitialDirContext(env);
194 break;
195 } catch (NamingException ne) {
196 // may be a connection or read timeout, try again
197 // no more than 5 times
198 if (i++ > 5) {
199 throw new Exception(
200 "Maybe timeout during context initialization", ne);
201 }
202 }
203 }
204
205 // searching
206 SearchControls scs = new SearchControls();
207 scs.setSearchScope(SearchControls.SUBTREE_SCOPE);
208
209 try {
210 NamingEnumeration answer =
211 context.search("o=sun,c=us", searchFilter, scs);
212 } catch (InvalidSearchFilterException isfe) {
213 if (filterIsValid) {
214 // unexpected filter exception.
215 throw new Exception("Unexpected ISFE", isfe);
216 } else {
217 // ignore, it is the expected filter exception.
218 System.out.println("Expected exception: " + isfe.getMessage());
219 }
220 } catch (NamingException ne) {
221 // maybe a read timeout exception, as the server does not response.
222 if (filterIsValid) {
223 System.out.println("Expected exception: " + ne.getMessage());
224 } else {
225 throw new Exception("Not an InvalidSearchFilterException", ne);
226 }
227 }
228
229 context.close();
230 }
|
1 /*
2 * Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
190 int i = 0;
191 while (true) {
192 try {
193 context = new InitialDirContext(env);
194 break;
195 } catch (NamingException ne) {
196 // may be a connection or read timeout, try again
197 // no more than 5 times
198 if (i++ > 5) {
199 throw new Exception(
200 "Maybe timeout during context initialization", ne);
201 }
202 }
203 }
204
205 // searching
206 SearchControls scs = new SearchControls();
207 scs.setSearchScope(SearchControls.SUBTREE_SCOPE);
208
209 try {
210 NamingEnumeration<SearchResult> answer =
211 context.search("o=sun,c=us", searchFilter, scs);
212 } catch (InvalidSearchFilterException isfe) {
213 if (filterIsValid) {
214 // unexpected filter exception.
215 throw new Exception("Unexpected ISFE", isfe);
216 } else {
217 // ignore, it is the expected filter exception.
218 System.out.println("Expected exception: " + isfe.getMessage());
219 }
220 } catch (NamingException ne) {
221 // maybe a read timeout exception, as the server does not response.
222 if (filterIsValid) {
223 System.out.println("Expected exception: " + ne.getMessage());
224 } else {
225 throw new Exception("Not an InvalidSearchFilterException", ne);
226 }
227 }
228
229 context.close();
230 }
|