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.ldap.codec;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.Asn1Object;
026import org.apache.directory.api.asn1.DecoderException;
027import org.apache.directory.api.asn1.EncoderException;
028import org.apache.directory.api.i18n.I18n;
029import org.apache.directory.api.ldap.codec.api.ControlDecorator;
030import org.apache.directory.api.ldap.codec.api.LdapApiService;
031import org.apache.directory.api.ldap.model.message.Control;
032
033
034/**
035 * A decorator for handling opaque Control objects where we know nothing about 
036 * their encoded value. These Controls are generated by default when a 
037 * ControlFactory for them has not been registered with the 
038 * {@link LdapApiService}.
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 * @version $Rev$, $Date$
042 */
043public class BasicControlDecorator extends ControlDecorator<Control>
044{
045    /**
046     * Creates a new instance of BasicControlDecorator, decorating a 
047     * {@link Control}.
048     *
049     * @param codec The LDAP codec service.
050     * @param control The {@link Control} to decorate.
051     */
052    public BasicControlDecorator( LdapApiService codec, Control control )
053    {
054        super( codec, control );
055    }
056
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public Asn1Object decode( byte[] controlBytes ) throws DecoderException
063    {
064        return null;
065    }
066
067
068    /**
069     * {@inheritDoc}
070     */
071    @Override
072    public int computeLength()
073    {
074        // Call the super class to compute the global control length
075        if ( getValue() == null )
076        {
077            valueLength = 0;
078        }
079        else
080        {
081            valueLength = getValue().length;
082        }
083
084        return valueLength;
085    }
086
087
088    /**
089     * {@inheritDoc}
090     */
091    @Override
092    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
093    {
094        if ( buffer == null )
095        {
096            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
097        }
098
099        if ( valueLength != 0 )
100        {
101            buffer.put( getValue() );
102        }
103
104        return buffer;
105    }
106}