use of org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator in project directory-ldap-api by apache.
the class ExtendedResponseTest method testDecodeExtendedResponseNoResponse.
/**
* Test the decoding of an ExtendedResponse with no response
*/
@Test
public void testDecodeExtendedResponseNoResponse() {
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 { ..., extendedResp Response, ...
0x78, // ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
0x16, // COMPONENTS OF LDAPResult,
0x0A, 0x01, // LDAPResult ::= SEQUENCE {
0x00, // },
0x04, // matchedDN LDAPDN,
0x00, 0x04, // errorMessage LDAPString,
0x00, // responseName [0] LDAPOID,
(byte) 0x8A, 0x0D, '1', '.', '3', '.', '6', '.', '1', '.', '5', '.', '5', '.', '2' });
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<ExtendedResponseDecorator<?>> container = new LdapMessageContainer<ExtendedResponseDecorator<?>>(codec);
// Decode the ExtendedResponse PDU
try {
ldapDecoder.decode(stream, container);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded ExtendedResponse PDU
ExtendedResponseDecorator<?> extendedResponse = container.getMessage();
assertEquals(1, extendedResponse.getMessageId());
assertEquals(ResultCodeEnum.SUCCESS, extendedResponse.getLdapResult().getResultCode());
assertEquals(Dn.EMPTY_DN, extendedResponse.getLdapResult().getMatchedDn());
assertEquals("", extendedResponse.getLdapResult().getDiagnosticMessage());
assertEquals("1.3.6.1.5.5.2", extendedResponse.getResponseName());
assertEquals("", Strings.utf8ToString(extendedResponse.getResponseValue()));
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(extendedResponse);
// 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.codec.decorators.ExtendedResponseDecorator in project directory-ldap-api by apache.
the class ExtendedResponseTest method testDecodeExtendedRequestNoName.
/**
* Test the decoding of a ExtendedRequest with no name
*/
@Test
public void testDecodeExtendedRequestNoName() {
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 { ..., extendedResp Response, ...
0x78, // ExtendedResponse ::= [APPLICATION 24] SEQUENCE {
0x07, // COMPONENTS OF LDAPResult,
0x0A, 0x01, // LDAPResult ::= SEQUENCE {
0x00, // },
0x04, // matchedDN LDAPDN,
0x00, 0x04, // errorMessage LDAPString,
0x00 // referral [3] Referral OPTIONAL }
// responseName [0] LDAPOID,
});
String decodedPdu = Strings.dumpBytes(stream.array());
stream.flip();
// Allocate a LdapMessage Container
LdapMessageContainer<ExtendedResponseDecorator<?>> container = new LdapMessageContainer<ExtendedResponseDecorator<?>>(codec);
// Decode the ExtendedResponse PDU
try {
ldapDecoder.decode(stream, container);
} catch (DecoderException de) {
de.printStackTrace();
fail(de.getMessage());
}
// Check the decoded ExtendedResponse PDU
ExtendedResponse extendedResponse = container.getMessage();
assertEquals(1, extendedResponse.getMessageId());
assertEquals(ResultCodeEnum.SUCCESS, extendedResponse.getLdapResult().getResultCode());
assertEquals("", extendedResponse.getLdapResult().getMatchedDn().getName());
assertEquals("", extendedResponse.getLdapResult().getDiagnosticMessage());
// Check the encoding
try {
ByteBuffer bb = encoder.encodeMessage(extendedResponse);
// 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.codec.decorators.ExtendedResponseDecorator in project directory-ldap-api by apache.
the class DefaultLdapCodecService method newExtendedResponse.
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
@Override
public <E extends ExtendedResponse> E newExtendedResponse(String responseName, int messageId, byte[] serializedResponse) throws DecoderException {
ExtendedResponseDecorator<ExtendedResponse> resp;
ExtendedOperationFactory extendedRequestFactory = extendedOperationFactories.get(responseName);
if (extendedRequestFactory != null) {
resp = (ExtendedResponseDecorator<ExtendedResponse>) extendedRequestFactory.newResponse(serializedResponse);
} else {
resp = new ExtendedResponseDecorator(this, new ExtendedResponseImpl(responseName));
resp.setResponseValue(serializedResponse);
resp.setResponseName(responseName);
}
resp.setMessageId(messageId);
return (E) resp;
}
Aggregations