1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
40
41 public final class GetInfo {
42
43 private static String host;
44 private static int port;
45
46 private static String infoType;
47
48
49
50
51
52
53
54
55 public static void main(final String[] args) {
56 parseArgs(args);
57 connect();
58 }
59
60
61
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());
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 = "+";
78 }
79
80 final SearchResultEntry entry = connection.searchSingleEntry(
81 "",
82 SearchScope.BASE_OBJECT,
83 "objectclass=*",
84 attributeList);
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
114
115
116
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];
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
141 }
142 }