Search in sources :

Example 1 with TeeOutputStream

use of com.unboundid.util.TeeOutputStream in project ldapsdk by pingidentity.

the class LDAPSearch method doToolProcessing.

/**
 * {@inheritDoc}
 */
@Override()
@NotNull()
public ResultCode doToolProcessing() {
    // If we should encrypt the output, then get the encryption passphrase.
    if (encryptOutput.isPresent()) {
        if (encryptionPassphraseFile.isPresent()) {
            try {
                encryptionPassphrase = ToolUtils.readEncryptionPassphraseFromFile(encryptionPassphraseFile.getValue());
            } catch (final LDAPException e) {
                Debug.debugException(e);
                wrapErr(0, WRAP_COLUMN, e.getMessage());
                return e.getResultCode();
            }
        } else {
            try {
                encryptionPassphrase = ToolUtils.promptForEncryptionPassphrase(false, true, getOut(), getErr());
            } catch (final LDAPException e) {
                Debug.debugException(e);
                wrapErr(0, WRAP_COLUMN, e.getMessage());
                return e.getResultCode();
            }
        }
    }
    // the header to standard output.
    if (outputFile.isPresent()) {
        if (!separateOutputFilePerSearch.isPresent()) {
            try {
                OutputStream s = new FileOutputStream(outputFile.getValue());
                if (encryptOutput.isPresent()) {
                    s = new PassphraseEncryptedOutputStream(encryptionPassphrase, s);
                }
                if (compressOutput.isPresent()) {
                    s = new GZIPOutputStream(s);
                }
                if (teeResultsToStandardOut.isPresent()) {
                    outStream = new PrintStream(new TeeOutputStream(s, getOut()));
                } else {
                    outStream = new PrintStream(s);
                }
                resultWriter.updateOutputStream(outStream);
                errStream = outStream;
            } catch (final Exception e) {
                Debug.debugException(e);
                wrapErr(0, WRAP_COLUMN, ERR_LDAPSEARCH_CANNOT_OPEN_OUTPUT_FILE.get(outputFile.getValue().getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
                return ResultCode.LOCAL_ERROR;
            }
            resultWriter.writeHeader();
        }
    } else {
        resultWriter.writeHeader();
    }
    // Examine the arguments to determine the sets of controls to use for each
    // type of request.
    final List<Control> searchControls = getSearchControls();
    // If appropriate, ensure that any search result entries that include
    // base64-encoded attribute values will also include comments that attempt
    // to provide a human-readable representation of that value.
    final boolean originalCommentAboutBase64EncodedValues = LDIFWriter.commentAboutBase64EncodedValues();
    LDIFWriter.setCommentAboutBase64EncodedValues(!suppressBase64EncodedValueComments.isPresent());
    LDAPConnectionPool pool = null;
    try {
        // directory server.
        if (!dryRun.isPresent()) {
            try {
                final StartAdministrativeSessionPostConnectProcessor p;
                if (useAdministrativeSession.isPresent()) {
                    p = new StartAdministrativeSessionPostConnectProcessor(new StartAdministrativeSessionExtendedRequest(getToolName(), true));
                } else {
                    p = null;
                }
                pool = getConnectionPool(1, 1, 0, p, null, true, new ReportBindResultLDAPConnectionPoolHealthCheck(this, true, false));
            } catch (final LDAPException le) {
                // This shouldn't happen since the pool won't throw an exception if an
                // attempt to create an initial connection fails.
                Debug.debugException(le);
                commentToErr(ERR_LDAPSEARCH_CANNOT_CREATE_CONNECTION_POOL.get(StaticUtils.getExceptionMessage(le)));
                return le.getResultCode();
            }
            if (retryFailedOperations.isPresent()) {
                pool.setRetryFailedOperationsDueToInvalidConnections(true);
            }
        }
        // If appropriate, create a rate limiter.
        final FixedRateBarrier rateLimiter;
        if (ratePerSecond.isPresent()) {
            rateLimiter = new FixedRateBarrier(1000L, ratePerSecond.getValue());
        } else {
            rateLimiter = null;
        }
        // requests from those URLs.
        if (ldapURLFile.isPresent()) {
            return searchWithLDAPURLs(pool, rateLimiter, searchControls);
        }
        // Get the set of requested attributes, as a combination of the
        // requestedAttribute argument values and any trailing arguments.
        final ArrayList<String> attrList = new ArrayList<>(10);
        if (requestedAttribute.isPresent()) {
            attrList.addAll(requestedAttribute.getValues());
        }
        final List<String> trailingArgs = parser.getTrailingArguments();
        if (!trailingArgs.isEmpty()) {
            final Iterator<String> trailingArgIterator = trailingArgs.iterator();
            if (!(filter.isPresent() || filterFile.isPresent())) {
                trailingArgIterator.next();
            }
            while (trailingArgIterator.hasNext()) {
                attrList.add(trailingArgIterator.next());
            }
        }
        final String[] attributes = new String[attrList.size()];
        attrList.toArray(attributes);
        // If either or both the filter or filterFile arguments are provided, then
        // use them to get the filters to process.  Otherwise, the first trailing
        // argument should be a filter.
        ResultCode resultCode = ResultCode.SUCCESS;
        if (filter.isPresent() || filterFile.isPresent()) {
            if (filter.isPresent()) {
                for (final Filter f : filter.getValues()) {
                    final ResultCode rc = searchWithFilter(pool, f, attributes, rateLimiter, searchControls);
                    if (rc != ResultCode.SUCCESS) {
                        if (resultCode == ResultCode.SUCCESS) {
                            resultCode = rc;
                        }
                        if (!continueOnError.isPresent()) {
                            return resultCode;
                        }
                    }
                }
            }
            if (filterFile.isPresent()) {
                final ResultCode rc = searchWithFilterFile(pool, attributes, rateLimiter, searchControls);
                if (rc != ResultCode.SUCCESS) {
                    if (resultCode == ResultCode.SUCCESS) {
                        resultCode = rc;
                    }
                    if (!continueOnError.isPresent()) {
                        return resultCode;
                    }
                }
            }
        } else {
            final Filter f;
            try {
                final String filterStr = parser.getTrailingArguments().iterator().next();
                f = Filter.create(filterStr);
            } catch (final LDAPException le) {
                // This should never happen.
                Debug.debugException(le);
                displayResult(le.toLDAPResult());
                return le.getResultCode();
            }
            resultCode = searchWithFilter(pool, f, attributes, rateLimiter, searchControls);
        }
        return resultCode;
    } finally {
        if (pool != null) {
            try {
                pool.close();
            } catch (final Exception e) {
                Debug.debugException(e);
            }
        }
        if (outStream != null) {
            try {
                outStream.close();
                outStream = null;
            } catch (final Exception e) {
                Debug.debugException(e);
            }
        }
        if (errStream != null) {
            try {
                errStream.close();
                errStream = null;
            } catch (final Exception e) {
                Debug.debugException(e);
            }
        }
        LDIFWriter.setCommentAboutBase64EncodedValues(originalCommentAboutBase64EncodedValues);
    }
}
Also used : StartAdministrativeSessionPostConnectProcessor(com.unboundid.ldap.sdk.unboundidds.extensions.StartAdministrativeSessionPostConnectProcessor) PrintStream(java.io.PrintStream) TeeOutputStream(com.unboundid.util.TeeOutputStream) LDAPConnectionPool(com.unboundid.ldap.sdk.LDAPConnectionPool) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) TeeOutputStream(com.unboundid.util.TeeOutputStream) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) VirtualListViewRequestControl(com.unboundid.ldap.sdk.controls.VirtualListViewRequestControl) RouteToServerRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.RouteToServerRequestControl) RFC3672SubentriesRequestControl(com.unboundid.ldap.sdk.controls.RFC3672SubentriesRequestControl) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl) MatchingEntryCountRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.MatchingEntryCountRequestControl) MatchedValuesRequestControl(com.unboundid.ldap.sdk.controls.MatchedValuesRequestControl) VirtualAttributesOnlyRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.VirtualAttributesOnlyRequestControl) AccountUsableRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.AccountUsableRequestControl) OverrideSearchLimitsRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.OverrideSearchLimitsRequestControl) SuppressOperationalAttributeUpdateRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.SuppressOperationalAttributeUpdateRequestControl) ProxiedAuthorizationV1RequestControl(com.unboundid.ldap.sdk.controls.ProxiedAuthorizationV1RequestControl) OperationPurposeRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.OperationPurposeRequestControl) SoftDeletedEntryAccessRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.SoftDeletedEntryAccessRequestControl) JoinRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.JoinRequestControl) ReturnConflictEntriesRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.ReturnConflictEntriesRequestControl) GetRecentLoginHistoryRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetRecentLoginHistoryRequestControl) PermitUnindexedSearchRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.PermitUnindexedSearchRequestControl) RejectUnindexedSearchRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.RejectUnindexedSearchRequestControl) AuthorizationIdentityRequestControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl) Control(com.unboundid.ldap.sdk.Control) GetUserResourceLimitsRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetUserResourceLimitsRequestControl) GetBackendSetIDRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetBackendSetIDRequestControl) GetAuthorizationEntryRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetAuthorizationEntryRequestControl) RealAttributesOnlyRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.RealAttributesOnlyRequestControl) ExcludeBranchRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.ExcludeBranchRequestControl) ProxiedAuthorizationV2RequestControl(com.unboundid.ldap.sdk.controls.ProxiedAuthorizationV2RequestControl) ServerSideSortRequestControl(com.unboundid.ldap.sdk.controls.ServerSideSortRequestControl) GetServerIDRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetServerIDRequestControl) PasswordPolicyRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.PasswordPolicyRequestControl) AssertionRequestControl(com.unboundid.ldap.sdk.controls.AssertionRequestControl) RouteToBackendSetRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.RouteToBackendSetRequestControl) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) PersistentSearchRequestControl(com.unboundid.ldap.sdk.controls.PersistentSearchRequestControl) GetEffectiveRightsRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetEffectiveRightsRequestControl) DraftLDUPSubentriesRequestControl(com.unboundid.ldap.sdk.controls.DraftLDUPSubentriesRequestControl) StartAdministrativeSessionExtendedRequest(com.unboundid.ldap.sdk.unboundidds.extensions.StartAdministrativeSessionExtendedRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) GZIPOutputStream(java.util.zip.GZIPOutputStream) Filter(com.unboundid.ldap.sdk.Filter) MatchedValuesFilter(com.unboundid.ldap.sdk.controls.MatchedValuesFilter) FileOutputStream(java.io.FileOutputStream) FixedRateBarrier(com.unboundid.util.FixedRateBarrier) ResultCode(com.unboundid.ldap.sdk.ResultCode) NotNull(com.unboundid.util.NotNull)

Example 2 with TeeOutputStream

use of com.unboundid.util.TeeOutputStream in project ldapsdk by pingidentity.

the class LDAPSearch method doSearch.

/**
 * Processes a search with the provided information.
 *
 * @param  pool            The connection pool to use to communicate with the
 *                         directory server.
 * @param  searchRequest   The search request to process.
 * @param  rateLimiter     An optional fixed-rate barrier that can be used for
 *                         request rate limiting.
 * @param  searchControls  The set of controls to include in search requests.
 *
 * @return  A result code indicating the result of the processing.
 */
@NotNull()
private ResultCode doSearch(@NotNull final LDAPConnectionPool pool, @NotNull final SearchRequest searchRequest, @Nullable final FixedRateBarrier rateLimiter, @NotNull final List<Control> searchControls) {
    if (separateOutputFilePerSearch.isPresent()) {
        try {
            final String path = outputFile.getValue().getAbsolutePath() + '.' + outputFileCounter.getAndIncrement();
            OutputStream s = new FileOutputStream(path);
            if (encryptOutput.isPresent()) {
                s = new PassphraseEncryptedOutputStream(encryptionPassphrase, s);
            }
            if (compressOutput.isPresent()) {
                s = new GZIPOutputStream(s);
            }
            if (teeResultsToStandardOut.isPresent()) {
                outStream = new PrintStream(new TeeOutputStream(s, getOut()));
            } else {
                outStream = new PrintStream(s);
            }
            resultWriter.updateOutputStream(outStream);
            errStream = outStream;
        } catch (final Exception e) {
            Debug.debugException(e);
            wrapErr(0, WRAP_COLUMN, ERR_LDAPSEARCH_CANNOT_OPEN_OUTPUT_FILE.get(outputFile.getValue().getAbsolutePath(), StaticUtils.getExceptionMessage(e)));
            return ResultCode.LOCAL_ERROR;
        }
        resultWriter.writeHeader();
    }
    try {
        if (rateLimiter != null) {
            rateLimiter.await();
        }
        ASN1OctetString pagedResultsCookie = null;
        boolean multiplePages = false;
        long totalEntries = 0;
        long totalReferences = 0;
        SearchResult searchResult;
        try {
            while (true) {
                searchRequest.setControls(searchControls);
                if (simplePageSize.isPresent()) {
                    searchRequest.addControl(new SimplePagedResultsControl(simplePageSize.getValue(), pagedResultsCookie));
                }
                if (dryRun.isPresent()) {
                    searchResult = new SearchResult(-1, ResultCode.SUCCESS, INFO_LDAPSEARCH_DRY_RUN_REQUEST_NOT_SENT.get(dryRun.getIdentifierString(), String.valueOf(searchRequest)), null, null, 0, 0, null);
                    break;
                } else {
                    if (!terse.isPresent()) {
                        if (verbose.isPresent() || persistentSearch.isPresent() || filterFile.isPresent() || ldapURLFile.isPresent() || (filter.isPresent() && (filter.getNumOccurrences() > 1))) {
                            commentToOut(INFO_LDAPSEARCH_SENDING_SEARCH_REQUEST.get(String.valueOf(searchRequest)));
                        }
                    }
                    searchResult = pool.search(searchRequest);
                }
                if (searchResult.getEntryCount() > 0) {
                    totalEntries += searchResult.getEntryCount();
                }
                if (searchResult.getReferenceCount() > 0) {
                    totalReferences += searchResult.getReferenceCount();
                }
                if (simplePageSize.isPresent()) {
                    final SimplePagedResultsControl pagedResultsControl;
                    try {
                        pagedResultsControl = SimplePagedResultsControl.get(searchResult);
                        if (pagedResultsControl == null) {
                            throw new LDAPSearchException(new SearchResult(searchResult.getMessageID(), ResultCode.CONTROL_NOT_FOUND, ERR_LDAPSEARCH_MISSING_PAGED_RESULTS_RESPONSE_CONTROL.get(), searchResult.getMatchedDN(), searchResult.getReferralURLs(), searchResult.getSearchEntries(), searchResult.getSearchReferences(), searchResult.getEntryCount(), searchResult.getReferenceCount(), searchResult.getResponseControls()));
                        }
                        if (pagedResultsControl.moreResultsToReturn()) {
                            if (verbose.isPresent()) {
                                commentToOut(INFO_LDAPSEARCH_INTERMEDIATE_PAGED_SEARCH_RESULT.get());
                                displayResult(searchResult);
                            }
                            multiplePages = true;
                            pagedResultsCookie = pagedResultsControl.getCookie();
                        } else {
                            break;
                        }
                    } catch (final LDAPException le) {
                        Debug.debugException(le);
                        throw new LDAPSearchException(new SearchResult(searchResult.getMessageID(), ResultCode.CONTROL_NOT_FOUND, ERR_LDAPSEARCH_CANNOT_DECODE_PAGED_RESULTS_RESPONSE_CONTROL.get(StaticUtils.getExceptionMessage(le)), searchResult.getMatchedDN(), searchResult.getReferralURLs(), searchResult.getSearchEntries(), searchResult.getSearchReferences(), searchResult.getEntryCount(), searchResult.getReferenceCount(), searchResult.getResponseControls()));
                    }
                } else {
                    break;
                }
            }
        } catch (final LDAPSearchException lse) {
            Debug.debugException(lse);
            searchResult = lse.toLDAPResult();
            if (searchResult.getEntryCount() > 0) {
                totalEntries += searchResult.getEntryCount();
            }
            if (searchResult.getReferenceCount() > 0) {
                totalReferences += searchResult.getReferenceCount();
            }
        }
        if ((searchResult.getResultCode() != ResultCode.SUCCESS) || (searchResult.getDiagnosticMessage() != null) || (!terse.isPresent())) {
            displayResult(searchResult);
        }
        if (multiplePages && (!terse.isPresent())) {
            commentToOut(INFO_LDAPSEARCH_TOTAL_SEARCH_ENTRIES.get(totalEntries));
            if (totalReferences > 0) {
                commentToOut(INFO_LDAPSEARCH_TOTAL_SEARCH_REFERENCES.get(totalReferences));
            }
        }
        if (countEntries.isPresent()) {
            return ResultCode.valueOf((int) Math.min(totalEntries, 255));
        } else if (requireMatch.isPresent() && (totalEntries == 0)) {
            return ResultCode.NO_RESULTS_RETURNED;
        } else {
            return searchResult.getResultCode();
        }
    } finally {
        if (separateOutputFilePerSearch.isPresent()) {
            try {
                outStream.close();
            } catch (final Exception e) {
                Debug.debugException(e);
            }
            outStream = null;
            errStream = null;
        }
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) PrintStream(java.io.PrintStream) TeeOutputStream(com.unboundid.util.TeeOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) TeeOutputStream(com.unboundid.util.TeeOutputStream) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) OutputStream(java.io.OutputStream) SearchResult(com.unboundid.ldap.sdk.SearchResult) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) PassphraseEncryptedOutputStream(com.unboundid.util.PassphraseEncryptedOutputStream) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) LDAPException(com.unboundid.ldap.sdk.LDAPException) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl) NotNull(com.unboundid.util.NotNull)

Aggregations

ASN1OctetString (com.unboundid.asn1.ASN1OctetString)2 LDAPException (com.unboundid.ldap.sdk.LDAPException)2 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)2 SimplePagedResultsControl (com.unboundid.ldap.sdk.controls.SimplePagedResultsControl)2 Control (com.unboundid.ldap.sdk.Control)1 Filter (com.unboundid.ldap.sdk.Filter)1 LDAPConnectionPool (com.unboundid.ldap.sdk.LDAPConnectionPool)1 ResultCode (com.unboundid.ldap.sdk.ResultCode)1 SearchResult (com.unboundid.ldap.sdk.SearchResult)1 AssertionRequestControl (com.unboundid.ldap.sdk.controls.AssertionRequestControl)1 AuthorizationIdentityRequestControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl)1 DraftLDUPSubentriesRequestControl (com.unboundid.ldap.sdk.controls.DraftLDUPSubentriesRequestControl)1 ManageDsaITRequestControl (com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl)1 MatchedValuesFilter (com.unboundid.ldap.sdk.controls.MatchedValuesFilter)1 MatchedValuesRequestControl (com.unboundid.ldap.sdk.controls.MatchedValuesRequestControl)1 PersistentSearchRequestControl (com.unboundid.ldap.sdk.controls.PersistentSearchRequestControl)1 ProxiedAuthorizationV1RequestControl (com.unboundid.ldap.sdk.controls.ProxiedAuthorizationV1RequestControl)1 ProxiedAuthorizationV2RequestControl (com.unboundid.ldap.sdk.controls.ProxiedAuthorizationV2RequestControl)1 RFC3672SubentriesRequestControl (com.unboundid.ldap.sdk.controls.RFC3672SubentriesRequestControl)1 ServerSideSortRequestControl (com.unboundid.ldap.sdk.controls.ServerSideSortRequestControl)1