001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    https://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.codec.api;
021
022
023import org.apache.directory.api.ldap.model.message.Control;
024
025
026/**
027 * Decorates Control objects by wrapping them, and enabling them as CodecControls
028 * so the codec to store transient information associated with the Control in the
029 * decorator while processing.
030 * 
031 * @param <E> The control type
032 * 
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public abstract class ControlDecorator<E extends Control> implements CodecControl<E>
036{
037    /** The decorated Control */
038    private E decorated;
039
040    /** The encoded value length */
041    protected int valueLength;
042
043    /** The encoded value of the control. */
044    protected byte[] value;
045
046    /** The codec service responsible for encoding decoding this object */
047    private LdapApiService codec;
048
049
050    /**
051     * Creates a ControlDecorator to codec enable it.
052     *
053     * @param codec The Ldap service to use
054     * @param decoratedControl The Control to decorate.
055     */
056    public ControlDecorator( LdapApiService codec, E decoratedControl )
057    {
058        this.decorated = decoratedControl;
059        this.codec = codec;
060    }
061
062
063    /**
064     * {@inheritDoc}
065     */
066    @Override
067    public E getDecorated()
068    {
069        return decorated;
070    }
071
072
073    /**
074     * Set the control to be decorated.
075     * 
076     * @param decorated The decorated control
077     */
078    public void setDecorated( E decorated )
079    {
080        this.decorated = decorated;
081    }
082
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public LdapApiService getCodecService()
089    {
090        return codec;
091    }
092
093
094    // ------------------------------------------------------------------------
095    // Control Methods
096    // ------------------------------------------------------------------------
097
098    /**
099     * Get the 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    // Object Method Overrides
179    // ------------------------------------------------------------------------
180
181    /**
182     * @see Object#hashCode()
183     */
184    @Override
185    public int hashCode()
186    {
187        return decorated.hashCode();
188    }
189
190
191    /**
192     * @see Object#equals(Object)
193     */
194    @Override
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    @Override
212    public String toString()
213    {
214        return decorated.toString();
215    }
216}