Search in sources :

Example 1 with LDAPConstraints

use of com.novell.ldap.LDAPConstraints in project ldapchai by ldapchai.

the class JLDAPProviderImpl method writeBinaryAttribute.

@ChaiProvider.LdapOperation
@ChaiProvider.ModifyOperation
public void writeBinaryAttribute(final String entryDN, final String attribute, final byte[][] values, final boolean overwrite, final ChaiRequestControl[] controls) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
    activityPreCheck();
    getInputValidator().writeBinaryAttribute(entryDN, attribute, values, overwrite);
    final LDAPAttribute ldapAttr = new LDAPAttribute(attribute);
    for (final byte[] value : values) {
        ldapAttr.addValue(value);
    }
    final LDAPModification mod = new LDAPModification(overwrite ? LDAPModification.REPLACE : LDAPModification.ADD, ldapAttr);
    try {
        if (controls != null && controls.length > 0) {
            final LDAPConstraints constraints = new LDAPConstraints();
            constraints.setControls(convertControls(controls));
            ldapConnection.modify(entryDN, mod, constraints);
        } else {
            ldapConnection.modify(entryDN, mod);
        }
    } catch (LDAPException e) {
        throw ChaiOperationException.forErrorMessage(e.getLDAPErrorMessage());
    }
}
Also used : LDAPAttribute(com.novell.ldap.LDAPAttribute) LDAPException(com.novell.ldap.LDAPException) LDAPConstraints(com.novell.ldap.LDAPConstraints) LDAPModification(com.novell.ldap.LDAPModification)

Example 2 with LDAPConstraints

use of com.novell.ldap.LDAPConstraints in project ldapchai by ldapchai.

the class JLDAPProviderImpl method init.

public void init(final ChaiConfiguration chaiConfig, final ChaiProviderFactory providerFactory) throws ChaiUnavailableException, IllegalStateException {
    super.init(chaiConfig, providerFactory);
    try {
        // grab the first URL from the list.
        final URI ldapURL = URI.create(chaiConfig.bindURLsAsList().get(0));
        if (ldapURL.getScheme().equalsIgnoreCase("ldaps")) {
            final boolean usePromiscuousSSL = Boolean.parseBoolean(chaiConfig.getSetting(ChaiSetting.PROMISCUOUS_SSL));
            if (usePromiscuousSSL) {
                try {
                    final SSLContext sc = SSLContext.getInstance("SSL");
                    sc.init(null, new X509TrustManager[] { new PromiscuousTrustManager() }, new java.security.SecureRandom());
                    ldapConnection = new LDAPConnection(new LDAPJSSESecureSocketFactory(sc.getSocketFactory()));
                } catch (Exception e) {
                    LOGGER.error("error creating promiscuous ssl ldap socket factory: " + e.getMessage());
                }
            } else if (chaiConfig.getTrustManager() != null) {
                try {
                    final SSLContext sc = SSLContext.getInstance("SSL");
                    sc.init(null, chaiConfig.getTrustManager(), new java.security.SecureRandom());
                    ldapConnection = new LDAPConnection(new LDAPJSSESecureSocketFactory(sc.getSocketFactory()));
                } catch (Exception e) {
                    LOGGER.error("error creating configured ssl ldap socket factory: " + e.getMessage());
                }
            } else {
                ldapConnection = new LDAPConnection(new LDAPJSSESecureSocketFactory());
            }
        } else {
            ldapConnection = new LDAPConnection();
        }
        ldapConnection.connect(ldapURL.getHost(), ldapURL.getPort());
        if (chaiConfig.getBooleanSetting(ChaiSetting.LDAP_FOLLOW_REFERRALS)) {
            final LDAPConstraints ldapConstraints = new LDAPConstraints();
            ldapConstraints.setReferralFollowing(true);
            ldapConnection.setConstraints(ldapConstraints);
        }
        final String characterEncoding = chaiConfig.getSetting(ChaiSetting.LDAP_CHARACTER_ENCODING);
        final byte[] bindPassword = chaiConfig.getSetting(ChaiSetting.BIND_PASSWORD).getBytes(Charset.forName(characterEncoding));
        final String bindDN = chaiConfig.getSetting(ChaiSetting.BIND_DN);
        ldapConnection.bind(LDAPConnection.LDAP_V3, bindDN, bindPassword);
    } catch (LDAPException e) {
        final String message = e.getMessage();
        if (message.contains("Connect Error")) {
            throw new ChaiUnavailableException(message, ChaiError.COMMUNICATION, false, false);
        }
        throw ChaiUnavailableException.forErrorMessage(message);
    }
}
Also used : ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) SSLContext(javax.net.ssl.SSLContext) LDAPConnection(com.novell.ldap.LDAPConnection) URI(java.net.URI) LDAPException(com.novell.ldap.LDAPException) NamingException(javax.naming.NamingException) ChaiOperationException(com.novell.ldapchai.exception.ChaiOperationException) ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) LDAPException(com.novell.ldap.LDAPException) LDAPConstraints(com.novell.ldap.LDAPConstraints) LDAPJSSESecureSocketFactory(com.novell.ldap.LDAPJSSESecureSocketFactory)

Aggregations

LDAPConstraints (com.novell.ldap.LDAPConstraints)2 LDAPException (com.novell.ldap.LDAPException)2 LDAPAttribute (com.novell.ldap.LDAPAttribute)1 LDAPConnection (com.novell.ldap.LDAPConnection)1 LDAPJSSESecureSocketFactory (com.novell.ldap.LDAPJSSESecureSocketFactory)1 LDAPModification (com.novell.ldap.LDAPModification)1 ChaiOperationException (com.novell.ldapchai.exception.ChaiOperationException)1 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)1 URI (java.net.URI)1 NamingException (javax.naming.NamingException)1 SSLContext (javax.net.ssl.SSLContext)1