Search in sources :

Example 31 with ASN1OctetString

use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.

the class PasswordPolicyStateExtendedResult method encodeValue.

/**
 * Encodes the provided information into a suitable value for this control.
 *
 * @param  userDN             The user DN from the response.
 * @param  operations         The set of operations from the response, mapped
 *                            from operation type to the corresponding
 *                            operation data.
 *
 * @return  An ASN.1 octet string containing the appropriately-encoded value
 *          for this control, or {@code null} if there should not be a value.
 */
@Nullable()
private static ASN1OctetString encodeValue(@Nullable final String userDN, @Nullable final PasswordPolicyStateOperation[] operations) {
    if ((userDN == null) && ((operations == null) || (operations.length == 0))) {
        return null;
    }
    final ArrayList<ASN1Element> elements = new ArrayList<>(2);
    elements.add(new ASN1OctetString(userDN));
    if ((operations != null) && (operations.length > 0)) {
        final ASN1Element[] opElements = new ASN1Element[operations.length];
        for (int i = 0; i < operations.length; i++) {
            opElements[i] = operations[i].encode();
        }
        elements.add(new ASN1Sequence(opElements));
    }
    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) Nullable(com.unboundid.util.Nullable)

Example 32 with ASN1OctetString

use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.

the class MultiUpdateExtendedRequest method encodeValue.

/**
 * Generates an ASN.1 octet string suitable for use as the value of a
 * multi-update extended request.
 *
 * @param  errorBehavior  The behavior to exhibit if errors are encountered.
 *                        It must not be {@code null}.
 * @param  requests       The  set of requests to be processed.  It must not
 *                        be {@code null} or empty.  Only add, delete, modify,
 *                        modify DN, and certain extended requests (as
 *                        determined by the server) should be included.  Each
 *                        request may include zero or more controls that
 *                        should apply only to that request.
 *
 * @return  An ASN.1 octet string suitable for use as the value of a
 *          multi-update extended request.
 */
@NotNull()
private static ASN1OctetString encodeValue(@NotNull final MultiUpdateErrorBehavior errorBehavior, @NotNull final List<LDAPRequest> requests) {
    final ArrayList<ASN1Element> requestElements = new ArrayList<>(requests.size());
    for (final LDAPRequest r : requests) {
        final ArrayList<ASN1Element> rsElements = new ArrayList<>(2);
        switch(r.getOperationType()) {
            case ADD:
                rsElements.add(((AddRequest) r).encodeProtocolOp());
                break;
            case DELETE:
                rsElements.add(((DeleteRequest) r).encodeProtocolOp());
                break;
            case MODIFY:
                rsElements.add(((ModifyRequest) r).encodeProtocolOp());
                break;
            case MODIFY_DN:
                rsElements.add(((ModifyDNRequest) r).encodeProtocolOp());
                break;
            case EXTENDED:
                rsElements.add(((ExtendedRequest) r).encodeProtocolOp());
                break;
        }
        if (r.hasControl()) {
            rsElements.add(Control.encodeControls(r.getControls()));
        }
        requestElements.add(new ASN1Sequence(rsElements));
    }
    final ASN1Sequence valueSequence = new ASN1Sequence(new ASN1Enumerated(errorBehavior.intValue()), new ASN1Sequence(requestElements));
    return new ASN1OctetString(valueSequence.encode());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) LDAPRequest(com.unboundid.ldap.sdk.LDAPRequest) ASN1Enumerated(com.unboundid.asn1.ASN1Enumerated) ASN1Element(com.unboundid.asn1.ASN1Element) ArrayList(java.util.ArrayList) NotNull(com.unboundid.util.NotNull)

Example 33 with ASN1OctetString

use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.

the class SubtreeDeleter method doPagedResultsSearch.

/**
 * Uses the simple paged results control to iterate through all entries in
 * the server that match the criteria from the provided search request.
 *
 * @param  connection
 *              The {@link LDAPInterface} instance to use to communicate with
 *              the directory server.  While this may be an individual
 *              {@link LDAPConnection}, it may be better as a connection
 *              pool with automatic retry enabled so that it's more likely to
 *              succeed in the event that a connection becomes invalid or an
 *              operation experiences a transient failure.  It must not be
 *              {@code null}.
 * @param  searchRequest
 *              The search request to be processed using the simple paged
 *              results control.  The request must not already include the
 *              simple paged results request control, but must otherwise be
 *              the request that should be processed, including any other
 *              controls that are desired.  It must not be {@code null}.
 * @param  pageSize
 *              The maximum number of entries that should be included in any
 *              page of results.  It must be greater than or equal to one.
 *
 * @throws  LDAPSearchException  If a problem is encountered during search
 *                               processing that prevents it from successfully
 *                               identifying all of the entries.
 */
private static void doPagedResultsSearch(@NotNull final LDAPInterface connection, @NotNull final SearchRequest searchRequest, final int pageSize) throws LDAPSearchException {
    final SubtreeDeleterSearchResultListener searchListener = (SubtreeDeleterSearchResultListener) searchRequest.getSearchResultListener();
    ASN1OctetString pagedResultsCookie = null;
    while (true) {
        final SearchRequest pagedResultsSearchRequest = searchRequest.duplicate();
        pagedResultsSearchRequest.addControl(new SimplePagedResultsControl(pageSize, pagedResultsCookie, true));
        SearchResult searchResult;
        try {
            searchResult = connection.search(pagedResultsSearchRequest);
        } catch (final LDAPSearchException e) {
            Debug.debugException(e);
            searchResult = e.getSearchResult();
        }
        if (searchResult.getResultCode() == ResultCode.NO_SUCH_OBJECT) {
            // It just means that there aren't any entries to delete.
            return;
        } else if (searchResult.getResultCode() != ResultCode.SUCCESS) {
            throw new LDAPSearchException(searchResult);
        } else if (searchListener.getFirstException() != null) {
            throw new LDAPSearchException(searchListener.getFirstException());
        }
        final SimplePagedResultsControl responseControl;
        try {
            responseControl = SimplePagedResultsControl.get(searchResult);
        } catch (final LDAPException e) {
            Debug.debugException(e);
            throw new LDAPSearchException(e);
        }
        if (responseControl == null) {
            throw new LDAPSearchException(ResultCode.CONTROL_NOT_FOUND, ERR_SUBTREE_DELETER_MISSING_PAGED_RESULTS_RESPONSE.get(searchRequest.getBaseDN(), searchRequest.getFilter()));
        }
        if (responseControl.moreResultsToReturn()) {
            pagedResultsCookie = responseControl.getCookie();
        } else {
            return;
        }
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) SearchRequest(com.unboundid.ldap.sdk.SearchRequest) LDAPException(com.unboundid.ldap.sdk.LDAPException) LDAPSearchException(com.unboundid.ldap.sdk.LDAPSearchException) SearchResult(com.unboundid.ldap.sdk.SearchResult) SimplePagedResultsControl(com.unboundid.ldap.sdk.controls.SimplePagedResultsControl)

Example 34 with ASN1OctetString

use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.

the class ControlArgument method addValue.

/**
 * {@inheritDoc}
 */
@Override()
protected void addValue(@NotNull final String valueString) throws ArgumentException {
    String oid = null;
    boolean isCritical = false;
    ASN1OctetString value = null;
    final int firstColonPos = valueString.indexOf(':');
    if (firstColonPos < 0) {
        oid = valueString;
    } else {
        oid = valueString.substring(0, firstColonPos);
        final String criticalityStr;
        final int secondColonPos = valueString.indexOf(':', (firstColonPos + 1));
        if (secondColonPos < 0) {
            criticalityStr = valueString.substring(firstColonPos + 1);
        } else {
            criticalityStr = valueString.substring(firstColonPos + 1, secondColonPos);
            final int doubleColonPos = valueString.indexOf("::");
            if (doubleColonPos == secondColonPos) {
                try {
                    value = new ASN1OctetString(Base64.decode(valueString.substring(doubleColonPos + 2)));
                } catch (final Exception e) {
                    Debug.debugException(e);
                    throw new ArgumentException(ERR_CONTROL_ARG_INVALID_BASE64_VALUE.get(valueString, getIdentifierString(), valueString.substring(doubleColonPos + 2)), e);
                }
            } else {
                value = new ASN1OctetString(valueString.substring(secondColonPos + 1));
            }
        }
        final String lowerCriticalityStr = StaticUtils.toLowerCase(criticalityStr);
        if (lowerCriticalityStr.equals("true") || lowerCriticalityStr.equals("t") || lowerCriticalityStr.equals("yes") || lowerCriticalityStr.equals("y") || lowerCriticalityStr.equals("on") || lowerCriticalityStr.equals("1")) {
            isCritical = true;
        } else if (lowerCriticalityStr.equals("false") || lowerCriticalityStr.equals("f") || lowerCriticalityStr.equals("no") || lowerCriticalityStr.equals("n") || lowerCriticalityStr.equals("off") || lowerCriticalityStr.equals("0")) {
            isCritical = false;
        } else {
            throw new ArgumentException(ERR_CONTROL_ARG_INVALID_CRITICALITY.get(valueString, getIdentifierString(), criticalityStr));
        }
    }
    if (!StaticUtils.isNumericOID(oid)) {
        final String providedOID = oid;
        oid = OIDS_BY_NAME.get(StaticUtils.toLowerCase(providedOID));
        if (oid == null) {
            throw new ArgumentException(ERR_CONTROL_ARG_INVALID_OID.get(valueString, getIdentifierString(), providedOID));
        }
    }
    if (values.size() >= getMaxOccurrences()) {
        throw new ArgumentException(ERR_ARG_MAX_OCCURRENCES_EXCEEDED.get(getIdentifierString()));
    }
    for (final ArgumentValueValidator v : validators) {
        v.validateArgumentValue(this, valueString);
    }
    values.add(new Control(oid, isCritical, value));
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) AuthorizationIdentityRequestControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl) DontUseCopyRequestControl(com.unboundid.ldap.sdk.controls.DontUseCopyRequestControl) Control(com.unboundid.ldap.sdk.Control) SubtreeDeleteRequestControl(com.unboundid.ldap.sdk.controls.SubtreeDeleteRequestControl) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) DraftZeilengaLDAPNoOp12RequestControl(com.unboundid.ldap.sdk.experimental.DraftZeilengaLDAPNoOp12RequestControl) DraftBeheraLDAPPasswordPolicy10RequestControl(com.unboundid.ldap.sdk.experimental.DraftBeheraLDAPPasswordPolicy10RequestControl) DraftLDUPSubentriesRequestControl(com.unboundid.ldap.sdk.controls.DraftLDUPSubentriesRequestControl) PermissiveModifyRequestControl(com.unboundid.ldap.sdk.controls.PermissiveModifyRequestControl) ASN1OctetString(com.unboundid.asn1.ASN1OctetString)

Example 35 with ASN1OctetString

use of com.mindbright.asn1.ASN1OctetString in project ldapsdk by pingidentity.

the class LDAPMessageTestCase method testSearchResultEntryMessage.

/**
 * Tests the behavior of the {@code LDAPMessage} class with a search result
 * entry protocol op.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSearchResultEntryMessage() throws Exception {
    LinkedList<Control> controls = new LinkedList<Control>();
    controls.add(new Control("1.2.3.4"));
    controls.add(new Control("1.2.3.5", true, new ASN1OctetString()));
    LinkedList<Attribute> attrs = new LinkedList<Attribute>();
    attrs.add(new Attribute("objectClass", "top", "domain"));
    attrs.add(new Attribute("dc", "example"));
    SearchResultEntryProtocolOp op = new SearchResultEntryProtocolOp("dc=example,dc=com", attrs);
    LDAPMessage m = new LDAPMessage(1, op, controls);
    ASN1Buffer b = new ASN1Buffer();
    m.writeTo(b);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(b.toByteArray());
    ASN1StreamReader reader = new ASN1StreamReader(inputStream);
    m = LDAPMessage.readFrom(reader, true);
    m = LDAPMessage.decode(m.encode());
    assertEquals(m.getMessageID(), 1);
    assertNotNull(m.getProtocolOp());
    assertTrue(m.getProtocolOp() instanceof SearchResultEntryProtocolOp);
    assertEquals(m.getProtocolOpType(), LDAPMessage.PROTOCOL_OP_TYPE_SEARCH_RESULT_ENTRY);
    assertNotNull(m.getControls());
    assertFalse(m.getControls().isEmpty());
    assertNotNull(m.getSearchResultEntryProtocolOp());
    inputStream = new ByteArrayInputStream(b.toByteArray());
    reader = new ASN1StreamReader(inputStream);
    LDAPResponse r = LDAPMessage.readLDAPResponseFrom(reader, true);
    assertNotNull(m.toString());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Control(com.unboundid.ldap.sdk.Control) Attribute(com.unboundid.ldap.sdk.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) ASN1Buffer(com.unboundid.asn1.ASN1Buffer) ASN1StreamReader(com.unboundid.asn1.ASN1StreamReader) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Aggregations

ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1173 Test (org.testng.annotations.Test)852 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)382 Control (com.unboundid.ldap.sdk.Control)310 ASN1Element (com.unboundid.asn1.ASN1Element)237 ArrayList (java.util.ArrayList)204 NotNull (com.unboundid.util.NotNull)191 LDAPException (com.unboundid.ldap.sdk.LDAPException)142 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)133 ExtendedResult (com.unboundid.ldap.sdk.ExtendedResult)99 ASN1Enumerated (com.unboundid.asn1.ASN1Enumerated)92 IOException (java.io.IOException)88 ASN1Integer (com.unboundid.asn1.ASN1Integer)80 ExtendedRequest (com.unboundid.ldap.sdk.ExtendedRequest)69 DN (com.unboundid.ldap.sdk.DN)65 LDAPConnection (com.unboundid.ldap.sdk.LDAPConnection)64 ByteArrayInputStream (java.io.ByteArrayInputStream)56 AuthorizationIdentityRequestControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl)53 ASN1Boolean (com.unboundid.asn1.ASN1Boolean)52 AuthorizationIdentityResponseControl (com.unboundid.ldap.sdk.controls.AuthorizationIdentityResponseControl)49