2017-09-20 21 views
-3

LDIFファイルを読み込み、エントリを分割し、変更して、LDIFファイルとして結果を書き込む必要があります。大文字と小文字を区別するLDIFリーダ/パーサー/ストリーム

私は、ApacheディレクトリのLDAP API(org.apache.directory.api:api-ldap-client-api)でLdifReaderを見つけたので、私はこのようなものことを試してみました:

Stream<LdifEntry> stream = StreamSupport.stream(reader.spliterator(), false); 
Predicate<LdifEntry> isEnabled = entry -> entry.get("pwdAccountLockedTime") == null; 
Map<Boolean, List<LdifEntry>> parts = stream.collect(Collectors.partitioningBy(isEnabled)); 
List<LdifEntry> enabledAccounts = parts.get(true); 
List<LdifEntry> disabledAccounts = parts.get(false); 

がうまく動作します。しかし、何らかの理由で、すべての属性名/ idsが小文字になります(「pwdAccountLockedTime」は「pwdaccountlockedtime」などになります)が、そのまま使用する必要があります(ケースを保持しています)人間の可読性

どうすればいいですか?必要に応じて別のライブラリを使用します。

注:いくつかのダウンボックスがあるので、私は質問を改善したいと思います。何が間違っているのか、何が欠けているのか教えてください。

+1

あなたの必要条件は想像上のものです。大部分のLDAPは大文字小文字を区別しません。 – EJP

+0

私が説明したように、要件は人間の可読性を保持することです。 – mbee

答えて

-1

ライブラリーをorg.springframework.ldap:spring-ldap-ldif-coreに置き換え、ちょっとしたヘルパーを書くことで問題を解決できました。

public class LdifUtils { 
    /** 
    * Reads an LDIF file and returns its entries as a collection 
    * of <code>LdapAttributes</code> (LDAP entries). 
    * <br> 
    * Note: This method is not for huge files, 
    * as the content is loaded completely into memory. 
    * 
    * @param pathToLdifFile the <code>Path</code> to the LDAP Data Interchange Format file 
    * @return a <code>Collection</code> of <code>LdapAttributes</code> (LDAP entries) 
    * @throws IOException if reading the file fails 
    */ 
    public static Collection<LdapAttributes> read(final Path pathToLdifFile) throws IOException { 
     final LdifParser ldifParser = new LdifParser(pathToLdifFile.toFile()); 
     ldifParser.open(); 
     final Collection<LdapAttributes> c = new LinkedList<>(); 
     while (ldifParser.hasMoreRecords()){ 
      c.add(ldifParser.getRecord()); 
     } 
     ldifParser.close(); 
     return c; 
    } 
} 

前のような使い方...

final Stream<LdapAttributes> stream = LdifUtils.read(path).stream(); 
final Predicate<LdapAttributes> isEnabled = entry -> entry.get("pwdAccountLockedTime") == null; 
final Map<Boolean, List<LdapAttributes>> parts = stream.collect(Collectors.partitioningBy(isEnabled)); 
final List<LdapAttributes> enabledAccounts = parts.get(true); 
final List<LdapAttributes> disabledAccounts = parts.get(false); 
logger.info("enabled accounts: " + enabledAccounts.size()); 
logger.info("disabled accounts: " + disabledAccounts.size()); 

注:私はいくつかのdownvotesを得たので、私は、私の答えを改善したいと思います。何が間違っているのか、何が欠けているのか教えてください。

関連する問題