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 SaslRequest object.
*
* @param request The SaslRequest POJO containing all the needed parameters
* @return A LdapResponse containing the result
* @throws LdapException if some error occurred
*/
public BindResponse bind(SaslRequest 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 LdapNetworkConnection method modify.
/**
* {@inheritDoc}
*/
@Override
public ModifyResponse modify(ModifyRequest modRequest) throws LdapException {
if (modRequest == null) {
String msg = "Cannot process a null modifyRequest";
LOG.debug(msg);
throw new IllegalArgumentException(msg);
}
ModifyFuture modifyFuture = modifyAsync(modRequest);
// Get the result from the future
try {
// Read the response, waiting for it if not available immediately
// Get the response, blocking
ModifyResponse modifyResponse = modifyFuture.get(timeout, TimeUnit.MILLISECONDS);
if (modifyResponse == null) {
// We didn't received anything : this is an error
if (LOG.isErrorEnabled()) {
LOG.error(I18n.err(I18n.ERR_03203_OP_FAILED_TIMEOUT, "Modify"));
}
throw new LdapException(TIME_OUT_ERROR);
}
if (modifyResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03224_MODIFY_SUCCESSFUL, modifyResponse));
}
} else {
if (modifyResponse instanceof ModifyNoDResponse) {
// A NoticeOfDisconnect : deserves a special treatment
throw new LdapException(modifyResponse.getLdapResult().getDiagnosticMessage());
}
// We have had an error
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03223_MODIFY_FAILED, modifyResponse));
}
}
return modifyResponse;
} catch (Exception ie) {
// Catch all other exceptions
LOG.error(NO_RESPONSE_ERROR, ie);
// Send an abandon request
if (!modifyFuture.isCancelled()) {
abandon(modRequest.getMessageId());
}
throw new LdapException(ie.getMessage(), ie);
}
}
use of org.apache.directory.api.ldap.model.exception.LdapException in project directory-ldap-api by apache.
the class LdapNetworkConnection method writeRequest.
/**
* a reusable code block to be used in various bind methods
*/
private void writeRequest(Request request) throws LdapException {
// Send the request to the server
WriteFuture writeFuture = ldapSession.write(request);
long localTimeout = timeout;
while (localTimeout > 0) {
// Wait only 100 ms
boolean done = writeFuture.awaitUninterruptibly(100);
if (done) {
return;
}
// Wait for the message to be sent to the server
if (!ldapSession.isConnected()) {
// We didn't received anything : this is an error
if (LOG.isErrorEnabled()) {
LOG.error(I18n.err(I18n.ERR_03207_SOMETHING_WRONG_HAPPENED));
}
Exception exception = (Exception) ldapSession.removeAttribute(EXCEPTION_KEY);
if (exception != null) {
if (exception instanceof LdapException) {
throw (LdapException) exception;
} else {
throw new InvalidConnectionException(exception.getMessage(), exception);
}
}
throw new InvalidConnectionException("Error while sending some message : the session has been closed");
}
localTimeout -= 100;
}
if (LOG.isErrorEnabled()) {
LOG.error(I18n.err(I18n.ERR_03208_TIMEOUT));
}
throw new LdapException(TIME_OUT_ERROR);
}
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);
}
}
Aggregations