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.extras.extended.ads_impl.cancel;
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.tlv.BerValue;
029import org.apache.directory.api.asn1.ber.tlv.TLV;
030import org.apache.directory.api.asn1.ber.tlv.UniversalTag;
031import org.apache.directory.api.i18n.I18n;
032import org.apache.directory.api.ldap.codec.api.ExtendedRequestDecorator;
033import org.apache.directory.api.ldap.codec.api.LdapApiService;
034import org.apache.directory.api.ldap.extras.extended.cancel.CancelRequest;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038
039/**
040 * A Decorator for CancelRequests.
041 *
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044public class CancelRequestDecorator extends ExtendedRequestDecorator<CancelRequest> implements
045    CancelRequest, Asn1Object
046{
047    private static final Logger LOG = LoggerFactory.getLogger( CancelRequestDecorator.class );
048
049    /** The Id of the the message to cancel */
050    private CancelRequest cancelRequest;
051
052    /** Length of the sequence */
053    private int cancelSequenceLength;
054
055
056    /**
057     * Creates a new instance of CancelRequestDecorator.
058     * 
059     * @param codec The LDAP Service to use
060     * @param decoratedMessage The canceled request
061     */
062    public CancelRequestDecorator( LdapApiService codec, CancelRequest decoratedMessage )
063    {
064        super( codec, decoratedMessage );
065        cancelRequest = decoratedMessage;
066    }
067
068
069    /**
070     * {@inheritDoc}
071     */
072    @Override
073    public int getCancelId()
074    {
075        return cancelRequest.getCancelId();
076    }
077
078
079    /**
080     * {@inheritDoc}
081     */
082    @Override
083    public void setCancelId( int cancelId )
084    {
085        if ( cancelId == cancelRequest.getCancelId() )
086        {
087            return;
088        }
089
090        this.requestValue = null;
091        cancelRequest.setCancelId( cancelId );
092    }
093
094
095    /**
096     * {@inheritDoc}
097     */
098    @Override
099    public byte[] getRequestValue()
100    {
101        if ( requestValue == null )
102        {
103            try
104            {
105                requestValue = encodeInternal().array();
106            }
107            catch ( EncoderException e )
108            {
109                LOG.error( I18n.err( I18n.ERR_04164 ), e );
110                throw new RuntimeException( e );
111            }
112        }
113
114        return requestValue;
115    }
116
117
118    /**
119     * Sets the extended request's <b>requestValue</b> portion of the PDU.
120     *
121     * @param requestValue byte array of data encapsulating ext. req. parameters
122     */
123    @Override
124    public void setRequestValue( byte[] requestValue )
125    {
126        CancelDecoder decoder = new CancelDecoder();
127
128        try
129        {
130            if ( requestValue != null )
131            {
132                CancelRequest cancel = decoder.decode( requestValue );
133                cancelRequest.setCancelId( cancel.getCancelId() );
134
135                this.requestValue = new byte[requestValue.length];
136                System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length );
137            }
138            else
139            {
140                this.requestValue = null;
141                cancelRequest.setCancelId( 0 );
142            }
143
144        }
145        catch ( DecoderException e )
146        {
147            LOG.error( I18n.err( I18n.ERR_04165 ), e );
148            throw new RuntimeException( e );
149        }
150    }
151
152
153    /**
154     * Compute the Cancel length 
155     * <pre>
156     * 0x30 L1 
157     *   | 
158     *   +--&gt; 0x02 0x0(1-4) [0..2^31-1]
159     * </pre> 
160     */
161    /* no qualifier */int computeLengthInternal()
162    {
163        // The messageId length
164        cancelSequenceLength = 1 + 1 + BerValue.getNbBytes( cancelRequest.getCancelId() );
165
166        // Add the sequence and the length
167        return 1 + 1 + cancelSequenceLength;
168    }
169
170
171    /**
172     * Encodes the cancel extended operation.
173     * 
174     * @return A ByteBuffer that contains the encoded PDU
175     * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
176     */
177    /* no qualifier */ByteBuffer encodeInternal() throws EncoderException
178    {
179        // Allocate the bytes buffer.
180        ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
181
182        // The sequence
183        bb.put( UniversalTag.SEQUENCE.getValue() );
184        bb.put( TLV.getBytes( cancelSequenceLength ) );
185
186        // The messageId
187        BerValue.encode( bb, cancelRequest.getCancelId() );
188
189        return bb;
190    }
191}