Search in sources :

Example 1 with MatchedValuesRequestControl

use of com.unboundid.ldap.sdk.controls.MatchedValuesRequestControl in project ldapsdk by pingidentity.

the class LDAPSearch method getSearchControls.

/**
 * Retrieves a list of the controls that should be used when processing search
 * operations.
 *
 * @return  A list of the controls that should be used when processing search
 *          operations.
 *
 * @throws  LDAPException  If a problem is encountered while generating the
 *                         controls for a search request.
 */
@NotNull()
private List<Control> getSearchControls() {
    final ArrayList<Control> controls = new ArrayList<>(10);
    if (searchControl.isPresent()) {
        controls.addAll(searchControl.getValues());
    }
    if (joinRequestControl != null) {
        controls.add(joinRequestControl);
    }
    if (matchedValuesRequestControl != null) {
        controls.add(matchedValuesRequestControl);
    }
    if (matchingEntryCountRequestControl != null) {
        controls.add(matchingEntryCountRequestControl);
    }
    if (overrideSearchLimitsRequestControl != null) {
        controls.add(overrideSearchLimitsRequestControl);
    }
    if (persistentSearchRequestControl != null) {
        controls.add(persistentSearchRequestControl);
    }
    if (sortRequestControl != null) {
        controls.add(sortRequestControl);
    }
    if (vlvRequestControl != null) {
        controls.add(vlvRequestControl);
    }
    controls.addAll(routeToBackendSetRequestControls);
    if (accountUsable.isPresent()) {
        controls.add(new AccountUsableRequestControl(true));
    }
    if (getBackendSetID.isPresent()) {
        controls.add(new GetBackendSetIDRequestControl(false));
    }
    if (getServerID.isPresent()) {
        controls.add(new GetServerIDRequestControl(false));
    }
    if (includeReplicationConflictEntries.isPresent()) {
        controls.add(new ReturnConflictEntriesRequestControl(true));
    }
    if (includeSoftDeletedEntries.isPresent()) {
        final String valueStr = StaticUtils.toLowerCase(includeSoftDeletedEntries.getValue());
        if (valueStr.equals("with-non-deleted-entries")) {
            controls.add(new SoftDeletedEntryAccessRequestControl(true, true, false));
        } else if (valueStr.equals("without-non-deleted-entries")) {
            controls.add(new SoftDeletedEntryAccessRequestControl(true, false, false));
        } else {
            controls.add(new SoftDeletedEntryAccessRequestControl(true, false, true));
        }
    }
    if (draftLDUPSubentries.isPresent()) {
        controls.add(new DraftLDUPSubentriesRequestControl(true));
    }
    if (rfc3672Subentries.isPresent()) {
        controls.add(new RFC3672SubentriesRequestControl(rfc3672Subentries.getValue()));
    }
    if (manageDsaIT.isPresent()) {
        controls.add(new ManageDsaITRequestControl(true));
    }
    if (realAttributesOnly.isPresent()) {
        controls.add(new RealAttributesOnlyRequestControl(true));
    }
    if (routeToServer.isPresent()) {
        controls.add(new RouteToServerRequestControl(false, routeToServer.getValue(), false, false, false));
    }
    if (virtualAttributesOnly.isPresent()) {
        controls.add(new VirtualAttributesOnlyRequestControl(true));
    }
    if (excludeBranch.isPresent()) {
        final ArrayList<String> dns = new ArrayList<>(excludeBranch.getValues().size());
        for (final DN dn : excludeBranch.getValues()) {
            dns.add(dn.toString());
        }
        controls.add(new ExcludeBranchRequestControl(true, dns));
    }
    if (assertionFilter.isPresent()) {
        controls.add(new AssertionRequestControl(assertionFilter.getValue(), true));
    }
    if (getEffectiveRightsAuthzID.isPresent()) {
        final String[] attributes;
        if (getEffectiveRightsAttribute.isPresent()) {
            attributes = new String[getEffectiveRightsAttribute.getValues().size()];
            for (int i = 0; i < attributes.length; i++) {
                attributes[i] = getEffectiveRightsAttribute.getValues().get(i);
            }
        } else {
            attributes = StaticUtils.NO_STRINGS;
        }
        controls.add(new GetEffectiveRightsRequestControl(true, getEffectiveRightsAuthzID.getValue(), attributes));
    }
    if (operationPurpose.isPresent()) {
        controls.add(new OperationPurposeRequestControl(true, "ldapsearch", Version.NUMERIC_VERSION_STRING, "LDAPSearch.getSearchControls", operationPurpose.getValue()));
    }
    if (proxyAs.isPresent()) {
        controls.add(new ProxiedAuthorizationV2RequestControl(proxyAs.getValue()));
    }
    if (proxyV1As.isPresent()) {
        controls.add(new ProxiedAuthorizationV1RequestControl(proxyV1As.getValue()));
    }
    if (suppressOperationalAttributeUpdates.isPresent()) {
        final EnumSet<SuppressType> suppressTypes = EnumSet.noneOf(SuppressType.class);
        for (final String s : suppressOperationalAttributeUpdates.getValues()) {
            if (s.equalsIgnoreCase("last-access-time")) {
                suppressTypes.add(SuppressType.LAST_ACCESS_TIME);
            } else if (s.equalsIgnoreCase("last-login-time")) {
                suppressTypes.add(SuppressType.LAST_LOGIN_TIME);
            } else if (s.equalsIgnoreCase("last-login-ip")) {
                suppressTypes.add(SuppressType.LAST_LOGIN_IP);
            }
        }
        controls.add(new SuppressOperationalAttributeUpdateRequestControl(suppressTypes));
    }
    if (rejectUnindexedSearch.isPresent()) {
        controls.add(new RejectUnindexedSearchRequestControl());
    }
    if (permitUnindexedSearch.isPresent()) {
        controls.add(new PermitUnindexedSearchRequestControl());
    }
    return controls;
}
Also used : RouteToServerRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.RouteToServerRequestControl) ArrayList(java.util.ArrayList) ProxiedAuthorizationV2RequestControl(com.unboundid.ldap.sdk.controls.ProxiedAuthorizationV2RequestControl) JoinBaseDN(com.unboundid.ldap.sdk.unboundidds.controls.JoinBaseDN) DN(com.unboundid.ldap.sdk.DN) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) GetEffectiveRightsRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetEffectiveRightsRequestControl) 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) AssertionRequestControl(com.unboundid.ldap.sdk.controls.AssertionRequestControl) DraftLDUPSubentriesRequestControl(com.unboundid.ldap.sdk.controls.DraftLDUPSubentriesRequestControl) SoftDeletedEntryAccessRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.SoftDeletedEntryAccessRequestControl) GetServerIDRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetServerIDRequestControl) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) AccountUsableRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.AccountUsableRequestControl) PermitUnindexedSearchRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.PermitUnindexedSearchRequestControl) ReturnConflictEntriesRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.ReturnConflictEntriesRequestControl) RealAttributesOnlyRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.RealAttributesOnlyRequestControl) SuppressType(com.unboundid.ldap.sdk.unboundidds.controls.SuppressType) SuppressOperationalAttributeUpdateRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.SuppressOperationalAttributeUpdateRequestControl) OperationPurposeRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.OperationPurposeRequestControl) ProxiedAuthorizationV1RequestControl(com.unboundid.ldap.sdk.controls.ProxiedAuthorizationV1RequestControl) RFC3672SubentriesRequestControl(com.unboundid.ldap.sdk.controls.RFC3672SubentriesRequestControl) GetBackendSetIDRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.GetBackendSetIDRequestControl) ExcludeBranchRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.ExcludeBranchRequestControl) RejectUnindexedSearchRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.RejectUnindexedSearchRequestControl) VirtualAttributesOnlyRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.VirtualAttributesOnlyRequestControl) NotNull(com.unboundid.util.NotNull)

Example 2 with MatchedValuesRequestControl

use of com.unboundid.ldap.sdk.controls.MatchedValuesRequestControl in project ldapsdk by pingidentity.

the class LDAPSearch method doExtendedNonLDAPArgumentValidation.

/**
 * {@inheritDoc}
 */
@Override()
public void doExtendedNonLDAPArgumentValidation() throws ArgumentException {
    // was provided, then use that.
    if (wrapColumn.isPresent()) {
        final int wc = wrapColumn.getValue();
        if (wc <= 0) {
            WRAP_COLUMN = Integer.MAX_VALUE;
        } else {
            WRAP_COLUMN = wc;
        }
    } else if (dontWrap.isPresent()) {
        WRAP_COLUMN = Integer.MAX_VALUE;
    }
    // If the ldapURLFile argument was provided, then there must not be any
    // trailing arguments.
    final List<String> trailingArgs = parser.getTrailingArguments();
    if (ldapURLFile.isPresent()) {
        if (!trailingArgs.isEmpty()) {
            throw new ArgumentException(ERR_LDAPSEARCH_TRAILING_ARGS_WITH_URL_FILE.get(ldapURLFile.getIdentifierString()));
        }
    }
    // not be a filter.
    if (filter.isPresent() || filterFile.isPresent()) {
        if (!trailingArgs.isEmpty()) {
            try {
                Filter.create(trailingArgs.get(0));
                throw new ArgumentException(ERR_LDAPSEARCH_TRAILING_FILTER_WITH_FILTER_FILE.get(filterFile.getIdentifierString()));
            } catch (final LDAPException le) {
            // This is the normal condition.  Not even worth debugging the
            // exception.
            }
        }
    }
    // argument must be a valid search filter.
    if (!(ldapURLFile.isPresent() || filter.isPresent() || filterFile.isPresent())) {
        if (trailingArgs.isEmpty()) {
            throw new ArgumentException(ERR_LDAPSEARCH_NO_TRAILING_ARGS.get(filterFile.getIdentifierString(), ldapURLFile.getIdentifierString()));
        }
        try {
            Filter.create(trailingArgs.get(0));
        } catch (final Exception e) {
            Debug.debugException(e);
            throw new ArgumentException(ERR_LDAPSEARCH_FIRST_TRAILING_ARG_NOT_FILTER.get(trailingArgs.get(0)), e);
        }
    }
    // fail.
    for (final String s : trailingArgs) {
        if (s.startsWith("-")) {
            commentToErr(WARN_LDAPSEARCH_TRAILING_ARG_STARTS_WITH_DASH.get(s));
            break;
        }
    }
    // pre-create the matched values request control.
    if (matchedValuesFilter.isPresent()) {
        final List<Filter> filterList = matchedValuesFilter.getValues();
        final MatchedValuesFilter[] matchedValuesFilters = new MatchedValuesFilter[filterList.size()];
        for (int i = 0; i < matchedValuesFilters.length; i++) {
            try {
                matchedValuesFilters[i] = MatchedValuesFilter.create(filterList.get(i));
            } catch (final Exception e) {
                Debug.debugException(e);
                throw new ArgumentException(ERR_LDAPSEARCH_INVALID_MATCHED_VALUES_FILTER.get(filterList.get(i).toString()), e);
            }
        }
        matchedValuesRequestControl = new MatchedValuesRequestControl(true, matchedValuesFilters);
    }
    // the argument value and pre-create the control.
    if (matchingEntryCountControl.isPresent()) {
        final MatchingEntryCountRequestControlProperties properties = new MatchingEntryCountRequestControlProperties();
        Integer examineCount = null;
        try {
            for (final String element : matchingEntryCountControl.getValue().toLowerCase().split(":")) {
                if (element.startsWith("examinecount=")) {
                    examineCount = Integer.parseInt(element.substring(13));
                } else if (element.equals("allowunindexed")) {
                    properties.setProcessSearchIfUnindexed(true);
                } else if (element.equals("alwaysexamine")) {
                    properties.setAlwaysExamineCandidates(true);
                } else if (element.equals("skipresolvingexplodedindexes")) {
                    properties.setSkipResolvingExplodedIndexes(true);
                } else if (element.startsWith("fastshortcircuitthreshold=")) {
                    properties.setFastShortCircuitThreshold(Long.parseLong(element.substring(26)));
                } else if (element.startsWith("slowshortcircuitthreshold=")) {
                    properties.setSlowShortCircuitThreshold(Long.parseLong(element.substring(26)));
                } else if (element.equals("extendedresponsedata")) {
                    properties.setIncludeExtendedResponseData(true);
                } else if (element.equals("debug")) {
                    properties.setIncludeDebugInfo(true);
                } else {
                    throw new ArgumentException(ERR_LDAPSEARCH_MATCHING_ENTRY_COUNT_INVALID_VALUE.get(matchingEntryCountControl.getIdentifierString()));
                }
            }
        } catch (final ArgumentException ae) {
            Debug.debugException(ae);
            throw ae;
        } catch (final Exception e) {
            Debug.debugException(e);
            throw new ArgumentException(ERR_LDAPSEARCH_MATCHING_ENTRY_COUNT_INVALID_VALUE.get(matchingEntryCountControl.getIdentifierString()), e);
        }
        if (examineCount == null) {
            throw new ArgumentException(ERR_LDAPSEARCH_MATCHING_ENTRY_COUNT_INVALID_VALUE.get(matchingEntryCountControl.getIdentifierString()));
        } else {
            properties.setMaxCandidatesToExamine(examineCount);
        }
        matchingEntryCountRequestControl = new MatchingEntryCountRequestControl(true, properties);
    }
    // validate the provided values.
    if (overrideSearchLimit.isPresent()) {
        final LinkedHashMap<String, String> properties = new LinkedHashMap<>(StaticUtils.computeMapCapacity(10));
        for (final String value : overrideSearchLimit.getValues()) {
            final int equalPos = value.indexOf('=');
            if (equalPos < 0) {
                throw new ArgumentException(ERR_LDAPSEARCH_OVERRIDE_LIMIT_NO_EQUAL.get(overrideSearchLimit.getIdentifierString()));
            } else if (equalPos == 0) {
                throw new ArgumentException(ERR_LDAPSEARCH_OVERRIDE_LIMIT_EMPTY_PROPERTY_NAME.get(overrideSearchLimit.getIdentifierString()));
            }
            final String propertyName = value.substring(0, equalPos);
            if (properties.containsKey(propertyName)) {
                throw new ArgumentException(ERR_LDAPSEARCH_OVERRIDE_LIMIT_DUPLICATE_PROPERTY_NAME.get(overrideSearchLimit.getIdentifierString(), propertyName));
            }
            if (equalPos == (value.length() - 1)) {
                throw new ArgumentException(ERR_LDAPSEARCH_OVERRIDE_LIMIT_EMPTY_PROPERTY_VALUE.get(overrideSearchLimit.getIdentifierString(), propertyName));
            }
            properties.put(propertyName, value.substring(equalPos + 1));
        }
        overrideSearchLimitsRequestControl = new OverrideSearchLimitsRequestControl(properties, false);
    }
    // the argument value and pre-create the control.
    if (persistentSearch.isPresent()) {
        boolean changesOnly = true;
        boolean returnECs = true;
        EnumSet<PersistentSearchChangeType> changeTypes = EnumSet.allOf(PersistentSearchChangeType.class);
        try {
            final String[] elements = persistentSearch.getValue().toLowerCase().split(":");
            if (elements.length == 0) {
                throw new ArgumentException(ERR_LDAPSEARCH_PERSISTENT_SEARCH_INVALID_VALUE.get(persistentSearch.getIdentifierString()));
            }
            final String header = StaticUtils.toLowerCase(elements[0]);
            if (!(header.equals("ps") || header.equals("persist") || header.equals("persistent") || header.equals("psearch") || header.equals("persistentsearch"))) {
                throw new ArgumentException(ERR_LDAPSEARCH_PERSISTENT_SEARCH_INVALID_VALUE.get(persistentSearch.getIdentifierString()));
            }
            if (elements.length > 1) {
                final String ctString = StaticUtils.toLowerCase(elements[1]);
                if (ctString.equals("any")) {
                    changeTypes = EnumSet.allOf(PersistentSearchChangeType.class);
                } else {
                    changeTypes.clear();
                    for (final String t : ctString.split(",")) {
                        if (t.equals("add")) {
                            changeTypes.add(PersistentSearchChangeType.ADD);
                        } else if (t.equals("del") || t.equals("delete")) {
                            changeTypes.add(PersistentSearchChangeType.DELETE);
                        } else if (t.equals("mod") || t.equals("modify")) {
                            changeTypes.add(PersistentSearchChangeType.MODIFY);
                        } else if (t.equals("moddn") || t.equals("modrdn") || t.equals("modifydn") || t.equals("modifyrdn")) {
                            changeTypes.add(PersistentSearchChangeType.MODIFY_DN);
                        } else {
                            throw new ArgumentException(ERR_LDAPSEARCH_PERSISTENT_SEARCH_INVALID_VALUE.get(persistentSearch.getIdentifierString()));
                        }
                    }
                }
            }
            if (elements.length > 2) {
                if (elements[2].equalsIgnoreCase("true") || elements[2].equals("1")) {
                    changesOnly = true;
                } else if (elements[2].equalsIgnoreCase("false") || elements[2].equals("0")) {
                    changesOnly = false;
                } else {
                    throw new ArgumentException(ERR_LDAPSEARCH_PERSISTENT_SEARCH_INVALID_VALUE.get(persistentSearch.getIdentifierString()));
                }
            }
            if (elements.length > 3) {
                if (elements[3].equalsIgnoreCase("true") || elements[3].equals("1")) {
                    returnECs = true;
                } else if (elements[3].equalsIgnoreCase("false") || elements[3].equals("0")) {
                    returnECs = false;
                } else {
                    throw new ArgumentException(ERR_LDAPSEARCH_PERSISTENT_SEARCH_INVALID_VALUE.get(persistentSearch.getIdentifierString()));
                }
            }
        } catch (final ArgumentException ae) {
            Debug.debugException(ae);
            throw ae;
        } catch (final Exception e) {
            Debug.debugException(e);
            throw new ArgumentException(ERR_LDAPSEARCH_PERSISTENT_SEARCH_INVALID_VALUE.get(persistentSearch.getIdentifierString()), e);
        }
        persistentSearchRequestControl = new PersistentSearchRequestControl(changeTypes, changesOnly, returnECs, true);
    }
    // sort order and pre-create the control.
    if (sortOrder.isPresent()) {
        final ArrayList<SortKey> sortKeyList = new ArrayList<>(5);
        final StringTokenizer tokenizer = new StringTokenizer(sortOrder.getValue(), ", ");
        while (tokenizer.hasMoreTokens()) {
            final String token = tokenizer.nextToken();
            final boolean ascending;
            String attributeName;
            if (token.startsWith("-")) {
                ascending = false;
                attributeName = token.substring(1);
            } else if (token.startsWith("+")) {
                ascending = true;
                attributeName = token.substring(1);
            } else {
                ascending = true;
                attributeName = token;
            }
            final String matchingRuleID;
            final int colonPos = attributeName.indexOf(':');
            if (colonPos >= 0) {
                matchingRuleID = attributeName.substring(colonPos + 1);
                attributeName = attributeName.substring(0, colonPos);
            } else {
                matchingRuleID = null;
            }
            final StringBuilder invalidReason = new StringBuilder();
            if (!PersistUtils.isValidLDAPName(attributeName, false, invalidReason)) {
                throw new ArgumentException(ERR_LDAPSEARCH_SORT_ORDER_INVALID_VALUE.get(sortOrder.getIdentifierString()));
            }
            sortKeyList.add(new SortKey(attributeName, matchingRuleID, (!ascending)));
        }
        if (sortKeyList.isEmpty()) {
            throw new ArgumentException(ERR_LDAPSEARCH_SORT_ORDER_INVALID_VALUE.get(sortOrder.getIdentifierString()));
        }
        final SortKey[] sortKeyArray = new SortKey[sortKeyList.size()];
        sortKeyList.toArray(sortKeyArray);
        sortRequestControl = new ServerSideSortRequestControl(sortKeyArray);
    }
    // argument value and pre-create the control.
    if (virtualListView.isPresent()) {
        try {
            final String[] elements = virtualListView.getValue().split(":");
            if (elements.length == 4) {
                vlvRequestControl = new VirtualListViewRequestControl(Integer.parseInt(elements[2]), Integer.parseInt(elements[0]), Integer.parseInt(elements[1]), Integer.parseInt(elements[3]), null);
            } else if (elements.length == 3) {
                vlvRequestControl = new VirtualListViewRequestControl(elements[2], Integer.parseInt(elements[0]), Integer.parseInt(elements[1]), null);
            } else {
                throw new ArgumentException(ERR_LDAPSEARCH_VLV_INVALID_VALUE.get(virtualListView.getIdentifierString()));
            }
        } catch (final ArgumentException ae) {
            Debug.debugException(ae);
            throw ae;
        } catch (final Exception e) {
            Debug.debugException(e);
            throw new ArgumentException(ERR_LDAPSEARCH_VLV_INVALID_VALUE.get(virtualListView.getIdentifierString()), e);
        }
    }
    // pre-create that control.
    if (joinRule.isPresent()) {
        final JoinRule rule;
        try {
            final String[] elements = joinRule.getValue().toLowerCase().split(":");
            final String ruleName = StaticUtils.toLowerCase(elements[0]);
            if (ruleName.equals("dn")) {
                rule = JoinRule.createDNJoin(elements[1]);
            } else if (ruleName.equals("reverse-dn") || ruleName.equals("reversedn")) {
                rule = JoinRule.createReverseDNJoin(elements[1]);
            } else if (ruleName.equals("equals") || ruleName.equals("equality")) {
                rule = JoinRule.createEqualityJoin(elements[1], elements[2], false);
            } else if (ruleName.equals("contains") || ruleName.equals("substring")) {
                rule = JoinRule.createContainsJoin(elements[1], elements[2], false);
            } else {
                throw new ArgumentException(ERR_LDAPSEARCH_JOIN_RULE_INVALID_VALUE.get(joinRule.getIdentifierString()));
            }
        } catch (final ArgumentException ae) {
            Debug.debugException(ae);
            throw ae;
        } catch (final Exception e) {
            Debug.debugException(e);
            throw new ArgumentException(ERR_LDAPSEARCH_JOIN_RULE_INVALID_VALUE.get(joinRule.getIdentifierString()), e);
        }
        final JoinBaseDN joinBase;
        if (joinBaseDN.isPresent()) {
            final String s = StaticUtils.toLowerCase(joinBaseDN.getValue());
            if (s.equals("search-base") || s.equals("search-base-dn")) {
                joinBase = JoinBaseDN.createUseSearchBaseDN();
            } else if (s.equals("source-entry-dn") || s.equals("source-dn")) {
                joinBase = JoinBaseDN.createUseSourceEntryDN();
            } else {
                try {
                    final DN dn = new DN(joinBaseDN.getValue());
                    joinBase = JoinBaseDN.createUseCustomBaseDN(joinBaseDN.getValue());
                } catch (final Exception e) {
                    Debug.debugException(e);
                    throw new ArgumentException(ERR_LDAPSEARCH_JOIN_BASE_DN_INVALID_VALUE.get(joinBaseDN.getIdentifierString()), e);
                }
            }
        } else {
            joinBase = JoinBaseDN.createUseSearchBaseDN();
        }
        final String[] joinAttrs;
        if (joinRequestedAttribute.isPresent()) {
            final List<String> valueList = joinRequestedAttribute.getValues();
            joinAttrs = new String[valueList.size()];
            valueList.toArray(joinAttrs);
        } else {
            joinAttrs = null;
        }
        joinRequestControl = new JoinRequestControl(new JoinRequestValue(rule, joinBase, joinScope.getValue(), DereferencePolicy.NEVER, joinSizeLimit.getValue(), joinFilter.getValue(), joinAttrs, joinRequireMatch.isPresent(), null));
    }
    // and pre-create those controls.
    if (routeToBackendSet.isPresent()) {
        final List<String> values = routeToBackendSet.getValues();
        final Map<String, List<String>> idsByRP = new LinkedHashMap<>(StaticUtils.computeMapCapacity(values.size()));
        for (final String value : values) {
            final int colonPos = value.indexOf(':');
            if (colonPos <= 0) {
                throw new ArgumentException(ERR_LDAPSEARCH_ROUTE_TO_BACKEND_SET_INVALID_FORMAT.get(value, routeToBackendSet.getIdentifierString()));
            }
            final String rpID = value.substring(0, colonPos);
            final String bsID = value.substring(colonPos + 1);
            List<String> idsForRP = idsByRP.get(rpID);
            if (idsForRP == null) {
                idsForRP = new ArrayList<>(values.size());
                idsByRP.put(rpID, idsForRP);
            }
            idsForRP.add(bsID);
        }
        for (final Map.Entry<String, List<String>> e : idsByRP.entrySet()) {
            final String rpID = e.getKey();
            final List<String> bsIDs = e.getValue();
            routeToBackendSetRequestControls.add(RouteToBackendSetRequestControl.createAbsoluteRoutingRequest(true, rpID, bsIDs));
        }
    }
    // Parse the dereference policy.
    final String derefStr = StaticUtils.toLowerCase(dereferencePolicy.getValue());
    if (derefStr.equals("always")) {
        derefPolicy = DereferencePolicy.ALWAYS;
    } else if (derefStr.equals("search")) {
        derefPolicy = DereferencePolicy.SEARCHING;
    } else if (derefStr.equals("find")) {
        derefPolicy = DereferencePolicy.FINDING;
    } else {
        derefPolicy = DereferencePolicy.NEVER;
    }
    // See if any entry transformations need to be applied.
    final ArrayList<EntryTransformation> transformations = new ArrayList<>(5);
    if (excludeAttribute.isPresent()) {
        transformations.add(new ExcludeAttributeTransformation(null, excludeAttribute.getValues()));
    }
    if (redactAttribute.isPresent()) {
        transformations.add(new RedactAttributeTransformation(null, true, (!hideRedactedValueCount.isPresent()), redactAttribute.getValues()));
    }
    if (scrambleAttribute.isPresent()) {
        final Long randomSeed;
        if (scrambleRandomSeed.isPresent()) {
            randomSeed = scrambleRandomSeed.getValue().longValue();
        } else {
            randomSeed = null;
        }
        transformations.add(new ScrambleAttributeTransformation(null, randomSeed, true, scrambleAttribute.getValues(), scrambleJSONField.getValues()));
    }
    if (renameAttributeFrom.isPresent()) {
        if (renameAttributeFrom.getNumOccurrences() != renameAttributeTo.getNumOccurrences()) {
            throw new ArgumentException(ERR_LDAPSEARCH_RENAME_ATTRIBUTE_MISMATCH.get());
        }
        final Iterator<String> sourceIterator = renameAttributeFrom.getValues().iterator();
        final Iterator<String> targetIterator = renameAttributeTo.getValues().iterator();
        while (sourceIterator.hasNext()) {
            transformations.add(new RenameAttributeTransformation(null, sourceIterator.next(), targetIterator.next(), true));
        }
    }
    if (moveSubtreeFrom.isPresent()) {
        if (moveSubtreeFrom.getNumOccurrences() != moveSubtreeTo.getNumOccurrences()) {
            throw new ArgumentException(ERR_LDAPSEARCH_MOVE_SUBTREE_MISMATCH.get());
        }
        final Iterator<DN> sourceIterator = moveSubtreeFrom.getValues().iterator();
        final Iterator<DN> targetIterator = moveSubtreeTo.getValues().iterator();
        while (sourceIterator.hasNext()) {
            transformations.add(new MoveSubtreeTransformation(sourceIterator.next(), targetIterator.next()));
        }
    }
    if (!transformations.isEmpty()) {
        entryTransformations = transformations;
    }
    // Create the result writer.
    final String outputFormatStr = StaticUtils.toLowerCase(outputFormat.getValue());
    if (outputFormatStr.equals("json")) {
        resultWriter = new JSONLDAPResultWriter(getOutStream());
    } else if (outputFormatStr.equals("csv") || outputFormatStr.equals("multi-valued-csv") || outputFormatStr.equals("tab-delimited") || outputFormatStr.equals("multi-valued-tab-delimited")) {
        // These output formats cannot be used with the --ldapURLFile argument.
        if (ldapURLFile.isPresent()) {
            throw new ArgumentException(ERR_LDAPSEARCH_OUTPUT_FORMAT_NOT_SUPPORTED_WITH_URLS.get(outputFormat.getValue(), ldapURLFile.getIdentifierString()));
        }
        // These output formats require the requested attributes to be specified
        // via the --requestedAttribute argument rather than as unnamed trailing
        // arguments.
        final List<String> requestedAttributes = requestedAttribute.getValues();
        if ((requestedAttributes == null) || requestedAttributes.isEmpty()) {
            throw new ArgumentException(ERR_LDAPSEARCH_OUTPUT_FORMAT_REQUIRES_REQUESTED_ATTR_ARG.get(outputFormat.getValue(), requestedAttribute.getIdentifierString()));
        }
        switch(trailingArgs.size()) {
            case 0:
                // This is fine.
                break;
            case 1:
                // filter nor filterFile argument was provided.
                if (filter.isPresent() || filterFile.isPresent()) {
                    throw new ArgumentException(ERR_LDAPSEARCH_OUTPUT_FORMAT_REQUIRES_REQUESTED_ATTR_ARG.get(outputFormat.getValue(), requestedAttribute.getIdentifierString()));
                }
                break;
            default:
                throw new ArgumentException(ERR_LDAPSEARCH_OUTPUT_FORMAT_REQUIRES_REQUESTED_ATTR_ARG.get(outputFormat.getValue(), requestedAttribute.getIdentifierString()));
        }
        final OutputFormat format;
        final boolean includeAllValues;
        switch(outputFormatStr) {
            case "multi-valued-csv":
                format = OutputFormat.CSV;
                includeAllValues = true;
                break;
            case "tab-delimited":
                format = OutputFormat.TAB_DELIMITED_TEXT;
                includeAllValues = false;
                break;
            case "multi-valued-tab-delimited":
                format = OutputFormat.TAB_DELIMITED_TEXT;
                includeAllValues = true;
                break;
            case "csv":
            default:
                format = OutputFormat.CSV;
                includeAllValues = false;
                break;
        }
        resultWriter = new ColumnBasedLDAPResultWriter(getOutStream(), format, requestedAttributes, WRAP_COLUMN, includeAllValues);
    } else if (outputFormatStr.equals("dns-only")) {
        resultWriter = new DNsOnlyLDAPResultWriter(getOutStream());
    } else if (outputFormatStr.equals("values-only")) {
        resultWriter = new ValuesOnlyLDAPResultWriter(getOutStream());
    } else {
        resultWriter = new LDIFLDAPResultWriter(getOutStream(), WRAP_COLUMN);
    }
}
Also used : ArrayList(java.util.ArrayList) JoinBaseDN(com.unboundid.ldap.sdk.unboundidds.controls.JoinBaseDN) DN(com.unboundid.ldap.sdk.DN) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) PersistentSearchRequestControl(com.unboundid.ldap.sdk.controls.PersistentSearchRequestControl) LinkedHashMap(java.util.LinkedHashMap) JoinRequestValue(com.unboundid.ldap.sdk.unboundidds.controls.JoinRequestValue) EntryTransformation(com.unboundid.ldap.sdk.transformations.EntryTransformation) ArrayList(java.util.ArrayList) List(java.util.List) ArgumentException(com.unboundid.util.args.ArgumentException) MatchingEntryCountRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.MatchingEntryCountRequestControl) RenameAttributeTransformation(com.unboundid.ldap.sdk.transformations.RenameAttributeTransformation) JoinRule(com.unboundid.ldap.sdk.unboundidds.controls.JoinRule) ServerSideSortRequestControl(com.unboundid.ldap.sdk.controls.ServerSideSortRequestControl) MoveSubtreeTransformation(com.unboundid.ldap.sdk.transformations.MoveSubtreeTransformation) ExcludeAttributeTransformation(com.unboundid.ldap.sdk.transformations.ExcludeAttributeTransformation) LDAPException(com.unboundid.ldap.sdk.LDAPException) AtomicLong(java.util.concurrent.atomic.AtomicLong) RedactAttributeTransformation(com.unboundid.ldap.sdk.transformations.RedactAttributeTransformation) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) MatchedValuesRequestControl(com.unboundid.ldap.sdk.controls.MatchedValuesRequestControl) SortKey(com.unboundid.ldap.sdk.controls.SortKey) JoinBaseDN(com.unboundid.ldap.sdk.unboundidds.controls.JoinBaseDN) MatchedValuesFilter(com.unboundid.ldap.sdk.controls.MatchedValuesFilter) OverrideSearchLimitsRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.OverrideSearchLimitsRequestControl) ScrambleAttributeTransformation(com.unboundid.ldap.sdk.transformations.ScrambleAttributeTransformation) OutputFormat(com.unboundid.util.OutputFormat) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) JoinRequestControl(com.unboundid.ldap.sdk.unboundidds.controls.JoinRequestControl) StringTokenizer(java.util.StringTokenizer) Filter(com.unboundid.ldap.sdk.Filter) MatchedValuesFilter(com.unboundid.ldap.sdk.controls.MatchedValuesFilter) VirtualListViewRequestControl(com.unboundid.ldap.sdk.controls.VirtualListViewRequestControl) MatchingEntryCountRequestControlProperties(com.unboundid.ldap.sdk.unboundidds.controls.MatchingEntryCountRequestControlProperties) PersistentSearchChangeType(com.unboundid.ldap.sdk.controls.PersistentSearchChangeType)

Aggregations

ASN1OctetString (com.unboundid.asn1.ASN1OctetString)2 DN (com.unboundid.ldap.sdk.DN)2 MatchedValuesRequestControl (com.unboundid.ldap.sdk.controls.MatchedValuesRequestControl)2 PersistentSearchRequestControl (com.unboundid.ldap.sdk.controls.PersistentSearchRequestControl)2 ServerSideSortRequestControl (com.unboundid.ldap.sdk.controls.ServerSideSortRequestControl)2 VirtualListViewRequestControl (com.unboundid.ldap.sdk.controls.VirtualListViewRequestControl)2 JoinBaseDN (com.unboundid.ldap.sdk.unboundidds.controls.JoinBaseDN)2 JoinRequestControl (com.unboundid.ldap.sdk.unboundidds.controls.JoinRequestControl)2 MatchingEntryCountRequestControl (com.unboundid.ldap.sdk.unboundidds.controls.MatchingEntryCountRequestControl)2 OverrideSearchLimitsRequestControl (com.unboundid.ldap.sdk.unboundidds.controls.OverrideSearchLimitsRequestControl)2 Control (com.unboundid.ldap.sdk.Control)1 Filter (com.unboundid.ldap.sdk.Filter)1 LDAPException (com.unboundid.ldap.sdk.LDAPException)1 LDAPSearchException (com.unboundid.ldap.sdk.LDAPSearchException)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 PersistentSearchChangeType (com.unboundid.ldap.sdk.controls.PersistentSearchChangeType)1