Search in sources :

Example 1 with ASN1Buffer

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();
        }
    }
}
Also used : ASN1Buffer(com.unboundid.asn1.ASN1Buffer) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) AtomicLong(java.util.concurrent.atomic.AtomicLong) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with ASN1Buffer

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());
}
Also used : Attribute(com.unboundid.ldap.sdk.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) ASN1Buffer(com.unboundid.asn1.ASN1Buffer) ASN1StreamReader(com.unboundid.asn1.ASN1StreamReader) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 3 with ASN1Buffer

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());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Control(com.unboundid.ldap.sdk.Control) Attribute(com.unboundid.ldap.sdk.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) ASN1Buffer(com.unboundid.asn1.ASN1Buffer) ASN1StreamReader(com.unboundid.asn1.ASN1StreamReader) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 4 with ASN1Buffer

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());
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) Control(com.unboundid.ldap.sdk.Control) ByteArrayInputStream(java.io.ByteArrayInputStream) ASN1Buffer(com.unboundid.asn1.ASN1Buffer) ASN1OctetString(com.unboundid.asn1.ASN1OctetString) ASN1StreamReader(com.unboundid.asn1.ASN1StreamReader) LinkedList(java.util.LinkedList) Test(org.testng.annotations.Test)

Example 5 with ASN1Buffer

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);
}
Also used : ASN1BufferSequence(com.unboundid.asn1.ASN1BufferSequence) ByteArrayInputStream(java.io.ByteArrayInputStream) ASN1Buffer(com.unboundid.asn1.ASN1Buffer) ASN1StreamReader(com.unboundid.asn1.ASN1StreamReader) Test(org.testng.annotations.Test)

Aggregations

ASN1Buffer (com.unboundid.asn1.ASN1Buffer)100 ASN1StreamReader (com.unboundid.asn1.ASN1StreamReader)91 ByteArrayInputStream (java.io.ByteArrayInputStream)91 Test (org.testng.annotations.Test)91 ASN1BufferSequence (com.unboundid.asn1.ASN1BufferSequence)47 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)32 LinkedList (java.util.LinkedList)28 DN (com.unboundid.ldap.sdk.DN)21 Control (com.unboundid.ldap.sdk.Control)17 ASN1Element (com.unboundid.asn1.ASN1Element)8 Attribute (com.unboundid.ldap.sdk.Attribute)6 SimpleBindRequest (com.unboundid.ldap.sdk.SimpleBindRequest)4 ASN1BufferSet (com.unboundid.asn1.ASN1BufferSet)3 Modification (com.unboundid.ldap.sdk.Modification)3 RDN (com.unboundid.ldap.sdk.RDN)2 JSONObjectFilter (com.unboundid.ldap.sdk.unboundidds.jsonfilter.JSONObjectFilter)2 OutputStream (java.io.OutputStream)2 ExtendedResponseProtocolOp (com.unboundid.ldap.protocol.ExtendedResponseProtocolOp)1 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)1 ExtendedRequest (com.unboundid.ldap.sdk.ExtendedRequest)1