use of org.apache.directory.api.ldap.model.message.IntermediateResponse in project directory-ldap-api by apache.
the class IntermediateResponseTest method testDecodeIntermediateResponseWithControls.
/**
* Test the decoding of a full IntermediateResponse with controls
*/
@Test
public void testDecodeIntermediateResponseWithControls() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x3A);
stream.put(new byte[] { 0x30, // LDAPMessage ::= SEQUENCE {
0x38, 0x02, 0x01, // messageID MessageID
0x01, // CHOICE { ..., intermediateResponse IntermediateResponse, ...
0x79, // IntermediateResponse ::= [APPLICATION 25] SEQUENCE {
0x16, // responseName [0] LDAPOID,
(byte) 0x80, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // requestValue [1] OCTET STRING OPTIONAL }
(byte) 0x81, 0x05, 'v', 'a', 'l', 'u', 'e', (byte) 0xA0, // A control
0x1B, 0x30, 0x19, 0x04, 0x17, '2', '.', '1', '6', '.', '8', '4', '0', '.', '1', '.', '1', '1', '3', '7', '3', '0', '.', '3', '.', '4', '.', '2' });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<IntermediateResponseDecorator<?>> ldapMessageContainer = new LdapMessageContainer<IntermediateResponseDecorator<?>>(codec);
// Decode the IntermediateResponse PDU
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded IntermediateResponse PDU
IntermediateResponse intermediateResponse = ldapMessageContainer.getMessage();
assertEquals(1, intermediateResponse.getMessageId());
assertEquals("1.3.6.1.5.5.2", intermediateResponse.getResponseName());
assertEquals("value", Strings.utf8ToString(intermediateResponse.getResponseValue()));
// Check the Control
Map<String, Control> controls = intermediateResponse.getControls();
assertEquals(1, controls.size());
@SuppressWarnings("unchecked") CodecControl<Control> control = (org.apache.directory.api.ldap.codec.api.CodecControl<Control>) controls.get("2.16.840.1.113730.3.4.2");
assertEquals("2.16.840.1.113730.3.4.2", control.getOid());
assertEquals("", Strings.dumpBytes((byte[]) control.getValue()));
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(intermediateResponse);
// Check the length
assertEquals(0x3A, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.model.message.IntermediateResponse in project directory-ldap-api by apache.
the class IntermediateResponseTest method testDecodeIntermediateResponseNoName.
/**
* Test the decoding of an IntermediateResponse without name
*/
@Test
public void testDecodeIntermediateResponseNoName() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x0E);
stream.put(new byte[] { // LDAPMessage ::= SEQUENCE {
0x30, // LDAPMessage ::= SEQUENCE {
0x0C, 0x02, 0x01, // messageID MessageID
0x01, // CHOICE { ..., intermediateResponse IntermediateResponse, ...
0x79, // IntermediateResponse ::= [APPLICATION 25] SEQUENCE {
0x07, // responseValue [1] OCTET STRING OPTIONAL,
(byte) 0x81, 0x05, 'v', 'a', 'l', 'u', 'e' });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<IntermediateResponseDecorator<?>> ldapMessageContainer = new LdapMessageContainer<IntermediateResponseDecorator<?>>(codec);
// Decode the IntermediateResponse PDU
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded IntermediateResponse PDU
IntermediateResponse intermediateResponse = ldapMessageContainer.getMessage();
assertEquals(1, intermediateResponse.getMessageId());
assertEquals("", intermediateResponse.getResponseName());
assertEquals("value", Strings.utf8ToString(intermediateResponse.getResponseValue()));
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(intermediateResponse);
// Check the length
assertEquals(0x0E, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.model.message.IntermediateResponse in project directory-ldap-api by apache.
the class IntermediateResponseTest method testDecodeIntermediateResponseSuccess.
/**
* Test the decoding of a full IntermediateResponse
*/
@Test
public void testDecodeIntermediateResponseSuccess() {
Asn1Decoder ldapDecoder = new Asn1Decoder();
ByteBuffer stream = ByteBuffer.allocate(0x1D);
stream.put(new byte[] { // LDAPMessage ::= SEQUENCE {
0x30, // LDAPMessage ::= SEQUENCE {
0x1B, 0x02, 0x01, // messageID MessageID
0x01, // CHOICE { ..., intermediateResponse IntermediateResponse, ...
0x79, // IntermediateResponse ::= [APPLICATION 25] SEQUENCE {
0x16, // responseName [0] LDAPOID,
(byte) 0x80, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2', // responseValue [1] OCTET STRING OPTIONAL }
(byte) 0x81, 0x05, 'v', 'a', 'l', 'u', 'e' });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<IntermediateResponseDecorator<?>> ldapMessageContainer = new LdapMessageContainer<IntermediateResponseDecorator<?>>(codec);
// Decode the IntermediateResponse PDU
try {
ldapDecoder.decode(stream, ldapMessageContainer);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded IntermediateResponse PDU
IntermediateResponse intermediateResponse = ldapMessageContainer.getMessage();
assertEquals(1, intermediateResponse.getMessageId());
assertEquals("1.3.6.1.5.5.2", intermediateResponse.getResponseName());
assertEquals("value", Strings.utf8ToString(intermediateResponse.getResponseValue()));
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(intermediateResponse);
// Check the length
assertEquals(0x1D, bb.limit());
String encodedPdu = Strings.dumpBytes(bb.array());
assertEquals(encodedPdu, decodedPdu);
} catch (EncoderException ee) {
ee.printStackTrace();
fail(ee.getMessage());
}
}
use of org.apache.directory.api.ldap.model.message.IntermediateResponse in project directory-ldap-api by apache.
the class LdapNetworkConnection method messageReceived.
/**
* Handle the incoming LDAP messages. This is where we feed the cursor for search
* requests, or call the listener.
*
* @param session The session that received a message
* @param message The received message
* @throws Exception If there is some error while processing the message
*/
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
// Feed the response and store it into the session
if (message instanceof SslFilter.SslFilterMessage) {
// This is a SSL message telling if the session has been secured or not
HandshakeFuture handshakeFuture = (HandshakeFuture) ldapSession.getAttribute("HANDSHAKE_FUTURE");
if (message == SslFilter.SESSION_SECURED) {
// SECURED
handshakeFuture.secured();
} else {
// UNSECURED
handshakeFuture.cancel();
}
ldapSession.removeAttribute("HANDSHAKE_FUTURE");
return;
}
Message response = (Message) message;
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03243_MESSAGE_RECEIVED, response));
}
int messageId = response.getMessageId();
// this check is necessary to prevent adding an abandoned operation's
// result(s) to corresponding queue
ResponseFuture<? extends Response> responseFuture = peekFromFutureMap(messageId);
boolean isNoD = isNoticeOfDisconnect(response);
if ((responseFuture == null) && !isNoD) {
LOG.info("There is no future associated with the messageId {}, ignoring the message", messageId);
return;
}
if (isNoD) {
// close the session
session.closeNow();
return;
}
switch(response.getType()) {
case ADD_RESPONSE:
// Transform the response
AddResponse addResponse = (AddResponse) response;
AddFuture addFuture = (AddFuture) responseFuture;
// remove the listener from the listener map
if (LOG.isDebugEnabled()) {
if (addResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
LOG.debug(I18n.msg(I18n.MSG_03209_ADD_SUCCESSFUL, addResponse));
} else {
// We have had an error
LOG.debug(I18n.msg(I18n.MSG_03208_ADD_FAILED, addResponse));
}
}
// Store the response into the future
addFuture.set(addResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case BIND_RESPONSE:
// Transform the response
BindResponse bindResponse = (BindResponse) response;
BindFuture bindFuture = (BindFuture) responseFuture;
// remove the listener from the listener map
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));
}
}
// Store the response into the future
bindFuture.set(bindResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case COMPARE_RESPONSE:
// Transform the response
CompareResponse compareResponse = (CompareResponse) response;
CompareFuture compareFuture = (CompareFuture) responseFuture;
// remove the listener from the listener map
if (LOG.isDebugEnabled()) {
if (compareResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
LOG.debug(I18n.msg(I18n.MSG_03215_COMPARE_SUCCESSFUL, compareResponse));
} else {
// We have had an error
LOG.debug(I18n.msg(I18n.MSG_03214_COMPARE_FAILED, compareResponse));
}
}
// Store the response into the future
compareFuture.set(compareResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case DEL_RESPONSE:
// Transform the response
DeleteResponse deleteResponse = (DeleteResponse) response;
DeleteFuture deleteFuture = (DeleteFuture) responseFuture;
if (LOG.isDebugEnabled()) {
if (deleteResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
LOG.debug(I18n.msg(I18n.MSG_03217_DELETE_SUCCESSFUL, deleteResponse));
} else {
// We have had an error
LOG.debug(I18n.msg(I18n.MSG_03216_DELETE_FAILED, deleteResponse));
}
}
// Store the response into the future
deleteFuture.set(deleteResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case EXTENDED_RESPONSE:
// Transform the response
ExtendedResponse extendedResponse = (ExtendedResponse) response;
ExtendedFuture extendedFuture = (ExtendedFuture) responseFuture;
// remove the listener from the listener map
if (LOG.isDebugEnabled()) {
if (extendedResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
LOG.debug(I18n.msg(I18n.MSG_03219_EXTENDED_SUCCESSFUL, extendedResponse));
} else {
// We have had an error
LOG.debug(I18n.msg(I18n.MSG_03218_EXTENDED_FAILED, extendedResponse));
}
}
// Store the response into the future
extendedFuture.set(extendedResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case INTERMEDIATE_RESPONSE:
IntermediateResponse intermediateResponse;
if (responseFuture instanceof SearchFuture) {
intermediateResponse = new IntermediateResponseImpl(messageId);
addControls(intermediateResponse, response);
((SearchFuture) responseFuture).set(intermediateResponse);
} else if (responseFuture instanceof ExtendedFuture) {
intermediateResponse = new IntermediateResponseImpl(messageId);
addControls(intermediateResponse, response);
((ExtendedFuture) responseFuture).set(intermediateResponse);
} else {
// currently we only support IR for search and extended operations
throw new UnsupportedOperationException("Unknown ResponseFuture type " + responseFuture.getClass().getName());
}
intermediateResponse.setResponseName(((IntermediateResponse) response).getResponseName());
intermediateResponse.setResponseValue(((IntermediateResponse) response).getResponseValue());
break;
case MODIFY_RESPONSE:
// Transform the response
ModifyResponse modifyResponse = (ModifyResponse) response;
ModifyFuture modifyFuture = (ModifyFuture) responseFuture;
if (LOG.isDebugEnabled()) {
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 {
// We have had an error
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03223_MODIFY_FAILED, modifyResponse));
}
}
}
// Store the response into the future
modifyFuture.set(modifyResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case MODIFYDN_RESPONSE:
// Transform the response
ModifyDnResponse modifyDnResponse = (ModifyDnResponse) response;
ModifyDnFuture modifyDnFuture = (ModifyDnFuture) responseFuture;
if (LOG.isDebugEnabled()) {
if (modifyDnResponse.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
LOG.debug(I18n.msg(I18n.MSG_03226_MODIFYDN_SUCCESSFUL, modifyDnResponse));
} else {
// We have had an error
LOG.debug(I18n.msg(I18n.MSG_03225_MODIFYDN_FAILED, modifyDnResponse));
}
}
// Store the response into the future
modifyDnFuture.set(modifyDnResponse);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case SEARCH_RESULT_DONE:
// Store the response into the responseQueue
SearchResultDone searchResultDone = (SearchResultDone) response;
SearchFuture searchFuture = (SearchFuture) responseFuture;
if (LOG.isDebugEnabled()) {
if (searchResultDone.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS) {
// Everything is fine, return the response
LOG.debug(I18n.msg(I18n.MSG_03232_SEARCH_SUCCESSFUL, searchResultDone));
} else {
// We have had an error
LOG.debug(I18n.msg(I18n.MSG_03230_SEARCH_FAILED, searchResultDone));
}
}
// Store the response into the future
searchFuture.set(searchResultDone);
// Remove the future from the map
removeFromFutureMaps(messageId);
break;
case SEARCH_RESULT_ENTRY:
// Store the response into the responseQueue
SearchResultEntry searchResultEntry = (SearchResultEntry) response;
if (schemaManager != null) {
searchResultEntry.setEntry(new DefaultEntry(schemaManager, searchResultEntry.getEntry()));
}
searchFuture = (SearchFuture) responseFuture;
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03229_SEARCH_ENTRY_FOUND, searchResultEntry));
}
// Store the response into the future
searchFuture.set(searchResultEntry);
break;
case SEARCH_RESULT_REFERENCE:
// Store the response into the responseQueue
SearchResultReference searchResultReference = (SearchResultReference) response;
searchFuture = (SearchFuture) responseFuture;
if (LOG.isDebugEnabled()) {
LOG.debug(I18n.msg(I18n.MSG_03231_SEARCH_REFERENCE_FOUND, searchResultReference));
}
// Store the response into the future
searchFuture.set(searchResultReference);
break;
default:
throw new IllegalStateException("Unexpected response type " + response.getType());
}
}
use of org.apache.directory.api.ldap.model.message.IntermediateResponse in project directory-ldap-api by apache.
the class StoreIntermediateResponseValue method action.
/**
* {@inheritDoc}
*/
public void action(LdapMessageContainer<IntermediateResponseDecorator<?>> container) throws DecoderException {
// We can get the IntermediateResponse Object
IntermediateResponse intermediateResponse = container.getMessage();
// Get the Value and store it in the IntermediateResponse
TLV tlv = container.getCurrentTLV();
// value
if (tlv.getLength() == 0) {
intermediateResponse.setResponseValue(Strings.EMPTY_BYTES);
} else {
intermediateResponse.setResponseValue(tlv.getValue().getData());
}
// We can have an END transition
container.setGrammarEndAllowed(true);
if (IS_DEBUG) {
LOG.debug("Value read : {}", Strings.dumpBytes(intermediateResponse.getResponseValue()));
}
}
Aggregations