use of com.unboundid.asn1.ASN1Buffer in project ldapsdk by pingidentity.
the class LDAPConnectionInternals method sendMessage.
/**
* Sends the provided LDAP message to the directory server.
*
* @param message The LDAP message to be sent.
* @param sendTimeoutMillis The maximum length of time, in milliseconds, to
* block while trying to send the request. If this
* is less than or equal to zero, then no send
* timeout will be enforced.
* @param allowRetry Indicates whether to allow retrying the send
* after a reconnect.
*
* @throws LDAPException If a problem occurs while sending the message.
*/
void sendMessage(@NotNull final LDAPMessage message, final long sendTimeoutMillis, final boolean allowRetry) throws LDAPException {
if (!isConnected()) {
throw new LDAPException(ResultCode.SERVER_DOWN, ERR_CONN_NOT_ESTABLISHED.get());
}
ASN1Buffer buffer = ASN1_BUFFERS.get().get();
if (buffer == null) {
buffer = new ASN1Buffer();
ASN1_BUFFERS.get().set(buffer);
}
buffer.clear();
try {
message.writeTo(buffer);
} catch (final LDAPRuntimeException lre) {
Debug.debugException(lre);
lre.throwLDAPException();
}
try {
final int soTimeout = Math.max(0, (int) sendTimeoutMillis);
if (Debug.debugEnabled()) {
Debug.debug(Level.INFO, DebugType.CONNECT, "Setting the SO_TIMEOUT value for connection " + connection + " to " + soTimeout + "ms.");
}
socket.setSoTimeout(soTimeout);
} catch (final Exception e) {
Debug.debugException(e);
}
final Long writeID;
if (sendTimeoutMillis > 0) {
writeID = writeTimeoutHandler.beginWrite(sendTimeoutMillis);
} else {
writeID = null;
}
try {
final OutputStream os = outputStream;
if (os == null) {
// reconnect first if appropriate.
if (message.getProtocolOpType() == LDAPMessage.PROTOCOL_OP_TYPE_UNBIND_REQUEST) {
return;
}
final boolean closeRequested = connection.closeRequested();
if (allowRetry && (!closeRequested) && (!connection.synchronousMode())) {
connection.reconnect();
try {
sendMessage(message, sendTimeoutMillis, false);
return;
} catch (final Exception e) {
Debug.debugException(e);
}
}
throw new LDAPException(ResultCode.SERVER_DOWN, ERR_CONN_SEND_ERROR_NOT_ESTABLISHED.get(host, port));
}
if (saslClient == null) {
buffer.writeTo(os);
} else {
// We need to wrap the data that was read using the SASL client, but we
// also need to precede that wrapped data with four bytes that specify
// the number of bytes of wrapped data.
final byte[] clearBytes = buffer.toByteArray();
final byte[] saslBytes = saslClient.wrap(clearBytes, 0, clearBytes.length);
final byte[] lengthBytes = new byte[4];
lengthBytes[0] = (byte) ((saslBytes.length >> 24) & 0xFF);
lengthBytes[1] = (byte) ((saslBytes.length >> 16) & 0xFF);
lengthBytes[2] = (byte) ((saslBytes.length >> 8) & 0xFF);
lengthBytes[3] = (byte) (saslBytes.length & 0xFF);
os.write(lengthBytes);
os.write(saslBytes);
}
os.flush();
} catch (final LDAPException e) {
Debug.debugException(e);
throw e;
} catch (final IOException ioe) {
Debug.debugException(ioe);
// first if appropriate.
if (message.getProtocolOpType() == LDAPMessage.PROTOCOL_OP_TYPE_UNBIND_REQUEST) {
return;
}
final boolean closeRequested = connection.closeRequested();
if (allowRetry && (!closeRequested) && (!connection.synchronousMode())) {
connection.reconnect();
try {
sendMessage(message, sendTimeoutMillis, false);
return;
} catch (final Exception e) {
Debug.debugException(e);
}
}
throw new LDAPException(ResultCode.SERVER_DOWN, ERR_CONN_SEND_ERROR.get(host + ':' + port, StaticUtils.getExceptionMessage(ioe)), ioe);
} catch (final Exception e) {
Debug.debugException(e);
throw new LDAPException(ResultCode.LOCAL_ERROR, ERR_CONN_ENCODE_ERROR.get(host + ':' + port, StaticUtils.getExceptionMessage(e)), e);
} finally {
if (writeID != null) {
writeTimeoutHandler.writeCompleted(writeID);
}
if (buffer.zeroBufferOnClear()) {
buffer.clear();
}
}
}
use of com.unboundid.asn1.ASN1Buffer 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.ASN1Buffer 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.ASN1Buffer 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());
}
use of com.unboundid.asn1.ASN1Buffer in project ldapsdk by pingidentity.
the class LDAPMessageTestCase method testReadLDAPResponseFromInvalidControlElementType.
/**
* Tests the behavior when trying to read a result containing a control with
* an invalid element type.
*
* @throws Exception If an unexpected problem occurs.
*/
@Test(expectedExceptions = { LDAPException.class })
public void testReadLDAPResponseFromInvalidControlElementType() throws Exception {
ASN1Buffer b = new ASN1Buffer();
ASN1BufferSequence msgSequence = b.beginSequence();
b.addInteger(1);
ASN1BufferSequence opSequence = b.beginSequence(LDAPMessage.PROTOCOL_OP_TYPE_ADD_RESPONSE);
b.addEnumerated(0);
b.addOctetString();
b.addOctetString();
opSequence.end();
ASN1BufferSequence controlsSequence = b.beginSequence();
ASN1BufferSequence controlSequence = b.beginSequence();
b.addOctetString("1.2.3.4");
b.addInteger(5);
controlSequence.end();
controlsSequence.end();
msgSequence.end();
ByteArrayInputStream inputStream = new ByteArrayInputStream(b.toByteArray());
ASN1StreamReader reader = new ASN1StreamReader(inputStream);
LDAPMessage.readLDAPResponseFrom(reader, true);
}
Aggregations