View Javadoc

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