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.model.ldif;
21  
22  
23  import java.io.Externalizable;
24  import java.io.IOException;
25  import java.io.ObjectInput;
26  import java.io.ObjectOutput;
27  
28  import org.apache.directory.api.ldap.model.message.Control;
29  import org.apache.directory.api.util.Strings;
30  
31  
32  /**
33   * The LdifControl class stores a control defined for an entry found in a LDIF
34   * file.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class LdifControl implements Control, Externalizable
39  {
40      /** The control type */
41      private String oid;
42  
43      /** The criticality (default value is false) */
44      private boolean criticality = false;
45  
46      /** Optional control value */
47      protected byte[] value;
48  
49  
50      /**
51       * Create a new Control
52       */
53      public LdifControl()
54      {
55      }
56  
57  
58      /**
59       * Create a new Control
60       * 
61       * @param oid OID of the created control
62       */
63      public LdifControl( String oid )
64      {
65          this.oid = oid;
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      public String toString()
73      {
74          return "LdifControl : {" + getOid() + ", " + isCritical() + ", " + Strings.dumpBytes( getValue() ) + "}";
75      }
76  
77  
78      /**
79       * {@inheritDoc}
80       */
81      public String getOid()
82      {
83          return oid;
84      }
85  
86  
87      /**
88       * {@inheritDoc}
89       */
90      public boolean isCritical()
91      {
92          return criticality;
93      }
94  
95  
96      /**
97       * {@inheritDoc}
98       */
99      public void setCritical( boolean criticality )
100     {
101         this.criticality = criticality;
102     }
103 
104 
105     /**
106      * {@inheritDoc}
107      */
108     public byte[] getValue()
109     {
110         return value;
111     }
112 
113 
114     /**
115      * {@inheritDoc}
116      */
117     public void setValue( byte[] value )
118     {
119         this.value = value;
120     }
121 
122 
123     /**
124      * {@inheritDoc}
125      */
126     public boolean hasValue()
127     {
128         return value != null;
129     }
130 
131 
132     /**
133      * {@inheritDoc}
134      */
135     public void writeExternal( ObjectOutput out ) throws IOException
136     {
137         out.writeUTF( oid );
138         out.writeBoolean( criticality );
139 
140         if ( hasValue() )
141         {
142             out.writeBoolean( true );
143             out.writeInt( value.length );
144 
145             if ( value.length > 0 )
146             {
147                 out.write( value );
148             }
149         }
150         else
151         {
152             out.writeBoolean( false );
153         }
154 
155         out.flush();
156     }
157 
158 
159     /**
160      * {@inheritDoc}
161      */
162     public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException
163     {
164         oid = in.readUTF();
165         criticality = in.readBoolean();
166 
167         if ( in.readBoolean() )
168         {
169             int valueLength = in.readInt();
170 
171             if ( valueLength > 0 )
172             {
173                 value = new byte[valueLength];
174                 in.readFully( value );
175             }
176         }
177     }
178 
179 
180     /**
181      * @see Object#hashCode()
182      */
183     public int hashCode()
184     {
185         int h = 17;
186         h = h * 37 + ( criticality ? 1 : 0 );
187         h = h * 37 + ( oid == null ? 0 : oid.hashCode() );
188 
189         if ( value != null )
190         {
191             for ( byte v : value )
192             {
193                 h = h * 37 + v;
194             }
195         }
196 
197         return h;
198     }
199 
200 
201     /**
202      * @see Object#equals(Object)
203      */
204     public boolean equals( Object o )
205     {
206         if ( o == this )
207         {
208             return true;
209         }
210 
211         if ( o == null )
212         {
213             return false;
214         }
215 
216         if ( !( o instanceof Control ) )
217         {
218             return false;
219         }
220 
221         Control otherControl = ( Control ) o;
222 
223         if ( !oid.equalsIgnoreCase( otherControl.getOid() ) )
224         {
225             return false;
226         }
227 
228         return criticality == otherControl.isCritical();
229     }
230 }