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());
}
}
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());
}
}
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());
}
}
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;
}
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());
}
}
Aggregations