View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions Copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2012-2014 ForgeRock AS.
15   */
16  package org.forgerock.opendj.examples;
17  
18  import java.io.IOException;
19  
20  import org.forgerock.opendj.ldap.Connection;
21  import org.forgerock.opendj.ldap.LdapException;
22  import org.forgerock.opendj.ldap.LDAPConnectionFactory;
23  import org.forgerock.opendj.ldap.ResultCode;
24  import org.forgerock.opendj.ldap.SearchScope;
25  import org.forgerock.opendj.ldap.responses.SearchResultEntry;
26  import org.forgerock.opendj.ldif.LDIFEntryWriter;
27  
28  /**
29   * Demonstrates accessing server information about capabilities and schema.
30   */
31  public final class GetInfo {
32      /** Connection information. */
33      private static String host;
34      private static int port;
35      /** The kind of server information to request (all, controls, extops). */
36      private static String infoType;
37  
38      /**
39       * Access the directory over LDAP to request information about capabilities
40       * and schema.
41       *
42       * @param args
43       *            The command line arguments
44       */
45      public static void main(final String[] args) {
46          parseArgs(args);
47          connect();
48      }
49  
50      /**
51       * Authenticate over LDAP.
52       */
53      private static void connect() {
54          // --- JCite ---
55          final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
56          Connection connection = null;
57  
58          try {
59              connection = factory.getConnection();
60              connection.bind("", "".toCharArray()); // Anonymous bind
61  
62              final String attributeList;
63              if ("controls".equals(infoType.toLowerCase())) {
64                  attributeList = "supportedControl";
65              } else if ("extops".equals(infoType.toLowerCase())) {
66                  attributeList = "supportedExtension";
67              } else {
68                  attributeList = "+"; // All operational attributes
69              }
70  
71              final SearchResultEntry entry = connection.searchSingleEntry(
72                      "", // DN is "" for root DSE.
73                      SearchScope.BASE_OBJECT, // Read only the root DSE.
74                      "(objectclass=*)", // Every object matches this filter.
75                      attributeList); // Return these requested attributes.
76  
77              final LDIFEntryWriter writer = new LDIFEntryWriter(System.out);
78              writer.writeComment("Root DSE for LDAP server at " + host + ":" + port);
79              if (entry != null) {
80                  writer.writeEntry(entry);
81              }
82              writer.flush();
83          } catch (final LdapException e) {
84              System.err.println(e.getMessage());
85              System.exit(e.getResult().getResultCode().intValue());
86              return;
87          } catch (final IOException e) {
88              System.err.println(e.getMessage());
89              System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue());
90              return;
91          } finally {
92              if (connection != null) {
93                  connection.close();
94              }
95          }
96          // --- JCite ---
97      }
98  
99      private static void giveUp() {
100         printUsage();
101         System.exit(1);
102     }
103 
104     /**
105      * Parse command line arguments.
106      *
107      * @param args
108      *            host port bind-dn bind-password info-type
109      */
110     private static void parseArgs(final String[] args) {
111         if (args.length != 3) {
112             giveUp();
113         }
114 
115         host = args[0];
116         port = Integer.parseInt(args[1]);
117         infoType = args[2];
118         final String infoTypeLc = infoType.toLowerCase();
119         if (!"all".equals(infoTypeLc)
120                 && !"controls".equals(infoTypeLc)
121                 && !"extops".equals(infoTypeLc)) {
122             giveUp();
123         }
124     }
125 
126     private static void printUsage() {
127         System.err.println("Usage: host port info-type");
128         System.err.println("\tAll arguments are required.");
129         System.err.println("\tinfo-type to get can be either all, controls, or extops.");
130     }
131 
132     private GetInfo() {
133         // Not used.
134     }
135 }