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 * @param buffer The encoded sink
117 * @return A ByteBuffer that contains the encoded PDU
118 * @throws EncoderException If anything goes wrong.
119 */
120 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
121 {
122 // Allocate the bytes buffer.
123 ByteBuffer bb = ByteBuffer.allocate( computeLength() );
124
125 // The sequence
126 bb.put( UniversalTag.SEQUENCE_TAG );
127 bb.put( TLV.getBytes( cancelSequenceLength ) );
128
129 // The messageId
130 Value.encode( bb, cancelId );
131
132 return bb;
133 }
134
135
136 /**
137 * Return a string representation of the cancel
138 */
139 public String toString()
140 {
141 StringBuffer sb = new StringBuffer();
142
143 sb.append( "Cancel extended operation" );
144 sb.append( " cancelId : " ).append( cancelId ).append( '\n' );
145
146 return sb.toString();
147 }
148 }