use of com.github.zhenwei.core.asn1.ASN1Sequence 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());
}
use of com.github.zhenwei.core.asn1.ASN1Sequence 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());
}
use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.
the class IntermediateResponseProtocolOpTestCase method testDecodeInvalidElementType.
/**
* Tests the behavior when trying to decode an intermediate response with an
* invalid element type.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { LDAPException.class })
public void testDecodeInvalidElementType() throws Exception {
final ASN1Sequence s = new ASN1Sequence(LDAPMessage.PROTOCOL_OP_TYPE_INTERMEDIATE_RESPONSE, new ASN1OctetString((byte) 0x79));
IntermediateResponseProtocolOp.decodeProtocolOp(s);
}
use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.
the class AttributeTestCase method testConstructor1.
/**
* Tests the first constructor, which takes an attribute name, using a valid
* name.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testConstructor1() throws Exception {
Attribute attr = new Attribute("cn");
assertEquals(attr.getName(), "cn");
assertEquals(attr.getBaseName(), "cn");
assertFalse(attr.hasOptions());
assertFalse(attr.hasOption("lang=en-US"));
assertTrue(attr.getOptions().isEmpty());
assertNotNull(attr.getMatchingRule());
assertEquals(attr.getMatchingRule(), MATCHING_RULE);
assertNull(attr.getValue());
assertNull(attr.getValueByteArray());
assertFalse(attr.hasValue());
assertEquals(attr.getValues().length, 0);
assertEquals(attr.getValueByteArrays().length, 0);
assertEquals(attr.size(), 0);
ASN1Sequence attrSequence = attr.encode();
Attribute attr2 = Attribute.decode(attrSequence);
assertTrue(attr.equals(attr2));
assertTrue(attr2.equals(attr));
assertEquals(attr.hashCode(), attr2.hashCode());
assertNotNull(attr.toString());
}
use of com.github.zhenwei.core.asn1.ASN1Sequence in project ldapsdk by pingidentity.
the class AttributeTestCase method testConstructor4Array.
/**
* Tests the fourth constructor, which takes an attribute name and set of
* string values, by providing the values as a string array.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testConstructor4Array() throws Exception {
Attribute attr = new Attribute("cn", new String[] { "John Doe", "Johnathan Doe" });
assertEquals(attr.getName(), "cn");
assertEquals(attr.getBaseName(), "cn");
assertFalse(attr.hasOptions());
assertFalse(attr.hasOption("lang=en-US"));
assertTrue(attr.getOptions().isEmpty());
assertNotNull(attr.getMatchingRule());
assertEquals(attr.getMatchingRule(), MATCHING_RULE);
assertEquals(attr.getValue(), "John Doe");
assertTrue(Arrays.equals(attr.getValueByteArray(), "John Doe".getBytes("UTF-8")));
assertTrue(attr.hasValue());
assertTrue(attr.hasValue("John Doe"));
assertTrue(attr.hasValue("John Doe".getBytes("UTF-8")));
assertTrue(attr.hasValue("john doe"));
assertTrue(attr.hasValue(" john doe "));
assertTrue(attr.hasValue("johnathan doe"));
assertFalse(attr.hasValue("Johnny Doe"));
assertEquals(attr.getValues().length, 2);
assertEquals(attr.getValueByteArrays().length, 2);
assertEquals(attr.size(), 2);
ASN1Sequence attrSequence = attr.encode();
Attribute attr2 = Attribute.decode(attrSequence);
assertTrue(attr.equals(attr2));
assertTrue(attr2.equals(attr));
assertEquals(attr.hashCode(), attr2.hashCode());
assertNotNull(attr.toString());
}
Aggregations