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.decorators;
21  
22  
23  import java.nio.BufferOverflowException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.api.asn1.EncoderException;
27  import org.apache.directory.api.asn1.ber.tlv.BerValue;
28  import org.apache.directory.api.i18n.I18n;
29  import org.apache.directory.api.ldap.codec.api.LdapApiService;
30  import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
31  import org.apache.directory.api.ldap.model.message.AbandonRequest;
32  import org.apache.directory.api.ldap.model.message.Control;
33  
34  
35  /**
36   * A decorator for the AddRequest message
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public final class AbandonRequestDecorator extends RequestDecorator<AbandonRequest> implements AbandonRequest
41  {
42      /**
43       * Makes a AddRequest a MessageDecorator.
44       *
45       * @param codec The LDAP service instance
46       * @param decoratedMessage the decorated AddRequest
47       */
48      public AbandonRequestDecorator( LdapApiService codec, AbandonRequest decoratedMessage )
49      {
50          super( codec, decoratedMessage );
51      }
52  
53  
54      //-------------------------------------------------------------------------
55      // The AbandonRequest methods
56      //-------------------------------------------------------------------------
57  
58      /**
59       * {@inheritDoc}
60       */
61      @Override
62      public int getAbandoned()
63      {
64          return getDecorated().getAbandoned();
65      }
66  
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public AbandonRequest setAbandoned( int requestId )
73      {
74          getDecorated().setAbandoned( requestId );
75  
76          return this;
77      }
78  
79  
80      /**
81       * {@inheritDoc}
82       */
83      @Override
84      public AbandonRequest setMessageId( int messageId )
85      {
86          super.setMessageId( messageId );
87  
88          return this;
89      }
90  
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public AbandonRequest addControl( Control control )
97      {
98          return ( AbandonRequest ) super.addControl( control );
99      }
100 
101 
102     /**
103      * {@inheritDoc}
104      */
105     @Override
106     public AbandonRequest addAllControls( Control[] controls )
107     {
108         return ( AbandonRequest ) super.addAllControls( controls );
109     }
110 
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
116     public AbandonRequest removeControl( Control control )
117     {
118         return ( AbandonRequest ) super.removeControl( control );
119     }
120 
121 
122     //-------------------------------------------------------------------------
123     // The Decorator methods
124     //-------------------------------------------------------------------------
125 
126     /**
127      * Encode the Abandon protocolOp part
128      */
129     @Override
130     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
131     {
132         try
133         {
134             // The tag
135             buffer.put( LdapCodecConstants.ABANDON_REQUEST_TAG );
136 
137             // The length. It has to be evaluated depending on
138             // the abandoned messageId value.
139             buffer.put( ( byte ) BerValue.getNbBytes( getAbandoned() ) );
140 
141             // The abandoned messageId
142             buffer.put( BerValue.getBytes( getAbandoned() ) );
143         }
144         catch ( BufferOverflowException boe )
145         {
146             String msg = I18n.err( I18n.ERR_04005 );
147             throw new EncoderException( msg, boe );
148         }
149 
150         return buffer;
151     }
152 
153 
154     /**
155      * Compute the AbandonRequest length 
156      * <br>
157      * AbandonRequest :
158      * <pre> 
159      * 0x50 0x0(1..4) abandoned MessageId 
160      * 
161      * Length(AbandonRequest) = Length(0x50) + 1 + Length(abandoned MessageId)
162      * </pre>
163      */
164     @Override
165     public int computeLength()
166     {
167         return 1 + 1 + BerValue.getNbBytes( getAbandoned() );
168     }
169 }