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());
}
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());
}
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;
}
}
}
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));
}
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());
}
Aggregations