001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2012-2014 ForgeRock AS
025 */
026package org.forgerock.opendj.examples;
027
028import java.io.IOException;
029
030import org.forgerock.opendj.ldap.Connection;
031import org.forgerock.opendj.ldap.LdapException;
032import org.forgerock.opendj.ldap.LDAPConnectionFactory;
033import org.forgerock.opendj.ldap.ResultCode;
034import org.forgerock.opendj.ldap.SearchScope;
035import org.forgerock.opendj.ldap.responses.SearchResultEntry;
036import org.forgerock.opendj.ldif.LDIFEntryWriter;
037
038/**
039 * Demonstrates accessing server information about capabilities and schema.
040 */
041public final class GetInfo {
042    /** Connection information. */
043    private static String host;
044    private static int port;
045    /** The kind of server information to request (all, controls, extops). */
046    private static String infoType;
047
048    /**
049     * Access the directory over LDAP to request information about capabilities
050     * and schema.
051     *
052     * @param args
053     *            The command line arguments
054     */
055    public static void main(final String[] args) {
056        parseArgs(args);
057        connect();
058    }
059
060    /**
061     * Authenticate over LDAP.
062     */
063    private static void connect() {
064        // --- JCite ---
065        final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
066        Connection connection = null;
067
068        try {
069            connection = factory.getConnection();
070            connection.bind("", "".toCharArray()); // Anonymous bind
071
072            final String attributeList;
073            if ("controls".equals(infoType.toLowerCase())) {
074                attributeList = "supportedControl";
075            } else if ("extops".equals(infoType.toLowerCase())) {
076                attributeList = "supportedExtension";
077            } else {
078                attributeList = "+"; // All operational attributes
079            }
080
081            final SearchResultEntry entry = connection.searchSingleEntry(
082                    "", // DN is "" for root DSE.
083                    SearchScope.BASE_OBJECT, // Read only the root DSE.
084                    "(objectclass=*)", // Every object matches this filter.
085                    attributeList); // Return these requested attributes.
086
087            final LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
088            writer.writeComment("Root DSE for LDAP server at " + host + ":" + port);
089            if (entry != null) {
090                writer.writeEntry(entry);
091            }
092            writer.flush();
093        } catch (final LdapException e) {
094            System.err.println(e.getMessage());
095            System.exit(e.getResult().getResultCode().intValue());
096            return;
097        } catch (final IOException e) {
098            System.err.println(e.getMessage());
099            System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
100            return;
101        } finally {
102            if (connection != null) {
103                connection.close();
104            }
105        }
106        // --- JCite ---
107    }
108
109    private static void giveUp() {
110        printUsage();
111        System.exit(1);
112    }
113
114    /**
115     * Parse command line arguments.
116     *
117     * @param args
118     *            host port bind-dn bind-password info-type
119     */
120    private static void parseArgs(final String[] args) {
121        if (args.length != 3) {
122            giveUp();
123        }
124
125        host = args[0];
126        port = Integer.parseInt(args[1]);
127        infoType = args[2];
128        final String infoTypeLc = infoType.toLowerCase();
129        if (!"all".equals(infoTypeLc)
130                && !"controls".equals(infoTypeLc)
131                && !"extops".equals(infoTypeLc)) {
132            giveUp();
133        }
134    }
135
136    private static void printUsage() {
137        System.err.println("Usage: host port info-type");
138        System.err.println("\tAll arguments are required.");
139        System.err.println("\tinfo-type to get can be either all, controls, or extops.");
140    }
141
142    private GetInfo() {
143        // Not used.
144    }
145}