Search in sources :

Example 11 with BerValue

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

the class StoredProcedureTest method testDecodeStoredProcedureOneParam.

@Test
public void testDecodeStoredProcedureOneParam() throws IntegerDecoderException {
    Asn1Decoder storedProcedureDecoder = new StoredProcedureDecoder();
    ByteBuffer stream = ByteBuffer.allocate(0x1D);
    stream.put(new byte[] { 0x30, 0x1B, 0x04, 0x04, 'J', 'a', 'v', 'a', 0x04, 0x07, 'e', 'x', 'e', 'c', 'u', 't', 'e', 0x30, 0x0A, 0x30, 0x08, 0x04, 0x03, 'i', 'n', 't', 0x04, 0x01, 0x01 });
    String decodedPdu = Strings.dumpBytes(stream.array());
    stream.flip();
    // Allocate a StoredProcedure Container
    StoredProcedureContainer storedProcedureContainer = new StoredProcedureContainer();
    // Decode a StoredProcedure message
    try {
        storedProcedureDecoder.decode(stream, storedProcedureContainer);
    } catch (DecoderException de) {
        de.printStackTrace();
        fail(de.getMessage());
    }
    StoredProcedureRequestDecorator storedProcedure = storedProcedureContainer.getStoredProcedure();
    assertEquals("Java", storedProcedure.getLanguage());
    assertEquals("execute", storedProcedure.getProcedureSpecification());
    assertEquals(1, storedProcedure.size());
    assertEquals("int", Strings.utf8ToString((byte[]) storedProcedure.getParameterType(0)));
    assertEquals(1, IntegerDecoder.parse(new BerValue((byte[]) storedProcedure.getParameterValue(0))));
    // Check the encoding
    try {
        ByteBuffer bb = storedProcedure.encodeInternal();
        String encodedPdu = Strings.dumpBytes(bb.array());
        assertEquals(encodedPdu, decodedPdu);
    } catch (EncoderException ee) {
        ee.printStackTrace();
        fail(ee.getMessage());
    }
}
Also used : StoredProcedureDecoder(org.apache.directory.api.ldap.extras.extended.ads_impl.storedProcedure.StoredProcedureDecoder) IntegerDecoderException(org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException) DecoderException(org.apache.directory.api.asn1.DecoderException) EncoderException(org.apache.directory.api.asn1.EncoderException) StoredProcedureContainer(org.apache.directory.api.ldap.extras.extended.ads_impl.storedProcedure.StoredProcedureContainer) Asn1Decoder(org.apache.directory.api.asn1.ber.Asn1Decoder) BerValue(org.apache.directory.api.asn1.ber.tlv.BerValue) StoredProcedureRequestDecorator(org.apache.directory.api.ldap.extras.extended.ads_impl.storedProcedure.StoredProcedureRequestDecorator) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 12 with BerValue

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

the class AbstractReadInteger method action.

/**
 * {@inheritDoc}
 */
@Override
public final void action(E container) throws DecoderException {
    TLV tlv = container.getCurrentTLV();
    // The Length should not be null
    if (tlv.getLength() == 0) {
        String msg = I18n.err(I18n.ERR_01101_NULL_LENGTH);
        LOG.error(msg);
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(msg);
    }
    BerValue value = tlv.getValue();
    try {
        int number = IntegerDecoder.parse(value, minValue, maxValue);
        if (IS_DEBUG) {
            LOG.debug(I18n.msg(I18n.MSG_01100_INTEGER_VALUE, number));
        }
        setIntegerValue(number, container);
    } catch (IntegerDecoderException ide) {
        LOG.error(I18n.err(I18n.ERR_01102_INVALID_INTEGER, Strings.dumpBytes(value.getData()), ide.getLocalizedMessage()));
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(ide.getMessage(), ide);
    }
}
Also used : IntegerDecoderException(org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException) DecoderException(org.apache.directory.api.asn1.DecoderException) BerValue(org.apache.directory.api.asn1.ber.tlv.BerValue) IntegerDecoderException(org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 13 with BerValue

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

the class Asn1Decoder method isTLVDecoded.

/**
 * Check if the TLV tree is fully decoded
 *
 * @param container The container
 * @return <code>true</code> if the TLV has been decoded
 */
private boolean isTLVDecoded(Asn1Container container) {
    TLV current = container.getCurrentTLV();
    TLV parent = current.getParent();
    while (parent != null) {
        if (parent.getExpectedLength() != 0) {
            return false;
        }
        parent = parent.getParent();
    }
    BerValue value = current.getValue();
    if ((value != null) && (value.getData() != null)) {
        return current.getExpectedLength() == value.getData().length;
    } else {
        return current.getExpectedLength() == 0;
    }
}
Also used : BerValue(org.apache.directory.api.asn1.ber.tlv.BerValue) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 14 with BerValue

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

the class StoreControlCriticality method action.

/**
 * {@inheritDoc}
 */
public void action(ControlsContainer container) throws DecoderException {
    TLV tlv = container.getCurrentTLV();
    // Get the current control
    Control control = container.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());
    }
}
Also used : BooleanDecoderException(org.apache.directory.api.asn1.ber.tlv.BooleanDecoderException) DecoderException(org.apache.directory.api.asn1.DecoderException) Control(org.apache.directory.api.ldap.model.message.Control) BooleanDecoderException(org.apache.directory.api.asn1.ber.tlv.BooleanDecoderException) BerValue(org.apache.directory.api.asn1.ber.tlv.BerValue) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 15 with BerValue

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

the class StoreControlValue method action.

/**
 * {@inheritDoc}
 */
@Override
public void action(ControlsContainer container) {
    TLV tlv = container.getCurrentTLV();
    CodecControl<?> control = container.getCurrentControl();
    // Get the current control
    BerValue value = tlv.getValue();
    // Store the value - have to handle the special case of a 0 length value
    if (tlv.getLength() == 0) {
        control.setValue(Strings.EMPTY_BYTES);
    } else {
        control.setValue(value.getData());
    }
    // We can have an END transition
    container.setGrammarEndAllowed(true);
    if (IS_DEBUG) {
        LOG.debug("Control value : {}", Strings.dumpBytes(control.getValue()));
    }
}
Also used : BerValue(org.apache.directory.api.asn1.ber.tlv.BerValue) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Aggregations

BerValue (org.apache.directory.api.asn1.ber.tlv.BerValue)22 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)19 DecoderException (org.apache.directory.api.asn1.DecoderException)18 IntegerDecoderException (org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException)11 BooleanDecoderException (org.apache.directory.api.asn1.ber.tlv.BooleanDecoderException)5 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)5 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)3 ByteBuffer (java.nio.ByteBuffer)2 EncoderException (org.apache.directory.api.asn1.EncoderException)2 StoredProcedureContainer (org.apache.directory.api.ldap.extras.extended.ads_impl.storedProcedure.StoredProcedureContainer)2 StoredProcedureDecoder (org.apache.directory.api.ldap.extras.extended.ads_impl.storedProcedure.StoredProcedureDecoder)2 StoredProcedureRequestDecorator (org.apache.directory.api.ldap.extras.extended.ads_impl.storedProcedure.StoredProcedureRequestDecorator)2 Control (org.apache.directory.api.ldap.model.message.Control)2 Test (org.junit.Test)2 NamingException (javax.naming.NamingException)1 CheckNotNullLength (org.apache.directory.api.asn1.actions.CheckNotNullLength)1 LongDecoderException (org.apache.directory.api.asn1.ber.tlv.LongDecoderException)1 ExtendedOperationFactory (org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory)1 AbandonRequestDecorator (org.apache.directory.api.ldap.codec.decorators.AbandonRequestDecorator)1 ExtendedRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedRequestDecorator)1