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