Search in sources :

Example 11 with LdapException

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

the class ApacheLdapProviderImpl method searchImpl.

private Map<String, Map<String, List<String>>> searchImpl(final String baseDN, final SearchHelper searchHelper, final boolean multivalued) throws ChaiUnavailableException, ChaiOperationException {
    try {
        final SearchRequest searchRequest = new SearchRequestImpl();
        searchRequest.setBase(new Dn(baseDN));
        searchRequest.setFilter(searchHelper.getFilter());
        searchRequest.setScope(figureSearchScope(searchHelper.getSearchScope()));
        searchRequest.setSizeLimit(searchHelper.getMaxResults());
        searchRequest.setTimeLimit(searchHelper.getTimeLimit());
        final SearchCursor searchCursor = connection.search(searchRequest);
        final Map<String, Map<String, List<String>>> returnObj = new LinkedHashMap<String, Map<String, List<String>>>();
        while (searchCursor.next()) {
            final Entry entry = searchCursor.getEntry();
            final String dnValue = entry.getDn().getName();
            final Map<String, List<String>> entryMap = new HashMap<String, List<String>>();
            for (Attribute returnAttr : entry) {
                final String attrName = returnAttr.getId();
                final List<String> valueList = new ArrayList<String>();
                if (multivalued) {
                    for (Value value : returnAttr) {
                        valueList.add(value.getString());
                    }
                } else {
                    final String value = returnAttr.iterator().next().getString();
                    valueList.add(value);
                }
                entryMap.put(attrName, Collections.unmodifiableList(valueList));
            }
            returnObj.put(dnValue, Collections.unmodifiableMap(entryMap));
        }
        return Collections.unmodifiableMap(returnObj);
    } catch (CursorException e) {
        throw ChaiOperationException.forErrorMessage(e.getMessage());
    } catch (LdapException e) {
        throw ChaiOperationException.forErrorMessage(e.getMessage());
    }
}
Also used : SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) ArrayList(java.util.ArrayList) SearchRequestImpl(org.apache.directory.api.ldap.model.message.SearchRequestImpl) SearchCursor(org.apache.directory.api.ldap.model.cursor.SearchCursor) Dn(org.apache.directory.api.ldap.model.name.Dn) LinkedHashMap(java.util.LinkedHashMap) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) BinaryValue(org.apache.directory.api.ldap.model.entry.BinaryValue) Value(org.apache.directory.api.ldap.model.entry.Value) StringValue(org.apache.directory.api.ldap.model.entry.StringValue) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 12 with LdapException

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

the class ApacheLdapProviderImpl method writeBinaryAttribute.

public void writeBinaryAttribute(final String entryDN, final String attributeName, final byte[][] values, final boolean overwrite) throws ChaiUnavailableException, ChaiOperationException {
    activityPreCheck();
    getInputValidator().writeBinaryAttribute(entryDN, attributeName, values, overwrite);
    try {
        final ModifyRequest modifyRequest = new ModifyRequestImpl();
        modifyRequest.setName(new Dn(entryDN));
        {
            final Modification modification = new DefaultModification();
            modification.setOperation(overwrite ? ModificationOperation.REPLACE_ATTRIBUTE : ModificationOperation.ADD_ATTRIBUTE);
            modification.setAttribute(new DefaultAttribute(attributeName, values));
            modifyRequest.addModification(modification);
        }
        final ModifyResponse response = connection.modify(modifyRequest);
        processResponse(response);
    } catch (LdapException e) {
        throw ChaiOperationException.forErrorMessage(e.getMessage());
    }
}
Also used : DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) ModifyRequestImpl(org.apache.directory.api.ldap.model.message.ModifyRequestImpl) DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Dn(org.apache.directory.api.ldap.model.name.Dn) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) ModifyResponse(org.apache.directory.api.ldap.model.message.ModifyResponse)

Example 13 with LdapException

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

the class ApacheLdapProviderImpl method extendedOperation.

public ExtendedResponse extendedOperation(final ExtendedRequest request) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
    final org.apache.directory.api.ldap.model.message.ExtendedRequest apacheRequest = new org.apache.directory.api.ldap.model.message.ExtendedRequest() {

        public String getRequestName() {
            return request.getID();
        }

        public org.apache.directory.api.ldap.model.message.ExtendedRequest setRequestName(final String oid) {
            return this;
        }

        public org.apache.directory.api.ldap.model.message.ExtendedRequest setMessageId(final int messageId) {
            return this;
        }

        public org.apache.directory.api.ldap.model.message.ExtendedRequest addControl(final Control control) {
            return null;
        }

        public org.apache.directory.api.ldap.model.message.ExtendedRequest addAllControls(final Control[] controls) {
            return null;
        }

        public org.apache.directory.api.ldap.model.message.ExtendedRequest removeControl(final Control control) {
            return null;
        }

        public MessageTypeEnum getResponseType() {
            return null;
        }

        public ResultResponse getResultResponse() {
            return null;
        }

        public boolean hasResponse() {
            return false;
        }

        public MessageTypeEnum getType() {
            return null;
        }

        public Map<String, Control> getControls() {
            return null;
        }

        public Control getControl(final String oid) {
            return null;
        }

        public boolean hasControl(final String oid) {
            return false;
        }

        public int getMessageId() {
            return 0;
        }

        public Object get(final Object key) {
            return null;
        }

        public Object put(final Object key, final Object value) {
            return null;
        }
    };
    try {
        final org.apache.directory.api.ldap.model.message.ExtendedResponse apacheResponse = connection.extended(apacheRequest);
        final ExtendedResponse extendedResponse = new ExtendedResponse() {

            public String getID() {
                return apacheResponse.getResponseName();
            }

            public byte[] getEncodedValue() {
                return null;
            }
        };
        return extendedResponse;
    } catch (LdapException e) {
        throw ChaiOperationException.forErrorMessage(e.getMessage());
    }
}
Also used : Control(org.apache.directory.api.ldap.model.message.Control) ChaiRequestControl(com.novell.ldapchai.ChaiRequestControl) ExtendedResponse(javax.naming.ldap.ExtendedResponse) ExtendedRequest(javax.naming.ldap.ExtendedRequest) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 14 with LdapException

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

the class ApacheLdapProviderImpl method init.

@Override
public void init(final ChaiConfiguration chaiConfig, final ChaiProviderFactory providerFactory) throws ChaiUnavailableException {
    this.chaiConfig = chaiConfig;
    super.init(chaiConfig, providerFactory);
    // grab the first URL from the list.
    currentLdapUrl = chaiConfig.bindURLsAsList().get(0);
    final URI ldapURL = URI.create(currentLdapUrl);
    final LdapConnectionConfig ldapConnectionConfig = new LdapConnectionConfig();
    ldapConnectionConfig.setLdapHost(ldapURL.getHost());
    ldapConnectionConfig.setLdapPort(ldapURL.getPort());
    if (ldapURL.getScheme().equalsIgnoreCase("ldaps")) {
        ldapConnectionConfig.setUseSsl(true);
        final boolean usePromiscuousSSL = Boolean.parseBoolean(chaiConfig.getSetting(ChaiSetting.PROMISCUOUS_SSL));
        if (usePromiscuousSSL) {
            try {
                final PromiscuousTrustManager promiscuousTrustManager = new PromiscuousTrustManager();
                ldapConnectionConfig.setTrustManagers(promiscuousTrustManager);
            } catch (Exception e) {
                LOGGER.error("error creating promiscuous ssl ldap socket factory: " + e.getMessage());
            }
        } else if (chaiConfig.getTrustManager() != null) {
            try {
                final X509TrustManager[] trustManager = chaiConfig.getTrustManager();
                ldapConnectionConfig.setTrustManagers(trustManager);
            } catch (Exception e) {
                LOGGER.error("error creating configured ssl ldap socket factory: " + e.getMessage());
            }
        }
    }
    final LdapConnection newConnection;
    try {
        newConnection = new LdapNetworkConnection(ldapConnectionConfig);
        newConnection.connect();
        final String bindPassword = chaiConfig.getSetting(ChaiSetting.BIND_PASSWORD);
        final String bindDN = chaiConfig.getSetting(ChaiSetting.BIND_DN);
        newConnection.bind(bindDN, bindPassword);
    } catch (LdapException e) {
        final String message = e.getMessage();
        if (message.contains("Cannot connect on the server")) {
            throw new ChaiUnavailableException(message, ChaiError.COMMUNICATION, false, false);
        }
        throw ChaiUnavailableException.forErrorMessage(message);
    } catch (Exception e) {
        e.printStackTrace();
        final String message = e.getMessage();
        throw new ChaiUnavailableException(message, ChaiError.UNKNOWN, false, false);
    }
    connection = newConnection;
}
Also used : ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) LdapConnectionConfig(org.apache.directory.ldap.client.api.LdapConnectionConfig) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) URI(java.net.URI) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) ChaiOperationException(com.novell.ldapchai.exception.ChaiOperationException) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 15 with LdapException

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

the class ApacheLdapProviderImpl method createEntry.

public void createEntry(final String entryDN, final Set<String> baseObjectClasses, final Map<String, String> stringAttributes) throws ChaiOperationException, ChaiUnavailableException, IllegalStateException {
    activityPreCheck();
    getInputValidator().createEntry(entryDN, baseObjectClasses, stringAttributes);
    try {
        final AddRequest addRequest = new AddRequestImpl();
        final Entry entry = new DefaultEntry();
        entry.setDn(entryDN);
        for (final String baseObjectClass : baseObjectClasses) {
            entry.add(ChaiConstant.ATTR_LDAP_OBJECTCLASS, baseObjectClass);
        }
        for (final Map.Entry<String, String> entryIter : stringAttributes.entrySet()) {
            final String name = entryIter.getKey();
            final String value = entryIter.getValue();
            entry.add(name, value);
        }
        final AddResponse response = connection.add(addRequest);
        processResponse(response);
    } catch (LdapException e) {
        throw ChaiOperationException.forErrorMessage(e.getMessage());
    }
}
Also used : AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) AddRequestImpl(org.apache.directory.api.ldap.model.message.AddRequestImpl) AddResponse(org.apache.directory.api.ldap.model.message.AddResponse) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Aggregations

LdapException (org.apache.directory.api.ldap.model.exception.LdapException)329 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)159 ArrayList (java.util.ArrayList)93 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)91 FinderException (org.apache.directory.fortress.core.FinderException)73 Entry (org.apache.directory.api.ldap.model.entry.Entry)63 Modification (org.apache.directory.api.ldap.model.entry.Modification)63 IOException (java.io.IOException)54 SearchCursor (org.apache.directory.api.ldap.model.cursor.SearchCursor)51 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)50 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)49 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 LdapAuthenticationException (org.apache.directory.api.ldap.model.exception.LdapAuthenticationException)25 AttributeType (org.apache.directory.api.ldap.model.schema.AttributeType)25 DecoderException (org.apache.directory.api.asn1.DecoderException)22 LdapNoPermissionException (org.apache.directory.api.ldap.model.exception.LdapNoPermissionException)22 LdapOtherException (org.apache.directory.api.ldap.model.exception.LdapOtherException)22 ConnectException (java.net.ConnectException)21