Search in sources :

Example 26 with Asn1Container

use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.

the class Asn1Decoder method treatValueStartState.

/**
 * Treat the Value part. We will distinguish two cases : - if the Tag is a
 * Primitive one, we will get the value. - if the Tag is a Constructed one,
 * nothing will be done.
 *
 * @param stream The ByteBuffer containing the PDU to decode
 * @param container The container that stores the current state,
 * the result and other informations.
 * @return <code>true</code> if there are more bytes to read, <code>false
 * </code> otherwise
 */
private boolean treatValueStartState(ByteBuffer stream, Asn1Container container) {
    TLV currentTlv = container.getCurrentTLV();
    if (TLV.isConstructed(currentTlv.getTag()) && !container.isGathering()) {
        container.setState(TLVStateEnum.TLV_STATE_DONE);
        return MORE;
    } else {
        int length = currentTlv.getLength();
        int nbBytes = stream.remaining();
        if (nbBytes < length) {
            currentTlv.getValue().init(length);
            currentTlv.getValue().setData(stream);
            container.setState(TLVStateEnum.VALUE_STATE_PENDING);
            return END;
        } else {
            currentTlv.getValue().init(length);
            stream.get(currentTlv.getValue().getData(), 0, length);
            container.setState(TLVStateEnum.TLV_STATE_DONE);
            return MORE;
        }
    }
}
Also used : TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 27 with Asn1Container

use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.

the class SearchRequestDecorator method unstackFilters.

/**
 * This method is used to clear the filter's stack for terminated elements. An element
 * is considered as terminated either if :
 *  - it's a final element (ie an element which cannot contains a Filter)
 *  - its current length equals its expected length.
 *
 * @param container The container being decoded
 */
@SuppressWarnings("unchecked")
public void unstackFilters(Asn1Container container) {
    LdapMessageContainer<MessageDecorator<Message>> ldapMessageContainer = (LdapMessageContainer<MessageDecorator<Message>>) container;
    TLV tlv = ldapMessageContainer.getCurrentTLV();
    TLV localParent = tlv.getParent();
    Filter localFilter = terminalFilter;
    // The parent has been completed, so fold it
    while ((localParent != null) && (localParent.getExpectedLength() == 0)) {
        int parentTlvId = localFilter.getParent() != null ? localFilter.getParent().getTlvId() : localFilter.getParentTlvId();
        if (localParent.getId() != parentTlvId) {
            localParent = localParent.getParent();
        } else {
            Filter filterParent = localFilter.getParent();
            // pushed on the stack, so we need to get its parent's parent
            if (localFilter instanceof PresentFilter) {
                if (filterParent == null) {
                    // We don't have parent, get out
                    break;
                }
                filterParent = filterParent.getParent();
            } else {
                filterParent = filterParent.getParent();
            }
            if (filterParent != null) {
                // The parent is a filter ; it will become the new currentFilter
                // and we will loop again.
                localFilter = currentFilter;
                currentFilter = filterParent;
                localParent = localParent.getParent();
            } else {
                // We can stop the recursion, we have reached the searchResult Object
                break;
            }
        }
    }
}
Also used : LdapMessageContainer(org.apache.directory.api.ldap.codec.api.LdapMessageContainer) MessageDecorator(org.apache.directory.api.ldap.codec.api.MessageDecorator) Message(org.apache.directory.api.ldap.model.message.Message) PresentFilter(org.apache.directory.api.ldap.codec.search.PresentFilter) AttributeValueAssertionFilter(org.apache.directory.api.ldap.codec.search.AttributeValueAssertionFilter) AndFilter(org.apache.directory.api.ldap.codec.search.AndFilter) ConnectorFilter(org.apache.directory.api.ldap.codec.search.ConnectorFilter) SubstringFilter(org.apache.directory.api.ldap.codec.search.SubstringFilter) NotFilter(org.apache.directory.api.ldap.codec.search.NotFilter) OrFilter(org.apache.directory.api.ldap.codec.search.OrFilter) PresentFilter(org.apache.directory.api.ldap.codec.search.PresentFilter) ExtensibleMatchFilter(org.apache.directory.api.ldap.codec.search.ExtensibleMatchFilter) Filter(org.apache.directory.api.ldap.codec.search.Filter) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 28 with Asn1Container

use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.

the class CancelRequestTest method testDecodeCancelNoCancelId.

/**
 * Test a Cancel message with no cancelId
 */
@Test
public void testDecodeCancelNoCancelId() {
    Asn1Decoder cancelDecoder = new CancelDecoder();
    ByteBuffer stream = ByteBuffer.allocate(0x02);
    stream.put(new byte[] { 0x30, 0x00 }).flip();
    // Allocate a Cancel Container
    Asn1Container cancelContainer = new CancelContainer();
    // Decode a Cancel message
    try {
        cancelDecoder.decode(stream, cancelContainer);
        fail("CancelID expected");
    } catch (DecoderException de) {
        assertTrue(true);
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) CancelDecoder(org.apache.directory.api.ldap.extras.extended.ads_impl.cancel.CancelDecoder) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) CancelContainer(org.apache.directory.api.ldap.extras.extended.ads_impl.cancel.CancelContainer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 29 with Asn1Container

use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.

the class WhoAmIResponseTest method testDecodeWhoAmINoWhoAmIAuthzIdUserId.

/**
 * Test a WhoAmI message with a UserId authzId
 */
@Test
public void testDecodeWhoAmINoWhoAmIAuthzIdUserId() {
    Asn1Decoder whoAmIResponseDecoder = new WhoAmIResponseDecoder();
    ByteBuffer stream = ByteBuffer.allocate(0x09);
    stream.put(new byte[] { 0x04, 0x07, 'u', ':', 't', 'e', 's', 't', 0x00 }).flip();
    String decodedPdu = Strings.dumpBytes(stream.array());
    // Allocate a WhoAmI Container
    Asn1Container whoAmIResponseContainer = new WhoAmIResponseContainer();
    // Decode a WhoAmI message
    try {
        whoAmIResponseDecoder.decode(stream, whoAmIResponseContainer);
    } catch (DecoderException de) {
        fail();
    }
    WhoAmIResponseDecorator whoAmIResponse = ((WhoAmIResponseContainer) whoAmIResponseContainer).getWhoAmIResponse();
    assertNotNull(whoAmIResponse.getAuthzId());
    // Check the encoding
    try {
        ByteBuffer bb = whoAmIResponse.encodeInternal();
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 30 with Asn1Container

use of org.apache.directory.api.asn1.ber.Asn1Container in project directory-ldap-api by apache.

the class WhoAmIResponseTest method testDecodeWhoAmINoWhoAmIAuthzIdEmpty.

/**
 * Test a WhoAmI message with no authzId
 */
@Test
public void testDecodeWhoAmINoWhoAmIAuthzIdEmpty() {
    Asn1Decoder whoAmIResponseDecoder = new WhoAmIResponseDecoder();
    ByteBuffer stream = ByteBuffer.allocate(0x02);
    stream.put(new byte[] { 0x04, 0x00 }).flip();
    String decodedPdu = Strings.dumpBytes(stream.array());
    // Allocate a WhoAmI Container
    Asn1Container whoAmIResponseContainer = new WhoAmIResponseContainer();
    // Decode a WhoAmI message
    try {
        whoAmIResponseDecoder.decode(stream, whoAmIResponseContainer);
    } catch (DecoderException de) {
        fail();
    }
    WhoAmIResponseDecorator whoAmIResponse = ((WhoAmIResponseContainer) whoAmIResponseContainer).getWhoAmIResponse();
    assertNull(whoAmIResponse.getAuthzId());
    // Check the encoding
    try {
        ByteBuffer bb = whoAmIResponse.encodeInternal();
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) Asn1Container(org.apache.directory.api.asn1.ber.Asn1Container) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

DecoderException (org.apache.directory.api.asn1.DecoderException)40 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)39 ByteBuffer (java.nio.ByteBuffer)38 Test (org.junit.Test)38 Asn1Container (org.apache.directory.api.asn1.ber.Asn1Container)36 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)30 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)29 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)10 EncoderException (org.apache.directory.api.asn1.EncoderException)6 CancelContainer (org.apache.directory.api.ldap.extras.extended.ads_impl.cancel.CancelContainer)5 CancelDecoder (org.apache.directory.api.ldap.extras.extended.ads_impl.cancel.CancelDecoder)5 Message (org.apache.directory.api.ldap.model.message.Message)4 ResponseCarryingException (org.apache.directory.api.ldap.codec.api.ResponseCarryingException)3 AddResponseImpl (org.apache.directory.api.ldap.model.message.AddResponseImpl)3 BerValue (org.apache.directory.api.asn1.ber.tlv.BerValue)2 BindRequestDecorator (org.apache.directory.api.ldap.codec.decorators.BindRequestDecorator)2 BindRequest (org.apache.directory.api.ldap.model.message.BindRequest)2 CheckNotNullLength (org.apache.directory.api.asn1.actions.CheckNotNullLength)1 MessageDecorator (org.apache.directory.api.ldap.codec.api.MessageDecorator)1 AndFilter (org.apache.directory.api.ldap.codec.search.AndFilter)1