Search in sources :

Example 1 with SearchResultEntryProtocolOp

use of com.unboundid.ldap.protocol.SearchResultEntryProtocolOp in project ldapsdk by pingidentity.

the class UNBOUNDIDTESTServer method run.

/**
 * Performs the processing for this server.
 */
@Override()
public void run() {
    try {
        serverSocket = new ServerSocket(0);
        listenPort = serverSocket.getLocalPort();
        while (!stopRequested.get()) {
            // Accept a connection from a client.
            clientSocket = serverSocket.accept();
            final InputStream inputStream = clientSocket.getInputStream();
            final OutputStream outputStream = clientSocket.getOutputStream();
            final ASN1StreamReader asn1Reader = new ASN1StreamReader(inputStream, 0);
            // The client must first send an UNBOUNDID-TEST bind request with no
            // credentials.
            LDAPMessage requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            BindRequestProtocolOp bindRequestOp = requestMessage.getBindRequestProtocolOp();
            assertEquals(bindRequestOp.getSASLMechanism(), "UNBOUNDID-TEST");
            assertNull(bindRequestOp.getSASLCredentials());
            // Return a "SASL bind in progress" response.
            LDAPMessage responseMessage = new LDAPMessage(requestMessage.getMessageID(), new BindResponseProtocolOp(ResultCode.SASL_BIND_IN_PROGRESS_INT_VALUE, null, null, null, null));
            outputStream.write(responseMessage.encode().encode());
            outputStream.flush();
            // The next request must be an UNBOUNDID-TEST bind request with
            // credentials.  We won't do anything to validate the credentials, but
            // we will look at the third element to see what QoP the client
            // requested.
            requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            bindRequestOp = requestMessage.getBindRequestProtocolOp();
            assertEquals(bindRequestOp.getSASLMechanism(), "UNBOUNDID-TEST");
            assertNotNull(bindRequestOp.getSASLCredentials());
            final ASN1Sequence credSequence = ASN1Sequence.decodeAsSequence(bindRequestOp.getSASLCredentials().getValue());
            final ASN1Element[] credElements = credSequence.elements();
            final SASLQualityOfProtection qop = SASLQualityOfProtection.forName(ASN1OctetString.decodeAsOctetString(credElements[2]).stringValue());
            assertNotNull(qop);
            final boolean qopEncode = ((qop == SASLQualityOfProtection.AUTH_INT) || (qop == SASLQualityOfProtection.AUTH_CONF));
            // Return a "success" response.  Include server SASL credentials with
            // the requested QoP.
            responseMessage = new LDAPMessage(requestMessage.getMessageID(), new BindResponseProtocolOp(ResultCode.SUCCESS_INT_VALUE, null, null, null, new ASN1OctetString(qop.toString())));
            outputStream.write(responseMessage.encode().encode());
            outputStream.flush();
            // request.
            if (qopEncode) {
                for (int i = 0; i < 4; i++) {
                    inputStream.read();
                }
            }
            requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            final SearchRequestProtocolOp searchRequestOp = requestMessage.getSearchRequestProtocolOp();
            assertEquals(searchRequestOp.getBaseDN(), "");
            assertEquals(searchRequestOp.getScope(), SearchScope.BASE);
            assertEquals(searchRequestOp.getFilter(), Filter.createPresenceFilter("objectClass"));
            assertEquals(searchRequestOp.getAttributes(), Arrays.asList("1.1"));
            // Return a search result entry message with a DN but no attributes.
            responseMessage = new LDAPMessage(requestMessage.getMessageID(), new SearchResultEntryProtocolOp("", Collections.<Attribute>emptyList()));
            byte[] messageBytes = responseMessage.encode().encode();
            if (qopEncode) {
                // Since we know it's a tiny response, we know the length will be
                // less than 127 bytes, so we can cheat.
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(messageBytes.length);
            }
            outputStream.write(messageBytes);
            outputStream.flush();
            // Return a "success" search result done message.
            responseMessage = new LDAPMessage(requestMessage.getMessageID(), new SearchResultDoneProtocolOp(ResultCode.SUCCESS_INT_VALUE, null, null, null));
            messageBytes = responseMessage.encode().encode();
            if (qopEncode) {
                // Since we know it's a tiny response, we know the length will be
                // less than 127 bytes, so we can cheat.
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(0);
                outputStream.write(messageBytes.length);
            }
            outputStream.write(messageBytes);
            outputStream.flush();
            // The next request should be an unbind request.
            if (qopEncode) {
                for (int i = 0; i < 4; i++) {
                    inputStream.read();
                }
            }
            requestMessage = LDAPMessage.readFrom(asn1Reader, false);
            final UnbindRequestProtocolOp unbindRequestOp = requestMessage.getUnbindRequestProtocolOp();
            // Close the connection.
            try {
                asn1Reader.close();
            } catch (final Exception e) {
            }
            try {
                outputStream.close();
            } catch (final Exception e) {
            }
            try {
                clientSocket.close();
            } catch (final Exception e) {
            }
            clientSocket = null;
        }
    } catch (final Exception e) {
        stopServer();
    }
}
Also used : ASN1OctetString(com.unboundid.asn1.ASN1OctetString) SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) SearchResultEntryProtocolOp(com.unboundid.ldap.protocol.SearchResultEntryProtocolOp) BindRequestProtocolOp(com.unboundid.ldap.protocol.BindRequestProtocolOp) ServerSocket(java.net.ServerSocket) BindResponseProtocolOp(com.unboundid.ldap.protocol.BindResponseProtocolOp) ASN1Sequence(com.unboundid.asn1.ASN1Sequence) ASN1Element(com.unboundid.asn1.ASN1Element) SearchResultDoneProtocolOp(com.unboundid.ldap.protocol.SearchResultDoneProtocolOp) ASN1StreamReader(com.unboundid.asn1.ASN1StreamReader) UnbindRequestProtocolOp(com.unboundid.ldap.protocol.UnbindRequestProtocolOp)

Example 2 with SearchResultEntryProtocolOp

use of com.unboundid.ldap.protocol.SearchResultEntryProtocolOp in project ldapsdk by pingidentity.

the class InMemoryOperationInterceptorRequestHandler method transformEntry.

/**
 * {@inheritDoc}
 */
@Override()
@Nullable()
public ObjectPair<SearchResultEntryProtocolOp, Control[]> transformEntry(final int messageID, @NotNull final SearchResultEntryProtocolOp entry, @NotNull final Control[] controls) {
    final InterceptedSearchOperation op = (InterceptedSearchOperation) activeOperations.get(messageID);
    if (op == null) {
        return new ObjectPair<>(entry, controls);
    }
    final InterceptedSearchEntry e = new InterceptedSearchEntry(op, entry, controls);
    for (final InMemoryOperationInterceptor i : interceptors) {
        try {
            i.processSearchEntry(e);
            if (e.getSearchEntry() == null) {
                return null;
            }
        } catch (final Exception ex) {
            Debug.debugException(ex);
            return null;
        }
    }
    return new ObjectPair<>(new SearchResultEntryProtocolOp(e.getSearchEntry()), e.getSearchEntry().getControls());
}
Also used : SearchResultEntryProtocolOp(com.unboundid.ldap.protocol.SearchResultEntryProtocolOp) LDAPException(com.unboundid.ldap.sdk.LDAPException) ObjectPair(com.unboundid.util.ObjectPair) Nullable(com.unboundid.util.Nullable)

Example 3 with SearchResultEntryProtocolOp

use of com.unboundid.ldap.protocol.SearchResultEntryProtocolOp in project ldapsdk by pingidentity.

the class InterceptedSearchEntryTestCase method testBasics.

/**
 * Provides basic test coverage for an intercepted search entry.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testBasics() throws Exception {
    // Create an intercepted search entry.  We'll use a null connection, which
    // shouldn't happen naturally but will be sufficient for this test.
    final SearchRequestProtocolOp requestOp = new SearchRequestProtocolOp(new SearchRequest("dc=example,dc=com", SearchScope.BASE, "(objectClass=*)"));
    final InterceptedSearchEntry e = new InterceptedSearchEntry(new InterceptedSearchOperation(null, 1, requestOp), new SearchResultEntryProtocolOp(new Entry("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example")));
    assertNotNull(e.toString());
    // Test methods for a generic intercepted operation.
    assertNull(e.getClientConnection());
    assertEquals(e.getConnectionID(), -1L);
    assertNull(e.getConnectedAddress());
    assertEquals(e.getConnectedPort(), -1);
    assertEquals(e.getMessageID(), 1);
    assertNull(e.getProperty("propX"));
    e.setProperty("propX", "valX");
    assertNotNull(e.getProperty("propX"));
    assertEquals(e.getProperty("propX"), "valX");
    assertNotNull(e.toString());
    e.setProperty("propX", null);
    assertNull(e.getProperty("propX"));
    // Test methods specific to an intercepted compare operation.
    assertNotNull(e.getRequest());
    assertNotNull(e.getSearchEntry());
    assertFalse(e.getSearchEntry().hasAttribute("description"));
    assertNotNull(e.getSearchEntry().getControls());
    assertEquals(e.getSearchEntry().getControls().length, 0);
    assertNotNull(e.toString());
    e.setSearchEntry(new Entry("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example", "description: foo"));
    assertNotNull(e.getSearchEntry());
    assertTrue(e.getSearchEntry().hasAttributeValue("description", "foo"));
    assertNotNull(e.getSearchEntry().getControls());
    assertEquals(e.getSearchEntry().getControls().length, 0);
    assertNotNull(e.toString());
    e.setSearchEntry(new SearchResultEntry(new Entry("dn: dc=example,dc=com", "objectClass: top", "objectClass: domain", "dc: example", "description: bar"), new Control("1.2.3.4"), new Control("1.2.3.5")));
    assertNotNull(e.getSearchEntry());
    assertTrue(e.getSearchEntry().hasAttributeValue("description", "bar"));
    assertNotNull(e.getSearchEntry().getControls());
    assertEquals(e.getSearchEntry().getControls().length, 2);
    assertNotNull(e.toString());
    e.setSearchEntry(null);
    assertNull(e.getSearchEntry());
}
Also used : SearchRequest(com.unboundid.ldap.sdk.SearchRequest) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) Entry(com.unboundid.ldap.sdk.Entry) Control(com.unboundid.ldap.sdk.Control) SearchRequestProtocolOp(com.unboundid.ldap.protocol.SearchRequestProtocolOp) SearchResultEntryProtocolOp(com.unboundid.ldap.protocol.SearchResultEntryProtocolOp) SearchResultEntry(com.unboundid.ldap.sdk.SearchResultEntry) Test(org.testng.annotations.Test)

Example 4 with SearchResultEntryProtocolOp

use of com.unboundid.ldap.protocol.SearchResultEntryProtocolOp in project ldapsdk by pingidentity.

the class LDAPDebuggerTestCase method testSuccessfulSearch.

/**
 * Provides test coverage for a successful search operation.
 *
 * @throws  Exception  If an unexpected problem occurs.
 */
@Test()
public void testSuccessfulSearch() throws Exception {
    TestRequestHandler.setReturnOp(new SearchResultDoneProtocolOp(0, null, null, null));
    TestRequestHandler.setReturnEntries(new SearchResultEntryProtocolOp("dc=example,dc=com", Arrays.asList(new Attribute("objectClass", "top", "domain"), new Attribute("dc", "example"))));
    TestRequestHandler.setReturnReferences(new SearchResultReferenceProtocolOp(Arrays.asList("ldap://server1.example.com/dc=example,dc=com", "ldap://server2.example.com/dc=example,dc=com")));
    try {
        final SearchRequest r = new SearchRequest("dc=example,dc=com", SearchScope.BASE, "(objectClass=*)");
        r.addControl(new Control("1.2.3.4"));
        conn.search(r);
    } finally {
        TestRequestHandler.setReturnEntries();
        TestRequestHandler.setReturnReferences();
    }
}
Also used : SearchResultReferenceProtocolOp(com.unboundid.ldap.protocol.SearchResultReferenceProtocolOp) SearchRequest(com.unboundid.ldap.sdk.SearchRequest) AuthorizationIdentityRequestControl(com.unboundid.ldap.sdk.controls.AuthorizationIdentityRequestControl) Control(com.unboundid.ldap.sdk.Control) ManageDsaITRequestControl(com.unboundid.ldap.sdk.controls.ManageDsaITRequestControl) Attribute(com.unboundid.ldap.sdk.Attribute) SearchResultDoneProtocolOp(com.unboundid.ldap.protocol.SearchResultDoneProtocolOp) SearchResultEntryProtocolOp(com.unboundid.ldap.protocol.SearchResultEntryProtocolOp) Test(org.testng.annotations.Test)

Example 5 with SearchResultEntryProtocolOp

use of com.unboundid.ldap.protocol.SearchResultEntryProtocolOp in project ldapsdk by pingidentity.

the class LDAPListenerClientConnection method sendSearchResultEntry.

/**
 * Sends a search result entry message to the client with the provided
 * information.
 *
 * @param  messageID   The message ID for the LDAP message to send to the
 *                     client.  It must match the message ID of the associated
 *                     search request.
 * @param  protocolOp  The search result entry protocol op to include in the
 *                     LDAP message to send to the client.  It must not be
 *                     {@code null}.
 * @param  controls    The set of controls to include in the response message.
 *                     It may be empty or {@code null} if no controls should
 *                     be included.
 *
 * @throws  LDAPException  If a problem occurs while attempting to send the
 *                         provided response message.  If an exception is
 *                         thrown, then the client connection will have been
 *                         terminated.
 */
public void sendSearchResultEntry(final int messageID, @NotNull final SearchResultEntryProtocolOp protocolOp, @Nullable final Control... controls) throws LDAPException {
    if (searchEntryTransformers.isEmpty()) {
        sendMessage(new LDAPMessage(messageID, protocolOp, controls));
    } else {
        Control[] c;
        SearchResultEntryProtocolOp op = protocolOp;
        if (controls == null) {
            c = EMPTY_CONTROL_ARRAY;
        } else {
            c = controls;
        }
        for (final SearchEntryTransformer t : searchEntryTransformers) {
            try {
                final ObjectPair<SearchResultEntryProtocolOp, Control[]> p = t.transformEntry(messageID, op, c);
                if (p == null) {
                    return;
                }
                op = p.getFirst();
                c = p.getSecond();
            } catch (final Exception e) {
                Debug.debugException(e);
                sendMessage(new LDAPMessage(messageID, protocolOp, c));
                throw new LDAPException(ResultCode.LOCAL_ERROR, ERR_CONN_SEARCH_ENTRY_TRANSFORMER_EXCEPTION.get(t.getClass().getName(), String.valueOf(op), StaticUtils.getExceptionMessage(e)), e);
            }
        }
        sendMessage(new LDAPMessage(messageID, op, c));
    }
}
Also used : Control(com.unboundid.ldap.sdk.Control) LDAPException(com.unboundid.ldap.sdk.LDAPException) LDAPMessage(com.unboundid.ldap.protocol.LDAPMessage) SearchResultEntryProtocolOp(com.unboundid.ldap.protocol.SearchResultEntryProtocolOp) LDAPRuntimeException(com.unboundid.ldap.sdk.LDAPRuntimeException) LDAPException(com.unboundid.ldap.sdk.LDAPException) IOException(java.io.IOException)

Aggregations

SearchResultEntryProtocolOp (com.unboundid.ldap.protocol.SearchResultEntryProtocolOp)6 Control (com.unboundid.ldap.sdk.Control)4 LDAPMessage (com.unboundid.ldap.protocol.LDAPMessage)2 SearchRequestProtocolOp (com.unboundid.ldap.protocol.SearchRequestProtocolOp)2 SearchResultDoneProtocolOp (com.unboundid.ldap.protocol.SearchResultDoneProtocolOp)2 LDAPException (com.unboundid.ldap.sdk.LDAPException)2 SearchRequest (com.unboundid.ldap.sdk.SearchRequest)2 SearchResultEntry (com.unboundid.ldap.sdk.SearchResultEntry)2 Test (org.testng.annotations.Test)2 ASN1Element (com.unboundid.asn1.ASN1Element)1 ASN1OctetString (com.unboundid.asn1.ASN1OctetString)1 ASN1Sequence (com.unboundid.asn1.ASN1Sequence)1 ASN1StreamReader (com.unboundid.asn1.ASN1StreamReader)1 BindRequestProtocolOp (com.unboundid.ldap.protocol.BindRequestProtocolOp)1 BindResponseProtocolOp (com.unboundid.ldap.protocol.BindResponseProtocolOp)1 SearchResultReferenceProtocolOp (com.unboundid.ldap.protocol.SearchResultReferenceProtocolOp)1 UnbindRequestProtocolOp (com.unboundid.ldap.protocol.UnbindRequestProtocolOp)1 Attribute (com.unboundid.ldap.sdk.Attribute)1 Entry (com.unboundid.ldap.sdk.Entry)1 LDAPRuntimeException (com.unboundid.ldap.sdk.LDAPRuntimeException)1