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   *    http://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.asn1.actions;
21  
22  
23  import org.apache.directory.api.asn1.DecoderException;
24  import org.apache.directory.api.asn1.ber.Asn1Container;
25  import org.apache.directory.api.asn1.ber.grammar.GrammarAction;
26  import org.apache.directory.api.asn1.ber.tlv.BerValue;
27  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoder;
28  import org.apache.directory.api.asn1.ber.tlv.IntegerDecoderException;
29  import org.apache.directory.api.asn1.ber.tlv.TLV;
30  import org.apache.directory.api.i18n.I18n;
31  import org.apache.directory.api.util.Strings;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  
36  /**
37   * The action used to read an integer value
38   *
39   * @param C The container type
40   * 
41   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
42   */
43  public abstract class AbstractReadInteger<E extends Asn1Container> extends GrammarAction<E>
44  {
45      /** The logger */
46      private static final Logger LOG = LoggerFactory.getLogger( AbstractReadInteger.class );
47  
48      /** Speedup for logs */
49      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
50  
51      /** the acceptable minimum value for the expected value to be parsed */
52      private int minValue = 0;
53  
54      /** the acceptable maximum value for the expected value to be parsed */
55      private int maxValue = Integer.MAX_VALUE;
56  
57  
58      /**
59       * Instantiates a new AbstractReadInteger action.
60       * 
61       * @param name the action's name
62       */
63      public AbstractReadInteger( String name )
64      {
65          super( name );
66      }
67  
68  
69      /**
70       *
71       * Creates a new instance of AbstractReadInteger.
72       *
73       * @param name the action's name
74       * @param minValue the acceptable minimum value for the expected value to be read
75       * @param maxValue the acceptable maximum value for the value to be read
76       */
77      public AbstractReadInteger( String name, int minValue, int maxValue )
78      {
79          super( name );
80  
81          this.minValue = minValue;
82          this.maxValue = maxValue;
83      }
84  
85  
86      /**
87       *
88       * set the integer value to the appropriate field of ASN.1 object present in the container
89       *
90       * @param value the integer value
91       * @param container the ASN.1 object's container
92       */
93      protected abstract void setIntegerValue( int value, E container );
94  
95  
96      /**
97       * {@inheritDoc}
98       */
99      public final void action( E container ) throws DecoderException
100     {
101         TLV tlv = container.getCurrentTLV();
102 
103         // The Length should not be null
104         if ( tlv.getLength() == 0 )
105         {
106             LOG.error( I18n.err( I18n.ERR_04066 ) );
107 
108             // This will generate a PROTOCOL_ERROR
109             throw new DecoderException( I18n.err( I18n.ERR_04067 ) );
110         }
111 
112         BerValue value = tlv.getValue();
113 
114         try
115         {
116             int number = IntegerDecoder.parse( value, minValue, maxValue );
117 
118             if ( IS_DEBUG )
119             {
120                 LOG.debug( "read integer value : {}", number );
121             }
122 
123             setIntegerValue( number, container );
124         }
125         catch ( IntegerDecoderException ide )
126         {
127             LOG.error( I18n.err( I18n.ERR_04070, Strings.dumpBytes( value.getData() ), ide
128                 .getLocalizedMessage() ) );
129 
130             // This will generate a PROTOCOL_ERROR
131             throw new DecoderException( ide.getMessage(), ide );
132         }
133     }
134 }