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.syncStateValue;
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.message.control.replication.SyncStateTypeEnum;
031 import org.apache.directory.shared.ldap.util.StringTools;
032
033
034 /**
035 * A syncStateValue object, as defined in RFC 4533
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 * @version $Rev:$, $Date:
039 */
040 public class SyncStateValueControlCodec extends AbstractAsn1Object
041 {
042 /** The syncStateEnum type */
043 private SyncStateTypeEnum syncStateType;
044
045 /** The Sync cookie */
046 private byte[] cookie;
047
048 /** The entryUUID */
049 private byte[] entryUUID;
050
051 /** global length for the control */
052 private int syncStateValueLength;
053
054
055 /**
056 * @return the cookie
057 */
058 public byte[] getCookie()
059 {
060 return cookie;
061 }
062
063
064 /**
065 * @param cookie the cookie to set
066 */
067 public void setCookie( byte[] cookie )
068 {
069 this.cookie = cookie;
070 }
071
072
073 /**
074 * @return the syncState's type
075 */
076 public SyncStateTypeEnum getSyncStateType()
077 {
078 return syncStateType;
079 }
080
081
082 /**
083 * set the syncState's type
084 *
085 * @param syncStateType the syncState's type
086 */
087 public void setSyncStateType( SyncStateTypeEnum syncStateType )
088 {
089 this.syncStateType = syncStateType;
090 }
091
092
093 /**
094 * @return the entryUUID
095 */
096 public byte[] getEntryUUID()
097 {
098 return entryUUID;
099 }
100
101
102 /**
103 * set the entryUUID
104 *
105 * @param entryUUID the entryUUID
106 */
107 public void setEntryUUID( byte[] entryUUID )
108 {
109 this.entryUUID = entryUUID;
110 }
111
112
113 /**
114 * Compute the SyncStateValue length.
115 *
116 * SyncStateValue :
117 * 0x30 L1
118 * |
119 * +--> 0x0A 0x01 [0x00|0x01|0x02|0x03] (type)
120 * [+--> 0x04 L2 abcd... (entryUUID)
121 * [+--> 0x04 L3 abcd... (cookie)
122 *
123 */
124 public int computeLength()
125 {
126 // The sync state type length
127 syncStateValueLength = 1 + 1 + 1;
128
129 syncStateValueLength += 1 + TLV.getNbBytes( entryUUID.length ) + entryUUID.length;
130
131 // The cookie length, if we have a cookie
132 if ( cookie != null )
133 {
134 syncStateValueLength += 1 + TLV.getNbBytes( cookie.length ) + cookie.length;
135 }
136
137 return 1 + TLV.getNbBytes( syncStateValueLength ) + syncStateValueLength;
138 }
139
140
141 /**
142 * Encode the SyncStateValue control
143 *
144 * @param buffer The encoded sink
145 * @return A ByteBuffer that contains the encoded PDU
146 * @throws EncoderException If anything goes wrong.
147 */
148 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
149 {
150 // Allocate the bytes buffer.
151 ByteBuffer bb = ByteBuffer.allocate( computeLength() );
152 bb.put( UniversalTag.SEQUENCE_TAG );
153 bb.put( TLV.getBytes( syncStateValueLength ) );
154
155 // The mode
156 bb.put( UniversalTag.ENUMERATED_TAG );
157 bb.put( ( byte ) 0x01 );
158 bb.put( Value.getBytes( syncStateType.getValue() ) );
159
160 // the entryUUID
161 Value.encode( bb, entryUUID );
162
163 // The cookie
164 if ( cookie != null )
165 {
166 Value.encode( bb, cookie );
167 }
168
169 return bb;
170 }
171
172
173 /**
174 * @see Object#toString()
175 */
176 public String toString()
177 {
178 StringBuilder sb = new StringBuilder();
179
180 sb.append( " SyncStateValue control :\n" );
181 sb.append( " syncStateType : '" ).append( syncStateType ).append( "'\n" );
182 sb.append( " entryUUID : '" ).append( StringTools.dumpBytes( entryUUID ) ).append( "'\n" );
183 sb.append( " cookie : '" ).append( StringTools.dumpBytes( cookie ) ).append( "'\n" );
184
185 return sb.toString();
186 }
187 }