use of com.github.zhenwei.core.asn1.ASN1Exception in project ldapsdk by pingidentity.
the class Modification method decode.
/**
* Decodes the provided ASN.1 sequence as an LDAP modification.
*
* @param modificationSequence The ASN.1 sequence to decode as an LDAP
* modification. It must not be {@code null}.
*
* @return The decoded LDAP modification.
*
* @throws LDAPException If a problem occurs while trying to decode the
* provided ASN.1 sequence as an LDAP modification.
*/
@NotNull()
public static Modification decode(@NotNull final ASN1Sequence modificationSequence) throws LDAPException {
Validator.ensureNotNull(modificationSequence);
final ASN1Element[] modificationElements = modificationSequence.elements();
if (modificationElements.length != 2) {
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_MOD_DECODE_INVALID_ELEMENT_COUNT.get(modificationElements.length));
}
final int modType;
try {
final ASN1Enumerated typeEnumerated = ASN1Enumerated.decodeAsEnumerated(modificationElements[0]);
modType = typeEnumerated.intValue();
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_MOD_DECODE_CANNOT_PARSE_MOD_TYPE.get(StaticUtils.getExceptionMessage(ae)), ae);
}
final ASN1Sequence attrSequence;
try {
attrSequence = ASN1Sequence.decodeAsSequence(modificationElements[1]);
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_MOD_DECODE_CANNOT_PARSE_ATTR.get(StaticUtils.getExceptionMessage(ae)), ae);
}
final ASN1Element[] attrElements = attrSequence.elements();
if (attrElements.length != 2) {
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_MOD_DECODE_INVALID_ATTR_ELEMENT_COUNT.get(attrElements.length));
}
final String attrName = ASN1OctetString.decodeAsOctetString(attrElements[0]).stringValue();
final ASN1Set valueSet;
try {
valueSet = ASN1Set.decodeAsSet(attrElements[1]);
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_MOD_DECODE_CANNOT_PARSE_ATTR_VALUE_SET.get(StaticUtils.getExceptionMessage(ae)), ae);
}
final ASN1Element[] valueElements = valueSet.elements();
final ASN1OctetString[] values = new ASN1OctetString[valueElements.length];
for (int i = 0; i < values.length; i++) {
values[i] = ASN1OctetString.decodeAsOctetString(valueElements[i]);
}
return new Modification(ModificationType.valueOf(modType), attrName, values);
}
use of com.github.zhenwei.core.asn1.ASN1Exception in project ldapsdk by pingidentity.
the class JNDIConverter method convertControl.
/**
* Converts the provided JNDI control to an LDAP SDK control.
*
* @param c The control to be converted.
*
* @return The LDAP SDK control that corresponds to the provided JNDI
* control.
*
* @throws NamingException If a problem is encountered during the conversion
* process.
*/
@Nullable
public static Control convertControl(@Nullable final javax.naming.ldap.Control c) throws NamingException {
if (c == null) {
return null;
}
final ASN1OctetString value;
final byte[] valueBytes = c.getEncodedValue();
if ((valueBytes == null) || (valueBytes.length == 0)) {
value = null;
} else {
try {
value = ASN1OctetString.decodeAsOctetString(valueBytes);
} catch (final ASN1Exception ae) {
throw new NamingException(StaticUtils.getExceptionMessage(ae));
}
}
return new Control(c.getID(), c.isCritical(), value);
}
use of com.github.zhenwei.core.asn1.ASN1Exception in project ldapsdk by pingidentity.
the class EndTransactionExtendedResult method decodeOpControls.
/**
* Decodes the provided ASN.1 element as an update controls sequence. Each
* element of the sequence should itself be a sequence containing the message
* ID associated with the operation in which the control was returned and a
* sequence of the controls included in the response for that operation.
*
* @param element The ASN.1 element to be decoded.
* @param controlMap The map into which to place the decoded controls.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* contents of the provided ASN.1 element.
*/
private static void decodeOpControls(@NotNull final ASN1Element element, @NotNull final Map<Integer, Control[]> controlMap) throws LDAPException {
final ASN1Sequence ctlsSequence;
try {
ctlsSequence = ASN1Sequence.decodeAsSequence(element);
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_END_TXN_RESPONSE_CONTROLS_NOT_SEQUENCE.get(ae), ae);
}
for (final ASN1Element e : ctlsSequence.elements()) {
final ASN1Sequence ctlSequence;
try {
ctlSequence = ASN1Sequence.decodeAsSequence(e);
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_END_TXN_RESPONSE_CONTROL_NOT_SEQUENCE.get(ae), ae);
}
final ASN1Element[] ctlSequenceElements = ctlSequence.elements();
if (ctlSequenceElements.length != 2) {
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_END_TXN_RESPONSE_CONTROL_INVALID_ELEMENT_COUNT.get(ctlSequenceElements.length));
}
final int msgID;
try {
msgID = ASN1Integer.decodeAsInteger(ctlSequenceElements[0]).intValue();
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_END_TXN_RESPONSE_CONTROL_MSGID_NOT_INT.get(ae), ae);
}
final ASN1Sequence controlsSequence;
try {
controlsSequence = ASN1Sequence.decodeAsSequence(ctlSequenceElements[1]);
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_END_TXN_RESPONSE_CONTROLS_ELEMENT_NOT_SEQUENCE.get(ae), ae);
}
final Control[] controls = Control.decodeControls(controlsSequence);
if (controls.length == 0) {
continue;
}
controlMap.put(msgID, controls);
}
}
use of com.github.zhenwei.core.asn1.ASN1Exception in project ldapsdk by pingidentity.
the class JNDIExtendedRequest method toSDKExtendedRequest.
/**
* Retrieves an LDAP SDK extended request that is the equivalent of the
* provided JNDI extended request.
*
* @param r The JNDI extended request to convert to an LDAP SDK extended
* request.
*
* @return The LDAP SDK extended request converted from the provided JNDI
* extended request.
*
* @throws NamingException If a problem occurs while decoding the provided
* JNDI extended request as an SDK extended request.
*/
@Nullable()
public static ExtendedRequest toSDKExtendedRequest(@Nullable final javax.naming.ldap.ExtendedRequest r) throws NamingException {
if (r == null) {
return null;
}
final ASN1OctetString value;
final byte[] valueBytes = r.getEncodedValue();
if (valueBytes == null) {
value = null;
} else {
try {
value = ASN1OctetString.decodeAsOctetString(valueBytes);
} catch (final ASN1Exception ae) {
throw new NamingException(StaticUtils.getExceptionMessage(ae));
}
}
return new ExtendedRequest(r.getID(), value);
}
use of com.github.zhenwei.core.asn1.ASN1Exception in project ldapsdk by pingidentity.
the class EndBatchedTransactionExtendedResult method decodeOpControls.
/**
* Decodes the provided ASN.1 element as an update controls sequence. Each
* element of the sequence should itself be a sequence containing the message
* ID associated with the operation in which the control was returned and a
* sequence of the controls included in the response for that operation.
*
* @param element The ASN.1 element to be decoded.
* @param controlMap The map into which to place the decoded controls.
*
* @throws LDAPException If a problem occurs while attempting to decode the
* contents of the provided ASN.1 element.
*/
private static void decodeOpControls(@NotNull final ASN1Element element, @NotNull final Map<Integer, Control[]> controlMap) throws LDAPException {
final ASN1Sequence ctlsSequence;
try {
ctlsSequence = ASN1Sequence.decodeAsSequence(element);
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_END_TXN_RESPONSE_CONTROLS_NOT_SEQUENCE.get(ae), ae);
}
for (final ASN1Element e : ctlsSequence.elements()) {
final ASN1Sequence ctlSequence;
try {
ctlSequence = ASN1Sequence.decodeAsSequence(e);
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_END_TXN_RESPONSE_CONTROL_NOT_SEQUENCE.get(ae), ae);
}
final ASN1Element[] ctlSequenceElements = ctlSequence.elements();
if (ctlSequenceElements.length != 2) {
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_END_TXN_RESPONSE_CONTROL_INVALID_ELEMENT_COUNT.get(ctlSequenceElements.length));
}
final int msgID;
try {
msgID = ASN1Integer.decodeAsInteger(ctlSequenceElements[0]).intValue();
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_END_TXN_RESPONSE_CONTROL_MSGID_NOT_INT.get(ae), ae);
}
final ASN1Sequence controlsSequence;
try {
controlsSequence = ASN1Sequence.decodeAsSequence(ctlSequenceElements[1]);
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_END_TXN_RESPONSE_CONTROLS_ELEMENT_NOT_SEQUENCE.get(ae), ae);
}
final Control[] controls = Control.decodeControls(controlsSequence);
if (controls.length == 0) {
continue;
}
controlMap.put(msgID, controls);
}
}
Aggregations