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    package org.apache.directory.shared.ldap.codec.extended.operations.cancel;
021    
022    
023    import java.nio.ByteBuffer;
024    
025    import org.apache.directory.shared.asn1.AbstractAsn1Object;
026    import org.apache.directory.shared.asn1.ber.tlv.TLV;
027    import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
028    import org.apache.directory.shared.asn1.ber.tlv.Value;
029    import org.apache.directory.shared.asn1.codec.EncoderException;
030    
031    
032    /**
033     * An extended operation to proceed a Cancel operation, as described 
034     * in RFC 3909
035     * 
036     * <pre>
037     *   cancelRequestValue ::= SEQUENCE {
038     *       cancelID        MessageID
039     *                       -- MessageID is as defined in [RFC2251]
040     *   }
041     * </pre>
042     * 
043     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044     * @version $Rev: 687720 $, $Date: 2008-08-21 14:05:50 +0200 (Thu, 21 Aug 2008) $, 
045     */
046    public class Cancel extends AbstractAsn1Object
047    {
048        /** The Id of the the message to cancel */
049        private int cancelId;
050        
051        /** Length of the sequence */
052        private int cancelSequenceLength;
053    
054        /**
055         * Create a Cancel object, with a messageId
056         * 
057         * @param cancelId The Id of the request to cancel
058         */
059        public Cancel( int cancelId )
060        {
061            this.cancelId = cancelId;
062        }
063    
064    
065        /**
066         * Default constructor.
067         */
068        public Cancel()
069        {
070            super();
071        }
072    
073    
074        /**
075         * Get the message Id of the request to cancel
076         * 
077         * @return The id of the request to cancel
078         */
079        public int getCancelId()
080        {
081            return cancelId;
082        }
083    
084    
085        /**
086         * Set the cancelId
087         * 
088         * @param cancelId The Id of the request to cancel
089         */
090        public void setCancelId( int cancelId )
091        {
092            this.cancelId = cancelId;
093        }
094    
095    
096        /**
097         * Compute the Cancel length 
098         * 
099         * 0x30 L1 
100         *   | 
101         *   +--> 0x02 0x0(1-4) [0..2^31-1] 
102         */
103        public int computeLength()
104        {
105            // The messageId length
106            cancelSequenceLength = 1 + 1 + Value.getNbBytes( cancelId );
107    
108            // Add the sequence and the length
109            return 1 + 1 + cancelSequenceLength;
110        }
111    
112    
113        /**
114         * Encodes the cancel extended operation.
115         * 
116         * @return A ByteBuffer that contains the encoded PDU
117         * @throws EncoderException If anything goes wrong.
118         */
119        public ByteBuffer encode() throws EncoderException
120        {
121            // Allocate the bytes buffer.
122            ByteBuffer bb = ByteBuffer.allocate( computeLength() );
123    
124            // The sequence
125            bb.put( UniversalTag.SEQUENCE_TAG );
126            bb.put( TLV.getBytes( cancelSequenceLength ) );
127    
128            // The messageId
129            Value.encode( bb, cancelId );
130    
131            return bb;
132        }
133    
134    
135        /**
136         * Return a string representation of the cancel
137         */
138        public String toString()
139        {
140            StringBuffer sb = new StringBuffer();
141    
142            sb.append( "Cancel extended operation" );
143            sb.append( "    cancelId : " ).append( cancelId ).append( '\n' );
144    
145            return sb.toString();
146        }
147    }