From 23f0a17495aaba68d469305fb4bcc99eb06138ae Mon Sep 17 00:00:00 2001
From: Mark Craig <mark.craig@forgerock.com>
Date: Fri, 20 Apr 2012 14:45:16 +0000
Subject: [PATCH] Example search-and-then-bind
---
opendj3/src/main/docbkx/dev-guide/chap-reading.xml | 49 +++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 43 insertions(+), 6 deletions(-)
diff --git a/opendj3/src/main/docbkx/dev-guide/chap-reading.xml b/opendj3/src/main/docbkx/dev-guide/chap-reading.xml
index 28a9671..1bae32d 100644
--- a/opendj3/src/main/docbkx/dev-guide/chap-reading.xml
+++ b/opendj3/src/main/docbkx/dev-guide/chap-reading.xml
@@ -99,12 +99,49 @@
</listitem>
</itemizedlist>
- <para>TODO: Explain how to do this, either with code from
- http://opendj.forgerock.org/opendj-ldap-sdk-examples/xref/org/forgerock/opendj/examples/search/Main.html
- or writing some more directly relevant sample code. The other sections
- in this chapter can expand more on filters, building search requests,
- iterating through results, potentially abandoning, but this section should
- stay focused on the basic example to make the idea clear.</para>
+ <para>The following code excerpt demonstrates how this might be done in a
+ minimal command-line program.</para>
+
+ <programlisting language="java">// Prompt for mail and password.
+Console c = System.console();
+if (c == null) {
+ System.err.println("No console.");
+ System.exit(1);
+}
+
+String mail = c.readLine("Email address: ");
+char[] password = c.readPassword("Password: ");
+
+// Search using mail address, and then bind with the DN and password.
+final LDAPConnectionFactory factory = new LDAPConnectionFactory(host,
+ port);
+Connection connection = null;
+try {
+ connection = factory.getConnection();
+ SearchResultEntry entry = connection.searchSingleEntry(baseDN,
+ SearchScope.WHOLE_SUBTREE, "(mail=" + mail + ")", "cn");
+ DN bindDN = entry.getName();
+ BindResult result = connection.bind(bindDN.toString(), password);
+
+ if (result.isSuccess()) {
+ String cn = entry.getAttribute("cn").firstValueAsString();
+ System.out.println("Hello, " + cn + "!");
+ } else {
+ System.err.println("Failed to bind.");
+ }
+} catch (final ErrorResultException e) {
+ System.err.println("Failed to bind.");
+ System.exit(e.getResult().getResultCode().intValue());
+ return;
+} catch (final InterruptedException e) {
+ System.err.println(e.getMessage());
+ System.exit(ResultCode.CLIENT_SIDE_USER_CANCELLED.intValue());
+ return;
+} finally {
+ if (connection != null) {
+ connection.close();
+ }
+}</programlisting>
</section>
<section xml:id="about-filters">
--
Gitblit v1.10.0