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 2015 ForgeRock AS. 015 */ 016 017package org.forgerock.opendj.examples; 018 019import org.forgerock.i18n.LocalizableMessage; 020import org.forgerock.opendj.ldap.Connection; 021import org.forgerock.opendj.ldap.DN; 022import org.forgerock.opendj.ldap.DecodeException; 023import org.forgerock.opendj.ldap.Entry; 024import org.forgerock.opendj.ldap.EntryNotFoundException; 025import org.forgerock.opendj.ldap.LDAPConnectionFactory; 026import org.forgerock.opendj.ldap.LdapException; 027import org.forgerock.opendj.ldap.ResultCode; 028import org.forgerock.opendj.ldap.responses.Result; 029import org.forgerock.opendj.ldap.schema.Schema; 030import org.forgerock.opendj.ldap.schema.SchemaValidationPolicy; 031import org.forgerock.opendj.ldif.LDIFEntryReader; 032 033import java.io.IOException; 034import java.util.LinkedList; 035import java.util.List; 036 037/** 038 * This example command-line client application validates an entry 039 * against the directory server schema before adding it. 040 * 041 * <br> 042 * 043 * This example takes the following command line parameters: 044 * 045 * <pre> 046 * {@code <host> <port> <bindDN> <bindPassword>} 047 * </pre> 048 * 049 * Then it reads an entry to add from System.in. 050 * If the entry is valid according to the directory schema, 051 * it tries to add the entry to the directory. 052 */ 053public final class UseSchema { 054 /** 055 * Main method. 056 * 057 * @param args 058 * The command line arguments: host, port, bindDN, bindPassword. 059 */ 060 public static void main(final String[] args) { 061 if (args.length != 4) { 062 System.err.println("Usage: host port bindDN bindPassword"); 063 System.exit(1); 064 } 065 066 // Parse command line arguments. 067 final String host = args[0]; 068 final int port = Integer.parseInt(args[1]); 069 final String bindDn = args[2]; 070 final String bindPassword = args[3]; 071 072 // --- JCite --- 073 // Connect and bind to the server. 074 final LDAPConnectionFactory factory = new LDAPConnectionFactory(host, port); 075 Connection connection = null; 076 077 try { 078 connection = factory.getConnection(); 079 connection.bind(bindDn, bindPassword.toCharArray()); 080 081 // Read the schema from the directory server. 082 // If that fails, use the default schema from the LDAP SDK. 083 Schema schema = null; 084 try { 085 schema = Schema.readSchema(connection, DN.valueOf("cn=schema")); 086 } catch (EntryNotFoundException e) { 087 System.err.println(e.getMessage()); 088 schema = Schema.getDefaultSchema(); 089 } finally { 090 if (schema == null) { 091 System.err.println("Failed to get schema."); 092 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue()); 093 } 094 } 095 096 // Read an entry from System.in. 097 final LDIFEntryReader reader = new LDIFEntryReader(System.in); 098 final Entry entry = reader.readEntry(); 099 100 // If the entry is valid, try to add it. Otherwise display errors. 101 final List<LocalizableMessage> schemaErrors = new LinkedList<>(); 102 boolean conformsToSchema = schema.validateEntry( 103 entry, SchemaValidationPolicy.defaultPolicy(), schemaErrors); 104 final String entryDn = entry.getName().toString(); 105 Result result = null; 106 if (conformsToSchema) { 107 System.out.println("Processing ADD request for " + entryDn); 108 result = connection.add(entry); 109 } else { 110 for (LocalizableMessage error : schemaErrors) { 111 System.err.println(error); 112 } 113 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue()); 114 } 115 116 // Display the result. (A failed add results in an LdapException.) 117 if (result != null) { 118 System.out.println("ADD operation successful for DN " + entryDn); 119 } 120 } catch (final LdapException e) { 121 System.err.println(e.getMessage()); 122 System.exit(e.getResult().getResultCode().intValue()); 123 } catch (DecodeException e) { 124 System.err.println(e.getMessage()); 125 System.exit(ResultCode.CLIENT_SIDE_DECODING_ERROR.intValue()); 126 } catch (IOException e) { 127 System.err.println(e.getMessage()); 128 System.exit(ResultCode.CLIENT_SIDE_LOCAL_ERROR.intValue()); 129 } finally { 130 if (connection != null) { 131 connection.close(); 132 } 133 } 134 // --- JCite --- 135 } 136 137 private UseSchema() { 138 // Not used. 139 } 140}