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.LdapConstants;
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  
46      /** The bytes containing the Dn */
47      private byte[] dnBytes;
48  
49  
50      /**
51       * Makes a DeleteRequest a MessageDecorator.
52       *
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      public Dn getName()
69      {
70          return getDecorated().getName();
71      }
72  
73  
74      /**
75       * {@inheritDoc}
76       */
77      public DeleteRequest setName( Dn name )
78      {
79          getDecorated().setName( name );
80  
81          return this;
82      }
83  
84  
85      /**
86       * {@inheritDoc}
87       */
88      public DeleteRequest setMessageId( int messageId )
89      {
90          super.setMessageId( messageId );
91  
92          return this;
93      }
94  
95  
96      /**
97       * {@inheritDoc}
98       */
99      public DeleteRequest addControl( Control control )
100     {
101         return ( DeleteRequest ) super.addControl( control );
102     }
103 
104 
105     /**
106      * {@inheritDoc}
107      */
108     public DeleteRequest addAllControls( Control[] controls )
109     {
110         return ( DeleteRequest ) super.addAllControls( controls );
111     }
112 
113 
114     /**
115      * {@inheritDoc}
116      */
117     public DeleteRequest removeControl( Control control )
118     {
119         return ( DeleteRequest ) super.removeControl( control );
120     }
121 
122 
123     //-------------------------------------------------------------------------
124     // The Decorator methods
125     //-------------------------------------------------------------------------
126     /**
127      * Compute the DelRequest length
128      * 
129      * DelRequest :
130      * 0x4A L1 entry
131      * 
132      * L1 = Length(entry)
133      * Length(DelRequest) = Length(0x4A) + Length(L1) + L1
134      */
135     public int computeLength()
136     {
137         dnBytes = Strings.getBytesUtf8( getName().getName() );
138         int dnLength = dnBytes.length;
139 
140         // The entry
141         return 1 + TLV.getNbBytes( dnLength ) + dnLength;
142     }
143 
144 
145     /**
146      * Encode the DelRequest message to a PDU.
147      * 
148      * DelRequest :
149      * 0x4A LL entry
150      * 
151      * @param buffer The buffer where to put the PDU
152      */
153     public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
154     {
155         try
156         {
157             // The DelRequest Tag
158             buffer.put( LdapConstants.DEL_REQUEST_TAG );
159 
160             // The entry
161             buffer.put( TLV.getBytes( dnBytes.length ) );
162             buffer.put( dnBytes );
163         }
164         catch ( BufferOverflowException boe )
165         {
166             throw new EncoderException( I18n.err( I18n.ERR_04005 ) );
167         }
168 
169         return buffer;
170     }
171 }