Search in sources :

Example 11 with ArgumentException

use of com.unboundid.util.args.ArgumentException 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)

Example 12 with ArgumentException

use of com.unboundid.util.args.ArgumentException in project ldapsdk by pingidentity.

the class LDAPDiff method setArgumentValueFromArgument.

/**
 * Updates the specified non-legacy argument with the value from the given
 * legacy argument, if it is present.
 *
 * @param  legacyArgument         The legacy argument to use to set the value
 *                                of the specified non-legacy argument.  It
 *                                must not be {@code null}.
 * @param  nonLegacyArgumentName  The name of the non-legacy argument to
 *                                update with the value of the legacy
 *                                argument.  It must not be {@code null} and
 *                                must reference a defined argument that takes
 *                                a value.
 *
 * @throws  ArgumentException  If a problem occurs while attempting to set the
 *                             value of the specified non-legacy argument from
 *                             the given legacy argument.
 */
private void setArgumentValueFromArgument(@NotNull final Argument legacyArgument, @NotNull final String nonLegacyArgumentName) throws ArgumentException {
    if (legacyArgument.isPresent()) {
        try {
            final Argument nonLegacyArgument = parser.getNamedArgument(nonLegacyArgumentName);
            final Method addValueMethod = Argument.class.getDeclaredMethod("addValue", String.class);
            addValueMethod.setAccessible(true);
            final Method incrementOccurrencesMethod = Argument.class.getDeclaredMethod("incrementOccurrences");
            incrementOccurrencesMethod.setAccessible(true);
            for (final String valueString : legacyArgument.getValueStringRepresentations(false)) {
                addValueMethod.invoke(nonLegacyArgument, valueString);
                incrementOccurrencesMethod.invoke(nonLegacyArgument);
            }
        } catch (final Exception e) {
            Debug.debugException(e);
            final String message = ERR_LDAP_DIFF_CANNOT_SET_ARG_FROM_LEGACY.get(legacyArgument.getIdentifierString(), nonLegacyArgumentName, StaticUtils.getExceptionMessage(e));
            toolCompletionMessageRef.compareAndSet(null, message);
            throw new ArgumentException(message, e);
        }
    }
}
Also used : IntegerArgument(com.unboundid.util.args.IntegerArgument) ScopeArgument(com.unboundid.util.args.ScopeArgument) Argument(com.unboundid.util.args.Argument) DNArgument(com.unboundid.util.args.DNArgument) BooleanArgument(com.unboundid.util.args.BooleanArgument) FileArgument(com.unboundid.util.args.FileArgument) StringArgument(com.unboundid.util.args.StringArgument) FilterArgument(com.unboundid.util.args.FilterArgument) Method(java.lang.reflect.Method) ArgumentException(com.unboundid.util.args.ArgumentException) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException)

Example 13 with ArgumentException

use of com.unboundid.util.args.ArgumentException in project ldapsdk by pingidentity.

the class LDAPDiff method doExtendedNonLDAPArgumentValidation.

/**
 * {@inheritDoc}
 */
@Override()
public void doExtendedNonLDAPArgumentValidation() throws ArgumentException {
    // Make sure that the provided base DN was not empty.
    final DN baseDN = baseDNArg.getValue();
    if ((baseDN == null) || baseDN.isNullDN()) {
        final String message = ERR_LDAP_DIFF_EMPTY_BASE_DN.get();
        toolCompletionMessageRef.compareAndSet(null, message);
        throw new ArgumentException(message);
    }
    // If any of the legacy arguments were provided, then use that argument to
    // set the value for the corresponding non-legacy argument(s).
    setArgumentValueFromArgument(legacySourceHostArg, "sourceHostname");
    setArgumentValueFromArgument(legacySourcePortArg, "sourcePort");
    setArgumentValueFromArgument(legacySourceBindDNArg, "sourceBindDN");
    setArgumentValueFromArgument(legacySourceBindPasswordArg, "sourceBindPassword");
    setArgumentValueFromArgument(legacyTargetHostArg, "targetHostname");
    setArgumentValueFromArgument(legacyTargetBindPasswordFileArg, "targetBindPasswordFile");
    setArgumentValueFromArgument(legacyKeyStorePathArg, "sourceKeyStorePath");
    setArgumentValueFromArgument(legacyKeyStorePathArg, "targetKeyStorePath");
    setArgumentValueFromArgument(legacyKeyStorePasswordArg, "sourceKeyStorePassword");
    setArgumentValueFromArgument(legacyKeyStorePasswordArg, "targetKeyStorePassword");
    setArgumentValueFromArgument(legacyKeyStorePasswordFileArg, "sourceKeyStorePasswordFile");
    setArgumentValueFromArgument(legacyKeyStorePasswordFileArg, "targetKeyStorePasswordFile");
    setArgumentValueFromArgument(legacyKeyStoreFormatArg, "sourceKeyStoreFormat");
    setArgumentValueFromArgument(legacyKeyStoreFormatArg, "targetKeyStoreFormat");
    setArgumentValueFromArgument(legacyCertNicknameArg, "sourceCertNickname");
    setArgumentValueFromArgument(legacyCertNicknameArg, "targetCertNickname");
    setArgumentValueFromArgument(legacyTrustStorePathArg, "sourceTrustStorePath");
    setArgumentValueFromArgument(legacyTrustStorePathArg, "targetTrustStorePath");
    setArgumentValueFromArgument(legacyTrustStorePasswordArg, "sourceTrustStorePassword");
    setArgumentValueFromArgument(legacyTrustStorePasswordArg, "targetTrustStorePassword");
    setArgumentValueFromArgument(legacyTrustStorePasswordFileArg, "sourceTrustStorePasswordFile");
    setArgumentValueFromArgument(legacyTrustStorePasswordFileArg, "targetTrustStorePasswordFile");
    setArgumentValueFromArgument(legacyTrustStoreFormatArg, "sourceTrustStoreFormat");
    setArgumentValueFromArgument(legacyTrustStoreFormatArg, "targetTrustStoreFormat");
    if (legacyTrustAllArg.isPresent()) {
        setArgumentPresent("sourceTrustAll");
        setArgumentPresent("targetTrustAll");
    }
    // If no source bind DN was specified, then use a default of
    // "cn=Directory Manager".
    final DNArgument sourceBindDNArg = parser.getDNArgument("sourceBindDN");
    if (!sourceBindDNArg.isPresent()) {
        try {
            final Method addValueMethod = Argument.class.getDeclaredMethod("addValue", String.class);
            addValueMethod.setAccessible(true);
            addValueMethod.invoke(sourceBindDNArg, DEFAULT_BIND_DN);
            final Method incrementOccurrencesMethod = Argument.class.getDeclaredMethod("incrementOccurrences");
            incrementOccurrencesMethod.setAccessible(true);
            incrementOccurrencesMethod.invoke(sourceBindDNArg);
        } catch (final Exception e) {
            Debug.debugException(e);
            throw new ArgumentException(ERR_LDAP_DIFF_CANNOT_SET_DEFAULT_BIND_DN.get(DEFAULT_BIND_DN, sourceBindDNArg.getIdentifierString(), StaticUtils.getExceptionMessage(e)), e);
        }
    }
    // If a source bind DN and password were provided but a target bind DN and
    // password were not, then use the source values for the target server.
    final DNArgument targetBindDNArg = parser.getDNArgument("targetBindDN");
    if (!targetBindDNArg.isPresent()) {
        setArgumentValueFromArgument(sourceBindDNArg, "targetBindDN");
    }
    final StringArgument sourceBindPasswordArg = parser.getStringArgument("sourceBindPassword");
    final StringArgument targetBindPasswordArg = parser.getStringArgument("targetBindPassword");
    final FileArgument targetBindPasswordFileArg = parser.getFileArgument("targetBindPasswordFile");
    if (sourceBindPasswordArg.isPresent() && (!(targetBindPasswordArg.isPresent() || targetBindPasswordFileArg.isPresent()))) {
        setArgumentValueFromArgument(sourceBindPasswordArg, "targetBindPassword");
    }
    final FileArgument sourceBindPasswordFileArg = parser.getFileArgument("sourceBindPasswordFile");
    if (sourceBindPasswordFileArg.isPresent() && (!(targetBindPasswordArg.isPresent() || targetBindPasswordFileArg.isPresent()))) {
        setArgumentValueFromArgument(sourceBindPasswordFileArg, "targetBindPasswordFile");
    }
}
Also used : DNArgument(com.unboundid.util.args.DNArgument) DN(com.unboundid.ldap.sdk.DN) ArgumentException(com.unboundid.util.args.ArgumentException) Method(java.lang.reflect.Method) FileArgument(com.unboundid.util.args.FileArgument) ArgumentException(com.unboundid.util.args.ArgumentException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException) StringArgument(com.unboundid.util.args.StringArgument)

Example 14 with ArgumentException

use of com.unboundid.util.args.ArgumentException in project ldapsdk by pingidentity.

the class LDAPModify method doExtendedNonLDAPArgumentValidation.

/**
 * {@inheritDoc}
 */
@Override()
public void doExtendedNonLDAPArgumentValidation() throws ArgumentException {
    // 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_LDAPMODIFY_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));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ArgumentException(com.unboundid.util.args.ArgumentException) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SortedMap(java.util.SortedMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ArgumentException (com.unboundid.util.args.ArgumentException)14 LDAPException (com.unboundid.ldap.sdk.LDAPException)10 ArrayList (java.util.ArrayList)6 DN (com.unboundid.ldap.sdk.DN)5 BooleanArgument (com.unboundid.util.args.BooleanArgument)5 FileArgument (com.unboundid.util.args.FileArgument)5 File (java.io.File)5 IOException (java.io.IOException)5 List (java.util.List)5 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)4 Filter (com.unboundid.ldap.sdk.Filter)4 Argument (com.unboundid.util.args.Argument)4 DNArgument (com.unboundid.util.args.DNArgument)4 StringArgument (com.unboundid.util.args.StringArgument)4 Method (java.lang.reflect.Method)3 LinkedHashMap (java.util.LinkedHashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 Map (java.util.Map)3 LDAPURL (com.unboundid.ldap.sdk.LDAPURL)2 SearchScope (com.unboundid.ldap.sdk.SearchScope)2