use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class LDAPResult method readLDAPResultFrom.
/**
* Creates a new LDAP result object with the provided message ID and with the
* protocol op and controls read from the given ASN.1 stream reader.
*
* @param messageID The LDAP message ID for the LDAP message that is
* associated with this LDAP result.
* @param messageSequence The ASN.1 stream reader sequence used in the
* course of reading the LDAP message elements.
* @param reader The ASN.1 stream reader from which to read the
* protocol op and controls.
*
* @return The decoded LDAP result.
*
* @throws LDAPException If a problem occurs while reading or decoding data
* from the ASN.1 stream reader.
*/
@NotNull()
static LDAPResult readLDAPResultFrom(final int messageID, @NotNull final ASN1StreamReaderSequence messageSequence, @NotNull final ASN1StreamReader reader) throws LDAPException {
try {
final ASN1StreamReaderSequence protocolOpSequence = reader.beginSequence();
final byte protocolOpType = protocolOpSequence.getType();
final ResultCode resultCode = ResultCode.valueOf(reader.readEnumerated());
String matchedDN = reader.readString();
if (matchedDN.isEmpty()) {
matchedDN = null;
}
String diagnosticMessage = reader.readString();
if (diagnosticMessage.isEmpty()) {
diagnosticMessage = null;
}
String[] referralURLs = StaticUtils.NO_STRINGS;
if (protocolOpSequence.hasMoreElements()) {
final ArrayList<String> refList = new ArrayList<>(1);
final ASN1StreamReaderSequence refSequence = reader.beginSequence();
while (refSequence.hasMoreElements()) {
refList.add(reader.readString());
}
referralURLs = new String[refList.size()];
refList.toArray(referralURLs);
}
Control[] responseControls = NO_CONTROLS;
if (messageSequence.hasMoreElements()) {
final ArrayList<Control> controlList = new ArrayList<>(1);
final ASN1StreamReaderSequence controlSequence = reader.beginSequence();
while (controlSequence.hasMoreElements()) {
controlList.add(Control.readFrom(reader));
}
responseControls = new Control[controlList.size()];
controlList.toArray(responseControls);
}
return new LDAPResult(protocolOpType, messageID, resultCode, diagnosticMessage, matchedDN, referralURLs, responseControls);
} catch (final LDAPException le) {
Debug.debugException(le);
throw le;
} catch (final ASN1Exception ae) {
Debug.debugException(ae);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_RESULT_CANNOT_DECODE.get(ae.getMessage()), ae);
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.DECODING_ERROR, ERR_RESULT_CANNOT_DECODE.get(StaticUtils.getExceptionMessage(e)), e);
}
}
use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class LDAPMessageTestCase method testAddRequestMessage.
/**
* Tests the behavior of the {@code LDAPMessage} class with an add request
* protocol op.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testAddRequestMessage() throws Exception {
LinkedList<Attribute> attrs = new LinkedList<Attribute>();
attrs.add(new Attribute("objectClass", "top", "domain"));
attrs.add(new Attribute("dc", "example"));
AddRequestProtocolOp op = new AddRequestProtocolOp("dc=example,dc=com", attrs);
LDAPMessage m = new LDAPMessage(1, op);
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 AddRequestProtocolOp);
assertEquals(m.getProtocolOpType(), LDAPMessage.PROTOCOL_OP_TYPE_ADD_REQUEST);
assertNotNull(m.getControls());
assertTrue(m.getControls().isEmpty());
assertNotNull(m.getAddRequestProtocolOp());
assertNotNull(m.toString());
}
use of com.unboundid.asn1.ASN1StreamReader 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());
}
use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class LDAPMessageTestCase method testReadLDAPResponseFromEmptyStream.
/**
* Tests the {@code LDAPMessage.readLDAPResponseFrom} method with an empty
* stream.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testReadLDAPResponseFromEmptyStream() throws Exception {
ByteArrayInputStream inputStream = new ByteArrayInputStream(new byte[0]);
ASN1StreamReader reader = new ASN1StreamReader(inputStream);
assertNull(LDAPMessage.readLDAPResponseFrom(reader, true));
}
use of com.unboundid.asn1.ASN1StreamReader in project ldapsdk by pingidentity.
the class LDAPMessageTestCase method testSearchResultDoneMessage.
/**
* Tests the behavior of the {@code LDAPMessage} class with a search result
* done protocol op.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test()
public void testSearchResultDoneMessage() throws Exception {
LinkedList<String> refs = new LinkedList<String>();
refs.add("ldap://server1.example.com:389/dc=example,dc=com");
refs.add("ldap://server2.example.com:389/dc=example,dc=com");
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()));
SearchResultDoneProtocolOp op = new SearchResultDoneProtocolOp(0, null, null, refs);
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 SearchResultDoneProtocolOp);
assertEquals(m.getProtocolOpType(), LDAPMessage.PROTOCOL_OP_TYPE_SEARCH_RESULT_DONE);
assertNotNull(m.getControls());
assertFalse(m.getControls().isEmpty());
assertNotNull(m.getSearchResultDoneProtocolOp());
inputStream = new ByteArrayInputStream(b.toByteArray());
reader = new ASN1StreamReader(inputStream);
LDAPResponse r = LDAPMessage.readLDAPResponseFrom(reader, true);
assertNotNull(m.toString());
}
Aggregations