use of org.apache.directory.api.ldap.model.exception.LdapOtherException in project directory-ldap-api by apache.
the class DefaultSchemaManager method addSchemaObjects.
private void addSchemaObjects(Schema schema, Registries registries) throws LdapException {
// Create a content container for this schema
registries.addSchema(schema.getSchemaName());
schemaMap.put(schema.getSchemaName(), schema);
// And inject any existing SchemaObject into the registries
try {
addComparators(schema, registries);
addNormalizers(schema, registries);
addSyntaxCheckers(schema, registries);
addSyntaxes(schema, registries);
addMatchingRules(schema, registries);
addAttributeTypes(schema, registries);
addObjectClasses(schema, registries);
// addMatchingRuleUses( schema, registries );
// addDitContentRules( schema, registries );
// addNameForms( schema, registries );
// addDitStructureRules( schema, registries );
} catch (IOException ioe) {
throw new LdapOtherException(ioe.getMessage(), ioe);
}
}
use of org.apache.directory.api.ldap.model.exception.LdapOtherException in project directory-ldap-api by apache.
the class LdapNetworkConnection method connect.
// -------------------------- The methods ---------------------------//
/**
* {@inheritDoc}
*/
@Override
public boolean connect() throws LdapException {
if ((ldapSession != null) && connected.get()) {
// No need to connect if we already have a connected session
return true;
}
// Create the connector if needed
if (connector == null) {
createConnector();
}
// Build the connection address
SocketAddress address = new InetSocketAddress(config.getLdapHost(), config.getLdapPort());
// And create the connection future
timeout = config.getTimeout();
long maxRetry = System.currentTimeMillis() + timeout;
ConnectFuture connectionFuture = null;
boolean interrupted = false;
while (maxRetry > System.currentTimeMillis() && !interrupted) {
connectionFuture = connector.connect(address);
boolean result = false;
// Wait until it's established
try {
result = connectionFuture.await(timeout);
} catch (InterruptedException e) {
connector.dispose();
connector = null;
LOG.debug(I18n.msg(I18n.MSG_03221_INTERRUPTED_WAITING_FOR_CONNECTION, config.getLdapHost(), config.getLdapPort()), e);
interrupted = true;
throw new LdapOtherException(e.getMessage(), e);
} finally {
if (result) {
boolean isConnected = connectionFuture.isConnected();
if (!isConnected) {
Throwable connectionException = connectionFuture.getException();
if ((connectionException instanceof ConnectException) || (connectionException instanceof UnresolvedAddressException)) {
// We know that there was a permanent error such as "connection refused".
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03245_CONNECTION_ERROR, connectionFuture.getException().getMessage()));
}
}
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03244_CONNECTION_RETRYING));
}
// Wait 500 ms and retry
try {
Thread.sleep(500);
} catch (InterruptedException e) {
connector = null;
LOG.debug(I18n.msg(I18n.MSG_03221_INTERRUPTED_WAITING_FOR_CONNECTION, config.getLdapHost(), config.getLdapPort()), e);
interrupted = true;
throw new LdapOtherException(e.getMessage(), e);
}
} else {
break;
}
}
}
}
if (connectionFuture == null) {
connector.dispose();
throw new InvalidConnectionException("Cannot connect");
}
boolean isConnected = connectionFuture.isConnected();
if (!isConnected) {
// disposing connector if not connected
try {
close();
} catch (IOException ioe) {
// Nothing to do
}
Throwable e = connectionFuture.getException();
if (e != null) {
StringBuilder message = new StringBuilder("Cannot connect to the server: ");
// (most of the time no message is associated with this exception)
if ((e instanceof UnresolvedAddressException) && (e.getMessage() == null)) {
message.append("Hostname '");
message.append(config.getLdapHost());
message.append("' could not be resolved.");
throw new InvalidConnectionException(message.toString(), e);
}
// Default case
message.append(e.getMessage());
throw new InvalidConnectionException(message.toString(), e);
}
return false;
}
// Get the close future for this session
CloseFuture closeFuture = connectionFuture.getSession().getCloseFuture();
// Add a listener to close the session in the session.
closeFuture.addListener(new IoFutureListener<IoFuture>() {
@Override
public void operationComplete(IoFuture future) {
// Process all the waiting operations and cancel them
LOG.debug(I18n.msg(I18n.MSG_03238_NOD_RECEIVED));
for (ResponseFuture<?> responseFuture : futureMap.values()) {
LOG.debug(I18n.msg(I18n.MSG_03235_CLOSING, responseFuture));
responseFuture.cancel();
try {
if (responseFuture instanceof AddFuture) {
((AddFuture) responseFuture).set(AddNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof BindFuture) {
((BindFuture) responseFuture).set(BindNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof CompareFuture) {
((CompareFuture) responseFuture).set(CompareNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof DeleteFuture) {
((DeleteFuture) responseFuture).set(DeleteNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof ExtendedFuture) {
((ExtendedFuture) responseFuture).set(ExtendedNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof ModifyFuture) {
((ModifyFuture) responseFuture).set(ModifyNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof ModifyDnFuture) {
((ModifyDnFuture) responseFuture).set(ModifyDnNoDResponse.PROTOCOLERROR);
} else if (responseFuture instanceof SearchFuture) {
((SearchFuture) responseFuture).set(SearchNoDResponse.PROTOCOLERROR);
}
} catch (InterruptedException e) {
LOG.error(I18n.err(I18n.ERR_03202_ERROR_PROCESSING_NOD, responseFuture), e);
}
futureMap.remove(messageId.get());
}
futureMap.clear();
}
});
// Get back the session
ldapSession = connectionFuture.getSession();
connected.set(true);
// Store the container into the session if we don't have one
@SuppressWarnings("unchecked") LdapMessageContainer<MessageDecorator<? extends Message>> container = (LdapMessageContainer<MessageDecorator<? extends Message>>) ldapSession.getAttribute(LdapDecoder.MESSAGE_CONTAINER_ATTR);
if (container != null) {
if ((schemaManager != null) && !(container.getBinaryAttributeDetector() instanceof SchemaBinaryAttributeDetector)) {
container.setBinaryAttributeDetector(new SchemaBinaryAttributeDetector(schemaManager));
}
} else {
BinaryAttributeDetector atDetector = new DefaultConfigurableBinaryAttributeDetector();
if (schemaManager != null) {
atDetector = new SchemaBinaryAttributeDetector(schemaManager);
}
ldapSession.setAttribute(LdapDecoder.MESSAGE_CONTAINER_ATTR, new LdapMessageContainer<MessageDecorator<? extends Message>>(codec, atDetector));
}
// Initialize the MessageId
messageId.set(0);
// And return
return true;
}
use of org.apache.directory.api.ldap.model.exception.LdapOtherException in project directory-ldap-api by apache.
the class WrappedPartialResultException method wrap.
/**
* Wraps a LDAP exception into a NaingException
*
* @param t The original exception
* @throws NamingException The wrapping JNDI exception
*/
public static void wrap(Throwable t) throws NamingException {
if (t instanceof NamingException) {
throw (NamingException) t;
}
NamingException ne;
if ((t instanceof LdapAffectMultipleDsaException) || (t instanceof LdapAliasDereferencingException) || (t instanceof LdapLoopDetectedException) || (t instanceof LdapAliasException) || (t instanceof LdapOperationErrorException) || (t instanceof LdapOtherException)) {
ne = new NamingException(t.getLocalizedMessage());
} else if (t instanceof LdapAttributeInUseException) {
ne = new AttributeInUseException(t.getLocalizedMessage());
} else if (t instanceof LdapAuthenticationException) {
ne = new AuthenticationException(t.getLocalizedMessage());
} else if (t instanceof LdapAuthenticationNotSupportedException) {
ne = new AuthenticationNotSupportedException(t.getLocalizedMessage());
} else if (t instanceof LdapContextNotEmptyException) {
ne = new ContextNotEmptyException(t.getLocalizedMessage());
} else if (t instanceof LdapEntryAlreadyExistsException) {
ne = new NameAlreadyBoundException(t.getLocalizedMessage());
} else if (t instanceof LdapInvalidAttributeTypeException) {
ne = new InvalidAttributeIdentifierException(t.getLocalizedMessage());
} else if (t instanceof LdapInvalidAttributeValueException) {
ne = new InvalidAttributeValueException(t.getLocalizedMessage());
} else if (t instanceof LdapInvalidDnException) {
ne = new InvalidNameException(t.getLocalizedMessage());
} else if (t instanceof LdapInvalidSearchFilterException) {
ne = new InvalidSearchFilterException(t.getLocalizedMessage());
} else if (t instanceof LdapNoPermissionException) {
ne = new NoPermissionException(t.getLocalizedMessage());
} else if (t instanceof LdapNoSuchAttributeException) {
ne = new NoSuchAttributeException(t.getLocalizedMessage());
} else if (t instanceof LdapNoSuchObjectException) {
ne = new NameNotFoundException(t.getLocalizedMessage());
} else if (t instanceof LdapProtocolErrorException) {
ne = new CommunicationException(t.getLocalizedMessage());
} else if (t instanceof LdapReferralException) {
ne = new WrappedReferralException((LdapReferralException) t);
} else if (t instanceof LdapPartialResultException) {
ne = new WrappedPartialResultException((LdapPartialResultException) t);
} else if (t instanceof LdapSchemaViolationException) {
ne = new SchemaViolationException(t.getLocalizedMessage());
} else if (t instanceof LdapServiceUnavailableException) {
ne = new ServiceUnavailableException(t.getLocalizedMessage());
} else if (t instanceof LdapTimeLimitExceededException) {
ne = new TimeLimitExceededException(t.getLocalizedMessage());
} else if (t instanceof LdapUnwillingToPerformException) {
ne = new OperationNotSupportedException(t.getLocalizedMessage());
} else {
ne = new NamingException(t.getLocalizedMessage());
}
ne.setRootCause(t);
throw ne;
}
use of org.apache.directory.api.ldap.model.exception.LdapOtherException in project directory-ldap-api by apache.
the class DefaultSchemaManager method cloneRegistries.
// -----------------------------------------------------------------------
// Helper methods
// -----------------------------------------------------------------------
/**
* Clone the registries before doing any modification on it. Relax it
* too so that we can update it.
*/
private Registries cloneRegistries() throws LdapException {
try {
// Relax the controls at first
errors = new ArrayList<>();
// Clone the Registries
Registries clonedRegistries = registries.clone();
// And update references. We may have errors, that may be fixed
// by the new loaded schemas.
errors = clonedRegistries.checkRefInteg();
// Now, relax the cloned Registries if there is no error
clonedRegistries.setRelaxed();
return clonedRegistries;
} catch (CloneNotSupportedException cnse) {
throw new LdapOtherException(cnse.getMessage(), cnse);
}
}
use of org.apache.directory.api.ldap.model.exception.LdapOtherException in project directory-ldap-api by apache.
the class DefaultSchemaManager method delete.
/**
* {@inheritDoc}
*/
@Override
public boolean delete(SchemaObject schemaObject) throws LdapException {
// First, clear the errors
errors.clear();
if (registries.isRelaxed()) {
// Apply the addition right away
registries.delete(errors, schemaObject);
return errors.isEmpty();
} else {
// The new schemaObject's OID must exist
if (!checkOidExist(schemaObject)) {
Throwable error = new LdapProtocolErrorException(I18n.err(I18n.ERR_16039_OID_DOES_NOT_EXIST, schemaObject.getOid()));
errors.add(error);
return false;
}
// Get the SchemaObject to delete if it's not a LoadableSchemaObject
SchemaObject toDelete = getSchemaObject(schemaObject);
// First check that this SchemaObject does not have any referencing SchemaObjects
Set<SchemaObjectWrapper> referencing = registries.getReferencing(toDelete);
if ((referencing != null) && !referencing.isEmpty()) {
String msg = I18n.err(I18n.ERR_16040_CANNOT_REMOVE_FROM_REGISTRY, schemaObject.getOid(), Strings.setToString(referencing));
Throwable error = new LdapProtocolErrorException(msg);
errors.add(error);
return false;
}
String schemaName = getSchemaName(toDelete);
// At this point, the deleted AttributeType may be referenced, it will be checked
// there, if the schema and the AttributeType are both enabled.
Schema schema = getLoadedSchema(schemaName);
if (schema == null) {
// The SchemaObject must be associated with an existing schema
String msg = I18n.err(I18n.ERR_16041_CANNOT_DELETE_SCHEMA_OBJECT, schemaObject.getOid());
LOG.info(msg);
Throwable error = new LdapProtocolErrorException(msg);
errors.add(error);
return false;
}
if (schema.isEnabled() && schemaObject.isEnabled()) {
// As we may break the registries, work on a cloned registries
Registries clonedRegistries = null;
try {
clonedRegistries = registries.clone();
} catch (CloneNotSupportedException cnse) {
throw new LdapOtherException(cnse.getMessage(), cnse);
}
// Delete the SchemaObject from the cloned registries
clonedRegistries.delete(errors, toDelete);
// Remove the cloned registries
clonedRegistries.clear();
// If we didn't get any error, apply the deletion to the real retistries
if (errors.isEmpty()) {
// Apply the deletion to the real registries
registries.delete(errors, toDelete);
LOG.debug(I18n.msg(I18n.MSG_16022_REMOVED_FROM_ENABLED_SCHEMA, toDelete.getName(), schemaName));
return true;
} else {
// We have some error : reject the deletion and get out
LOG.info(I18n.msg(I18n.MSG_16023_CANNOT_DELETE_SCHEMAOBJECT, schemaObject.getOid(), Strings.listToString(errors)));
return false;
}
} else {
// At least, we register the OID in the globalOidRegistry, and associates it with the
// schema
registries.associateWithSchema(errors, schemaObject);
LOG.debug(I18n.msg(I18n.MSG_16024_REMOVED_FROM_DISABLED_SCHEMA, schemaObject.getName(), schemaName));
return errors.isEmpty();
}
}
}
Aggregations