001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2012-2014 ForgeRock AS. 015 */ 016package org.forgerock.opendj.examples; 017 018import java.io.IOException; 019 020import org.forgerock.opendj.ldap.Connection; 021import org.forgerock.opendj.ldap.LdapException; 022import org.forgerock.opendj.ldap.LDAPConnectionFactory; 023import org.forgerock.opendj.ldap.ResultCode; 024import org.forgerock.opendj.ldap.SearchScope; 025import org.forgerock.opendj.ldap.responses.SearchResultEntry; 026import org.forgerock.opendj.ldif.LDIFEntryWriter; 027 028/** 029 * Demonstrates accessing server information about capabilities and schema. 030 */ 031public final class GetInfo { 032 /** Connection information. */ 033 private static String host; 034 private static int port; 035 /** The kind of server information to request (all, controls, extops). */ 036 private static String infoType; 037 038 /** 039 * Access the directory over LDAP to request information about capabilities 040 * and schema. 041 * 042 * @param args 043 * The command line arguments 044 */ 045 public static void main(final String[] args) { 046 parseArgs(args); 047 connect(); 048 } 049 050 /** 051 * Authenticate over LDAP. 052 */ 053 private static void connect() { 054 // --- JCite --- 055 final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port); 056 Connection connection = null; 057 058 try { 059 connection = factory.getConnection(); 060 connection.bind("", "".toCharArray()); // Anonymous bind 061 062 final String attributeList; 063 if ("controls".equals(infoType.toLowerCase())) { 064 attributeList = "supportedControl"; 065 } else if ("extops".equals(infoType.toLowerCase())) { 066 attributeList = "supportedExtension"; 067 } else { 068 attributeList = "+"; // All operational attributes 069 } 070 071 final SearchResultEntry entry = connection.searchSingleEntry( 072 "", // DN is "" for root DSE. 073 SearchScope.BASE_OBJECT, // Read only the root DSE. 074 "(objectclass=*)", // Every object matches this filter. 075 attributeList); // Return these requested attributes. 076 077 final LDIFEntryWriter writer = new LDIFEntryWriter(System.out); 078 writer.writeComment("Root DSE for LDAP server at " + host + ":" + port); 079 if (entry != null) { 080 writer.writeEntry(entry); 081 } 082 writer.flush(); 083 } catch (final LdapException e) { 084 System.err.println(e.getMessage()); 085 System.exit(e.getResult().getResultCode().intValue()); 086 return; 087 } catch (final IOException e) { 088 System.err.println(e.getMessage()); 089 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue()); 090 return; 091 } finally { 092 if (connection != null) { 093 connection.close(); 094 } 095 } 096 // --- JCite --- 097 } 098 099 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}