Search in sources :

Example 11 with Oid

use of org.apache.directory.api.asn1.util.Oid in project directory-ldap-api by apache.

the class BerValue method encode.

/**
 * Encode an OID value
 *
 * @param buffer The PDU in which the value will be put
 * @param oid The OID to be encoded
 * @throws EncoderException if the PDU in which the value should be encoded is
 * two small
 */
public static void encode(ByteBuffer buffer, Oid oid) throws EncoderException {
    if (buffer == null) {
        throw new EncoderException(I18n.err(I18n.ERR_01300_CANNOT_PUT_PDU_IN_NULL_BUFFER));
    }
    try {
        buffer.put(UniversalTag.OCTET_STRING.getValue());
        buffer.put(TLV.getBytes(oid.getEncodedLength()));
        if (oid.getEncodedLength() != 0) {
            oid.writeBytesTo(buffer);
        }
    } catch (BufferOverflowException boe) {
        throw new EncoderException(I18n.err(I18n.ERR_01301_PDU_BUFFER_SIZE_TOO_SMALL), boe);
    }
}
Also used : EncoderException(org.apache.directory.api.asn1.EncoderException) BufferOverflowException(java.nio.BufferOverflowException)

Example 12 with Oid

use of org.apache.directory.api.asn1.util.Oid in project directory-ldap-api by apache.

the class AddControl method action.

/**
 * {@inheritDoc}
 */
public void action(ControlsContainer container) throws DecoderException {
    TLV tlv = container.getCurrentTLV();
    // We have to handle the special case of a 0 length OID
    if (tlv.getLength() == 0) {
        String msg = I18n.err(I18n.ERR_04097_NULL_CONTROL_OID);
        LOG.error(msg);
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(msg);
    }
    byte[] value = tlv.getValue().getData();
    String oidValue = Strings.asciiBytesToString(value);
    // The OID is encoded as a String, not an Object Id
    if (!Oid.isOid(oidValue)) {
        String msg = I18n.err(I18n.ERR_04098_INVALID_CONTROL_OID, oidValue);
        LOG.error(msg);
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(msg);
    }
    CodecControl<?> control = container.getLdapCodecService().newControl(oidValue);
    container.setCurrentControl(control);
    container.addControl(control);
    // We can have an END transition
    container.setGrammarEndAllowed(true);
    if (IS_DEBUG) {
        LOG.debug("Control OID : {}", oidValue);
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 13 with Oid

use of org.apache.directory.api.asn1.util.Oid in project directory-ldap-api by apache.

the class StoreExtendedResponseName method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<ExtendedResponseDecorator<?>> container) throws DecoderException {
    // We can allocate the ExtendedResponse Object
    ExtendedResponse extendedResponse;
    // Get the Value and store it in the ExtendedResponse
    TLV tlv = container.getCurrentTLV();
    // OID
    if (tlv.getLength() == 0) {
        String msg = I18n.err(I18n.ERR_04017);
        LOG.error(msg);
        throw new DecoderException(msg);
    } else {
        String responseName = Oid.fromString(Strings.asciiBytesToString(tlv.getValue().getData())).toString();
        extendedResponse = LdapApiServiceFactory.getSingleton().newExtendedResponse(responseName, container.getMessageId(), null);
        ((ExtendedResponseDecorator<?>) extendedResponse).setLdapResult((LdapResultDecorator) (container.getMessage().getLdapResult()));
        container.setMessage(LdapApiServiceFactory.getSingleton().decorate(extendedResponse));
    }
    // We can have an END transition
    container.setGrammarEndAllowed(true);
    if (IS_DEBUG) {
        LOG.debug("OID read : {}", extendedResponse.getResponseName());
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 14 with Oid

use of org.apache.directory.api.asn1.util.Oid in project directory-ldap-api by apache.

the class StoreIntermediateResponseName 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();
    // OID.
    if (tlv.getLength() == 0) {
        String msg = I18n.err(I18n.ERR_04095);
        LOG.error(msg);
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(msg);
    } else {
        byte[] responseNameBytes = tlv.getValue().getData();
        String oidStr = Strings.utf8ToString(responseNameBytes);
        if (Oid.isOid(oidStr)) {
            Oid.isOid(oidStr);
            intermediateResponse.setResponseName(oidStr);
        } else {
            String msg = "The Intermediate Response name is not a valid OID : " + Strings.utf8ToString(responseNameBytes) + " (" + Strings.dumpBytes(responseNameBytes) + ") is invalid";
            LOG.error("{} : {}", msg, oidStr);
            // Rethrow the exception, we will get a PROTOCOL_ERROR
            throw new DecoderException(msg);
        }
    }
    // We can have an END transition
    container.setGrammarEndAllowed(true);
    if (IS_DEBUG) {
        LOG.debug("OID read : {}", intermediateResponse.getResponseName());
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) IntermediateResponse(org.apache.directory.api.ldap.model.message.IntermediateResponse) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 15 with Oid

use of org.apache.directory.api.asn1.util.Oid in project directory-ldap-api by apache.

the class StoreExtendedRequestName method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<ExtendedRequestDecorator<?>> container) throws DecoderException {
    ExtendedRequest req;
    // Get the Value and store it in the ExtendedRequest
    TLV tlv = container.getCurrentTLV();
    // OID
    if (tlv.getLength() == 0) {
        String msg = I18n.err(I18n.ERR_04095);
        LOG.error(msg);
        // This will generate a PROTOCOL_ERROR
        throw new DecoderException(msg);
    } else {
        byte[] requestNameBytes = tlv.getValue().getData();
        try {
            String requestName = Strings.utf8ToString(requestNameBytes);
            if (!Oid.isOid(requestName)) {
                String msg = "The Request name is not a valid OID : " + Strings.utf8ToString(requestNameBytes) + " (" + Strings.dumpBytes(requestNameBytes) + ") is invalid";
                LOG.error(msg);
                // throw an exception, we will get a PROTOCOL_ERROR
                throw new DecoderException(msg);
            }
            req = LdapApiServiceFactory.getSingleton().newExtendedRequest(requestName, null);
            req.setMessageId(container.getMessageId());
            container.setMessage(LdapApiServiceFactory.getSingleton().decorate(req));
        } catch (DecoderException de) {
            String msg = "The Request name is not a valid OID : " + Strings.utf8ToString(requestNameBytes) + " (" + Strings.dumpBytes(requestNameBytes) + ") is invalid";
            LOG.error("{} : {}", msg, de.getMessage());
            // Rethrow the exception, we will get a PROTOCOL_ERROR
            throw de;
        }
    }
    // We can have an END transition
    container.setGrammarEndAllowed(true);
    if (IS_DEBUG) {
        LOG.debug("OID read : {}", req.getRequestName());
    }
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) ExtendedRequest(org.apache.directory.api.ldap.model.message.ExtendedRequest) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Aggregations

DecoderException (org.apache.directory.api.asn1.DecoderException)14 ByteBuffer (java.nio.ByteBuffer)7 Asn1Decoder (org.apache.directory.api.asn1.ber.Asn1Decoder)7 LdapMessageContainer (org.apache.directory.api.ldap.codec.api.LdapMessageContainer)7 AbstractCodecServiceTest (org.apache.directory.api.ldap.codec.osgi.AbstractCodecServiceTest)7 Test (org.junit.Test)7 TLV (org.apache.directory.api.asn1.ber.tlv.TLV)6 EncoderException (org.apache.directory.api.asn1.EncoderException)3 Control (org.apache.directory.api.ldap.model.message.Control)3 ExtendedRequest (org.apache.directory.api.ldap.model.message.ExtendedRequest)3 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)3 Asn1Container (org.apache.directory.api.asn1.ber.Asn1Container)2 ExtendedRequestDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedRequestDecorator)2 ExtendedResponseDecorator (org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator)2 SearchRequestDecorator (org.apache.directory.api.ldap.codec.decorators.SearchRequestDecorator)2 ExprNode (org.apache.directory.api.ldap.model.filter.ExprNode)2 BigInteger (java.math.BigInteger)1 BufferOverflowException (java.nio.BufferOverflowException)1 NamingException (javax.naming.NamingException)1 Oid (org.apache.directory.api.asn1.util.Oid)1