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