2017-09-20 4 views
-3

LDIF 파일을 읽고 항목을 분할하고 수정 한 다음 결과를 LDIF 파일로 써야합니다. 대소 문자 보존 LDIF 판독기/파서/스트림

나는 아파치 디렉토리의 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); 

작품 잘. 그러나 어떤 이유로 모든 속성 이름/아이디가 소문자로되어 있습니다 ("pwdAccountLockedTime"은 "pwdaccountlockedtime"이됩니다.)하지만 그대로 유지해야합니다. (그대로 유지하기 위해) 인간의 가독성은 이전과 마찬가지로과 같습니다. .

어떻게 할 수 있습니까? 필요한 경우 다른 라이브러리를 사용하겠습니다.

참고 : 일부 downvotes가 있기 때문에 제 질문을 개선하고 싶습니다. 무엇이 잘못되었거나 무엇이 빠졌는지 말해주십시오.

+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을 가지고 있기 때문에 나는 내 대답을 개선하고 싶습니다. 무엇이 잘못되었거나 무엇이 빠졌는지 말해주십시오.