use of org.apache.directory.api.ldap.model.message.Control in project ldapchai by ldapchai.
the class ApacheLdapProviderImpl method figureControls.
private static Control[] figureControls(final ChaiRequestControl[] chaiControls) {
final List<Control> returnObj = new ArrayList<Control>();
for (final ChaiRequestControl chaiControl : chaiControls) {
final Control control = new Control() {
public String getOid() {
return chaiControl.getId();
}
public boolean isCritical() {
return chaiControl.isCritical();
}
public void setCritical(final boolean isCritical) {
}
};
returnObj.add(control);
}
return returnObj.toArray(new Control[returnObj.size()]);
}
use of org.apache.directory.api.ldap.model.message.Control in project directory-ldap-api by apache.
the class MessageDecorator method getDecorator.
/**
* Gets the decorator associated with a given message
*
* @param codec The LdapApiService to use
* @param decoratedMessage The message to decorate
* @return The decorator instance
*/
public static MessageDecorator<? extends Message> getDecorator(LdapApiService codec, Message decoratedMessage) {
if (decoratedMessage instanceof MessageDecorator) {
return (MessageDecorator<?>) decoratedMessage;
}
MessageDecorator<?> decorator;
switch(decoratedMessage.getType()) {
case ABANDON_REQUEST:
decorator = new AbandonRequestDecorator(codec, (AbandonRequest) decoratedMessage);
break;
case ADD_REQUEST:
decorator = new AddRequestDecorator(codec, (AddRequest) decoratedMessage);
break;
case ADD_RESPONSE:
decorator = new AddResponseDecorator(codec, (AddResponse) decoratedMessage);
break;
case BIND_REQUEST:
decorator = new BindRequestDecorator(codec, (BindRequest) decoratedMessage);
break;
case BIND_RESPONSE:
decorator = new BindResponseDecorator(codec, (BindResponse) decoratedMessage);
break;
case COMPARE_REQUEST:
decorator = new CompareRequestDecorator(codec, (CompareRequest) decoratedMessage);
break;
case COMPARE_RESPONSE:
decorator = new CompareResponseDecorator(codec, (CompareResponse) decoratedMessage);
break;
case DEL_REQUEST:
decorator = new DeleteRequestDecorator(codec, (DeleteRequest) decoratedMessage);
break;
case DEL_RESPONSE:
decorator = new DeleteResponseDecorator(codec, (DeleteResponse) decoratedMessage);
break;
case EXTENDED_REQUEST:
decorator = codec.decorate((ExtendedRequest) decoratedMessage);
break;
case EXTENDED_RESPONSE:
decorator = codec.decorate((ExtendedResponse) decoratedMessage);
break;
case INTERMEDIATE_RESPONSE:
decorator = new IntermediateResponseDecorator(codec, (IntermediateResponse) decoratedMessage);
break;
case MODIFY_REQUEST:
decorator = new ModifyRequestDecorator(codec, (ModifyRequest) decoratedMessage);
break;
case MODIFY_RESPONSE:
decorator = new ModifyResponseDecorator(codec, (ModifyResponse) decoratedMessage);
break;
case MODIFYDN_REQUEST:
decorator = new ModifyDnRequestDecorator(codec, (ModifyDnRequest) decoratedMessage);
break;
case MODIFYDN_RESPONSE:
decorator = new ModifyDnResponseDecorator(codec, (ModifyDnResponse) decoratedMessage);
break;
case SEARCH_REQUEST:
decorator = new SearchRequestDecorator(codec, (SearchRequest) decoratedMessage);
break;
case SEARCH_RESULT_DONE:
decorator = new SearchResultDoneDecorator(codec, (SearchResultDone) decoratedMessage);
break;
case SEARCH_RESULT_ENTRY:
decorator = new SearchResultEntryDecorator(codec, (SearchResultEntry) decoratedMessage);
break;
case SEARCH_RESULT_REFERENCE:
decorator = new SearchResultReferenceDecorator(codec, (SearchResultReference) decoratedMessage);
break;
case UNBIND_REQUEST:
decorator = new UnbindRequestDecorator(codec, (UnbindRequest) decoratedMessage);
break;
default:
return null;
}
Map<String, Control> controls = decoratedMessage.getControls();
if (controls != null) {
for (Control control : controls.values()) {
decorator.addControl(control);
}
}
return decorator;
}
use of org.apache.directory.api.ldap.model.message.Control in project directory-ldap-api by apache.
the class StoreControlCriticality method action.
/**
* {@inheritDoc}
*/
public void action(LdapMessageContainer<MessageDecorator<? extends Message>> container) throws DecoderException {
TLV tlv = container.getCurrentTLV();
// Get the current control
MessageDecorator<? extends Message> message = container.getMessage();
Control control = message.getCurrentControl();
// Store the criticality
// We get the value. If it's a 0, it's a FALSE. If it's
// a FF, it's a TRUE. Any other value should be an error,
// but we could relax this constraint. So if we have
// something
// which is not 0, it will be interpreted as TRUE, but we
// will generate a warning.
BerValue value = tlv.getValue();
try {
control.setCritical(BooleanDecoder.parse(value));
} catch (BooleanDecoderException bde) {
LOG.error(I18n.err(I18n.ERR_04100_BAD_CONTROL_CRITICALITY, Strings.dumpBytes(value.getData()), bde.getMessage()));
// This will generate a PROTOCOL_ERROR
throw new DecoderException(bde.getMessage(), bde);
}
// We can have an END transition
container.setGrammarEndAllowed(true);
if (IS_DEBUG) {
LOG.debug("Control criticality : {}", control.isCritical());
}
}
use of org.apache.directory.api.ldap.model.message.Control in project directory-ldap-api by apache.
the class LdapNetworkConnection method bindSasl.
/**
* Process the SASL Bind. It's a dialog with the server, we will send a first BindRequest, receive
* a response and the, if this response is a challenge, continue by sending a new BindRequest with
* the requested informations.
*
* @param saslRequest The SASL request object containing all the needed parameters
* @return A {@link BindResponse} containing the result
* @throws LdapException if some error occurred
*/
public BindFuture bindSasl(SaslRequest saslRequest) throws LdapException {
// First switch to anonymous state
authenticated.set(false);
// try to connect, if we aren't already connected.
connect();
// If the session has not been establish, or is closed, we get out immediately
checkSession();
BindRequest bindRequest = createBindRequest((String) null, null, saslRequest.getSaslMechanism(), saslRequest.getControls());
// Update the messageId
int newId = messageId.incrementAndGet();
bindRequest.setMessageId(newId);
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03205_SENDING_REQUEST, bindRequest));
}
// Create a future for this Bind operation
BindFuture bindFuture = new BindFuture(this, newId);
// Store it in the future Map
addToFutureMap(newId, bindFuture);
try {
BindResponse bindResponse;
byte[] response;
ResultCodeEnum result;
// Creating a map for SASL properties
Map<String, Object> properties = new HashMap<>();
// Quality of Protection SASL property
if (saslRequest.getQualityOfProtection() != null) {
properties.put(Sasl.QOP, saslRequest.getQualityOfProtection().getValue());
}
// Security Strength SASL property
if (saslRequest.getSecurityStrength() != null) {
properties.put(Sasl.STRENGTH, saslRequest.getSecurityStrength().getValue());
}
// Mutual Authentication SASL property
if (saslRequest.isMutualAuthentication()) {
properties.put(Sasl.SERVER_AUTH, "true");
}
// Creating a SASL Client
SaslClient sc = Sasl.createSaslClient(new String[] { bindRequest.getSaslMechanism() }, saslRequest.getAuthorizationId(), "ldap", config.getLdapHost(), properties, new SaslCallbackHandler(saslRequest));
// for the requested mechanism. We then produce an Exception
if (sc == null) {
String message = "Cannot find a SASL factory for the " + bindRequest.getSaslMechanism() + " mechanism";
LOG.error(message);
throw new LdapException(message);
}
// deal with it immediately.
if (sc.hasInitialResponse()) {
byte[] challengeResponse = sc.evaluateChallenge(Strings.EMPTY_BYTES);
// Stores the challenge's response, and send it to the server
bindRequest.setCredentials(challengeResponse);
writeRequest(bindRequest);
// Get the server's response, blocking
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);
}
result = bindResponse.getLdapResult().getResultCode();
} else {
// Copy the bindRequest without setting the credentials
BindRequest bindRequestCopy = new BindRequestImpl();
bindRequestCopy.setMessageId(newId);
bindRequestCopy.setName(bindRequest.getName());
bindRequestCopy.setSaslMechanism(bindRequest.getSaslMechanism());
bindRequestCopy.setSimple(bindRequest.isSimple());
bindRequestCopy.setVersion3(bindRequest.getVersion3());
bindRequestCopy.addAllControls(bindRequest.getControls().values().toArray(new Control[0]));
writeRequest(bindRequestCopy);
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);
}
result = bindResponse.getLdapResult().getResultCode();
}
while (!sc.isComplete() && ((result == ResultCodeEnum.SASL_BIND_IN_PROGRESS) || (result == ResultCodeEnum.SUCCESS))) {
response = sc.evaluateChallenge(bindResponse.getServerSaslCreds());
if (result == ResultCodeEnum.SUCCESS) {
if (response != null) {
throw new LdapException("protocol error");
}
} else {
newId = messageId.incrementAndGet();
bindRequest.setMessageId(newId);
bindRequest.setCredentials(response);
addToFutureMap(newId, bindFuture);
writeRequest(bindRequest);
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);
}
result = bindResponse.getLdapResult().getResultCode();
}
}
bindFuture.set(bindResponse);
return bindFuture;
} catch (LdapException e) {
throw e;
} catch (Exception e) {
LOG.error(e.getMessage());
throw new LdapException(e);
}
}
use of org.apache.directory.api.ldap.model.message.Control in project directory-ldap-api by apache.
the class WrappedPartialResultException method toJndiControls.
/**
* Convert some LDAP API controls to JNDI controls
* @param codec The LDAP API service to use
* @param controls The controls to convert
* @return Array of JNDI control
* @throws EncoderException If the conversion failed
* @deprecated We don't use JNDI anymore
*/
@Deprecated
public static javax.naming.ldap.Control[] toJndiControls(LdapApiService codec, Control... controls) throws EncoderException {
if (controls != null) {
javax.naming.ldap.Control[] jndiControls = new javax.naming.ldap.Control[controls.length];
int i = 0;
for (Control control : controls) {
jndiControls[i++] = toJndiControl(codec, control);
}
return jndiControls;
} else {
return null;
}
}
Aggregations