001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    https://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.api.ldap.codec.actions.request.bind;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
025import org.apache.directory.api.asn1.ber.tlv.BerValue;
026import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
027import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
028import org.apache.directory.api.asn1.ber.tlv.TLV;
029import org.apache.directory.api.i18n.I18n;
030import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
031import org.apache.directory.api.ldap.model.message.BindRequest;
032import org.apache.directory.api.util.Strings;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036
037/**
038 * The action used to store the BindRequest version.
039 * <pre>
040 * BindRequest ::= [APPLICATION 0] SEQUENCE {
041 *     version                 INTEGER (1 ..  127),
042 *     ....
043 * </pre>
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 */
046public class StoreVersion extends GrammarAction<LdapMessageContainer<BindRequest>>
047{
048    /** The logger */
049    private static final Logger LOG = LoggerFactory.getLogger( StoreVersion.class );
050
051    /**
052     * Instantiates a new action.
053     */
054    public StoreVersion()
055    {
056        super( "Store BindRequest Version" );
057    }
058
059
060    /**
061     * {@inheritDoc}
062     */
063    @Override
064    public void action( LdapMessageContainer<BindRequest> container ) throws DecoderException
065    {
066        BindRequest bindRequestMessage = container.getMessage();
067
068        // The current TLV should be a integer between 1 and 127
069        // We get it and store it in Version
070        TLV tlv = container.getCurrentTLV();
071
072        BerValue value = tlv.getValue();
073
074        try
075        {
076            int version = IntegerDecoder.parse( value, 1, 127 );
077
078            if ( LOG.isDebugEnabled() )
079            {
080                LOG.debug( I18n.msg( I18n.MSG_05114_LDAP_VERSION, Integer.valueOf( version ) ) );
081            }
082
083            bindRequestMessage.setVersion3( version == 3 );
084        }
085        catch ( IntegerDecoderException ide )
086        {
087            LOG.error( I18n
088                .err( I18n.ERR_05117_INVALID_VERSION, Strings.dumpBytes( value.getData() ), ide.getMessage() ) );
089
090            // This will generate a PROTOCOL_ERROR
091            throw new DecoderException( ide.getMessage(), ide );
092        }
093    }
094}