View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.api.ldap.extras.controls.syncrepl.syncInfoValue;
21  
22  
23  import java.util.Arrays;
24  
25  import org.apache.directory.api.ldap.extras.controls.SynchronizationModeEnum;
26  import org.apache.directory.api.ldap.model.message.controls.AbstractControl;
27  import org.apache.directory.api.util.Strings;
28  
29  
30  /**
31   * A syncRequestValue object, as defined in RFC 4533 :
32   * <pre>
33   * 2.2.  Sync Request Control
34   *
35   *    The Sync Request Control is an LDAP Control [RFC4511] where the
36   *    controlType is the object identifier 1.3.6.1.4.1.4203.1.9.1.1 and the
37   *    controlValue, an OCTET STRING, contains a BER-encoded
38   *    syncRequestValue.  The criticality field is either TRUE or FALSE.
39   *
40   *       syncRequestValue ::= SEQUENCE {
41   *           mode ENUMERATED {
42   *               -- 0 unused
43   *               refreshOnly       (1),
44   *               -- 2 reserved
45   *               refreshAndPersist (3)
46   *           },
47   *           cookie     syncCookie OPTIONAL,
48   *           reloadHint BOOLEAN DEFAULT FALSE
49   *       }
50   *
51   *    The Sync Request Control is only applicable to the SearchRequest
52   *    Message.
53   * </pre>
54   *
55   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
56   * @version $Rev$, $Date$
57   */
58  public class SyncRequestValueImpl extends AbstractControl implements SyncRequestValue
59  {
60      /** The synchronization type */
61      private SynchronizationModeEnum mode;
62  
63      /** The Sync cookie */
64      private byte[] cookie;
65  
66      /** The reloadHint flag */
67      private boolean isReloadHint;
68  
69  
70      /**
71       * Creates a new instance of SyncRequestValueImpl.
72       */
73      public SyncRequestValueImpl()
74      {
75          super( OID );
76      }
77  
78  
79      /**
80       *
81       * Creates a new instance of SyncRequestValueImpl.
82       *
83       * @param isCritical The critical flag
84       */
85      public SyncRequestValueImpl( boolean isCritical )
86      {
87          super( OID, isCritical );
88      }
89  
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public byte[] getCookie()
96      {
97          return this.cookie;
98      }
99  
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 }