View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.codec.actions.request.bind;
21  
22  
23  import org.apache.directory.api.asn1.DecoderException;
24  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
25  import org.apache.directory.api.asn1.ber.tlv.BerValue;
26  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
27  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
28  import org.apache.directory.api.asn1.ber.tlv.TLV;
29  import org.apache.directory.api.i18n.I18n;
30  import org.apache.directory.api.ldap.codec.api.LdapMessageContainer;
31  import org.apache.directory.api.ldap.model.message.BindRequest;
32  import org.apache.directory.api.util.Strings;
33  import org.slf4j.Logger;
34  import org.slf4j.LoggerFactory;
35  
36  
37  /**
38   * The action used to store the BindRequest version.
39   * <pre>
40   * BindRequest ::= [APPLICATION 0] SEQUENCE {
41   *     version                 INTEGER (1 ..  127),
42   *     ....
43   * </pre>
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class StoreVersion extends GrammarAction<LdapMessageContainer<BindRequest>>
47  {
48      /** The logger */
49      private static final Logger LOG = LoggerFactory.getLogger( StoreVersion.class );
50  
51      /**
52       * Instantiates a new action.
53       */
54      public StoreVersion()
55      {
56          super( "Store BindRequest Version" );
57      }
58  
59  
60      /**
61       * {@inheritDoc}
62       */
63      @Override
64      public void action( LdapMessageContainer<BindRequest> container ) throws DecoderException
65      {
66          BindRequest bindRequestMessage = container.getMessage();
67  
68          // The current TLV should be a integer between 1 and 127
69          // We get it and store it in Version
70          TLV tlv = container.getCurrentTLV();
71  
72          BerValue value = tlv.getValue();
73  
74          try
75          {
76              int version = IntegerDecoder.parse( value, 1, 127 );
77  
78              if ( LOG.isDebugEnabled() )
79              {
80                  LOG.debug( I18n.msg( I18n.MSG_05114_LDAP_VERSION, Integer.valueOf( version ) ) );
81              }
82  
83              bindRequestMessage.setVersion3( version == 3 );
84          }
85          catch ( IntegerDecoderException ide )
86          {
87              LOG.error( I18n
88                  .err( I18n.ERR_05117_INVALID_VERSION, Strings.dumpBytes( value.getData() ), ide.getMessage() ) );
89  
90              // This will generate a PROTOCOL_ERROR
91              throw new DecoderException( ide.getMessage(), ide );
92          }
93      }
94  }