1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
30
31 public final class GetInfo {
32
33 private static String host;
34 private static int port;
35
36 private static String infoType;
37
38
39
40
41
42
43
44
45 public static void main(final String[] args) {
46 parseArgs(args);
47 connect();
48 }
49
50
51
52
53 private static void connect() {
54
55 final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port);
56 Connection connection = null;
57
58 try {
59 connection = factory.getConnection();
60 connection.bind("", "".toCharArray());
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 = "+";
69 }
70
71 final SearchResultEntry entry = connection.searchSingleEntry(
72 "",
73 SearchScope.BASE_OBJECT,
74 "(objectclass=*)",
75 attributeList);
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
97 }
98
99 private static void giveUp() {
100 printUsage();
101 System.exit(1);
102 }
103
104
105
106
107
108
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
134 }
135 }