001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2009-2010 Sun Microsystems, Inc.
015 * Portions Copyright 2011-2015 ForgeRock AS.
016 */
017
018package org.forgerock.opendj.examples;
019
020import java.io.IOException;
021import java.util.Arrays;
022
023import org.forgerock.opendj.ldap.Connection;
024import org.forgerock.opendj.ldap.LdapException;
025import org.forgerock.opendj.ldap.LDAPConnectionFactory;
026import org.forgerock.opendj.ldap.ResultCode;
027import org.forgerock.opendj.ldap.SearchScope;
028import org.forgerock.opendj.ldap.responses.SearchResultEntry;
029import org.forgerock.opendj.ldap.responses.SearchResultReference;
030import org.forgerock.opendj.ldif.ConnectionEntryReader;
031import org.forgerock.opendj.ldif.LDIFEntryWriter;
032
033/**
034 * An example client application which searches a Directory Server. This example
035 * takes the following command line parameters:
036 *
037 * <pre>
038 *  {@code <host> <port> <username> <password>
039 *      <baseDN> <scope> <filter> [<attribute> <attribute> ...]}
040 * </pre>
041 */
042public final class Search {
043    /**
044     * Main method.
045     *
046     * @param args
047     *            The command line arguments: host, port, username, password,
048     *            base DN, scope, filter, and zero or more attributes to be
049     *            retrieved.
050     */
051    public static void main(final String[] args) {
052        if (args.length < 7) {
053            System.err.println("Usage: host port username password baseDN scope "
054                    + "filter [attribute ...]");
055            System.exit(1);
056        }
057
058        // Parse command line arguments.
059        final String hostName = args[0];
060        final int port = Integer.parseInt(args[1]);
061        final String userName = args[2];
062        final String password = args[3];
063        final String baseDN = args[4];
064        final String scopeString = args[5];
065        final String filter = args[6];
066        String[] attributes;
067        if (args.length > 7) {
068            attributes = Arrays.copyOfRange(args, 7, args.length);
069        } else {
070            attributes = new String[0];
071        }
072
073        final SearchScope scope = SearchScope.valueOf(scopeString);
074        if (scope == null) {
075            System.err.println("Unknown scope: " + scopeString);
076            System.exit(ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue());
077            return;
078        }
079
080        // --- JCite ---
081        // Create an LDIF writer which will write the search results to stdout.
082        final LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
083
084        // Connect and bind to the server.
085        final LDAPConnectionFactory factory = new LDAPConnectionFactory(hostName, port);
086        Connection connection = null;
087
088        try {
089            connection = factory.getConnection();
090            connection.bind(userName, password.toCharArray());
091
092            // Read the entries and output them as LDIF.
093            final ConnectionEntryReader reader =
094                    connection.search(baseDN, scope, filter, attributes);
095            while (reader.hasNext()) {
096                if (!reader.isReference()) {
097                    final SearchResultEntry entry = reader.readEntry();
098                    writer.writeComment("Search result entry: " + entry.getName());
099                    writer.writeEntry(entry);
100                } else {
101                    final SearchResultReference ref = reader.readReference();
102
103                    // Got a continuation reference.
104                    writer.writeComment("Search result reference: " + ref.getURIs());
105                }
106            }
107            writer.flush();
108        } catch (final LdapException e) {
109            System.err.println(e.getMessage());
110            System.exit(e.getResult().getResultCode().intValue());
111        } catch (final IOException e) {
112            System.err.println(e.getMessage());
113            System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
114        } finally {
115            if (connection != null) {
116                connection.close();
117            }
118        }
119        // --- JCite ---
120    }
121
122    private Search() {
123        // Not used.
124    }
125}