/** * Imports given LDIF file to the directory using given directory service and schema manager. * * @param ldifFiles * @throws Exception */ private void importLdif(List<String> ldifFiles) throws Exception { if (ldifFiles == null || ldifFiles.isEmpty()) { System.out.println("Importing default data\n"); importLdif(new LdifReader(LdapServer.class.getResourceAsStream("/" + LDIF_FILENAME_JBOSS_ORG))); } else { for (String ldifFile : ldifFiles) { System.out.println("Importing " + ldifFile + "\n"); importLdif(new LdifReader(ldifFile)); } } }
StringWriter writer = new StringWriter(); JsonWriter jsonWriter = new JsonWriter(writer); FileReader reader = new FileReader("entry.ldif"); LdifReader ldifReader = new LdifReader(reader); SearchResult result = ldifReader.read(); jsonWriter.write(result); System.out.println(writer.toString());
/** * Load the entries from a Ldif file recursively * @throws LdapLdifException * @throws IOException */ private static void loadEntries( File ldifFile, List<LdifEntry> entries ) throws LdapLdifException, IOException { if ( ldifFile.isDirectory() ) { File[] files = ldifFile.listFiles( ldifFilter ); for ( File f : files ) { loadEntries( f, entries ); } } else { LdifReader reader = new LdifReader(); try { entries.addAll( reader.parseLdifFile( ldifFile.getAbsolutePath() ) ); } finally { reader.close(); } } }
/** * Inject an ldif String into the server. DN must be relative to the * root. * * @param ldif the entries to inject * @throws NamingException if the entries cannot be added */ protected void addEntry(String ldif) throws Exception { ByteArrayInputStream in = new ByteArrayInputStream(ldif.getBytes("utf-8")); LdifReader reader = new LdifReader(in); loadLdif(reader); }
LdifReader reader = new LdifReader();
/** * {@inheritDoc} */ @Override public List<Entry> loadSyntaxes( Schema... schemas ) throws LdapException, IOException { List<Entry> syntaxList = new ArrayList<>(); if ( schemas == null ) { return syntaxList; } for ( Schema schema : schemas ) { String start = getSchemaDirectoryString( schema ) + SchemaConstants.SYNTAXES_PATH + "/" + "m-oid="; String end = "." + LDIF_EXT; for ( String resourcePath : RESOURCE_MAP.keySet() ) { if ( resourcePath.startsWith( start ) && resourcePath.endsWith( end ) ) { URL resource = getResource( resourcePath, "syntax LDIF file" ); LdifReader reader = new LdifReader( resource.openStream() ); LdifEntry entry = reader.next(); reader.close(); syntaxList.add( entry.getEntry() ); } } } return syntaxList; }
/** * {@inheritDoc} */ @Override public List<Entry> loadMatchingRules( Schema... schemas ) throws LdapException, IOException { List<Entry> matchingRuleList = new ArrayList<>(); if ( schemas == null ) { return matchingRuleList; } for ( Schema schema : schemas ) { String start = getSchemaDirectoryString( schema ) + SchemaConstants.MATCHING_RULES_PATH + "/" + "m-oid="; String end = "." + LDIF_EXT; for ( String resourcePath : RESOURCE_MAP.keySet() ) { if ( resourcePath.startsWith( start ) && resourcePath.endsWith( end ) ) { URL resource = getResource( resourcePath, "matchingRules LDIF file" ); LdifReader reader = new LdifReader( resource.openStream() ); LdifEntry entry = reader.next(); reader.close(); matchingRuleList.add( entry.getEntry() ); } } } return matchingRuleList; }
/** * {@inheritDoc} */ @Override public List<Entry> loadDitStructureRules( Schema... schemas ) throws LdapException, IOException { List<Entry> ditStructureRuleList = new ArrayList<>(); if ( schemas == null ) { return ditStructureRuleList; } for ( Schema schema : schemas ) { String start = getSchemaDirectoryString( schema ) + SchemaConstants.DIT_STRUCTURE_RULES_PATH + "/" + "m-oid="; String end = "." + LDIF_EXT; for ( String resourcePath : RESOURCE_MAP.keySet() ) { if ( resourcePath.startsWith( start ) && resourcePath.endsWith( end ) ) { URL resource = getResource( resourcePath, "ditStructureRule LDIF file" ); LdifReader reader = new LdifReader( resource.openStream() ); LdifEntry entry = reader.next(); reader.close(); ditStructureRuleList.add( entry.getEntry() ); } } } return ditStructureRuleList; }
/** * {@inheritDoc} */ @Override public List<Entry> loadComparators( Schema... schemas ) throws LdapException, IOException { List<Entry> comparatorList = new ArrayList<>(); if ( schemas == null ) { return comparatorList; } for ( Schema schema : schemas ) { String start = getSchemaDirectoryString( schema ) + SchemaConstants.COMPARATORS_PATH + "/" + "m-oid="; String end = "." + LDIF_EXT; for ( String resourcePath : RESOURCE_MAP.keySet() ) { if ( resourcePath.startsWith( start ) && resourcePath.endsWith( end ) ) { URL resource = getResource( resourcePath, "comparator LDIF file" ); LdifReader reader = new LdifReader( resource.openStream() ); LdifEntry entry = reader.next(); reader.close(); comparatorList.add( entry.getEntry() ); } } } return comparatorList; }
/** * {@inheritDoc} */ @Override public List<Entry> loadDitContentRules( Schema... schemas ) throws LdapException, IOException { List<Entry> ditContentRulesList = new ArrayList<>(); if ( schemas == null ) { return ditContentRulesList; } for ( Schema schema : schemas ) { String start = getSchemaDirectoryString( schema ) + SchemaConstants.DIT_CONTENT_RULES_PATH + "/" + "m-oid="; String end = "." + LDIF_EXT; for ( String resourcePath : RESOURCE_MAP.keySet() ) { if ( resourcePath.startsWith( start ) && resourcePath.endsWith( end ) ) { URL resource = getResource( resourcePath, "ditContentRule LDIF file" ); LdifReader reader = new LdifReader( resource.openStream() ); LdifEntry entry = reader.next(); reader.close(); ditContentRulesList.add( entry.getEntry() ); } } } return ditContentRulesList; }
/** * {@inheritDoc} */ @Override public List<Entry> loadSyntaxCheckers( Schema... schemas ) throws LdapException, IOException { List<Entry> syntaxCheckerList = new ArrayList<>(); if ( schemas == null ) { return syntaxCheckerList; } for ( Schema schema : schemas ) { String start = getSchemaDirectoryString( schema ) + SchemaConstants.SYNTAX_CHECKERS_PATH + "/" + "m-oid="; String end = "." + LDIF_EXT; for ( String resourcePath : RESOURCE_MAP.keySet() ) { if ( resourcePath.startsWith( start ) && resourcePath.endsWith( end ) ) { URL resource = getResource( resourcePath, "syntaxChecker LDIF file" ); LdifReader reader = new LdifReader( resource.openStream() ); LdifEntry entry = reader.next(); reader.close(); syntaxCheckerList.add( entry.getEntry() ); } } } return syntaxCheckerList; }
/** * {@inheritDoc} */ @Override public List<Entry> loadSyntaxes( Schema... schemas ) throws LdapException, IOException { List<Entry> syntaxList = new ArrayList<>(); if ( schemas == null ) { return syntaxList; } for ( Schema schema : schemas ) { String start = getSchemaDirectoryString( schema ) + SchemaConstants.SYNTAXES_PATH + "/" + "m-oid="; String end = "." + LDIF_EXT; for ( String resourcePath : RESOURCE_MAP.keySet() ) { if ( resourcePath.startsWith( start ) && resourcePath.endsWith( end ) ) { URL resource = getResource( resourcePath, "syntax LDIF file" ); LdifReader reader = new LdifReader( resource.openStream() ); LdifEntry entry = reader.next(); reader.close(); syntaxList.add( entry.getEntry() ); } } } return syntaxList; }
/** * {@inheritDoc} */ @Override public List<Entry> loadMatchingRuleUses( Schema... schemas ) throws LdapException, IOException { List<Entry> matchingRuleUseList = new ArrayList<>(); if ( schemas == null ) { return matchingRuleUseList; } for ( Schema schema : schemas ) { String start = getSchemaDirectoryString( schema ) + SchemaConstants.MATCHING_RULE_USE_PATH + "/" + "m-oid="; String end = "." + LDIF_EXT; for ( String resourcePath : RESOURCE_MAP.keySet() ) { if ( resourcePath.startsWith( start ) && resourcePath.endsWith( end ) ) { URL resource = getResource( resourcePath, "matchingRuleUse LDIF file" ); LdifReader reader = new LdifReader( resource.openStream() ); LdifEntry entry = reader.next(); reader.close(); matchingRuleUseList.add( entry.getEntry() ); } } } return matchingRuleUseList; }
/** * Process an LDIF file from a reader. */ private List<String> processLdif(Reader reader) throws CamelException { LdapConnection conn = getLdapConnection(); LdifReader ldifReader; List<String> results = new ArrayList<>(); // Create the reader try { ldifReader = new LdifReader(reader); } catch (LdapException e) { throw new CamelException("Unable to create LDIF reader", e); } // Process each entry for (LdifEntry e : ldifReader) { results.add(processLdifEntry(conn, e)); } IOHelper.close(conn, ldifReader, reader); return results; }
/** * Creates a principal in the KDC with the specified user and password. * * @param principal principal name, do not include the domain. * @param password password. * @throws Exception thrown if the principal could not be created. */ public synchronized void createPrincipal(String principal, String password) throws Exception { String orgName= conf.getProperty(ORG_NAME); String orgDomain = conf.getProperty(ORG_DOMAIN); String baseDn = "ou=users,dc=" + orgName.toLowerCase() + ",dc=" + orgDomain.toLowerCase(); String content = "dn: uid=" + principal + "," + baseDn + "\n" + "objectClass: top\n" + "objectClass: person\n" + "objectClass: inetOrgPerson\n" + "objectClass: krb5principal\n" + "objectClass: krb5kdcentry\n" + "cn: " + principal + "\n" + "sn: " + principal + "\n" + "uid: " + principal + "\n" + "userPassword: " + password + "\n" + "krb5PrincipalName: " + principal + "@" + getRealm() + "\n" + "krb5KeyVersionNumber: 0"; for (LdifEntry ldifEntry : new LdifReader(new StringReader(content))) { ds.getAdminSession().add(new DefaultEntry(ds.getSchemaManager(), ldifEntry.getEntry())); } }
/** * Apply LDIF text. * * @param ldif * LDIF text. * @throws LdapException * if some LDAP related error occurs. * @throws IOException * if stream could not be closed. */ private void applyLdif(final String ldif) throws LdapException, IOException { try (LdifReader entries = new LdifReader(new StringReader(ldif))) { for (LdifEntry ldifEntry : entries) { final DefaultEntry newEntry = new DefaultEntry(this.service.getSchemaManager(), ldifEntry.getEntry()); LOGGER.debug("LDIF entry: {}", newEntry); this.service.getAdminSession().add(newEntry); } } }
/** * Loads an LDIF from an input stream and adds the entries it contains to * the server. It appears as though the administrator added these entries * to the server. * * @param in the input stream containing the LDIF entries to load * @return a list of entries added to the server in the order they were added * @throws NamingException of the load fails */ protected List<LdifEntry> loadLdif(InputStream in) throws Exception { if (in == null) { return EMPTY_LIST; } LdifReader ldifReader = new LdifReader(in); return loadLdif(ldifReader); }
/** * Inject an ldif String into the server. Dn must be relative to the root. * * @param service the directory service to use * @param ldif the ldif containing entries to add to the server. * @throws Exception if there is a problem adding the entries from the LDIF */ public static void injectEntries( DirectoryService service, String ldif ) throws Exception { LdifReader reader = new LdifReader(); List<LdifEntry> entries = reader.parseLdif( ldif ); for ( LdifEntry entry : entries ) { injectEntry( entry, service ); } // And close the reader reader.close(); }
public synchronized void createPrincipal(String principal, String password) throws Exception { String baseDn = getKdcServer().getSearchBaseDn(); String content = "dn: uid=" + principal + "," + baseDn + "\n" + "objectClass: top\n" + "objectClass: person\n" + "objectClass: inetOrgPerson\n" + "objectClass: krb5principal\n" + "objectClass: krb5kdcentry\n" + "cn: " + principal + "\n" + "sn: " + principal + "\n" + "uid: " + principal + "\n" + "userPassword: " + password + "\n" // using businessCategory as a proxy for memberoOf attribute pending: https://issues.apache.org/jira/browse/DIRSERVER-1844 + "businessCategory: " + "cn=admins,ou=system" + "\n" + "businessCategory: " + "cn=bees,ou=system" + "\n" + "krb5PrincipalName: " + principal + "@" + getRealm() + "\n" + "krb5KeyVersionNumber: 0"; for (LdifEntry ldifEntry : new LdifReader(new StringReader(content))) { service.getAdminSession().add(new DefaultEntry(service.getSchemaManager(), ldifEntry.getEntry())); } }
/** * Stream will be closed automatically. */ public void importLdif(InputStream is) throws Exception { try (LdifReader reader = new LdifReader(is)) { CoreSession coreSession = directoryService.getAdminSession(); // see LdifFileLoader for (LdifEntry ldifEntry : reader) { String ldif = ldifEntry.toString(); LOG.info(ldif); if (ChangeType.Add == ldifEntry.getChangeType() || /* assume "add" by default */ ChangeType.None == ldifEntry.getChangeType()) { coreSession.add(new DefaultEntry(coreSession.getDirectoryService().getSchemaManager(), ldifEntry.getEntry())); } else if (ChangeType.Modify == ldifEntry.getChangeType()) { coreSession.modify(ldifEntry.getDn(), ldifEntry.getModifications()); } else if (ChangeType.Delete == ldifEntry.getChangeType()) { coreSession.delete(ldifEntry.getDn()); } else { throw new IllegalStateException(); } } } }