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.extras.controls.syncrepl.syncInfoValue;
021
022
023import java.util.Arrays;
024
025import org.apache.directory.api.ldap.extras.controls.SynchronizationModeEnum;
026import org.apache.directory.api.ldap.model.message.controls.AbstractControl;
027import org.apache.directory.api.util.Strings;
028
029
030/**
031 * A syncRequestValue object, as defined in RFC 4533 :
032 * <pre>
033 * 2.2.  Sync Request Control
034 *
035 *    The Sync Request Control is an LDAP Control [RFC4511] where the
036 *    controlType is the object identifier 1.3.6.1.4.1.4203.1.9.1.1 and the
037 *    controlValue, an OCTET STRING, contains a BER-encoded
038 *    syncRequestValue.  The criticality field is either TRUE or FALSE.
039 *
040 *       syncRequestValue ::= SEQUENCE {
041 *           mode ENUMERATED {
042 *               -- 0 unused
043 *               refreshOnly       (1),
044 *               -- 2 reserved
045 *               refreshAndPersist (3)
046 *           },
047 *           cookie     syncCookie OPTIONAL,
048 *           reloadHint BOOLEAN DEFAULT FALSE
049 *       }
050 *
051 *    The Sync Request Control is only applicable to the SearchRequest
052 *    Message.
053 * </pre>
054 *
055 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
056 * @version $Rev$, $Date$
057 */
058public class SyncRequestValueImpl extends AbstractControl implements SyncRequestValue
059{
060    /** The synchronization type */
061    private SynchronizationModeEnum mode;
062
063    /** The Sync cookie */
064    private byte[] cookie;
065
066    /** The reloadHint flag */
067    private boolean isReloadHint;
068
069
070    /**
071     * Creates a new instance of SyncRequestValueImpl.
072     */
073    public SyncRequestValueImpl()
074    {
075        super( OID );
076    }
077
078
079    /**
080     *
081     * Creates a new instance of SyncRequestValueImpl.
082     *
083     * @param isCritical The critical flag
084     */
085    public SyncRequestValueImpl( boolean isCritical )
086    {
087        super( OID, isCritical );
088    }
089
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    public byte[] getCookie()
096    {
097        return this.cookie;
098    }
099
100
101    /**
102     * {@inheritDoc}
103     */
104    @Override
105    public void setCookie( byte[] cookie )
106    {
107        this.cookie = cookie;
108    }
109
110
111    /**
112     * {@inheritDoc}
113     */
114    @Override
115    public SynchronizationModeEnum getMode()
116    {
117        return mode;
118    }
119
120
121    /**
122     * {@inheritDoc}
123     */
124    @Override
125    public void setMode( SynchronizationModeEnum mode )
126    {
127        this.mode = mode;
128    }
129
130
131    /**
132     * {@inheritDoc}
133     */
134    @Override
135    public boolean isReloadHint()
136    {
137        return isReloadHint;
138    }
139
140
141    /**
142     * {@inheritDoc}
143     */
144    @Override
145    public void setReloadHint( boolean reloadHint )
146    {
147        this.isReloadHint = reloadHint;
148    }
149
150
151    /**
152     * @see Object#hashCode()
153     */
154    @Override
155    public int hashCode()
156    {
157        int h = 37;
158
159        h = h * 17 + super.hashCode();
160        h = h * 17 + ( isReloadHint ? 1 : 0 );
161        h = h * 17 + mode.getValue();
162
163        if ( cookie != null )
164        {
165            for ( byte b : cookie )
166            {
167                h = h * 17 + b;
168            }
169        }
170
171        return h;
172    }
173
174
175    /**
176     * @see Object#equals(Object)
177     */
178    @Override
179    public boolean equals( Object o )
180    {
181        if ( !super.equals( o ) )
182        {
183            return false;
184        }
185
186        if ( !( o instanceof SyncRequestValue ) )
187        {
188            return false;
189        }
190
191        SyncRequestValue otherControl = ( SyncRequestValue ) o;
192
193        return ( mode == otherControl.getMode() )
194            && ( isReloadHint == otherControl.isReloadHint() )
195            && ( Arrays.equals( cookie, otherControl.getCookie() ) );
196    }
197
198
199    /**
200     * @see Object#toString()
201     */
202    @Override
203    public String toString()
204    {
205        StringBuilder sb = new StringBuilder();
206
207        sb.append( "    SyncRequestValue control :\n" );
208        sb.append( "        oid : " ).append( getOid() ).append( '\n' );
209        sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
210        sb.append( "        mode              : '" ).append( getMode() ).append( "'\n" );
211        sb.append( "        cookie            : '" ).
212            append( Strings.dumpBytes( getCookie() ) ).append( "'\n" );
213        sb.append( "        reloadHint : '" ).append( isReloadHint() ).append( "'\n" );
214
215        return sb.toString();
216    }
217}