Search in sources :

Example 1 with SearchEntryParer

use of com.unboundid.ldap.listener.SearchEntryParer in project ldapsdk by pingidentity.

the class LDIFSearch method doToolProcessing.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public ResultCode doToolProcessing() {
    // Get the schema to use when performing LDIF processing.
    final Schema schema;
    try {
        if (schemaPath.isPresent()) {
            schema = getSchema(schemaPath.getValues());
        } else if (PING_SERVER_AVAILABLE) {
            schema = getSchema(Collections.singletonList(StaticUtils.constructPath(PING_SERVER_ROOT, "config", "schema")));
        } else {
            schema = Schema.getDefaultStandardSchema();
        }
    } catch (final Exception e) {
        Debug.debugException(e);
        logCompletionMessage(true, ERR_LDIFSEARCH_CANNOT_GET_SCHEMA.get(StaticUtils.getExceptionMessage(e)));
        return ResultCode.LOCAL_ERROR;
    }
    // Create search entry parers for all of the search URLs.
    final Map<LDAPURL, SearchEntryParer> urlMap = new LinkedHashMap<>();
    for (final LDAPURL url : searchURLs) {
        final SearchEntryParer parer = new SearchEntryParer(Arrays.asList(url.getAttributes()), schema);
        urlMap.put(url, parer);
    }
    // If we should check schema, then create the entry validator.
    final EntryValidator entryValidator;
    if (checkSchema.isPresent()) {
        entryValidator = new EntryValidator(schema);
    } else {
        entryValidator = null;
    }
    // Create the output files, if appropriate.
    OutputStream outputStream = null;
    SearchEntryParer singleParer = null;
    final Map<LDAPURL, LDIFSearchSeparateSearchDetails> separateWriters = new LinkedHashMap<>();
    try {
        if (outputFile.isPresent()) {
            final int numURLs = searchURLs.size();
            if (separateOutputFilePerSearch.isPresent() && (numURLs > 1)) {
                int i = 1;
                for (final LDAPURL url : searchURLs) {
                    final File f = new File(outputFile.getValue().getAbsolutePath() + '.' + i);
                    final LDIFSearchSeparateSearchDetails details = new LDIFSearchSeparateSearchDetails(url, f, createLDIFWriter(f, url), schema);
                    separateWriters.put(url, details);
                    i++;
                }
            } else {
                try {
                    outputStream = createOutputStream(outputFile.getValue());
                    resultWriter.updateOutputStream(outputStream);
                } catch (final Exception e) {
                    Debug.debugException(e);
                    throw new LDAPException(ResultCode.LOCAL_ERROR, ERR_LDIFSEARCH_CANNOT_WRITE_TO_FILE.get(outputFile.getValue().getAbsolutePath(), StaticUtils.getExceptionMessage(e)), e);
                }
            }
        }
        // to the top of the output.
        if (separateWriters.isEmpty()) {
            resultWriter.writeHeader();
        }
        // Iterate through the LDIF files and process the entries they contain.
        boolean errorEncountered = false;
        final List<LDAPURL> matchingURLs = new ArrayList<>();
        final List<String> entryInvalidReasons = new ArrayList<>();
        for (final File f : ldifFile.getValues()) {
            final LDIFReader ldifReader;
            try {
                ldifReader = new LDIFReader(openInputStream(f));
                if (stripTrailingSpaces.isPresent()) {
                    ldifReader.setTrailingSpaceBehavior(TrailingSpaceBehavior.STRIP);
                } else {
                    ldifReader.setTrailingSpaceBehavior(TrailingSpaceBehavior.REJECT);
                }
            } catch (final Exception e) {
                Debug.debugException(e);
                logCompletionMessage(true, ERR_LDIFSEARCH_CANNOT_OPEN_LDIF_FILE.get(f.getName(), StaticUtils.getExceptionMessage(e)));
                return ResultCode.LOCAL_ERROR;
            }
            try {
                while (true) {
                    final Entry entry;
                    try {
                        entry = ldifReader.readEntry();
                    } catch (final LDIFException e) {
                        Debug.debugException(e);
                        if (e.mayContinueReading()) {
                            commentToErr(ERR_LDIFSEARCH_RECOVERABLE_READ_ERROR.get(f.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
                            errorEncountered = true;
                            continue;
                        } else {
                            logCompletionMessage(true, ERR_LDIFSEARCH_UNRECOVERABLE_READ_ERROR.get(f.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
                            return ResultCode.LOCAL_ERROR;
                        }
                    } catch (final Exception e) {
                        logCompletionMessage(true, ERR_LDIFSEARCH_UNRECOVERABLE_READ_ERROR.get(f.getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
                        return ResultCode.LOCAL_ERROR;
                    }
                    if (entry == null) {
                        break;
                    }
                    if (entryValidator != null) {
                        entryInvalidReasons.clear();
                        if (!entryValidator.entryIsValid(entry, entryInvalidReasons)) {
                            commentToErr(ERR_LDIFSEARCH_ENTRY_VIOLATES_SCHEMA.get(entry.getDN()));
                            for (final String invalidReason : entryInvalidReasons) {
                                commentToErr("- " + invalidReason);
                            }
                            err();
                            errorEncountered = true;
                            continue;
                        }
                    }
                    if (separateWriters.isEmpty()) {
                        matchingURLs.clear();
                        for (final LDAPURL url : searchURLs) {
                            if (urlMatchesEntry(url, entry)) {
                                matchingURLs.add(url);
                            }
                        }
                        if (matchingURLs.isEmpty()) {
                            continue;
                        }
                        try {
                            if (searchURLs.size() > 1) {
                                resultWriter.writeComment(INFO_LDIFSEARCH_ENTRY_MATCHES_URLS.get(entry.getDN()));
                                for (final LDAPURL url : matchingURLs) {
                                    resultWriter.writeComment(url.toString());
                                }
                            }
                            if (singleParer == null) {
                                singleParer = new SearchEntryParer(Arrays.asList(searchURLs.get(0).getAttributes()), schema);
                            }
                            resultWriter.writeSearchResultEntry(new SearchResultEntry(singleParer.pareEntry(entry)));
                            if (!outputFile.isPresent()) {
                                resultWriter.flush();
                            }
                        } catch (final Exception e) {
                            Debug.debugException(e);
                            if (outputFile.isPresent()) {
                                logCompletionMessage(true, ERR_LDIFSEARCH_WRITE_ERROR_WITH_FILE.get(entry.getDN(), outputFile.getValue().getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
                            } else {
                                logCompletionMessage(true, ERR_LDIFSEARCH_WRITE_ERROR_NO_FILE.get(entry.getDN(), StaticUtils.getExceptionMessage(e)));
                            }
                            return ResultCode.LOCAL_ERROR;
                        }
                    } else {
                        for (final LDIFSearchSeparateSearchDetails details : separateWriters.values()) {
                            final LDAPURL url = details.getLDAPURL();
                            if (urlMatchesEntry(url, entry)) {
                                try {
                                    final Entry paredEntry = details.getSearchEntryParer().pareEntry(entry);
                                    details.getLDIFWriter().writeEntry(paredEntry);
                                } catch (final Exception ex) {
                                    Debug.debugException(ex);
                                    logCompletionMessage(true, ERR_LDIFSEARCH_WRITE_ERROR_WITH_FILE.get(entry.getDN(), details.getOutputFile().getAbsolutePath(), StaticUtils.getExceptionMessage(ex)));
                                    return ResultCode.LOCAL_ERROR;
                                }
                            }
                        }
                    }
                }
            } finally {
                try {
                    ldifReader.close();
                } catch (final Exception e) {
                    Debug.debugException(e);
                }
            }
        }
        if (errorEncountered) {
            logCompletionMessage(true, WARN_LDIFSEARCH_COMPLETED_WITH_ERRORS.get());
            return ResultCode.PARAM_ERROR;
        } else {
            logCompletionMessage(false, INFO_LDIFSEARCH_COMPLETED_SUCCESSFULLY.get());
            return ResultCode.SUCCESS;
        }
    } catch (final LDAPException e) {
        Debug.debugException(e);
        logCompletionMessage(true, e.getMessage());
        return e.getResultCode();
    } finally {
        try {
            resultWriter.flush();
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (final Exception e) {
            Debug.debugException(e);
        }
        for (final LDIFSearchSeparateSearchDetails details : separateWriters.values()) {
            try {
                details.getLDIFWriter().close();
            } catch (final Exception e) {
                Debug.debugException(e);
            }
        }
    }
}
Also used : Schema(com.unboundid.ldap.sdk.schema.Schema) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) EntryValidator(com.unboundid.ldap.sdk.schema.EntryValidator) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) SearchEntryParer(com.unboundid.ldap.listener.SearchEntryParer) LinkedHashMap(java.util.LinkedHashMap) Entry(com.unboundid.ldap.sdk.Entry) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) LDAPURL(com.unboundid.ldap.sdk.LDAPURL) LDAPException(com.unboundid.ldap.sdk.LDAPException) File(java.io.File) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) NotNull(com.unboundid.util.NotNull)

Aggregations

SearchEntryParer (com.unboundid.ldap.listener.SearchEntryParer)1 Entry (com.unboundid.ldap.sdk.Entry)1 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 LDAPURL (com.unboundid.ldap.sdk.LDAPURL)1 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)1 EntryValidator (com.unboundid.ldap.sdk.schema.EntryValidator)1 Schema (com.unboundid.ldap.sdk.schema.Schema)1 NotNull (com.unboundid.util.NotNull)1 PassphraseEncryptedOutputStream (com.unboundid.util.PassphraseEncryptedOutputStream)1 ArgumentException (com.unboundid.util.args.ArgumentException)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1