Search in sources :

Example 16 with ExtendedResponseDecorator

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());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 17 with ExtendedResponseDecorator

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());
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) AbstractCodecServiceTest(org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)

Example 18 with ExtendedResponseDecorator

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;
}
Also used : ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) ExtendedOperationFactory(org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory) ExtendedResponseImpl(org.apache.directory.api.ldap.model.message.ExtendedResponseImpl) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator)

Aggregations

ExtendedResponseDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator)18 DecoderException (org.apache.directory.api.asn1.DecoderException)15 ByteBuffer (java.nio.ByteBuffer)13 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)13 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)11 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)11 Test (org.junit.Test)11 EncoderException (org.apache.directory.api.asn1.EncoderException)8 ExtendedResponse (org.apache.directory.api.ldap.model.message.ExtendedResponse)8 Control (org.apache.directory.api.ldap.model.message.Control)5 CodecControl (org.apache.directory.api.ldap.codec.api.CodecControl)4 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 ExtendedOperationFactory (org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory)2 ExtendedResponseImpl (org.apache.directory.api.ldap.model.message.ExtendedResponseImpl)2 NamingException (javax.naming.NamingException)1 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)1 ExtendedRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedRequestDecorator)1 PasswordModifyResponse (org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponse)1 PasswordModifyResponseImpl (org.apache.directory.api.ldap.extras.extended.pwdModify.PasswordModifyResponseImpl)1