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.abandon;
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.AbandonRequest;
32  import org.apache.directory.api.ldap.model.message.AbandonRequestImpl;
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 initialize the AbandonRequest
40   * <pre>
41   * LdapMessage ::= ... AbandonRequest ...
42   * AbandonRequest ::= [APPLICATION 16] MessageID
43   * </pre>
44   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
45   */
46  public class InitAbandonRequest extends GrammarAction<LdapMessageContainer<AbandonRequest>>
47  {
48      /** The logger */
49      private static final Logger LOG = LoggerFactory.getLogger( InitAbandonRequest.class );
50  
51  
52      /**
53       * Instantiates a new action.
54       */
55      public InitAbandonRequest()
56      {
57          super( "Init Abandon Request" );
58      }
59  
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      public void action( LdapMessageContainer<AbandonRequest> container ) throws DecoderException
66      {
67          // Create the AbandonRequest LdapMessage instance and store it in the container
68          AbandonRequest abandonRequest = new AbandonRequestImpl();
69          abandonRequest.setMessageId( container.getMessageId() );
70          container.setMessage( abandonRequest );
71  
72          // The current TLV should be a integer
73          // We get it and store it in MessageId
74          TLV tlv = container.getCurrentTLV();
75  
76          BerValue value = tlv.getValue();
77  
78          if ( ( value == null ) || ( value.getData() == null ) )
79          {
80              String msg = I18n.err( I18n.ERR_05109_ABANDON_REQ_MSG_ID_NULL );
81              LOG.error( msg );
82  
83              // This will generate a PROTOCOL_ERROR
84              throw new DecoderException( msg );
85          }
86  
87          try
88          {
89              int abandonnedMessageId = IntegerDecoder.parse( value, 0, Integer.MAX_VALUE );
90  
91              abandonRequest.setAbandoned( abandonnedMessageId );
92  
93              if ( LOG.isDebugEnabled() )
94              {
95                  LOG.debug( I18n.msg( I18n.MSG_05110_ABANDON_MSG_ID_DECODED, Integer.valueOf( abandonnedMessageId ) ) );
96              }
97  
98              container.setGrammarEndAllowed( true );
99          }
100         catch ( IntegerDecoderException ide )
101         {
102             LOG.error( I18n
103                 .err( I18n.ERR_05110_INVALID_ABANDON_REQ_MSG_ID, Strings.dumpBytes( value.getData() ), ide.getMessage() ) );
104 
105             // This will generate a PROTOCOL_ERROR
106             throw new DecoderException( ide.getMessage(), ide );
107         }
108     }
109 }