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    public byte[] getCookie()
095    {
096        return this.cookie;
097    }
098
099
100    /**
101     * {@inheritDoc}
102     */
103    public void setCookie( byte[] cookie )
104    {
105        this.cookie = cookie;
106    }
107
108
109    /**
110     * {@inheritDoc}
111     */
112    public SynchronizationModeEnum getMode()
113    {
114        return mode;
115    }
116
117
118    /**
119     * {@inheritDoc}
120     */
121    public void setMode( SynchronizationModeEnum mode )
122    {
123        this.mode = mode;
124    }
125
126
127    /**
128     * {@inheritDoc}
129     */
130    public boolean isReloadHint()
131    {
132        return isReloadHint;
133    }
134
135
136    /**
137     * {@inheritDoc}
138     */
139    public void setReloadHint( boolean reloadHint )
140    {
141        this.isReloadHint = reloadHint;
142    }
143
144
145    /**
146     * @see Object#hashCode()
147     */
148    @Override
149    public int hashCode()
150    {
151        int h = 37;
152
153        h = h * 17 + super.hashCode();
154        h = h * 17 + ( isReloadHint ? 1 : 0 );
155        h = h * 17 + mode.getValue();
156
157        if ( cookie != null )
158        {
159            for ( byte b : cookie )
160            {
161                h = h * 17 + b;
162            }
163        }
164
165        return h;
166    }
167
168
169    /**
170     * @see Object#equals(Object)
171     */
172    @Override
173    public boolean equals( Object o )
174    {
175        if ( !super.equals( o ) )
176        {
177            return false;
178        }
179
180        if ( !( o instanceof SyncRequestValue ) )
181        {
182            return false;
183        }
184
185        SyncRequestValue otherControl = ( SyncRequestValue ) o;
186
187        return ( mode == otherControl.getMode() ) &&
188            ( isReloadHint == otherControl.isReloadHint() ) &&
189            ( Arrays.equals( cookie, otherControl.getCookie() ) );
190    }
191
192
193    /**
194     * @see Object#toString()
195     */
196    @Override
197    public String toString()
198    {
199        StringBuilder sb = new StringBuilder();
200
201        sb.append( "    SyncRequestValue control :\n" );
202        sb.append( "        oid : " ).append( getOid() ).append( '\n' );
203        sb.append( "        critical : " ).append( isCritical() ).append( '\n' );
204        sb.append( "        mode              : '" ).append( getMode() ).append( "'\n" );
205        sb.append( "        cookie            : '" ).
206            append( Strings.dumpBytes( getCookie() ) ).append( "'\n" );
207        sb.append( "        reloadHint : '" ).append( isReloadHint() ).append( "'\n" );
208
209        return sb.toString();
210    }
211}