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     * @param codec The LDAP service instance
053     */
054    public SubentriesDecorator( LdapApiService codec )
055    {
056        this( codec, new SubentriesImpl() );
057    }
058
059
060    /**
061     * Creates a Subentries decorating implementation for use with the codec,
062     * while decorating the supplied Subentries control.
063     *
064     * @param codec The LDAP service instance
065     * @param control The Subentries Control to wrap with this decorator.
066     */
067    public SubentriesDecorator( LdapApiService codec, Subentries control )
068    {
069        super( codec, control );
070    }
071
072
073    /**
074     * Compute the SubEntryControl length 
075     * <pre>
076     * 0x01 0x01 [0x00|0xFF]
077     * </pre>
078     * 
079     * @return the control length.
080     */
081    @Override
082    public int computeLength()
083    {
084        return 1 + 1 + 1;
085    }
086
087
088    /**
089     * Encodes the Subentries control.
090     * 
091     * @param buffer The encoded sink
092     * @return A ByteBuffer that contains the encoded PDU
093     * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
094     */
095    @Override
096    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
097    {
098        if ( buffer == null )
099        {
100            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
101        }
102
103        // Now encode the Subentries specific part
104        BerValue.encode( buffer, isVisible() );
105
106        return buffer;
107    }
108
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    public byte[] getValue()
115    {
116        if ( value == null )
117        {
118            try
119            {
120                computeLength();
121                ByteBuffer buffer = ByteBuffer.allocate( valueLength );
122
123                // Now encode the Subentries specific part
124                BerValue.encode( buffer, isVisible() );
125
126                value = buffer.array();
127            }
128            catch ( Exception e )
129            {
130                return null;
131            }
132        }
133
134        return value;
135    }
136
137
138    /**
139     * {@inheritDoc}
140     */
141    @Override
142    public boolean isVisible()
143    {
144        return getDecorated().isVisible();
145    }
146
147
148    /**
149     * {@inheritDoc}
150     */
151    @Override
152    public void setVisibility( boolean visibility )
153    {
154        getDecorated().setVisibility( visibility );
155    }
156
157
158    /**
159     * {@inheritDoc}
160     */
161    @Override
162    public Asn1Object decode( byte[] controlBytes ) throws DecoderException
163    {
164        ByteBuffer bb = ByteBuffer.wrap( controlBytes );
165        SubentriesContainer container = new SubentriesContainer( this );
166        DECODER.decode( bb, container );
167
168        return this;
169    }
170}