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.codec.api;
21  
22  
23  import org.apache.directory.api.asn1.Asn1Object;
24  import org.apache.directory.api.ldap.model.message.Control;
25  
26  
27  /**
28   * Decorates Control objects by wrapping them, and enabling them as CodecControls
29   * so the codec to store transient information associated with the Control in the
30   * decorator while processing.
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   * @param <E> The control type
34   */
35  public abstract class ControlDecorator<E extends Control> implements CodecControl<E>, Asn1Object
36  {
37      /** The decorated Control */
38      private E decorated;
39  
40      /** The encoded value length */
41      protected int valueLength;
42  
43      /** The encoded value of the control. */
44      protected byte[] value;
45  
46      /** The codec service responsible for encoding decoding this object */
47      private LdapApiService codec;
48  
49  
50      /**
51       * Creates a ControlDecorator to codec enable it.
52       *
53       * @param codec The Ldap service to use
54       * @param decoratedControl The Control to decorate.
55       */
56      public ControlDecorator( LdapApiService codec, E decoratedControl )
57      {
58          this.decorated = decoratedControl;
59          this.codec = codec;
60      }
61  
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public E getDecorated()
68      {
69          return decorated;
70      }
71  
72  
73      /**
74       * Set the control to be decorated.
75       * 
76       * @param decorated The decorated control
77       */
78      public void setDecorated( E decorated )
79      {
80          this.decorated = decorated;
81      }
82  
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public LdapApiService getCodecService()
89      {
90          return codec;
91      }
92  
93  
94      // ------------------------------------------------------------------------
95      // Control Methods
96      // ------------------------------------------------------------------------
97  
98      /**
99       * Get the control OID
100      * 
101      * @return A string which represent the control oid
102      */
103     @Override
104     public String getOid()
105     {
106         return decorated.getOid();
107     }
108 
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public boolean hasValue()
115     {
116         return value != null;
117     }
118 
119 
120     /**
121      * Get the control value
122      * 
123      * @return The control value
124      */
125     @Override
126     public byte[] getValue()
127     {
128         return value;
129     }
130 
131 
132     /**
133      * Set the encoded control value
134      * 
135      * @param value The encoded control value to store
136      */
137     @Override
138     public void setValue( byte[] value )
139     {
140         if ( value != null )
141         {
142             byte[] copy = new byte[value.length];
143             System.arraycopy( value, 0, copy, 0, value.length );
144             this.value = copy;
145         }
146         else
147         {
148             this.value = null;
149         }
150     }
151 
152 
153     /**
154      * Get the criticality
155      * 
156      * @return <code>true</code> if the criticality flag is true.
157      */
158     @Override
159     public boolean isCritical()
160     {
161         return decorated.isCritical();
162     }
163 
164 
165     /**
166      * Set the criticality
167      * 
168      * @param criticality The criticality value
169      */
170     @Override
171     public void setCritical( boolean criticality )
172     {
173         decorated.setCritical( criticality );
174     }
175 
176 
177     // ------------------------------------------------------------------------
178     // CodecControl Methods
179     // ------------------------------------------------------------------------
180     /**
181      * {@inheritDoc}
182      */
183     @Override
184     public int computeLength()
185     {
186         return 0;
187     }
188 
189 
190     // ------------------------------------------------------------------------
191     // Object Method Overrides
192     // ------------------------------------------------------------------------
193     /**
194      * @see Object#hashCode()
195      */
196     @Override
197     public int hashCode()
198     {
199         return decorated.hashCode();
200     }
201 
202 
203     /**
204      * @see Object#equals(Object)
205      */
206     @Override
207     public boolean equals( Object o )
208     {
209         if ( decorated == null )
210         {
211             return o == null;
212         }
213         else
214         {
215             return decorated.equals( o );
216         }
217     }
218 
219 
220     /**
221      * Return a String representing a Control
222      */
223     @Override
224     public String toString()
225     {
226         return decorated.toString();
227     }
228 }