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.api;
021
022import java.nio.ByteBuffer;
023
024import org.apache.directory.api.asn1.DecoderException;
025import org.apache.directory.api.asn1.ber.Asn1Container;
026import org.apache.directory.api.asn1.ber.Asn1Decoder;
027import org.apache.directory.api.asn1.util.Asn1Buffer;
028import org.apache.directory.api.ldap.model.message.Control;
029
030/**
031 * A factory that encode the Control value
032 * 
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 * @param <C> The Control type
035 */
036public abstract class AbstractControlFactory<C extends Control> implements ControlFactory<C>
037{
038    /** The LDAP codec responsible for encoding and decoding ManageDsaIT Control */
039    protected LdapApiService codec;
040    
041    /** The control's OID */
042    protected String oid;
043
044    /**
045     *
046     * Creates a new instance of AbstractControlFactory.
047     *
048     * @param codec The LdapApiSevice instance
049     * @param oid The control's OID
050     */
051    protected AbstractControlFactory( LdapApiService codec, String oid )
052    {
053        this.codec = codec;
054        this.oid = oid;
055    }
056
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public String getOid()
063    {
064        return oid;
065    }
066
067    /**
068     * {@inheritDoc}
069     */
070    @Override
071    public void encodeValue( Asn1Buffer buffer, Control control )
072    {
073        // Nothing to do by default
074    }
075
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public void decodeValue( Control control, byte[] controlBytes ) throws DecoderException
082    {
083        // Nothing to do by default
084    }
085
086
087    /**
088     * {@inheritDoc}
089     */
090    @Override
091    public void decodeValue( ControlContainer container, Control control, byte[] controlBytes ) throws DecoderException
092    {
093        ByteBuffer buffer = ByteBuffer.wrap( controlBytes );
094        container.setControl( control );
095        Asn1Decoder.decode( buffer, ( Asn1Container ) container );
096    }
097}