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      public byte[] getCookie()
95      {
96          return this.cookie;
97      }
98  
99  
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 }