Search in sources :

Example 6 with Oid

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

the class DefaultLdapCodecService method toJndi.

/**
 * {@inheritDoc}
 */
@Override
public javax.naming.ldap.ExtendedRequest toJndi(final ExtendedRequest modelRequest) throws EncoderException {
    final String oid = modelRequest.getRequestName();
    final byte[] value;
    if (modelRequest instanceof ExtendedRequestDecorator) {
        ExtendedRequestDecorator<?> decorator = (ExtendedRequestDecorator<?>) modelRequest;
        value = decorator.getRequestValue();
    } else {
        // have to ask the factory to decorate for us - can't do it ourselves
        ExtendedOperationFactory extendedRequestFactory = extendedOperationFactories.get(modelRequest.getRequestName());
        ExtendedRequestDecorator<?> decorator = (ExtendedRequestDecorator<?>) extendedRequestFactory.decorate(modelRequest);
        value = decorator.getRequestValue();
    }
    return new javax.naming.ldap.ExtendedRequest() {

        private static final long serialVersionUID = -4160980385909987475L;

        @Override
        public String getID() {
            return oid;
        }

        @Override
        public byte[] getEncodedValue() {
            return value;
        }

        @Override
        public javax.naming.ldap.ExtendedResponse createExtendedResponse(String id, byte[] berValue, int offset, int length) throws NamingException {
            ExtendedOperationFactory factory = extendedOperationFactories.get(modelRequest.getRequestName());
            try {
                final ExtendedResponseDecorator<?> resp = (ExtendedResponseDecorator<?>) factory.newResponse(berValue);
                return new javax.naming.ldap.ExtendedResponse() {

                    private static final long serialVersionUID = -7686354122066100703L;

                    @Override
                    public String getID() {
                        return oid;
                    }

                    @Override
                    public byte[] getEncodedValue() {
                        return resp.getResponseValue();
                    }
                };
            } catch (DecoderException de) {
                NamingException ne = new NamingException("Unable to decode encoded response value: " + Strings.dumpBytes(berValue));
                ne.setRootCause(de);
                throw ne;
            }
        }
    };
}
Also used : ExtendedOperationFactory(org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory) ExtendedRequestDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedRequestDecorator) DecoderException(org.apache.directory.api.asn1.DecoderException) ExtendedResponse(org.apache.directory.api.ldap.model.message.ExtendedResponse) ExtendedRequest(org.apache.directory.api.ldap.model.message.ExtendedRequest) ExtendedResponseDecorator(org.apache.directory.api.ldap.codec.decorators.ExtendedResponseDecorator) NamingException(javax.naming.NamingException)

Example 7 with Oid

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

the class QuirkySchemaTest method createFakeConnection.

private LdapConnection createFakeConnection(final String schemaFileName) {
    return new LdapConnection() {

        @Override
        public void unBind() throws LdapException {
        }

        @Override
        public void setTimeOut(long timeOut) {
        }

        @Override
        public void setSchemaManager(SchemaManager schemaManager) {
        }

        @Override
        public void setBinaryAttributeDetector(BinaryAttributeDetector binaryAttributeDetecter) {
        }

        @Override
        public SearchCursor search(SearchRequest searchRequest) throws LdapException {
            return null;
        }

        @Override
        public EntryCursor search(String baseDn, String filter, SearchScope scope, String... attributes) throws LdapException {
            return null;
        }

        @Override
        public EntryCursor search(Dn baseDn, String filter, SearchScope scope, String... attributes) throws LdapException {
            return null;
        }

        @Override
        public void rename(Dn entryDn, Rdn newRdn, boolean deleteOldRdn) throws LdapException {
        }

        @Override
        public void rename(String entryDn, String newRdn, boolean deleteOldRdn) throws LdapException {
        }

        @Override
        public void rename(Dn entryDn, Rdn newRdn) throws LdapException {
        }

        @Override
        public void rename(String entryDn, String newRdn) throws LdapException {
        }

        @Override
        public void moveAndRename(String entryDn, String newDn, boolean deleteOldRdn) throws LdapException {
        }

        @Override
        public void moveAndRename(Dn entryDn, Dn newDn, boolean deleteOldRdn) throws LdapException {
        }

        @Override
        public void moveAndRename(String entryDn, String newDn) throws LdapException {
        }

        @Override
        public void moveAndRename(Dn entryDn, Dn newDn) throws LdapException {
        }

        @Override
        public void move(Dn entryDn, Dn newSuperiorDn) throws LdapException {
        }

        @Override
        public void move(String entryDn, String newSuperiorDn) throws LdapException {
        }

        @Override
        public ModifyDnResponse modifyDn(ModifyDnRequest modDnRequest) throws LdapException {
            return null;
        }

        @Override
        public ModifyResponse modify(ModifyRequest modRequest) throws LdapException {
            return null;
        }

        @Override
        public void modify(Entry entry, ModificationOperation modOp) throws LdapException {
        }

        @Override
        public void modify(String dn, Modification... modifications) throws LdapException {
        }

        @Override
        public void modify(Dn dn, Modification... modifications) throws LdapException {
        }

        @Override
        public Entry lookup(String dn, Control[] controls, String... attributes) throws LdapException {
            return lookup(new Dn(dn));
        }

        @Override
        public Entry lookup(String dn, String... attributes) throws LdapException {
            return lookup(new Dn(dn));
        }

        @Override
        public Entry lookup(Dn dn, Control[] controls, String... attributes) throws LdapException {
            return lookup(dn);
        }

        @Override
        public Entry lookup(Dn dn, String... attributes) throws LdapException {
            return lookup(dn);
        }

        @Override
        public Entry lookup(String dn) throws LdapException {
            return lookup(new Dn(dn));
        }

        @Override
        public Entry lookup(Dn dn) throws LdapException {
            if (dn.isRootDse()) {
                Entry entry = new DefaultEntry(dn);
                entry.add(SchemaConstants.SUBSCHEMA_SUBENTRY_AT, SCHEMA_DN);
                return entry;
            } else if (dn.toString().equals(SCHEMA_DN)) {
                Entry entry = loadSchemaEntry(schemaFileName);
                return entry;
            } else {
                throw new UnsupportedOperationException("Unexpected DN " + dn);
            }
        }

        @Override
        public void loadSchemaRelaxed() throws LdapException {
        }

        @Override
        public void loadSchema() throws LdapException {
        }

        @Override
        public boolean isRequestCompleted(int messageId) {
            return true;
        }

        @Override
        public boolean isControlSupported(String controlOID) throws LdapException {
            return true;
        }

        @Override
        public boolean isConnected() {
            return true;
        }

        @Override
        public boolean isAuthenticated() {
            return false;
        }

        @Override
        public List<String> getSupportedControls() throws LdapException {
            return null;
        }

        @Override
        public SchemaManager getSchemaManager() {
            return null;
        }

        @Override
        public Entry getRootDse(String... attributes) throws LdapException {
            return lookup(Dn.ROOT_DSE);
        }

        @Override
        public Entry getRootDse() throws LdapException {
            return lookup(Dn.ROOT_DSE);
        }

        @Override
        public LdapApiService getCodecService() {
            return null;
        }

        @Override
        public BinaryAttributeDetector getBinaryAttributeDetector() {
            return null;
        }

        @Override
        public ExtendedResponse extended(ExtendedRequest extendedRequest) throws LdapException {
            return null;
        }

        @Override
        public ExtendedResponse extended(Oid oid, byte[] value) throws LdapException {
            return null;
        }

        @Override
        public ExtendedResponse extended(Oid oid) throws LdapException {
            return null;
        }

        @Override
        public ExtendedResponse extended(String oid, byte[] value) throws LdapException {
            return null;
        }

        @Override
        public ExtendedResponse extended(String oid) throws LdapException {
            return null;
        }

        @Override
        public boolean exists(Dn dn) throws LdapException {
            return false;
        }

        @Override
        public boolean exists(String dn) throws LdapException {
            return false;
        }

        @Override
        public boolean doesFutureExistFor(int messageId) {
            return false;
        }

        @Override
        public DeleteResponse delete(DeleteRequest deleteRequest) throws LdapException {
            return null;
        }

        @Override
        public void delete(Dn dn) throws LdapException {
        }

        @Override
        public void delete(String dn) throws LdapException {
        }

        @Override
        public boolean connect() throws LdapException {
            return true;
        }

        @Override
        public CompareResponse compare(CompareRequest compareRequest) throws LdapException {
            return null;
        }

        @Override
        public boolean compare(Dn dn, String attributeName, byte[] value) throws LdapException {
            return false;
        }

        @Override
        public boolean compare(Dn dn, String attributeName, String value) throws LdapException {
            return false;
        }

        @Override
        public boolean compare(String dn, String attributeName, byte[] value) throws LdapException {
            return false;
        }

        @Override
        public boolean compare(String dn, String attributeName, String value) throws LdapException {
            return false;
        }

        @Override
        public void close() throws IOException {
        }

        @Override
        public BindResponse bind(BindRequest bindRequest) throws LdapException {
            return null;
        }

        @Override
        public void bind(Dn name, String credentials) throws LdapException {
        }

        @Override
        public void bind(Dn name) throws LdapException {
        }

        @Override
        public void bind(String name, String credentials) throws LdapException {
        }

        @Override
        public void bind(String name) throws LdapException {
        }

        @Override
        public void bind() throws LdapException {
        }

        @Override
        public void anonymousBind() throws LdapException {
        }

        @Override
        public AddResponse add(AddRequest addRequest) throws LdapException {
            return null;
        }

        @Override
        public void add(Entry entry) throws LdapException {
        }

        @Override
        public void abandon(AbandonRequest abandonRequest) {
        }

        @Override
        public void abandon(int messageId) {
        }

        @Override
        public boolean compare(String dn, String attributeName, Value value) throws LdapException {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean compare(Dn dn, String attributeName, Value value) throws LdapException {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public BindResponse bind(SaslRequest saslRequest) throws LdapException {
            // TODO Auto-generated method stub
            return null;
        }
    };
}
Also used : SearchRequest(org.apache.directory.api.ldap.model.message.SearchRequest) Modification(org.apache.directory.api.ldap.model.entry.Modification) BindRequest(org.apache.directory.api.ldap.model.message.BindRequest) AbandonRequest(org.apache.directory.api.ldap.model.message.AbandonRequest) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Dn(org.apache.directory.api.ldap.model.name.Dn) DefaultSchemaManager(org.apache.directory.api.ldap.schema.manager.impl.DefaultSchemaManager) SchemaManager(org.apache.directory.api.ldap.model.schema.SchemaManager) ModifyDnRequest(org.apache.directory.api.ldap.model.message.ModifyDnRequest) ModifyRequest(org.apache.directory.api.ldap.model.message.ModifyRequest) Oid(org.apache.directory.api.asn1.util.Oid) BinaryAttributeDetector(org.apache.directory.api.ldap.codec.api.BinaryAttributeDetector) AddRequest(org.apache.directory.api.ldap.model.message.AddRequest) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry) CompareRequest(org.apache.directory.api.ldap.model.message.CompareRequest) ModificationOperation(org.apache.directory.api.ldap.model.entry.ModificationOperation) ExtendedRequest(org.apache.directory.api.ldap.model.message.ExtendedRequest) SearchScope(org.apache.directory.api.ldap.model.message.SearchScope) Value(org.apache.directory.api.ldap.model.entry.Value) Rdn(org.apache.directory.api.ldap.model.name.Rdn) DeleteRequest(org.apache.directory.api.ldap.model.message.DeleteRequest)

Example 8 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(LdapMessageContainer<MessageDecorator<? extends Message>> 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);
    }
    Message message = container.getMessage();
    Control control = container.getLdapCodecService().newControl(oidValue);
    message.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) Control(org.apache.directory.api.ldap.model.message.Control) Message(org.apache.directory.api.ldap.model.message.Message) TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 9 with Oid

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

the class StoreExtendedResponseValue method action.

/**
 * {@inheritDoc}
 */
public void action(LdapMessageContainer<ExtendedResponseDecorator<?>> container) throws DecoderException {
    // We can allocate the ExtendedResponse Object
    ExtendedResponseDecorator<?> extendedResponse = container.getMessage();
    // Get the Value and store it in the ExtendedResponse
    TLV tlv = container.getCurrentTLV();
    // OID
    if (tlv.getLength() == 0) {
        extendedResponse.setResponseValue(Strings.EMPTY_BYTES);
    } else {
        extendedResponse.setResponseValue(tlv.getValue().getData());
    }
    // We can have an END transition
    container.setGrammarEndAllowed(true);
    if (IS_DEBUG) {
        LOG.debug("Extended value : {}", extendedResponse.getResponseValue());
    }
}
Also used : TLV(org.apache.directory.api.asn1.ber.tlv.TLV)

Example 10 with Oid

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

the class Oid method fromBytes.

/**
 * Decodes an OID from a <code>byte[]</code>.
 *
 * @param oidBytes The encoded<code>byte[]</code>
 * @return A new Oid
 * @throws DecoderException When the OID is not valid
 */
public static Oid fromBytes(byte[] oidBytes) throws DecoderException {
    if ((oidBytes == null) || (oidBytes.length < 1)) {
        throw new DecoderException(I18n.err(I18n.ERR_00003_INVALID_OID, Arrays.toString(oidBytes)));
    }
    StringBuilder builder = new StringBuilder();
    long value = 0;
    int valStart = 0;
    int valLength = 0;
    boolean firstArc = true;
    for (int i = 0; i < oidBytes.length; i++) {
        value |= oidBytes[i] & 0x7F;
        if (oidBytes[i] < 0) {
            // leading 1, so value continues
            value = value << 7;
            valLength++;
        } else {
            valLength++;
            if (valLength > 8) {
                // Above 9 bytes, we won't be able to store the value in a long...
                // Compute the number of necessary bytes
                int nbBytes = valLength * 7 / 8;
                if (valLength % 7 != 0) {
                    nbBytes++;
                }
                byte[] result = new byte[nbBytes];
                // Now iterate on the incoming bytes
                int pos = nbBytes - 1;
                int valEnd = valStart + valLength - 1;
                int j = 0;
                while (j < valLength - 8) {
                    result[pos--] = (byte) ((oidBytes[valEnd - j - 1] << 7) | (oidBytes[valEnd - j] & 0x7F));
                    result[pos--] = (byte) ((oidBytes[valEnd - j - 2] << 6) | ((oidBytes[valEnd - j - 1] & 0x7E) >> 1));
                    result[pos--] = (byte) ((oidBytes[valEnd - j - 3] << 5) | ((oidBytes[valEnd - j - 2] & 0x7C) >> 2));
                    result[pos--] = (byte) ((oidBytes[valEnd - j - 4] << 4) | ((oidBytes[valEnd - j - 3] & 0x78) >> 3));
                    result[pos--] = (byte) ((oidBytes[valEnd - j - 5] << 3) | ((oidBytes[valEnd - j - 4] & 0x70) >> 4));
                    result[pos--] = (byte) ((oidBytes[valEnd - j - 6] << 2) | ((oidBytes[valEnd - j - 5] & 0x60) >> 5));
                    result[pos--] = (byte) ((oidBytes[valEnd - j - 7] << 1) | ((oidBytes[valEnd - j - 6] & 0x40) >> 6));
                    j += 8;
                }
                switch(valLength - j) {
                    case 7:
                        result[pos--] = (byte) ((oidBytes[5] << 7) | (oidBytes[6] & 0x7F));
                        result[pos--] = (byte) ((oidBytes[4] << 6) | ((oidBytes[5] & 0x7E) >> 1));
                        result[pos--] = (byte) ((oidBytes[3] << 5) | ((oidBytes[4] & 0x7C) >> 2));
                        result[pos--] = (byte) ((oidBytes[2] << 4) | ((oidBytes[3] & 0x78) >> 3));
                        result[pos--] = (byte) ((oidBytes[1] << 3) | ((oidBytes[2] & 0x70) >> 4));
                        result[pos--] = (byte) ((oidBytes[0] << 2) | ((oidBytes[1] & 0x60) >> 5));
                        result[pos] = (byte) ((oidBytes[0] & 0x40) >> 6);
                        break;
                    case 6:
                        result[pos--] = (byte) ((oidBytes[4] << 7) | (oidBytes[5] & 0x7F));
                        result[pos--] = (byte) ((oidBytes[3] << 6) | ((oidBytes[4] & 0x7E) >> 1));
                        result[pos--] = (byte) ((oidBytes[2] << 5) | ((oidBytes[3] & 0x7C) >> 2));
                        result[pos--] = (byte) ((oidBytes[1] << 4) | ((oidBytes[2] & 0x78) >> 3));
                        result[pos--] = (byte) ((oidBytes[0] << 3) | ((oidBytes[1] & 0x70) >> 4));
                        result[pos] = (byte) ((oidBytes[0] & 0x60) >> 5);
                        break;
                    case 5:
                        result[pos--] = (byte) ((oidBytes[3] << 7) | (oidBytes[4] & 0x7F));
                        result[pos--] = (byte) ((oidBytes[2] << 6) | ((oidBytes[3] & 0x7E) >> 1));
                        result[pos--] = (byte) ((oidBytes[1] << 5) | ((oidBytes[2] & 0x7C) >> 2));
                        result[pos--] = (byte) ((oidBytes[0] << 4) | ((oidBytes[1] & 0x78) >> 3));
                        result[pos] = (byte) ((oidBytes[0] & 0x70) >> 4);
                        break;
                    case 4:
                        result[pos--] = (byte) ((oidBytes[2] << 7) | (oidBytes[3] & 0x7F));
                        result[pos--] = (byte) ((oidBytes[1] << 6) | ((oidBytes[2] & 0x7E) >> 1));
                        result[pos--] = (byte) ((oidBytes[0] << 5) | ((oidBytes[1] & 0x7C) >> 2));
                        result[pos] = (byte) ((oidBytes[0] & 0x78) >> 3);
                        break;
                    case 3:
                        result[pos--] = (byte) ((oidBytes[1] << 7) | (oidBytes[2] & 0x7F));
                        result[pos--] = (byte) ((oidBytes[0] << 6) | ((oidBytes[1] & 0x7E) >> 1));
                        result[pos] = (byte) ((oidBytes[0] & 0x7C) >> 2);
                        break;
                    case 2:
                        result[pos--] = (byte) ((oidBytes[0] << 7) | (oidBytes[1] & 0x7F));
                        result[pos] = (byte) ((oidBytes[0] & 0x7E) >> 1);
                        break;
                    case 1:
                        result[pos] = (byte) (oidBytes[0] & 0x7F);
                        break;
                    default:
                        // Exist to please checkstyle...
                        break;
                }
                BigInteger bigInteger;
                if ((result[0] & 0x80) == 0x80) {
                    byte[] newResult = new byte[result.length + 1];
                    System.arraycopy(result, 0, newResult, 1, result.length);
                    result = newResult;
                }
                bigInteger = new BigInteger(result);
                if (firstArc) {
                    // This is a joint-iso-itu-t(2) arc
                    bigInteger = bigInteger.subtract(JOINT_ISO_ITU_T);
                    builder.append('2');
                }
                builder.append('.').append(bigInteger.toString());
            } else {
                // value completed
                if (firstArc) {
                    // first value special processing
                    if (value >= 80) {
                        // starts with 2
                        builder.append('2');
                        value = value - 80;
                    } else {
                        // starts with 0 or 1
                        long one = value / 40;
                        long two = value % 40;
                        if ((one < 0) || (one > 2) || (two < 0) || ((one < 2) && (two > 39))) {
                            throw new DecoderException(I18n.err(I18n.ERR_00003_INVALID_OID, Arrays.toString(oidBytes)));
                        }
                        if (one < 2) {
                            builder.append(one);
                            value = two;
                        }
                    }
                    firstArc = false;
                }
                // normal processing
                builder.append('.').append(value);
            }
            valStart = i;
            valLength = 0;
            value = 0;
        }
    }
    return new Oid(builder.toString(), oidBytes);
}
Also used : DecoderException(org.apache.directory.api.asn1.DecoderException) BigInteger(java.math.BigInteger)

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