001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2009-2010 Sun Microsystems, Inc. 025 * Portions Copyright 2011-2015 ForgeRock AS. 026 */ 027 028package org.forgerock.opendj.examples; 029 030import java.io.IOException; 031import java.util.Arrays; 032 033import org.forgerock.opendj.ldap.Connection; 034import org.forgerock.opendj.ldap.LdapException; 035import org.forgerock.opendj.ldap.LDAPConnectionFactory; 036import org.forgerock.opendj.ldap.ResultCode; 037import org.forgerock.opendj.ldap.SearchScope; 038import org.forgerock.opendj.ldap.responses.SearchResultEntry; 039import org.forgerock.opendj.ldap.responses.SearchResultReference; 040import org.forgerock.opendj.ldif.ConnectionEntryReader; 041import org.forgerock.opendj.ldif.LDIFEntryWriter; 042 043/** 044 * An example client application which searches a Directory Server. This example 045 * takes the following command line parameters: 046 * 047 * <pre> 048 * {@code <host> <port> <username> <password> 049 * <baseDN> <scope> <filter> [<attribute> <attribute> ...]} 050 * </pre> 051 */ 052public final class Search { 053 /** 054 * Main method. 055 * 056 * @param args 057 * The command line arguments: host, port, username, password, 058 * base DN, scope, filter, and zero or more attributes to be 059 * retrieved. 060 */ 061 public static void main(final String[] args) { 062 if (args.length < 7) { 063 System.err.println("Usage: host port username password baseDN scope " 064 + "filter [attribute ...]"); 065 System.exit(1); 066 } 067 068 // Parse command line arguments. 069 final String hostName = args[0]; 070 final int port = Integer.parseInt(args[1]); 071 final String userName = args[2]; 072 final String password = args[3]; 073 final String baseDN = args[4]; 074 final String scopeString = args[5]; 075 final String filter = args[6]; 076 String[] attributes; 077 if (args.length > 7) { 078 attributes = Arrays.copyOfRange(args, 7, args.length); 079 } else { 080 attributes = new String[0]; 081 } 082 083 final SearchScope scope = SearchScope.valueOf(scopeString); 084 if (scope == null) { 085 System.err.println("Unknown scope: " + scopeString); 086 System.exit(ResultCode.CLIENT_SIDE_PARAM_ERROR.intValue()); 087 return; 088 } 089 090 // --- JCite --- 091 // Create an LDIF writer which will write the search results to stdout. 092 final LDIFEntryWriter writer = new LDIFEntryWriter(System.out); 093 094 // Connect and bind to the server. 095 final LDAPConnectionFactory factory = new LDAPConnectionFactory(hostName, port); 096 Connection connection = null; 097 098 try { 099 connection = factory.getConnection(); 100 connection.bind(userName, password.toCharArray()); 101 102 // Read the entries and output them as LDIF. 103 final ConnectionEntryReader reader = 104 connection.search(baseDN, scope, filter, attributes); 105 while (reader.hasNext()) { 106 if (!reader.isReference()) { 107 final SearchResultEntry entry = reader.readEntry(); 108 writer.writeComment("Search result entry: " + entry.getName()); 109 writer.writeEntry(entry); 110 } else { 111 final SearchResultReference ref = reader.readReference(); 112 113 // Got a continuation reference. 114 writer.writeComment("Search result reference: " + ref.getURIs()); 115 } 116 } 117 writer.flush(); 118 } catch (final LdapException e) { 119 System.err.println(e.getMessage()); 120 System.exit(e.getResult().getResultCode().intValue()); 121 return; 122 } catch (final IOException e) { 123 System.err.println(e.getMessage()); 124 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue()); 125 return; 126 } finally { 127 if (connection != null) { 128 connection.close(); 129 } 130 } 131 // --- JCite --- 132 } 133 134 private Search() { 135 // Not used. 136 } 137}