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.TLV;
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.Control;
32  import org.apache.directory.api.ldap.model.message.DeleteRequest;
33  import org.apache.directory.api.ldap.model.name.Dn;
34  import org.apache.directory.api.util.Strings;
35  
36  
37  /**
38   * A decorator for the DeleteRequest message
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public class DeleteRequestDecorator extends SingleReplyRequestDecorator<DeleteRequest>
43      implements DeleteRequest
44  {
45      /** The bytes containing the Dn */
46      private byte[] dnBytes;
47  
48  
49      /**
50       * Makes a DeleteRequest a MessageDecorator.
51       *
52       * @param codec The LDAP service instance
53       * @param decoratedMessage the decorated DeleteRequest
54       */
55      public DeleteRequestDecorator( LdapApiService codec, DeleteRequest decoratedMessage )
56      {
57          super( codec, decoratedMessage );
58      }
59  
60  
61      //-------------------------------------------------------------------------
62      // The DeleteRequest methods
63      //-------------------------------------------------------------------------
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      public Dn getName()
70      {
71          return getDecorated().getName();
72      }
73  
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public DeleteRequest setName( Dn name )
80      {
81          getDecorated().setName( name );
82  
83          return this;
84      }
85  
86  
87      /**
88       * {@inheritDoc}
89       */
90      @Override
91      public DeleteRequest setMessageId( int messageId )
92      {
93          super.setMessageId( messageId );
94  
95          return this;
96      }
97  
98  
99      /**
100      * {@inheritDoc}
101      */
102     @Override
103     public DeleteRequest addControl( Control control )
104     {
105         return ( DeleteRequest ) super.addControl( control );
106     }
107 
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
113     public DeleteRequest addAllControls( Control[] controls )
114     {
115         return ( DeleteRequest ) super.addAllControls( controls );
116     }
117 
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public DeleteRequest removeControl( Control control )
124     {
125         return ( DeleteRequest ) super.removeControl( control );
126     }
127 
128 
129     //-------------------------------------------------------------------------
130     // The Decorator methods
131     //-------------------------------------------------------------------------
132     /**
133      * Compute the DelRequest length
134      * <br>
135      * DelRequest :
136      * <pre>
137      * 0x4A L1 entry
138      * 
139      * L1 = Length(entry)
140      * Length(DelRequest) = Length(0x4A) + Length(L1) + L1
141      * </pre>
142      */
143     @Override
144     public int computeLength()
145     {
146         dnBytes = Strings.getBytesUtf8( getName().getName() );
147         int dnLength = dnBytes.length;
148 
149         // The entry
150         return 1 + TLV.getNbBytes( dnLength ) + dnLength;
151     }
152 
153 
154     /**
155      * Encode the DelRequest message to a PDU.
156      * <br>
157      * DelRequest :
158      * <pre>
159      * 0x4A LL entry
160      * </pre>
161      * 
162      * @param buffer The buffer where to put the PDU
163      */
164     @Override
165     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
166     {
167         try
168         {
169             // The DelRequest Tag
170             buffer.put( LdapCodecConstants.DEL_REQUEST_TAG );
171 
172             // The entry
173             buffer.put( TLV.getBytes( dnBytes.length ) );
174             buffer.put( dnBytes );
175         }
176         catch ( BufferOverflowException boe )
177         {
178             throw new EncoderException( I18n.err( I18n.ERR_04005 ), boe );
179         }
180 
181         return buffer;
182     }
183 }