Search in sources :

Example 56 with LdapException

use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.

the class LdapNetworkConnection method loadSchema.

/**
 * loads schema using the specified schema loader
 *
 * @param loader the {@link SchemaLoader} to be used to load schema
 * @throws LdapException If the schema loading failed
 */
public void loadSchema(SchemaLoader loader) throws LdapException {
    try {
        SchemaManager tmp = new DefaultSchemaManager(loader);
        tmp.loadAllEnabled();
        if (!tmp.getErrors().isEmpty() && loader.isStrict()) {
            String msg = I18n.err(I18n.ERR_03204_ERROR_LOADING_SCHEMA);
            if (LOG.isErrorEnabled()) {
                LOG.error("{} {}", msg, Strings.listToString(tmp.getErrors()));
            }
            throw new LdapException(msg);
        }
        schemaManager = tmp;
        // Change the container's BinaryDetector
        ldapSession.setAttribute(LdapDecoder.MESSAGE_CONTAINER_ATTR, new LdapMessageContainer<MessageDecorator<? extends Message>>(codec, new SchemaBinaryAttributeDetector(schemaManager)));
    } catch (LdapException le) {
        throw le;
    } catch (Exception e) {
        LOG.error(I18n.err(I18n.ERR_03205_FAIL_LOAD_SCHEMA), e);
        throw new LdapException(e);
    }
}
Also used : MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) Message(org.apache.directory.api.ldap.model.message.Message) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager) SchemaManager(org.apache.directory.api.ldap.model.schema.SchemaManager) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) SchemaBinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.SchemaBinaryAttributeDetector) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager)

Example 57 with LdapException

use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.

the class LdapNetworkConnection method bind.

/**
 * Bind to the server using a DigestMd5Request object.
 *
 * @param request The DigestMd5Request POJO containing all the needed parameters
 * @return A LdapResponse containing the result
 * @throws LdapException if some error occurred
 */
public BindResponse bind(SaslDigestMd5Request request) throws LdapException {
    if (request == null) {
        String msg = I18n.msg(I18n.MSG_03204_NULL_REQUEST);
        LOG.debug(msg);
        throw new IllegalArgumentException(msg);
    }
    BindFuture bindFuture = bindAsync(request);
    // Get the result from the future
    try {
        // Read the response, waiting for it if not available immediately
        // Get the response, blocking
        BindResponse bindResponse = bindFuture.get(timeout, TimeUnit.MILLISECONDS);
        if (bindResponse == null) {
            // We didn't received anything : this is an error
            if (LOG.isErrorEnabled()) {
                LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Bind"));
            }
            throw new LdapException(TIME_OUT_ERROR);
        }
        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));
            }
        }
        return bindResponse;
    } catch (Exception ie) {
        // Catch all other exceptions
        LOG.error(NO_RESPONSE_ERROR, ie);
        throw new LdapException(NO_RESPONSE_ERROR, ie);
    }
}
Also used : BindFuture(org.apache.directory.ldap.client.api.future.BindFuture) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 58 with LdapException

use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.

the class LdifAnonymizer method anonymizeFile.

/**
 * Anonymize a LDIF
 *
 * @param ldifFile The ldif file to anonymize
 * @param writer The Writer to use to write the result
 * @throws LdapException If we got some LDAP related exception
 * @throws IOException If we had some issue during some IO operations
 */
public void anonymizeFile(String ldifFile, Writer writer) throws LdapException, IOException {
    File inputFile = new File(ldifFile);
    if (!inputFile.exists()) {
        println("Cannot open file " + ldifFile);
        return;
    }
    LdifReader ldifReader = new LdifReader(inputFile, schemaManager);
    int count = 0;
    List<LdifEntry> errors = new ArrayList<>();
    List<String> errorTexts = new ArrayList<>();
    try {
        for (LdifEntry ldifEntry : ldifReader) {
            count++;
            try {
                if (ldifEntry.isEntry() && !ldifEntry.isChangeAdd()) {
                    // process a full entry. Add changes aren't processed here.
                    Entry newEntry = anonymizeEntry(ldifEntry);
                    writer.write(LdifUtils.convertToLdif(newEntry));
                    writer.write("\n");
                } else if (ldifEntry.isChangeDelete()) {
                    // A Delete operation
                    LdifEntry newLdifEntry = anonymizeChangeDelete(ldifEntry);
                    if (ldifEntry != null) {
                        writer.write(newLdifEntry.toString());
                        writer.write("\n");
                    }
                } else if (ldifEntry.isChangeAdd()) {
                    // A Add operation
                    LdifEntry newLdifEntry = anonymizeChangeAdd(ldifEntry);
                    if (ldifEntry != null) {
                        writer.write(newLdifEntry.toString());
                        writer.write("\n");
                    }
                } else if (ldifEntry.isChangeModify()) {
                    // A Modify operation
                    LdifEntry newLdifEntry = anonymizeChangeModify(ldifEntry);
                    if (ldifEntry != null) {
                        writer.write(newLdifEntry.toString());
                        writer.write("\n");
                    }
                } else if (ldifEntry.isChangeModDn() || ldifEntry.isChangeModRdn()) {
                    // A MODDN operation
                    LdifEntry newLdifEntry = anonymizeChangeModDn(ldifEntry);
                    if (ldifEntry != null) {
                        writer.write(newLdifEntry.toString());
                        writer.write("\n");
                    }
                }
                System.out.print('.');
                if (count % 100 == 0) {
                    println();
                }
            } catch (Exception e) {
                System.out.print('*');
                if (count % 100 == 0) {
                    println();
                }
                errors.add(ldifEntry);
                errorTexts.add(e.getMessage());
            }
        }
        println();
        if (!errors.isEmpty()) {
            println("There are " + errors.size() + " bad entries");
            int i = 0;
            for (LdifEntry ldifEntry : errors) {
                println("---------------------------------------------------");
                println("error : " + errorTexts.get(i));
                println(ldifEntry.getDn().toString());
                i++;
            }
        }
    } finally {
        println();
        if (!errors.isEmpty()) {
            println("There are " + errors.size() + " bad entries");
        }
        println("Nb entries : " + count);
        ldifReader.close();
    }
}
Also used : LdifReader(org.apache.directory.api.ldap.model.ldif.LdifReader) 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) ArrayList(java.util.ArrayList) File(java.io.File) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 59 with LdapException

use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.

the class SchemaAwareEntryTest method createEntry.

/**
 * Helper method which creates an entry with 4 attributes.
 */
private Entry createEntry() {
    try {
        Entry entry = new DefaultEntry(exampleDn);
        Attribute attrOC = new DefaultAttribute("objectClass", "top", "person");
        Attribute attrCN = new DefaultAttribute("cn", "test1", "test2");
        Attribute attrSN = new DefaultAttribute("sn", "Test1", "Test2");
        Attribute attrPWD = new DefaultAttribute("userPassword", BYTES1, BYTES2);
        entry.put(attrOC, attrCN, attrSN, attrPWD);
        return entry;
    } catch (LdapException ne) {
        // Do nothing
        return null;
    }
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 60 with LdapException

use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.

the class SearchRequestTest method testRequestWithAttributes1Attribute.

/**
 * Test parsing of a request with an Attributes element with 1 Attribute element
 * @throws NamingException
 */
@Test
public void testRequestWithAttributes1Attribute() throws LdapException {
    Dsmlv2Parser parser = null;
    try {
        parser = newParser();
        parser.setInput(SearchRequestTest.class.getResource("request_with_attributes_1_attribute.xml").openStream(), "UTF-8");
        parser.parse();
    } catch (Exception e) {
        fail(e.getMessage());
    }
    SearchRequest searchRequest = (SearchRequest) parser.getBatchRequest().getCurrentRequest();
    List<String> attributes = searchRequest.getAttributes();
    assertEquals(1, attributes.size());
    String attribute = attributes.get(0);
    assertEquals("sn", attribute);
}
Also used : SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) Dsmlv2Parser(org.apache.directory.api.dsmlv2.Dsmlv2Parser) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Test(org.junit.Test) AbstractTest(org.apache.directory.api.dsmlv2.AbstractTest)

Aggregations

LdapException (org.apache.directory.api.ldap.model.exception.LdapException)361 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)161 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)111 ArrayList (java.util.ArrayList)94 FinderException (org.apache.directory.fortress.core.FinderException)73 Modification (org.apache.directory.api.ldap.model.entry.Modification)70 Entry (org.apache.directory.api.ldap.model.entry.Entry)68 IOException (java.io.IOException)57 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)57 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)53 SearchCursor (org.apache.directory.api.ldap.model.cursor.SearchCursor)51 UpdateException (org.apache.directory.fortress.core.UpdateException)41 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)35 Dn (org.apache.directory.api.ldap.model.name.Dn)34 SEPASecurityException (it.unibo.arces.wot.sepa.commons.exceptions.SEPASecurityException)29 LdapAuthenticationException (org.apache.directory.api.ldap.model.exception.LdapAuthenticationException)25 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)25 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)23 DecoderException (org.apache.directory.api.asn1.DecoderException)22 LdapNoPermissionException (org.apache.directory.api.ldap.model.exception.LdapNoPermissionException)22