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.ldap.codec.actions.ldapMessage;
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.codec.api.MessageDecorator;
32  import org.apache.directory.api.ldap.model.message.Message;
33  import org.apache.directory.api.util.Strings;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  
38  /**
39   * The action used to store the LdapMessage MessageID.
40   * <pre>
41   * LDAPMessage --&gt; ... MessageId ...
42   *
43   * Checks that MessageId is in [0 .. 2147483647] and store the value in
44   * the LdapMessage Object
45   *
46   * (2147483647 = Integer.MAX_VALUE)
47   * The next state will be MESSAGE_ID_STATE
48   *
49   * The message ID will be temporarily stored in the container, because we can't store it
50   * into an object.
51   * </pre>
52   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
53   */
54  public class StoreMessageId extends GrammarAction<LdapMessageContainer<MessageDecorator<? extends Message>>>
55  {
56      /** The logger */
57      private static final Logger LOG = LoggerFactory.getLogger( StoreMessageId.class );
58  
59      /** Speedup for logs */
60      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
61  
62  
63      /**
64       * Instantiates a new action.
65       */
66      public StoreMessageId()
67      {
68          super( "Store MessageID" );
69      }
70  
71  
72      /**
73       * {@inheritDoc}
74       */
75      public void action( LdapMessageContainer<MessageDecorator<? extends Message>> container ) throws DecoderException
76      {
77          // The current TLV should be a integer
78          // We get it and store it in MessageId
79          TLV tlv = container.getCurrentTLV();
80  
81          // The Length should not be null
82          if ( tlv.getLength() == 0 )
83          {
84              LOG.error( I18n.err( I18n.ERR_04068 ) );
85  
86              // This will generate a PROTOCOL_ERROR
87              throw new DecoderException( I18n.err( I18n.ERR_04069 ) );
88          }
89  
90          BerValue value = tlv.getValue();
91  
92          try
93          {
94              int messageId = IntegerDecoder.parse( value, 0, Integer.MAX_VALUE );
95  
96              container.setMessageId( messageId );
97  
98              if ( IS_DEBUG )
99              {
100                 LOG.debug( "Ldap Message Id has been decoded : " + messageId );
101             }
102         }
103         catch ( IntegerDecoderException ide )
104         {
105             LOG.error( I18n.err( I18n.ERR_04070, Strings.dumpBytes( value.getData() ), ide
106                 .getLocalizedMessage() ) );
107 
108             // This will generate a PROTOCOL_ERROR
109             throw new DecoderException( ide.getMessage(), ide );
110         }
111     }
112 }