use of com.unboundid.asn1.ASN1StreamReaderSet in project ldapsdk by pingidentity.
the class Filter method readFrom.
/**
* Reads and decodes a search filter from the provided ASN.1 stream reader.
*
* @param reader The ASN.1 stream reader from which to read the filter.
*
* @return The decoded search filter.
*
* @throws LDAPException If an error occurs while reading or parsing the
* search filter.
*/
@NotNull()
public static Filter readFrom(@NotNull final ASN1StreamReader reader) throws LDAPException {
try {
final Filter[] filterComps;
final Filter notComp;
final String attrName;
final ASN1OctetString assertionValue;
final ASN1OctetString subInitial;
final ASN1OctetString[] subAny;
final ASN1OctetString subFinal;
final String matchingRuleID;
final boolean dnAttributes;
final byte filterType = (byte) reader.peek();
switch(filterType) {
case FILTER_TYPE_AND:
case FILTER_TYPE_OR:
final ArrayList<Filter> comps = new ArrayList<>(5);
final ASN1StreamReaderSet elementSet = reader.beginSet();
while (elementSet.hasMoreElements()) {
comps.add(readFrom(reader));
}
filterComps = new Filter[comps.size()];
comps.toArray(filterComps);
notComp = null;
attrName = null;
assertionValue = null;
subInitial = null;
subAny = NO_SUB_ANY;
subFinal = null;
matchingRuleID = null;
dnAttributes = false;
break;
case FILTER_TYPE_NOT:
final ASN1Element notFilterElement;
try {
final ASN1Element e = reader.readElement();
notFilterElement = ASN1Element.decode(e.getValue());
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_CANNOT_DECODE_NOT_COMP.get(StaticUtils.getExceptionMessage(ae)), ae);
}
notComp = decode(notFilterElement);
filterComps = NO_FILTERS;
attrName = null;
assertionValue = null;
subInitial = null;
subAny = NO_SUB_ANY;
subFinal = null;
matchingRuleID = null;
dnAttributes = false;
break;
case FILTER_TYPE_EQUALITY:
case FILTER_TYPE_GREATER_OR_EQUAL:
case FILTER_TYPE_LESS_OR_EQUAL:
case FILTER_TYPE_APPROXIMATE_MATCH:
reader.beginSequence();
attrName = reader.readString();
assertionValue = new ASN1OctetString(reader.readBytes());
filterComps = NO_FILTERS;
notComp = null;
subInitial = null;
subAny = NO_SUB_ANY;
subFinal = null;
matchingRuleID = null;
dnAttributes = false;
break;
case FILTER_TYPE_SUBSTRING:
reader.beginSequence();
attrName = reader.readString();
ASN1OctetString tempSubInitial = null;
ASN1OctetString tempSubFinal = null;
final ArrayList<ASN1OctetString> subAnyList = new ArrayList<>(1);
final ASN1StreamReaderSequence subSequence = reader.beginSequence();
while (subSequence.hasMoreElements()) {
final byte type = (byte) reader.peek();
final ASN1OctetString s = new ASN1OctetString(type, reader.readBytes());
switch(type) {
case SUBSTRING_TYPE_SUBINITIAL:
tempSubInitial = s;
break;
case SUBSTRING_TYPE_SUBANY:
subAnyList.add(s);
break;
case SUBSTRING_TYPE_SUBFINAL:
tempSubFinal = s;
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_INVALID_SUBSTR_TYPE.get(StaticUtils.toHex(type)));
}
}
subInitial = tempSubInitial;
subFinal = tempSubFinal;
subAny = new ASN1OctetString[subAnyList.size()];
subAnyList.toArray(subAny);
filterComps = NO_FILTERS;
notComp = null;
assertionValue = null;
matchingRuleID = null;
dnAttributes = false;
break;
case FILTER_TYPE_PRESENCE:
attrName = reader.readString();
filterComps = NO_FILTERS;
notComp = null;
assertionValue = null;
subInitial = null;
subAny = NO_SUB_ANY;
subFinal = null;
matchingRuleID = null;
dnAttributes = false;
break;
case FILTER_TYPE_EXTENSIBLE_MATCH:
String tempAttrName = null;
ASN1OctetString tempAssertionValue = null;
String tempMatchingRuleID = null;
boolean tempDNAttributes = false;
final ASN1StreamReaderSequence emSequence = reader.beginSequence();
while (emSequence.hasMoreElements()) {
final byte type = (byte) reader.peek();
switch(type) {
case EXTENSIBLE_TYPE_ATTRIBUTE_NAME:
tempAttrName = reader.readString();
break;
case EXTENSIBLE_TYPE_MATCHING_RULE_ID:
tempMatchingRuleID = reader.readString();
break;
case EXTENSIBLE_TYPE_MATCH_VALUE:
tempAssertionValue = new ASN1OctetString(type, reader.readBytes());
break;
case EXTENSIBLE_TYPE_DN_ATTRIBUTES:
tempDNAttributes = reader.readBoolean();
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_EXTMATCH_INVALID_TYPE.get(StaticUtils.toHex(type)));
}
}
if ((tempAttrName == null) && (tempMatchingRuleID == null)) {
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_EXTMATCH_NO_ATTR_OR_MRID.get());
}
if (tempAssertionValue == null) {
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_EXTMATCH_NO_VALUE.get());
}
attrName = tempAttrName;
assertionValue = tempAssertionValue;
matchingRuleID = tempMatchingRuleID;
dnAttributes = tempDNAttributes;
filterComps = NO_FILTERS;
notComp = null;
subInitial = null;
subAny = NO_SUB_ANY;
subFinal = null;
break;
default:
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_ELEMENT_INVALID_TYPE.get(StaticUtils.toHex(filterType)));
}
return new Filter(null, filterType, filterComps, notComp, attrName, assertionValue, subInitial, subAny, subFinal, matchingRuleID, dnAttributes);
} catch (final LDAPException le) {
Debug.debugException(le);
throw le;
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_FILTER_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
}
}
use of com.unboundid.asn1.ASN1StreamReaderSet in project ldapsdk by pingidentity.
the class Attribute method readFrom.
/**
* Reads and decodes an attribute from the provided ASN.1 stream reader.
*
* @param reader The ASN.1 stream reader from which to read the attribute.
* @param schema The schema to use to select the appropriate matching rule
* for this attribute. It may be {@code null} if the default
* matching rule should be selected.
*
* @return The decoded attribute.
*
* @throws LDAPException If a problem occurs while trying to read or decode
* the attribute.
*/
@NotNull()
public static Attribute readFrom(@NotNull final ASN1StreamReader reader, @Nullable final Schema schema) throws LDAPException {
try {
Validator.ensureNotNull(reader.beginSequence());
final String attrName = reader.readString();
Validator.ensureNotNull(attrName);
final MatchingRule matchingRule = MatchingRule.selectEqualityMatchingRule(attrName, schema);
final ArrayList<ASN1OctetString> valueList = new ArrayList<>(10);
final ASN1StreamReaderSet valueSet = reader.beginSet();
while (valueSet.hasMoreElements()) {
valueList.add(new ASN1OctetString(reader.readBytes()));
}
final ASN1OctetString[] values = new ASN1OctetString[valueList.size()];
valueList.toArray(values);
return new Attribute(attrName, matchingRule, values);
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_ATTR_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
}
}
use of com.unboundid.asn1.ASN1StreamReaderSet in project ldapsdk by pingidentity.
the class Modification method readFrom.
/**
* Reads and decodes an LDAP modification from the provided ASN.1 stream
* reader.
*
* @param reader The ASN.1 stream reader from which to read the
* modification.
*
* @return The decoded modification.
*
* @throws LDAPException If a problem occurs while trying to read or decode
* the modification.
*/
@NotNull()
public static Modification readFrom(@NotNull final ASN1StreamReader reader) throws LDAPException {
try {
Validator.ensureNotNull(reader.beginSequence());
final ModificationType modType = ModificationType.valueOf(reader.readEnumerated());
Validator.ensureNotNull(reader.beginSequence());
final String attrName = reader.readString();
final ArrayList<ASN1OctetString> valueList = new ArrayList<>(5);
final ASN1StreamReaderSet valueSet = reader.beginSet();
while (valueSet.hasMoreElements()) {
valueList.add(new ASN1OctetString(reader.readBytes()));
}
final ASN1OctetString[] values = new ASN1OctetString[valueList.size()];
valueList.toArray(values);
return new Modification(modType, attrName, values);
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_MOD_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
}
}
Aggregations