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>
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 decoratedControl The Control to decorate.
54       */
55      public ControlDecorator( LdapApiService codec, E decoratedControl )
56      {
57          this.decorated = decoratedControl;
58          this.codec = codec;
59      }
60  
61  
62      /**
63       * {@inheritDoc}
64       */
65      public E getDecorated()
66      {
67          return decorated;
68      }
69  
70  
71      /**
72       * {@inheritDoc}
73       */
74      public void setDecorated( E decorated )
75      {
76          this.decorated = decorated;
77      }
78  
79  
80      /**
81       * {@inheritDoc}
82       */
83      public LdapApiService getCodecService()
84      {
85          return codec;
86      }
87  
88  
89      // ------------------------------------------------------------------------
90      // Control Methods
91      // ------------------------------------------------------------------------
92  
93      /**
94       * Get the OID
95       * 
96       * @return A string which represent the control oid
97       */
98      public String getOid()
99      {
100         return decorated.getOid();
101     }
102 
103 
104     /**
105      * {@inheritDoc}
106      */
107     public boolean hasValue()
108     {
109         return value != null;
110     }
111 
112 
113     /**
114      * Get the control value
115      * 
116      * @return The control value
117      */
118     public byte[] getValue()
119     {
120         return value;
121     }
122 
123 
124     /**
125      * Set the encoded control value
126      * 
127      * @param value The encoded control value to store
128      */
129     public void setValue( byte[] value )
130     {
131         if ( value != null )
132         {
133             byte[] copy = new byte[value.length];
134             System.arraycopy( value, 0, copy, 0, value.length );
135             this.value = copy;
136         }
137         else
138         {
139             this.value = null;
140         }
141     }
142 
143 
144     /**
145      * Get the criticality
146      * 
147      * @return <code>true</code> if the criticality flag is true.
148      */
149     public boolean isCritical()
150     {
151         return decorated.isCritical();
152     }
153 
154 
155     /**
156      * Set the criticality
157      * 
158      * @param criticality The criticality value
159      */
160     public void setCritical( boolean criticality )
161     {
162         decorated.setCritical( criticality );
163     }
164 
165 
166     // ------------------------------------------------------------------------
167     // CodecControl Methods
168     // ------------------------------------------------------------------------
169 
170     /**
171      * {@inheritDoc}
172      */
173     public int computeLength()
174     {
175         return 0;
176     }
177 
178 
179     // ------------------------------------------------------------------------
180     // Object Method Overrides
181     // ------------------------------------------------------------------------
182 
183     /**
184      * @see Object#hashCode()
185      */
186     public int hashCode()
187     {
188         return decorated.hashCode();
189     }
190 
191 
192     /**
193      * @see Object#equals(Object)
194      */
195     public boolean equals( Object o )
196     {
197         if ( decorated == null )
198         {
199             return o == null;
200         }
201         else
202         {
203             return decorated.equals( o );
204         }
205     }
206 
207 
208     /**
209      * Return a String representing a Control
210      */
211     public String toString()
212     {
213         return decorated.toString();
214     }
215 }