Search in sources :

Example 16 with ASN1OctetString

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

the class Entry method toLDIF.

/**
 * Retrieves an LDIF representation of this entry, with each attribute value
 * on a separate line.  Long lines will be wrapped at the specified column.
 *
 * @param  wrapColumn  The column at which long lines should be wrapped.  A
 *                     value less than or equal to two indicates that no
 *                     wrapping should be performed.
 *
 * @return  An LDIF representation of this entry.
 */
@Override()
@NotNull()
public final String[] toLDIF(final int wrapColumn) {
    List<String> ldifLines = new ArrayList<>(2 * attributes.size());
    encodeNameAndValue("dn", new ASN1OctetString(dn), ldifLines);
    for (final Attribute a : attributes.values()) {
        final String name = a.getName();
        if (a.hasValue()) {
            for (final ASN1OctetString value : a.getRawValues()) {
                encodeNameAndValue(name, value, ldifLines);
            }
        } else {
            encodeNameAndValue(name, EMPTY_OCTET_STRING, ldifLines);
        }
    }
    if (wrapColumn > 2) {
        ldifLines = LDIFWriter.wrapLines(wrapColumn, ldifLines);
    }
    final String[] lineArray = new String[ldifLines.size()];
    ldifLines.toArray(lineArray);
    return lineArray;
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ArrayList(java.util.ArrayList) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) NotNull(com.unboundid.util.NotNull)

Example 17 with ASN1OctetString

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

the class Entry method toLDIF.

/**
 * Appends an LDIF representation of this entry to the provided buffer.
 *
 * @param  buffer      The buffer to which the LDIF representation of this
 *                     entry should be written.
 * @param  wrapColumn  The column at which long lines should be wrapped.  A
 *                     value less than or equal to two indicates that no
 *                     wrapping should be performed.
 */
@Override()
public final void toLDIF(@NotNull final ByteStringBuffer buffer, final int wrapColumn) {
    LDIFWriter.encodeNameAndValue("dn", new ASN1OctetString(dn), buffer, wrapColumn);
    buffer.append(StaticUtils.EOL_BYTES);
    for (final Attribute a : attributes.values()) {
        final String name = a.getName();
        if (a.hasValue()) {
            for (final ASN1OctetString value : a.getRawValues()) {
                LDIFWriter.encodeNameAndValue(name, value, buffer, wrapColumn);
                buffer.append(StaticUtils.EOL_BYTES);
            }
        } else {
            LDIFWriter.encodeNameAndValue(name, EMPTY_OCTET_STRING, buffer, wrapColumn);
            buffer.append(StaticUtils.EOL_BYTES);
        }
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1OctetString(com.unboundid.asn1.ASN1OctetString)

Example 18 with ASN1OctetString

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

the class Control method readFrom.

/**
 * Reads an LDAP control from the provided ASN.1 stream reader.
 *
 * @param  reader  The ASN.1 stream reader from which to read the control.
 *
 * @return  The decoded control.
 *
 * @throws  LDAPException  If a problem occurs while attempting to read or
 *                         parse the control.
 */
@NotNull()
public static Control readFrom(@NotNull final ASN1StreamReader reader) throws LDAPException {
    try {
        final ASN1StreamReaderSequence controlSequence = reader.beginSequence();
        final String oid = reader.readString();
        boolean isCritical = false;
        ASN1OctetString value = null;
        while (controlSequence.hasMoreElements()) {
            final byte type = (byte) reader.peek();
            switch(type) {
                case ASN1Constants.UNIVERSAL_BOOLEAN_TYPE:
                    isCritical = reader.readBoolean();
                    break;
                case ASN1Constants.UNIVERSAL_OCTET_STRING_TYPE:
                    value = new ASN1OctetString(reader.readBytes());
                    break;
                default:
                    throw new LDAPException(ResultCode.DECODING_ERROR, ERR_CONTROL_INVALID_TYPE.get(StaticUtils.toHex(type)));
            }
        }
        return decode(oid, isCritical, value);
    } catch (final LDAPException le) {
        Debug.debugException(le);
        throw le;
    } catch (final Exception e) {
        Debug.debugException(e);
        throw new LDAPException(ResultCode.DECODING_ERROR, ERR_CONTROL_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1StreamReaderSequence(com.unboundid.asn1.ASN1StreamReaderSequence) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Exception(com.unboundid.asn1.ASN1Exception) NotNull(com.unboundid.util.NotNull)

Example 19 with ASN1OctetString

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

the class Control method decode.

/**
 * Decodes the provided ASN.1 sequence as an LDAP control.
 *
 * @param  controlSequence  The ASN.1 sequence to be decoded.
 *
 * @return  The decoded control.
 *
 * @throws  LDAPException  If a problem occurs while attempting to decode the
 *                         provided ASN.1 sequence as an LDAP control.
 */
@NotNull()
public static Control decode(@NotNull final ASN1Sequence controlSequence) throws LDAPException {
    final ASN1Element[] elements = controlSequence.elements();
    if ((elements.length < 1) || (elements.length > 3)) {
        throw new LDAPException(ResultCode.DECODING_ERROR, ERR_CONTROL_DECODE_INVALID_ELEMENT_COUNT.get(elements.length));
    }
    final String oid = ASN1OctetString.decodeAsOctetString(elements[0]).stringValue();
    boolean isCritical = false;
    ASN1OctetString value = null;
    if (elements.length == 2) {
        switch(elements[1].getType()) {
            case ASN1Constants.UNIVERSAL_BOOLEAN_TYPE:
                try {
                    isCritical = ASN1Boolean.decodeAsBoolean(elements[1]).booleanValue();
                } catch (final ASN1Exception ae) {
                    Debug.debugException(ae);
                    throw new LDAPException(ResultCode.DECODING_ERROR, ERR_CONTROL_DECODE_CRITICALITY.get(StaticUtils.getExceptionMessage(ae)), ae);
                }
                break;
            case ASN1Constants.UNIVERSAL_OCTET_STRING_TYPE:
                value = ASN1OctetString.decodeAsOctetString(elements[1]);
                break;
            default:
                throw new LDAPException(ResultCode.DECODING_ERROR, ERR_CONTROL_INVALID_TYPE.get(StaticUtils.toHex(elements[1].getType())));
        }
    } else if (elements.length == 3) {
        try {
            isCritical = ASN1Boolean.decodeAsBoolean(elements[1]).booleanValue();
        } catch (final ASN1Exception ae) {
            Debug.debugException(ae);
            throw new LDAPException(ResultCode.DECODING_ERROR, ERR_CONTROL_DECODE_CRITICALITY.get(StaticUtils.getExceptionMessage(ae)), ae);
        }
        value = ASN1OctetString.decodeAsOctetString(elements[2]);
    }
    return decode(oid, isCritical, value);
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1Exception(com.unboundid.asn1.ASN1Exception) ASN1Element(com.unboundid.asn1.ASN1Element) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) NotNull(com.unboundid.util.NotNull)

Example 20 with ASN1OctetString

use of com.mindbright.asn1.ASN1OctetString 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)

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