Search in sources :

Example 11 with DefaultEntry

use of org.apache.directory.api.ldap.model.entry.DefaultEntry in project directory-ldap-api by apache.

the class LdapNetworkConnection method messageReceived.

/**
 * Handle the incoming LDAP messages. This is where we feed the cursor for search
 * requests, or call the listener.
 *
 * @param session The session that received a message
 * @param message The received message
 * @throws Exception If there is some error while processing the message
 */
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
    // Feed the response and store it into the session
    if (message instanceof SslFilter.SslFilterMessage) {
        // This is a SSL message telling if the session has been secured or not
        HandshakeFuture handshakeFuture = (HandshakeFuture) ldapSession.getAttribute("HANDSHAKE_FUTURE");
        if (message == SslFilter.SESSION_SECURED) {
            // SECURED
            handshakeFuture.secured();
        } else {
            // UNSECURED
            handshakeFuture.cancel();
        }
        ldapSession.removeAttribute("HANDSHAKE_FUTURE");
        return;
    }
    Message response = (Message) message;
    if (LOG.isDebugEnabled()) {
        LOG.debug(I18n.msg(I18n.MSG_03243_MESSAGE_RECEIVED, response));
    }
    int messageId = response.getMessageId();
    // this check is necessary to prevent adding an abandoned operation's
    // result(s) to corresponding queue
    ResponseFuture<? extends Response> responseFuture = peekFromFutureMap(messageId);
    boolean isNoD = isNoticeOfDisconnect(response);
    if ((responseFuture == null) && !isNoD) {
        LOG.info("There is no future associated with the messageId {}, ignoring the message", messageId);
        return;
    }
    if (isNoD) {
        // close the session
        session.closeNow();
        return;
    }
    switch(response.getType()) {
        case ADD_RESPONSE:
            // Transform the response
            AddResponse addResponse = (AddResponse) response;
            AddFuture addFuture = (AddFuture) responseFuture;
            // remove the listener from the listener map
            if (LOG.isDebugEnabled()) {
                if (addResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
                    // Everything is fine, return the response
                    LOG.debug(I18n.msg(I18n.MSG_03209_ADD_SUCCESSFUL, addResponse));
                } else {
                    // We have had an error
                    LOG.debug(I18n.msg(I18n.MSG_03208_ADD_FAILED, addResponse));
                }
            }
            // Store the response into the future
            addFuture.set(addResponse);
            // Remove the future from the map
            removeFromFutureMaps(messageId);
            break;
        case BIND_RESPONSE:
            // Transform the response
            BindResponse bindResponse = (BindResponse) response;
            BindFuture bindFuture = (BindFuture) responseFuture;
            // remove the listener from the listener map
            if (bindResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
                authenticated.set(true);
                // Everything is fine, return the response
                if (LOG.isDebugEnabled()) {
                    LOG.debug(I18n.msg(I18n.MSG_03202_BIND_SUCCESSFUL, bindResponse));
                }
            } else {
                // We have had an error
                if (LOG.isDebugEnabled()) {
                    LOG.debug(I18n.msg(I18n.MSG_03201_BIND_FAIL, bindResponse));
                }
            }
            // Store the response into the future
            bindFuture.set(bindResponse);
            // Remove the future from the map
            removeFromFutureMaps(messageId);
            break;
        case COMPARE_RESPONSE:
            // Transform the response
            CompareResponse compareResponse = (CompareResponse) response;
            CompareFuture compareFuture = (CompareFuture) responseFuture;
            // remove the listener from the listener map
            if (LOG.isDebugEnabled()) {
                if (compareResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
                    // Everything is fine, return the response
                    LOG.debug(I18n.msg(I18n.MSG_03215_COMPARE_SUCCESSFUL, compareResponse));
                } else {
                    // We have had an error
                    LOG.debug(I18n.msg(I18n.MSG_03214_COMPARE_FAILED, compareResponse));
                }
            }
            // Store the response into the future
            compareFuture.set(compareResponse);
            // Remove the future from the map
            removeFromFutureMaps(messageId);
            break;
        case DEL_RESPONSE:
            // Transform the response
            DeleteResponse deleteResponse = (DeleteResponse) response;
            DeleteFuture deleteFuture = (DeleteFuture) responseFuture;
            if (LOG.isDebugEnabled()) {
                if (deleteResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
                    // Everything is fine, return the response
                    LOG.debug(I18n.msg(I18n.MSG_03217_DELETE_SUCCESSFUL, deleteResponse));
                } else {
                    // We have had an error
                    LOG.debug(I18n.msg(I18n.MSG_03216_DELETE_FAILED, deleteResponse));
                }
            }
            // Store the response into the future
            deleteFuture.set(deleteResponse);
            // Remove the future from the map
            removeFromFutureMaps(messageId);
            break;
        case EXTENDED_RESPONSE:
            // Transform the response
            ExtendedResponse extendedResponse = (ExtendedResponse) response;
            ExtendedFuture extendedFuture = (ExtendedFuture) responseFuture;
            // remove the listener from the listener map
            if (LOG.isDebugEnabled()) {
                if (extendedResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
                    // Everything is fine, return the response
                    LOG.debug(I18n.msg(I18n.MSG_03219_EXTENDED_SUCCESSFUL, extendedResponse));
                } else {
                    // We have had an error
                    LOG.debug(I18n.msg(I18n.MSG_03218_EXTENDED_FAILED, extendedResponse));
                }
            }
            // Store the response into the future
            extendedFuture.set(extendedResponse);
            // Remove the future from the map
            removeFromFutureMaps(messageId);
            break;
        case INTERMEDIATE_RESPONSE:
            IntermediateResponse intermediateResponse;
            if (responseFuture instanceof SearchFuture) {
                intermediateResponse = new IntermediateResponseImpl(messageId);
                addControls(intermediateResponse, response);
                ((SearchFuture) responseFuture).set(intermediateResponse);
            } else if (responseFuture instanceof ExtendedFuture) {
                intermediateResponse = new IntermediateResponseImpl(messageId);
                addControls(intermediateResponse, response);
                ((ExtendedFuture) responseFuture).set(intermediateResponse);
            } else {
                // currently we only support IR for search and extended operations
                throw new UnsupportedOperationException("Unknown ResponseFuture type " + responseFuture.getClass().getName());
            }
            intermediateResponse.setResponseName(((IntermediateResponse) response).getResponseName());
            intermediateResponse.setResponseValue(((IntermediateResponse) response).getResponseValue());
            break;
        case MODIFY_RESPONSE:
            // Transform the response
            ModifyResponse modifyResponse = (ModifyResponse) response;
            ModifyFuture modifyFuture = (ModifyFuture) responseFuture;
            if (LOG.isDebugEnabled()) {
                if (modifyResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
                    // Everything is fine, return the response
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(I18n.msg(I18n.MSG_03224_MODIFY_SUCCESSFUL, modifyResponse));
                    }
                } else {
                    // We have had an error
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(I18n.msg(I18n.MSG_03223_MODIFY_FAILED, modifyResponse));
                    }
                }
            }
            // Store the response into the future
            modifyFuture.set(modifyResponse);
            // Remove the future from the map
            removeFromFutureMaps(messageId);
            break;
        case MODIFYDN_RESPONSE:
            // Transform the response
            ModifyDnResponse modifyDnResponse = (ModifyDnResponse) response;
            ModifyDnFuture modifyDnFuture = (ModifyDnFuture) responseFuture;
            if (LOG.isDebugEnabled()) {
                if (modifyDnResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
                    // Everything is fine, return the response
                    LOG.debug(I18n.msg(I18n.MSG_03226_MODIFYDN_SUCCESSFUL, modifyDnResponse));
                } else {
                    // We have had an error
                    LOG.debug(I18n.msg(I18n.MSG_03225_MODIFYDN_FAILED, modifyDnResponse));
                }
            }
            // Store the response into the future
            modifyDnFuture.set(modifyDnResponse);
            // Remove the future from the map
            removeFromFutureMaps(messageId);
            break;
        case SEARCH_RESULT_DONE:
            // Store the response into the responseQueue
            SearchResultDone searchResultDone = (SearchResultDone) response;
            SearchFuture searchFuture = (SearchFuture) responseFuture;
            if (LOG.isDebugEnabled()) {
                if (searchResultDone.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
                    // Everything is fine, return the response
                    LOG.debug(I18n.msg(I18n.MSG_03232_SEARCH_SUCCESSFUL, searchResultDone));
                } else {
                    // We have had an error
                    LOG.debug(I18n.msg(I18n.MSG_03230_SEARCH_FAILED, searchResultDone));
                }
            }
            // Store the response into the future
            searchFuture.set(searchResultDone);
            // Remove the future from the map
            removeFromFutureMaps(messageId);
            break;
        case SEARCH_RESULT_ENTRY:
            // Store the response into the responseQueue
            SearchResultEntry searchResultEntry = (SearchResultEntry) response;
            if (schemaManager != null) {
                searchResultEntry.setEntry(new DefaultEntry(schemaManager, searchResultEntry.getEntry()));
            }
            searchFuture = (SearchFuture) responseFuture;
            if (LOG.isDebugEnabled()) {
                LOG.debug(I18n.msg(I18n.MSG_03229_SEARCH_ENTRY_FOUND, searchResultEntry));
            }
            // Store the response into the future
            searchFuture.set(searchResultEntry);
            break;
        case SEARCH_RESULT_REFERENCE:
            // Store the response into the responseQueue
            SearchResultReference searchResultReference = (SearchResultReference) response;
            searchFuture = (SearchFuture) responseFuture;
            if (LOG.isDebugEnabled()) {
                LOG.debug(I18n.msg(I18n.MSG_03231_SEARCH_REFERENCE_FOUND, searchResultReference));
            }
            // Store the response into the future
            searchFuture.set(searchResultReference);
            break;
        default:
            throw new IllegalStateException("Unexpected response type " + response.getType());
    }
}
Also used : ModifyFuture(org.apache.directory.ldap.client.api.future.ModifyFuture) HandshakeFuture(org.apache.directory.ldap.client.api.future.HandshakeFuture) Message(org.apache.directory.api.ldap.model.message.Message) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) SearchResultReference(org.apache.directory.api.ldap.model.message.SearchResultReference) IntermediateResponseImpl(org.apache.directory.api.ldap.model.message.IntermediateResponseImpl) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) BindFuture(org.apache.directory.ldap.client.api.future.BindFuture) ModifyDnFuture(org.apache.directory.ldap.client.api.future.ModifyDnFuture) IntermediateResponse(org.apache.directory.api.ldap.model.message.IntermediateResponse) ModifyDnResponse(org.apache.directory.api.ldap.model.message.ModifyDnResponse) CompareFuture(org.apache.directory.ldap.client.api.future.CompareFuture) ModifyResponse(org.apache.directory.api.ldap.model.message.ModifyResponse) CompareResponse(org.apache.directory.api.ldap.model.message.CompareResponse) SearchResultDone(org.apache.directory.api.ldap.model.message.SearchResultDone) ExtendedFuture(org.apache.directory.ldap.client.api.future.ExtendedFuture) AddResponse(org.apache.directory.api.ldap.model.message.AddResponse) DeleteFuture(org.apache.directory.ldap.client.api.future.DeleteFuture) DeleteResponse(org.apache.directory.api.ldap.model.message.DeleteResponse) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) AddFuture(org.apache.directory.ldap.client.api.future.AddFuture) SearchFuture(org.apache.directory.ldap.client.api.future.SearchFuture) SearchResultEntry(org.apache.directory.api.ldap.model.message.SearchResultEntry)

Example 12 with DefaultEntry

use of org.apache.directory.api.ldap.model.entry.DefaultEntry in project directory-ldap-api by apache.

the class LdifAnonymizer method anonymizeEntry.

/**
 * Anonymize the full entry
 */
private Entry anonymizeEntry(LdifEntry ldifEntry) throws LdapException {
    Entry entry = ldifEntry.getEntry();
    Entry newEntry = new DefaultEntry(schemaManager);
    // Process the DN first
    Dn entryDn = entry.getDn();
    Dn anonymizedDn = anonymizeDn(entryDn);
    // Now, process the entry's attributes
    for (Attribute attribute : entry) {
        AttributeType attributeType = attribute.getAttributeType();
        // Deal with the special case of DN
        if (attributeType.getSyntax().getSyntaxChecker() instanceof DnSyntaxChecker) {
            for (Value dnValue : attribute) {
                Dn dn = new Dn(schemaManager, dnValue.getValue());
                Dn newdDn = anonymizeDn(dn);
                newEntry.add(attributeType, newdDn.toString());
            }
        } else // Deal with the special case of a NameAndOptionalUID
        if (attributeType.getSyntax().getSyntaxChecker() instanceof NameAndOptionalUIDSyntaxChecker) {
            for (Value dnValue : attribute) {
                // Get rid of the # part (UID)
                String valueStr = dnValue.getValue();
                int uidPos = valueStr.indexOf('#');
                String uid = null;
                if (uidPos != -1) {
                    uid = valueStr.substring(uidPos + 1);
                    valueStr = valueStr.substring(0, uidPos);
                }
                Dn dn = new Dn(schemaManager, valueStr);
                Dn newDn = anonymizeDn(dn);
                String newDnStr = newDn.toString();
                if (uid != null) {
                    newDnStr = newDnStr + '#' + uid;
                }
                newEntry.add(attributeType, newDnStr);
            }
        } else {
            Anonymizer anonymizer = attributeAnonymizers.get(attribute.getAttributeType().getOid());
            if (anonymizer == null) {
                newEntry.add(attribute);
            } else {
                Attribute anonymizedAttribute = anonymizer.anonymize(valueMap, valueSet, attribute);
                if (anonymizedAttribute != null) {
                    newEntry.add(anonymizedAttribute);
                }
            }
        }
    }
    newEntry.setDn(anonymizedDn);
    return newEntry;
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) NameAndOptionalUIDSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.NameAndOptionalUIDSyntaxChecker) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) TelephoneNumberAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.TelephoneNumberAnonymizer) StringAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.StringAnonymizer) BinaryAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.BinaryAnonymizer) IntegerAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.IntegerAnonymizer) CaseSensitiveStringAnonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.CaseSensitiveStringAnonymizer) Anonymizer(org.apache.directory.api.ldap.model.ldif.anonymizer.Anonymizer) Value(org.apache.directory.api.ldap.model.entry.Value) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Dn(org.apache.directory.api.ldap.model.name.Dn) DnSyntaxChecker(org.apache.directory.api.ldap.model.schema.syntaxCheckers.DnSyntaxChecker)

Example 13 with DefaultEntry

use of org.apache.directory.api.ldap.model.entry.DefaultEntry in project directory-ldap-api by apache.

the class SchemaAwareEntrySerializationTest method testEntryNoDnSerialization.

@Test
public void testEntryNoDnSerialization() throws IOException, LdapException, ClassNotFoundException {
    Entry entry1 = new DefaultEntry(schemaManager, "", "ObjectClass: top", "ObjectClass: domain", "dc: example", "l: test");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    entry1.writeExternal(out);
    ObjectInputStream in = null;
    byte[] data = baos.toByteArray();
    in = new ObjectInputStream(new ByteArrayInputStream(data));
    Entry entry2 = new DefaultEntry(schemaManager);
    entry2.readExternal(in);
    assertEquals(entry1, entry2);
    assertTrue(entry2.contains("ObjectClass", "top", "domain"));
    assertEquals("", entry2.getDn().toString());
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 14 with DefaultEntry

use of org.apache.directory.api.ldap.model.entry.DefaultEntry in project directory-ldap-api by apache.

the class SchemaAwareEntrySerializationTest method testEntryFullSerialization.

@Test
public void testEntryFullSerialization() throws IOException, LdapException, ClassNotFoundException {
    Entry entry1 = new DefaultEntry(schemaManager, "dc=example, dc=com", "ObjectClass: top", "ObjectClass: domain", "dc: example", "l: test");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    entry1.writeExternal(out);
    ObjectInputStream in = null;
    byte[] data = baos.toByteArray();
    in = new ObjectInputStream(new ByteArrayInputStream(data));
    Entry entry2 = new DefaultEntry(schemaManager);
    entry2.readExternal(in);
    assertEquals(entry1, entry2);
    assertTrue(entry2.contains("2.5.4.0", "top", "domain"));
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 15 with DefaultEntry

use of org.apache.directory.api.ldap.model.entry.DefaultEntry in project directory-ldap-api by apache.

the class SchemaAwareEntrySerializationTest method testEntryNoAttributesSerialization.

@Test
public void testEntryNoAttributesSerialization() throws IOException, LdapException, ClassNotFoundException {
    Entry entry1 = new DefaultEntry(schemaManager, "dc=example, dc=com");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    entry1.writeExternal(out);
    ObjectInputStream in = null;
    byte[] data = baos.toByteArray();
    in = new ObjectInputStream(new ByteArrayInputStream(data));
    Entry entry2 = new DefaultEntry(schemaManager);
    entry2.readExternal(in);
    assertEquals(entry1, entry2);
    assertEquals(0, entry2.size());
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)128 Entry (org.apache.directory.api.ldap.model.entry.Entry)116 Test (org.junit.Test)55 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)41 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)39 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)23 LdifEntry (org.apache.directory.api.ldap.model.ldif.LdifEntry)20 Modification (org.apache.directory.api.ldap.model.entry.Modification)16 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)16 Dn (org.apache.directory.api.ldap.model.name.Dn)15 CreateException (org.apache.directory.fortress.core.CreateException)15 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)14 Value (org.apache.directory.api.ldap.model.entry.Value)12 LdifReader (org.apache.directory.api.ldap.model.ldif.LdifReader)12 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)5 IOException (java.io.IOException)4 ObjectInputStream (java.io.ObjectInputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4