Search in sources :

Example 1 with ASN1Boolean

use of com.github.zhenwei.core.asn1.ASN1Boolean in project ldapsdk by pingidentity.

the class Filter method encode.

/**
 * Encodes this search filter to an ASN.1 element suitable for inclusion in an
 * LDAP search request protocol op.
 *
 * @return  An ASN.1 element containing the encoded search filter.
 */
@NotNull()
public ASN1Element encode() {
    switch(filterType) {
        case FILTER_TYPE_AND:
        case FILTER_TYPE_OR:
            final ASN1Element[] filterElements = new ASN1Element[filterComps.length];
            for (int i = 0; i < filterComps.length; i++) {
                filterElements[i] = filterComps[i].encode();
            }
            return new ASN1Set(filterType, filterElements);
        case FILTER_TYPE_NOT:
            return new ASN1Element(filterType, notComp.encode().encode());
        case FILTER_TYPE_EQUALITY:
        case FILTER_TYPE_GREATER_OR_EQUAL:
        case FILTER_TYPE_LESS_OR_EQUAL:
        case FILTER_TYPE_APPROXIMATE_MATCH:
            final ASN1OctetString[] attrValueAssertionElements = { new ASN1OctetString(attrName), assertionValue };
            return new ASN1Sequence(filterType, attrValueAssertionElements);
        case FILTER_TYPE_SUBSTRING:
            final ArrayList<ASN1OctetString> subList = new ArrayList<>(2 + subAny.length);
            if (subInitial != null) {
                subList.add(new ASN1OctetString(SUBSTRING_TYPE_SUBINITIAL, subInitial.getValue()));
            }
            for (final ASN1Element subAnyElement : subAny) {
                subList.add(new ASN1OctetString(SUBSTRING_TYPE_SUBANY, subAnyElement.getValue()));
            }
            if (subFinal != null) {
                subList.add(new ASN1OctetString(SUBSTRING_TYPE_SUBFINAL, subFinal.getValue()));
            }
            final ASN1Element[] subFilterElements = { new ASN1OctetString(attrName), new ASN1Sequence(subList) };
            return new ASN1Sequence(filterType, subFilterElements);
        case FILTER_TYPE_PRESENCE:
            return new ASN1OctetString(filterType, attrName);
        case FILTER_TYPE_EXTENSIBLE_MATCH:
            final ArrayList<ASN1Element> emElementList = new ArrayList<>(4);
            if (matchingRuleID != null) {
                emElementList.add(new ASN1OctetString(EXTENSIBLE_TYPE_MATCHING_RULE_ID, matchingRuleID));
            }
            if (attrName != null) {
                emElementList.add(new ASN1OctetString(EXTENSIBLE_TYPE_ATTRIBUTE_NAME, attrName));
            }
            emElementList.add(new ASN1OctetString(EXTENSIBLE_TYPE_MATCH_VALUE, assertionValue.getValue()));
            if (dnAttributes) {
                emElementList.add(new ASN1Boolean(EXTENSIBLE_TYPE_DN_ATTRIBUTES, true));
            }
            return new ASN1Sequence(filterType, emElementList);
        default:
            throw new AssertionError(ERR_FILTER_INVALID_TYPE.get(StaticUtils.toHex(filterType)));
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Set(com.unboundid.asn1.ASN1Set) ASN1Element(com.unboundid.asn1.ASN1Element) ArrayList(java.util.ArrayList) ASN1Boolean(com.unboundid.asn1.ASN1Boolean) NotNull(com.unboundid.util.NotNull)

Example 2 with ASN1Boolean

use of com.github.zhenwei.core.asn1.ASN1Boolean in project ldapsdk by pingidentity.

the class GetSupportedOTPDeliveryMechanismsExtendedResult method encodeValue.

/**
 * Encodes the provided information into an appropriate format for the value
 * of this extended operation.
 *
 * @param  resultCode             The result code from the response.  It must
 *                                not be {@code null}.
 * @param  deliveryMechanismInfo  The set of supported delivery mechanism info
 *                                for the result, if appropriate.  It should
 *                                be {@code null} or empty for non-success
 *                                results.
 *
 * @return  The ASN.1 octet string containing the encoded value.
 */
@Nullable()
private static ASN1OctetString encodeValue(@NotNull final ResultCode resultCode, @Nullable final Collection<SupportedOTPDeliveryMechanismInfo> deliveryMechanismInfo) {
    if (resultCode != ResultCode.SUCCESS) {
        return null;
    }
    if ((deliveryMechanismInfo == null) || deliveryMechanismInfo.isEmpty()) {
        return new ASN1OctetString(new ASN1Sequence().encode());
    }
    final ArrayList<ASN1Element> elements = new ArrayList<>(deliveryMechanismInfo.size());
    for (final SupportedOTPDeliveryMechanismInfo i : deliveryMechanismInfo) {
        final ArrayList<ASN1Element> infoElements = new ArrayList<>(3);
        infoElements.add(new ASN1OctetString(TYPE_DELIVERY_MECHANISM, i.getDeliveryMechanism()));
        if (i.isSupported() != null) {
            infoElements.add(new ASN1Boolean(TYPE_IS_SUPPORTED, i.isSupported()));
        }
        if (i.getRecipientID() != null) {
            infoElements.add(new ASN1OctetString(TYPE_RECIPIENT_ID, i.getRecipientID()));
        }
        elements.add(new ASN1Sequence(infoElements));
    }
    return new ASN1OctetString(new ASN1Sequence(elements).encode());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) ArrayList(java.util.ArrayList) ASN1Boolean(com.unboundid.asn1.ASN1Boolean) Nullable(com.unboundid.util.Nullable)

Example 3 with ASN1Boolean

use of com.github.zhenwei.core.asn1.ASN1Boolean in project ldapsdk by pingidentity.

the class GetPasswordQualityRequirementsExtendedResult method encodeValue.

/**
 * Encodes the provided information into an ASN.1 octet string suitable for
 * use as the value for this extended result, if appropriate.
 *
 * @param  resultCode               The result code for the response.  This
 *                                  must not be {@code null}.
 * @param  passwordRequirements     The password quality requirements for this
 *                                  result.  This must be {@code null} or
 *                                  empty if this result is for an operation
 *                                  that was not processed successfully.  It
 *                                  may be {@code null} or empty if the
 *                                  server will not enforce any password
 *                                  quality requirements for the target
 *                                  operation.
 * @param  currentPasswordRequired  Indicates whether the user will be
 *                                  required to provide his/her current
 *                                  password when performing a self change.
 *                                  This must be {@code null} if this result
 *                                  is for an operation that was not processed
 *                                  successfully or if the target operation is
 *                                  not a self change.
 * @param  mustChangePassword       Indicates whether the user will be
 *                                  required to change their password after
 *                                  the associated add or administrative
 *                                  reset before that user will be allowed to
 *                                  issue any other requests.  This must be
 *                                  {@code null} if this result is for an
 *                                  operation that was not processed
 *                                  successfully or if the target operation is
 *                                  not an add or an administrative reset.
 * @param  secondsUntilExpiration   Indicates the maximum length of time, in
 *                                  seconds, that the password set in the
 *                                  target operation will be valid.  If
 *                                  {@code mustChangePassword} is {@code true}
 *                                  then this will indicate the length of time
 *                                  that the user has to change his/her
 *                                  password after the add/reset.  If
 *                                  {@code mustChangePassword} is {@code null}
 *                                  or {@code false} then this will indicate
 *                                  the length of time until the password
 *                                  expires.  This must be {@code null} if
 *                                  this result is for an operation that was
 *                                  not processed successfully, or if the new
 *                                  password will be valid indefinitely.
 *
 * @return  The ASN.1 element with the encoded result value, or {@code null}
 *          if the result should not have a value.
 */
@Nullable()
private static ASN1OctetString encodeValue(@NotNull final ResultCode resultCode, @Nullable final Collection<PasswordQualityRequirement> passwordRequirements, @Nullable final Boolean currentPasswordRequired, @Nullable final Boolean mustChangePassword, @Nullable final Integer secondsUntilExpiration) {
    if (resultCode != ResultCode.SUCCESS) {
        Validator.ensureTrue((passwordRequirements == null) || passwordRequirements.isEmpty());
        Validator.ensureTrue(currentPasswordRequired == null);
        Validator.ensureTrue(mustChangePassword == null);
        Validator.ensureTrue(secondsUntilExpiration == null);
        return null;
    }
    final ArrayList<ASN1Element> valueSequence = new ArrayList<>(4);
    if (passwordRequirements == null) {
        valueSequence.add(new ASN1Sequence());
    } else {
        final ArrayList<ASN1Element> requirementElements = new ArrayList<>(passwordRequirements.size());
        for (final PasswordQualityRequirement r : passwordRequirements) {
            requirementElements.add(r.encode());
        }
        valueSequence.add(new ASN1Sequence(requirementElements));
    }
    if (currentPasswordRequired != null) {
        valueSequence.add(new ASN1Boolean(TYPE_CURRENT_PW_REQUIRED, currentPasswordRequired));
    }
    if (mustChangePassword != null) {
        valueSequence.add(new ASN1Boolean(TYPE_MUST_CHANGE_PW, mustChangePassword));
    }
    if (secondsUntilExpiration != null) {
        valueSequence.add(new ASN1Integer(TYPE_SECONDS_UNTIL_EXPIRATION, secondsUntilExpiration));
    }
    return new ASN1OctetString(new ASN1Sequence(valueSequence).encode());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) ArrayList(java.util.ArrayList) ASN1Boolean(com.unboundid.asn1.ASN1Boolean) ASN1Integer(com.unboundid.asn1.ASN1Integer) Nullable(com.unboundid.util.Nullable)

Example 4 with ASN1Boolean

use of com.github.zhenwei.core.asn1.ASN1Boolean in project ldapsdk by pingidentity.

the class ReplaceInterServerCertificateExtendedRequest method encodeValue.

/**
 * Encodes the provided information into an ASN.1 octet string suitable for
 * use as the encoded value for a replace inter-server certificate extended
 * request.
 *
 * @param  keyStoreContent
 *              An object with information about how the server should obtain
 *              the new inter-server certificate data.  It must not be
 *              {@code null}.
 * @param  skipCertificateValidation
 *              Indicates whether to skip validation for the new certificate
 *              chain.
 *
 * @return  An ASN.1 octet string containing the encoded request value.
 */
@NotNull()
private static ASN1OctetString encodeValue(@NotNull final ReplaceCertificateKeyStoreContent keyStoreContent, final boolean skipCertificateValidation) {
    Validator.ensureNotNullWithMessage(keyStoreContent, "ReplaceInterServerCertificateExtendedRequest.keyStoreContent must " + "not be null.");
    final List<ASN1Element> elements = new ArrayList<>();
    elements.add(keyStoreContent.encode());
    if (skipCertificateValidation) {
        elements.add(new ASN1Boolean(TYPE_SKIP_CERT_VALIDATION, true));
    }
    return new ASN1OctetString(new ASN1Sequence(elements).encode());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) ArrayList(java.util.ArrayList) ASN1Boolean(com.unboundid.asn1.ASN1Boolean) NotNull(com.unboundid.util.NotNull)

Example 5 with ASN1Boolean

use of com.github.zhenwei.core.asn1.ASN1Boolean in project ldapsdk by pingidentity.

the class GetChangelogBatchExtendedResultTestCase method testDecodeMissingResumeToken.

/**
 * Tests the behavior when attempting to decode an instance of the extended
 * result with a value sequence that is missing a resume token.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testDecodeMissingResumeToken() throws Exception {
    final ASN1Sequence valueSequence = new ASN1Sequence(new ASN1Boolean((byte) 0x81, true));
    final GetChangelogBatchExtendedResult r = new GetChangelogBatchExtendedResult(new ExtendedResult(1, ResultCode.SUCCESS, null, null, null, null, new ASN1OctetString(valueSequence.encode()), null), -1);
    assertTrue(r.hasValue());
    assertNotNull(r.getValue());
    assertNull(r.getResumeToken());
    assertTrue(r.moreChangesAvailable());
    assertFalse(r.changesAlreadyPurged());
    assertNull(r.getAdditionalInfo());
    assertEquals(r.getEntryCount(), -1);
    assertNull(r.getChangelogEntries());
    assertNotNull(r.getExtendedResultName());
    assertNotNull(r.toString());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ExtendedResult(com.unboundid.ldap.sdk.ExtendedResult) ASN1Boolean(com.unboundid.asn1.ASN1Boolean) Test(org.testng.annotations.Test)

Aggregations

ASN1Boolean (com.unboundid.asn1.ASN1Boolean)51 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)51 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)51 ASN1Element (com.unboundid.asn1.ASN1Element)38 NotNull (com.unboundid.util.NotNull)32 ArrayList (java.util.ArrayList)32 ASN1Integer (com.unboundid.asn1.ASN1Integer)15 Test (org.testng.annotations.Test)14 ASN1Enumerated (com.unboundid.asn1.ASN1Enumerated)12 ASN1Boolean (com.github.zhenwei.core.asn1.ASN1Boolean)5 Nullable (com.unboundid.util.Nullable)5 IOException (java.io.IOException)5 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)4 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)4 ASN1TaggedObject (com.github.zhenwei.core.asn1.ASN1TaggedObject)4 ASN1Set (com.unboundid.asn1.ASN1Set)4 DERTaggedObject (com.github.zhenwei.core.asn1.DERTaggedObject)3 ASN1Long (com.unboundid.asn1.ASN1Long)3 Control (com.unboundid.ldap.sdk.Control)3 ASN1Boolean (org.bouncycastle.asn1.ASN1Boolean)3