use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.
the class LdapNetworkConnection method lookup.
/**
* {@inheritDoc}
*/
@Override
public Entry lookup(Dn dn, Control[] controls, String... attributes) throws LdapException {
Entry entry = null;
try {
SearchRequest searchRequest = new SearchRequestImpl();
searchRequest.setBase(dn);
searchRequest.setFilter(LdapConstants.OBJECT_CLASS_STAR);
searchRequest.setScope(SearchScope.OBJECT);
searchRequest.addAttributes(attributes);
searchRequest.setDerefAliases(AliasDerefMode.DEREF_ALWAYS);
if ((controls != null) && (controls.length > 0)) {
searchRequest.addAllControls(controls);
}
try (Cursor<Response> cursor = search(searchRequest)) {
// Read the response
if (cursor.next()) {
// cursor will always hold SearchResultEntry objects cause there is no ManageDsaITControl passed with search request
entry = ((SearchResultEntry) cursor.get()).getEntry();
}
// Pass through the SaerchResultDone, or stop
// if we have other responses
cursor.next();
}
} catch (CursorException e) {
throw new LdapException(e.getMessage(), e);
} catch (IOException ioe) {
throw new LdapException(ioe.getMessage(), ioe);
}
return entry;
}
use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.
the class LdapNetworkConnection method bind.
/**
* {@inheritDoc}
*/
@Override
public BindResponse bind(BindRequest bindRequest) throws LdapException {
if (bindRequest == null) {
String msg = "Cannot process a null bindRequest";
LOG.debug(msg);
throw new IllegalArgumentException(msg);
}
BindFuture bindFuture = bindAsync(bindRequest);
// 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);
}
}
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);
}
}
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);
}
}
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();
}
}
Aggregations