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.TLV;
28  import org.apache.directory.api.i18n.I18n;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  
33  /**
34   * The action used to read an OCTET STRING value
35   *
36   * @param <C> The container type
37   * 
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public abstract class AbstractReadOctetString<C extends Asn1Container> extends GrammarAction<C>
41  {
42      /** The logger */
43      private static final Logger LOG = LoggerFactory.getLogger( AbstractReadOctetString.class );
44  
45      /** the acceptable maximum value for the expected value to be parsed */
46      private boolean canBeNull = Boolean.FALSE;
47  
48  
49      /**
50       * Instantiates a new AbstractReadInteger action.
51       * 
52       * @param name the action's name
53       */
54      public AbstractReadOctetString( String name )
55      {
56          super( name );
57      }
58  
59  
60      /**
61       * Instantiates a new AbstractReadInteger action.
62       *
63       * @param name the action's name
64       * @param canBeNull Tells if the byte array can be null or not
65       */
66      public AbstractReadOctetString( String name, boolean canBeNull )
67      {
68          super( name );
69  
70          this.canBeNull = canBeNull;
71      }
72  
73  
74      /**
75       * Sets the OCTET STRING value to the appropriate field of ASN.1 object present in the container
76       *
77       * @param value the OCTET STRING value
78       * @param container the ASN.1 object's container
79       */
80      protected abstract void setOctetString( byte[] value, C container );
81  
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public final void action( C container ) throws DecoderException
88      {
89          TLV tlv = container.getCurrentTLV();
90  
91          // The Length should not be null
92          if ( ( tlv.getLength() == 0 ) && ( !canBeNull ) )
93          {
94              LOG.error( I18n.err( I18n.ERR_04066 ) );
95  
96              // This will generate a PROTOCOL_ERROR
97              throw new DecoderException( I18n.err( I18n.ERR_04067 ) );
98          }
99  
100         BerValue value = tlv.getValue();
101 
102         // The data should not be null
103         if ( ( value.getData() == null ) && ( !canBeNull ) )
104         {
105             LOG.error( I18n.err( I18n.ERR_04066 ) );
106 
107             // This will generate a PROTOCOL_ERROR
108             throw new DecoderException( I18n.err( I18n.ERR_04067 ) );
109         }
110 
111         setOctetString( value.getData(), container );
112     }
113 }