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     */
020    package org.apache.directory.shared.ldap.codec.controls.replication.syncDoneValue;
021    
022    
023    import java.nio.ByteBuffer;
024    
025    import org.apache.directory.shared.asn1.AbstractAsn1Object;
026    import org.apache.directory.shared.asn1.ber.tlv.TLV;
027    import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
028    import org.apache.directory.shared.asn1.ber.tlv.Value;
029    import org.apache.directory.shared.asn1.codec.EncoderException;
030    import org.apache.directory.shared.ldap.util.StringTools;
031    
032    
033    /**
034     * 
035     * A syncDoneValue object as described in rfc4533.
036     *
037     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038     * @version $Rev$, $Date$
039     */
040    public class SyncDoneValueControlCodec extends AbstractAsn1Object
041    {
042        /** The Sync cookie */
043        private byte[] cookie;
044    
045        /** the refreshDeletes flag */
046        private boolean refreshDeletes;
047    
048        /** The global length for this control */
049        private int syncDoneValueLength;
050    
051    
052        /**
053         * Compute the syncDoneValue length.
054         * 0x30 L1
055         * |
056         * +--> 0x04 L2 xkcd!!!...     (cookie)
057         * +--> 0x01 0x01 [0x00|0xFF]  (refreshDeletes)
058         */
059        @Override
060        public int computeLength()
061        {
062            // cookie's length
063            if ( cookie != null )
064            {
065                syncDoneValueLength = 1 + TLV.getNbBytes( cookie.length ) + cookie.length;
066            }
067    
068            // the refreshDeletes flag length
069            syncDoneValueLength += 1 + 1 + 1;
070    
071            return 1 + TLV.getNbBytes( syncDoneValueLength ) + syncDoneValueLength;
072        }
073    
074    
075        /**
076         * Encode the SyncDoneValue control
077         * 
078         * @param buffer The encoded sink
079         * @return A ByteBuffer that contains the encoded PDU
080         * @throws EncoderException If anything goes wrong while encoding.
081         */
082        @Override
083        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
084        {
085            ByteBuffer bb = ByteBuffer.allocate( computeLength() );
086            bb.put( UniversalTag.SEQUENCE_TAG );
087            bb.put( TLV.getBytes( syncDoneValueLength ) );
088    
089            if ( cookie != null )
090            {
091                Value.encode( bb, cookie );
092            }
093    
094            Value.encode( bb, refreshDeletes );
095    
096            return bb;
097        }
098    
099    
100        /**
101         * @return the cookie
102         */
103        public byte[] getCookie()
104        {
105            return cookie;
106        }
107    
108    
109        /**
110         * @param cookie cookie to be set
111         */
112        public void setCookie( byte[] cookie )
113        {
114            this.cookie = cookie;
115        }
116    
117    
118        /**
119         * @return true, if refreshDeletes flag is set, false otherwise
120         */
121        public boolean isRefreshDeletes()
122        {
123            return refreshDeletes;
124        }
125    
126    
127        /**
128         * @param refreshDeletes set the refreshDeletes flag 
129         */
130        public void setRefreshDeletes( boolean refreshDeletes )
131        {
132            this.refreshDeletes = refreshDeletes;
133        }
134    
135    
136        /**
137         * @see Object#toString()
138         */
139        public String toString()
140        {
141            StringBuilder sb = new StringBuilder();
142    
143            sb.append( "    SyncDoneValue control :\n" );
144            sb.append( "        cookie            : '" ).append( StringTools.dumpBytes( cookie ) ).append( "'\n" );
145            sb.append( "        refreshDeletes : '" ).append( refreshDeletes ).append( "'\n" );
146    
147            return sb.toString();
148        }
149    }