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.controls.search.subentries;
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.asn1.ber.Asn1Decoder;
029import org.apache.directory.api.asn1.ber.tlv.BerValue;
030import org.apache.directory.api.i18n.I18n;
031import org.apache.directory.api.ldap.codec.api.ControlDecorator;
032import org.apache.directory.api.ldap.codec.api.LdapApiService;
033import org.apache.directory.api.ldap.model.message.controls.Subentries;
034import org.apache.directory.api.ldap.model.message.controls.SubentriesImpl;
035
036
037/**
038 * A Subentries Control implementation which wraps and decorates Subentries
039 * Controls to enable them to be encoded and decoded by the codec.
040 * 
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 */
043public class SubentriesDecorator extends ControlDecorator<Subentries> implements Subentries
044{
045    /** The sub entry decoder */
046    private static final Asn1Decoder decoder = new Asn1Decoder();
047
048
049    /**
050     * Default constructor
051     */
052    public SubentriesDecorator( LdapApiService codec )
053    {
054        this( codec, new SubentriesImpl() );
055    }
056
057
058    /**
059     * Creates a Subentries decorating implementation for use with the codec,
060     * while decorating the supplied Subentries control.
061     *
062     * @param control The Subentries Control to wrap with this decorator.
063     */
064    public SubentriesDecorator( LdapApiService codec, Subentries control )
065    {
066        super( codec, control );
067    }
068
069
070    /**
071     * Compute the SubEntryControl length 0x01 0x01 [0x00|0xFF]
072     */
073    public int computeLength()
074    {
075        return 1 + 1 + 1;
076    }
077
078
079    /**
080     * Encodes the Subentries control.
081     * 
082     * @param buffer The encoded sink
083     * @return A ByteBuffer that contains the encoded PDU
084     * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
085     */
086    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
087    {
088        if ( buffer == null )
089        {
090            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
091        }
092
093        // Now encode the Subentries specific part
094        BerValue.encode( buffer, isVisible() );
095
096        return buffer;
097    }
098
099
100    /**
101     * {@inheritDoc}
102     */
103    public byte[] getValue()
104    {
105        if ( value == null )
106        {
107            try
108            {
109                computeLength();
110                ByteBuffer buffer = ByteBuffer.allocate( valueLength );
111
112                // Now encode the Subentries specific part
113                BerValue.encode( buffer, isVisible() );
114
115                value = buffer.array();
116            }
117            catch ( Exception e )
118            {
119                return null;
120            }
121        }
122
123        return value;
124    }
125
126
127    public boolean isVisible()
128    {
129        return getDecorated().isVisible();
130    }
131
132
133    public void setVisibility( boolean visibility )
134    {
135        getDecorated().setVisibility( visibility );
136    }
137
138
139    /**
140     * {@inheritDoc}
141     */
142    public Asn1Object decode( byte[] controlBytes ) throws DecoderException
143    {
144        ByteBuffer bb = ByteBuffer.wrap( controlBytes );
145        SubentriesContainer container = new SubentriesContainer( this );
146        decoder.decode( bb, container );
147
148        return this;
149    }
150}