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/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/SearchBind.java | 119 +++++++++++++++++++++++++++++++++++++++
opendj3/src/main/docbkx/dev-guide/chap-reading.xml | 49 ++++++++++++++--
2 files changed, 162 insertions(+), 6 deletions(-)
diff --git a/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/SearchBind.java b/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/SearchBind.java
new file mode 100644
index 0000000..53068f5
--- /dev/null
+++ b/opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/SearchBind.java
@@ -0,0 +1,119 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License"). You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opendj3/legal-notices/CDDLv1_0.txt
+ * or http://forgerock.org/license/CDDLv1.0.html.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opendj3/legal-notices/CDDLv1_0.txt. If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying information:
+ * Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ * Copyright 2012 ForgeRock AS
+ */
+
+package org.forgerock.opendj.examples;
+
+import java.io.Console;
+
+import org.forgerock.opendj.ldap.Connection;
+import org.forgerock.opendj.ldap.DN;
+import org.forgerock.opendj.ldap.ErrorResultException;
+import org.forgerock.opendj.ldap.LDAPConnectionFactory;
+import org.forgerock.opendj.ldap.ResultCode;
+import org.forgerock.opendj.ldap.SearchScope;
+import org.forgerock.opendj.ldap.responses.BindResult;
+import org.forgerock.opendj.ldap.responses.SearchResultEntry;
+
+/**
+ * An interactive command-line client that performs a search and subsequent
+ * simple bind. The client prompts for email address and for a password, and
+ * then searches based on the email address, to bind as the user with the
+ * password. If successful, the client displays the common name from the user's
+ * entry.
+ * <ul>
+ * <li>host - host name of the directory server</li>
+ * <li>port - port number of the directory server, e.g. 1389, 1636</li>
+ * <li>base-dn - base DN for the search, e.g. dc=example,dc=com</li>
+ * </ul>
+ * All arguments are required.
+ */
+public final class SearchBind {
+ /**
+ * Prompt for email and password, search and bind, then display message.
+ *
+ * @param args
+ * The command line arguments: host, post, base-dn.
+ */
+ public static void main(final String[] args) {
+ if (args.length != 3) {
+ System.err.println("Usage: host port base-dn");
+ System.err.println("For example: localhost 1389 dc=example,dc=com");
+ System.exit(1);
+ }
+ String host = args[0];
+ int port = Integer.parseInt(args[1]);
+ String baseDN = args[2];
+
+ // 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();
+ }
+ }
+ }
+
+ /**
+ * Constructor not used.
+ */
+ private SearchBind() {
+ // Not used
+ }
+}
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