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 */
020
021package org.apache.directory.api.ldap.extras.controls.vlv_impl;
022
023
024import java.nio.ByteBuffer;
025
026import org.apache.directory.api.asn1.Asn1Object;
027import org.apache.directory.api.asn1.DecoderException;
028import org.apache.directory.api.asn1.EncoderException;
029import org.apache.directory.api.asn1.ber.Asn1Decoder;
030import org.apache.directory.api.asn1.ber.tlv.BerValue;
031import org.apache.directory.api.asn1.ber.tlv.TLV;
032import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
033import org.apache.directory.api.i18n.I18n;
034import org.apache.directory.api.ldap.codec.api.ControlDecorator;
035import org.apache.directory.api.ldap.codec.api.LdapApiService;
036import org.apache.directory.api.ldap.extras.controls.vlv.VirtualListViewResponse;
037import org.apache.directory.api.ldap.extras.controls.vlv.VirtualListViewResponseImpl;
038import org.apache.directory.api.ldap.extras.controls.vlv.VirtualListViewResultCode;
039
040
041/**
042 * The VirtualListView response decorator
043 *
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 */
046public class VirtualListViewResponseDecorator extends ControlDecorator<VirtualListViewResponse> implements
047    VirtualListViewResponse
048{
049    private int vlvSeqLength;
050
051    private static final Asn1Decoder DECODER = new Asn1Decoder();
052
053
054    /**
055     * Create a new SyncRequestValueDecorator instance 
056     * 
057     * @param codec The LDAP API service to use
058     */
059    public VirtualListViewResponseDecorator( LdapApiService codec )
060    {
061        this( codec, new VirtualListViewResponseImpl() );
062    }
063
064
065    /**
066     * Create a new SyncRequestValueDecorator instance 
067     * 
068     * @param codec The LDAP API service to use
069     * @param vlvRequest The decorated VLV request
070     */
071    public VirtualListViewResponseDecorator( LdapApiService codec, VirtualListViewResponse vlvRequest )
072    {
073        super( codec, vlvRequest );
074    }
075
076
077    /**
078     * {@inheritDoc}
079     */
080    @Override
081    public int computeLength()
082    {
083        vlvSeqLength = 1 + 1 + BerValue.getNbBytes( getTargetPosition() );
084        vlvSeqLength += 1 + 1 + BerValue.getNbBytes( getContentCount() );
085
086        // result code : always one byte long
087        vlvSeqLength += 1 + 1 + 1;
088
089        if ( getContextId() != null )
090        {
091            vlvSeqLength += 1 + TLV.getNbBytes( getContextId().length ) + getContextId().length;
092        }
093
094        valueLength = 1 + TLV.getNbBytes( vlvSeqLength ) + vlvSeqLength;
095
096        return valueLength;
097    }
098
099
100    /**
101     * {@inheritDoc}
102     */
103    @Override
104    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
105    {
106        if ( buffer == null )
107        {
108            throw new EncoderException( I18n.err( I18n.ERR_04023 ) );
109        }
110
111        buffer.put( UniversalTag.SEQUENCE.getValue() );
112        buffer.put( TLV.getBytes( vlvSeqLength ) );
113
114        BerValue.encode( buffer, getTargetPosition() );
115        BerValue.encode( buffer, getContentCount() );
116
117        BerValue.encodeEnumerated( buffer, getVirtualListViewResult().getValue() );
118
119        if ( getContextId() != null )
120        {
121            BerValue.encode( buffer, getContextId() );
122        }
123
124        return buffer;
125    }
126
127
128    /**
129     * {@inheritDoc}
130     */
131    @Override
132    public byte[] getValue()
133    {
134        if ( value == null )
135        {
136            try
137            {
138                computeLength();
139                ByteBuffer buffer = ByteBuffer.allocate( valueLength );
140
141                value = encode( buffer ).array();
142            }
143            catch ( Exception e )
144            {
145                return null;
146            }
147        }
148
149        return value;
150    }
151
152
153    /**
154     * {@inheritDoc}
155     */
156    @Override
157    public Asn1Object decode( byte[] controlBytes ) throws DecoderException
158    {
159        ByteBuffer buffer = ByteBuffer.wrap( controlBytes );
160        VirtualListViewResponseContainer container = new VirtualListViewResponseContainer( this, getCodecService() );
161        DECODER.decode( buffer, container );
162
163        return this;
164    }
165
166
167    /**
168     * {@inheritDoc}
169     */
170    @Override
171    public int getTargetPosition()
172    {
173        return getDecorated().getTargetPosition();
174    }
175
176
177    /**
178     * {@inheritDoc}
179     */
180    @Override
181    public void setTargetPosition( int targetPosition )
182    {
183        getDecorated().setTargetPosition( targetPosition );
184    }
185
186
187    /**
188     * {@inheritDoc}
189     */
190    @Override
191    public int getContentCount()
192    {
193        return getDecorated().getContentCount();
194    }
195
196
197    /**
198     * {@inheritDoc}
199     */
200    @Override
201    public void setContentCount( int contentCount )
202    {
203        getDecorated().setContentCount( contentCount );
204    }
205
206
207    /**
208     * {@inheritDoc}
209     */
210    @Override
211    public VirtualListViewResultCode getVirtualListViewResult()
212    {
213        return getDecorated().getVirtualListViewResult();
214    }
215
216
217    /**
218     * {@inheritDoc}
219     */
220    @Override
221    public void setVirtualListViewResult( VirtualListViewResultCode virtualListViewResult )
222    {
223        getDecorated().setVirtualListViewResult( virtualListViewResult );
224    }
225
226
227    /**
228     * {@inheritDoc}
229     */
230    @Override
231    public byte[] getContextId()
232    {
233        return getDecorated().getContextId();
234    }
235
236
237    /**
238     * {@inheritDoc}
239     */
240    @Override
241    public void setContextId( byte[] contextId )
242    {
243        getDecorated().setContextId( contextId );
244    }
245
246}