001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.codec.controls.search.entryChange;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.Asn1Object;
026import org.apache.directory.api.asn1.DecoderException;
027import org.apache.directory.api.asn1.EncoderException;
028import org.apache.directory.api.asn1.ber.Asn1Decoder;
029import org.apache.directory.api.asn1.ber.tlv.BerValue;
030import org.apache.directory.api.asn1.ber.tlv.TLV;
031import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
032import org.apache.directory.api.i18n.I18n;
033import org.apache.directory.api.ldap.codec.api.ControlDecorator;
034import org.apache.directory.api.ldap.codec.api.LdapApiService;
035import org.apache.directory.api.ldap.model.message.controls.ChangeType;
036import org.apache.directory.api.ldap.model.message.controls.EntryChange;
037import org.apache.directory.api.ldap.model.message.controls.EntryChangeImpl;
038import org.apache.directory.api.ldap.model.name.Dn;
039import org.apache.directory.api.util.Strings;
040
041
042/**
043 * An EntryChange implementation, that wraps and decorates the Control with codec
044 * specific functionality.
045 *
046 *
047 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
048 */
049public class EntryChangeDecorator extends ControlDecorator<EntryChange> implements EntryChange
050{
051
052    public static final int UNDEFINED_CHANGE_NUMBER = -1;
053
054    /** A temporary storage for the previous Dn */
055    private byte[] previousDnBytes = null;
056
057    /** The entry change global length */
058    private int eccSeqLength;
059
060    /** An instance of this decoder */
061    private static final Asn1Decoder DECODER = new Asn1Decoder();
062
063
064    /**
065     * Creates a new instance of EntryChangeDecoder wrapping a newly created
066     * EntryChange Control object.
067     * 
068     * @param codec The LDAP service instance
069     */
070    public EntryChangeDecorator( LdapApiService codec )
071    {
072        super( codec, new EntryChangeImpl() );
073    }
074
075
076    /**
077     * Creates a new instance of EntryChangeDecorator wrapping the supplied
078     * EntryChange Control.
079     *
080     * @param codec The LDAP service instance
081     * @param control The EntryChange Control to be decorated.
082     */
083    public EntryChangeDecorator( LdapApiService codec, EntryChange control )
084    {
085        super( codec, control );
086    }
087
088
089    /**
090     * Internally used to not have to cast the decorated Control.
091     *
092     * @return the decorated Control.
093     */
094    private EntryChange getEntryChange()
095    {
096        return ( EntryChange ) getDecorated();
097    }
098
099
100    /**
101     * Compute the EntryChangeControl length 
102     * 
103     * <pre>
104     * 0x30 L1 
105     *   | 
106     *   +--&gt; 0x0A 0x0(1-4) [1|2|4|8] (changeType) 
107     *  [+--&gt; 0x04 L2 previousDN] 
108     *  [+--&gt; 0x02 0x0(1-4) [0..2^63-1] (changeNumber)]
109     *  </pre>
110     *  
111     * @return the control length.
112     */
113    public int computeLength()
114    {
115        int changeTypesLength = 1 + 1 + 1;
116
117        int previousDnLength = 0;
118        int changeNumberLength = 0;
119
120        if ( getPreviousDn() != null )
121        {
122            previousDnBytes = Strings.getBytesUtf8( getPreviousDn().getName() );
123            previousDnLength = 1 + TLV.getNbBytes( previousDnBytes.length ) + previousDnBytes.length;
124        }
125
126        if ( getChangeNumber() != UNDEFINED_CHANGE_NUMBER )
127        {
128            changeNumberLength = 1 + 1 + BerValue.getNbBytes( getChangeNumber() );
129        }
130
131        eccSeqLength = changeTypesLength + previousDnLength + changeNumberLength;
132        valueLength = 1 + TLV.getNbBytes( eccSeqLength ) + eccSeqLength;
133
134        return valueLength;
135    }
136
137
138    /**
139     * Encodes the entry change control.
140     * 
141     * @param buffer The encoded sink
142     * @return A ByteBuffer that contains the encoded PDU
143     * @throws EncoderException If anything goes wrong.
144     */
145    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
146    {
147        if ( buffer == null )
148        {
149            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
150        }
151
152        buffer.put( UniversalTag.SEQUENCE.getValue() );
153        buffer.put( TLV.getBytes( eccSeqLength ) );
154
155        buffer.put( UniversalTag.ENUMERATED.getValue() );
156        buffer.put( ( byte ) 1 );
157        buffer.put( BerValue.getBytes( getChangeType().getValue() ) );
158
159        if ( getPreviousDn() != null )
160        {
161            BerValue.encode( buffer, previousDnBytes );
162        }
163
164        if ( getChangeNumber() != UNDEFINED_CHANGE_NUMBER )
165        {
166            BerValue.encode( buffer, getChangeNumber() );
167        }
168
169        return buffer;
170    }
171
172
173    /**
174     * {@inheritDoc}
175     */
176    public byte[] getValue()
177    {
178        if ( value == null )
179        {
180            try
181            {
182                computeLength();
183                ByteBuffer buffer = ByteBuffer.allocate( valueLength );
184
185                buffer.put( UniversalTag.SEQUENCE.getValue() );
186                buffer.put( TLV.getBytes( eccSeqLength ) );
187
188                buffer.put( UniversalTag.ENUMERATED.getValue() );
189                buffer.put( ( byte ) 1 );
190                buffer.put( BerValue.getBytes( getChangeType().getValue() ) );
191
192                if ( getPreviousDn() != null )
193                {
194                    BerValue.encode( buffer, previousDnBytes );
195                }
196
197                if ( getChangeNumber() != UNDEFINED_CHANGE_NUMBER )
198                {
199                    BerValue.encode( buffer, getChangeNumber() );
200                }
201
202                value = buffer.array();
203            }
204            catch ( Exception e )
205            {
206                return null;
207            }
208        }
209
210        return value;
211    }
212
213
214    /**
215     * {@inheritDoc}
216     */
217    public ChangeType getChangeType()
218    {
219        return getEntryChange().getChangeType();
220    }
221
222
223    /**
224     * {@inheritDoc}
225     */
226    public void setChangeType( ChangeType changeType )
227    {
228        getEntryChange().setChangeType( changeType );
229    }
230
231
232    /**
233     * {@inheritDoc}
234     */
235    public Dn getPreviousDn()
236    {
237        return getEntryChange().getPreviousDn();
238    }
239
240
241    /**
242     * {@inheritDoc}
243     */
244    public void setPreviousDn( Dn previousDn )
245    {
246        getEntryChange().setPreviousDn( previousDn );
247    }
248
249
250    /**
251     * {@inheritDoc}
252     */
253    public long getChangeNumber()
254    {
255        return getEntryChange().getChangeNumber();
256    }
257
258
259    /**
260     * {@inheritDoc}
261     */
262    public void setChangeNumber( long changeNumber )
263    {
264        getEntryChange().setChangeNumber( changeNumber );
265    }
266
267
268    /**
269     * {@inheritDoc}
270     */
271    public Asn1Object decode( byte[] controlBytes ) throws DecoderException
272    {
273        ByteBuffer bb = ByteBuffer.wrap( controlBytes );
274        EntryChangeContainer container = new EntryChangeContainer( getCodecService(), this );
275        DECODER.decode( bb, container );
276        return this;
277    }
278}