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 *     http://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.dsmlv2;
021
022
023import org.apache.directory.api.ldap.codec.api.LdapApiService;
024import org.apache.directory.api.ldap.model.message.Control;
025import org.dom4j.Element;
026
027
028/**
029 * A DSML decorator for a {@link Control}.
030 *
031 * @param <C> The decorated Control
032 * 
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class DsmlControl<C extends Control> implements Control, DsmlDecorator<C>
036{
037    /** The decorated Control */
038    private C decorated;
039
040    /** The encoded value of the control. */
041    protected byte[] value;
042
043    /** The codec service responsible for encoding decoding this object */
044    private LdapApiService codec;
045
046
047    /**
048     * Creates a new instance of DsmlControl
049     * @param codec The Codec used to encode/decode the Control
050     * @param decorated The decorated control
051     */
052    public DsmlControl( LdapApiService codec, C decorated )
053    {
054        this.codec = codec;
055        this.decorated = decorated;
056    }
057
058
059    /**
060     * @return The LDAP codec service.
061     */
062    public LdapApiService getCodecService()
063    {
064        return codec;
065    }
066
067
068    /**
069     * Checks to see if this DSML control decorator has a value.
070     *
071     * @return true if the DSML control has a value, false otherwise.
072     */
073    public boolean hasValue()
074    {
075        return value != null;
076    }
077
078
079    /**
080     * Gets the control value
081     * 
082     * @return The control value
083     */
084    public byte[] getValue()
085    {
086        return value;
087    }
088
089
090    /**
091     * Sets the encoded control value
092     * 
093     * @param value The encoded control value to store
094     */
095    public void setValue( byte[] value )
096    {
097        if ( value != null )
098        {
099            byte[] copy = new byte[value.length];
100            System.arraycopy( value, 0, copy, 0, value.length );
101            this.value = copy;
102        }
103        else
104        {
105            this.value = null;
106        }
107    }
108
109
110    /**
111     * {@inheritDoc}
112     */
113    public String getOid()
114    {
115        return decorated.getOid();
116    }
117
118
119    /**
120     * {@inheritDoc}
121     */
122    public boolean isCritical()
123    {
124        return decorated.isCritical();
125    }
126
127
128    /**
129     * {@inheritDoc}
130     */
131    public void setCritical( boolean isCritical )
132    {
133        decorated.setCritical( isCritical );
134    }
135
136
137    /**
138     * {@inheritDoc}
139     */
140    public Element toDsml( Element root )
141    {
142        return null;
143    }
144
145
146    /**
147     * {@inheritDoc}
148     */
149    public C getDecorated()
150    {
151        return decorated;
152    }
153}