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 decoratedMessage the decorated AddRequest
46       */
47      public AbandonRequestDecorator( LdapApiService codec, AbandonRequest decoratedMessage )
48      {
49          super( codec, decoratedMessage );
50      }
51  
52  
53      //-------------------------------------------------------------------------
54      // The AbandonRequest methods
55      //-------------------------------------------------------------------------
56  
57      /**
58       * {@inheritDoc}
59       */
60      public int getAbandoned()
61      {
62          return getDecorated().getAbandoned();
63      }
64  
65  
66      /**
67       * {@inheritDoc}
68       */
69      public AbandonRequest setAbandoned( int requestId )
70      {
71          getDecorated().setAbandoned( requestId );
72  
73          return this;
74      }
75  
76  
77      /**
78       * {@inheritDoc}
79       */
80      public AbandonRequest setMessageId( int messageId )
81      {
82          super.setMessageId( messageId );
83  
84          return this;
85      }
86  
87  
88      /**
89       * {@inheritDoc}
90       */
91      public AbandonRequest addControl( Control control )
92      {
93          return ( AbandonRequest ) super.addControl( control );
94      }
95  
96  
97      /**
98       * {@inheritDoc}
99       */
100     public AbandonRequest addAllControls( Control[] controls )
101     {
102         return ( AbandonRequest ) super.addAllControls( controls );
103     }
104 
105 
106     /**
107      * {@inheritDoc}
108      */
109     public AbandonRequest removeControl( Control control )
110     {
111         return ( AbandonRequest ) super.removeControl( control );
112     }
113 
114 
115     //-------------------------------------------------------------------------
116     // The Decorator methods
117     //-------------------------------------------------------------------------
118 
119     /**
120      * Encode the Abandon protocolOp part
121      */
122     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
123     {
124         try
125         {
126             // The tag
127             buffer.put( LdapCodecConstants.ABANDON_REQUEST_TAG );
128 
129             // The length. It has to be evaluated depending on
130             // the abandoned messageId value.
131             buffer.put( ( byte ) BerValue.getNbBytes( getAbandoned() ) );
132 
133             // The abandoned messageId
134             buffer.put( BerValue.getBytes( getAbandoned() ) );
135         }
136         catch ( BufferOverflowException boe )
137         {
138             String msg = I18n.err( I18n.ERR_04005 );
139             throw new EncoderException( msg );
140         }
141 
142         return buffer;
143     }
144 
145 
146     /**
147      * Compute the AbandonRequest length 
148      * 
149      * AbandonRequest : 
150      * 0x50 0x0(1..4) abandoned MessageId 
151      * 
152      * Length(AbandonRequest) = Length(0x50) + 1 + Length(abandoned MessageId)
153      */
154     public int computeLength()
155     {
156         int length = 1 + 1 + BerValue.getNbBytes( getAbandoned() );
157 
158         return length;
159     }
160 }