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    public CancelRequestDecorator( LdapApiService codec, CancelRequest decoratedMessage )
057    {
058        super( codec, decoratedMessage );
059        cancelRequest = decoratedMessage;
060    }
061
062
063    /**
064     * {@inheritDoc}
065     */
066    public int getCancelId()
067    {
068        return cancelRequest.getCancelId();
069    }
070
071
072    /**
073     * {@inheritDoc}
074     */
075    public void setCancelId( int cancelId )
076    {
077        if ( cancelId == cancelRequest.getCancelId() )
078        {
079            return;
080        }
081
082        this.requestValue = null;
083        cancelRequest.setCancelId( cancelId );
084    }
085
086
087    /**
088     * {@inheritDoc}
089     */
090    public byte[] getRequestValue()
091    {
092        if ( requestValue == null )
093        {
094            try
095            {
096                requestValue = encodeInternal().array();
097            }
098            catch ( EncoderException e )
099            {
100                LOG.error( I18n.err( I18n.ERR_04164 ), e );
101                throw new RuntimeException( e );
102            }
103        }
104
105        return requestValue;
106    }
107
108
109    /**
110     * Sets the extended request's <b>requestValue</b> portion of the PDU.
111     *
112     * @param payload byte array of data encapsulating ext. req. parameters
113     */
114    @Override
115    public void setRequestValue( byte[] requestValue )
116    {
117        CancelDecoder decoder = new CancelDecoder();
118
119        try
120        {
121            CancelRequest cancel = decoder.decode( requestValue );
122
123            if ( requestValue != null )
124            {
125                this.requestValue = new byte[requestValue.length];
126                System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length );
127            }
128            else
129            {
130                this.requestValue = null;
131            }
132
133            cancelRequest.setCancelId( cancel.getCancelId() );
134        }
135        catch ( DecoderException e )
136        {
137            LOG.error( I18n.err( I18n.ERR_04165 ), e );
138            throw new RuntimeException( e );
139        }
140    }
141
142
143    /**
144     * Compute the Cancel length 
145     * 
146     * 0x30 L1 
147     *   | 
148     *   +--> 0x02 0x0(1-4) [0..2^31-1] 
149     */
150    /* no qualifier */ int computeLengthInternal()
151    {
152        // The messageId length
153        cancelSequenceLength = 1 + 1 + BerValue.getNbBytes( cancelRequest.getCancelId() );
154
155        // Add the sequence and the length
156        return 1 + 1 + cancelSequenceLength;
157    }
158
159
160    /**
161     * Encodes the cancel extended operation.
162     * 
163     * @return A ByteBuffer that contains the encoded PDU
164     * @throws org.apache.directory.api.asn1.EncoderException If anything goes wrong.
165     */
166    /* no qualifier */ ByteBuffer encodeInternal() throws EncoderException
167    {
168        // Allocate the bytes buffer.
169        ByteBuffer bb = ByteBuffer.allocate( computeLengthInternal() );
170
171        // The sequence
172        bb.put( UniversalTag.SEQUENCE.getValue() );
173        bb.put( TLV.getBytes( cancelSequenceLength ) );
174
175        // The messageId
176        BerValue.encode( bb, cancelRequest.getCancelId() );
177
178        return bb;
179    }
180}