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.abandon;
021
022
023 import java.nio.BufferOverflowException;
024 import java.nio.ByteBuffer;
025
026 import org.apache.directory.shared.asn1.ber.tlv.Value;
027 import org.apache.directory.shared.asn1.codec.EncoderException;
028 import org.apache.directory.shared.ldap.codec.LdapConstants;
029 import org.apache.directory.shared.ldap.codec.LdapMessageCodec;
030 import org.slf4j.Logger;
031 import org.slf4j.LoggerFactory;
032
033
034 /**
035 * A AbandonRequest Message.
036 *
037 * Its syntax is :
038 * AbandonRequest ::= [APPLICATION 16] MessageID
039 *
040 * MessageID ::= INTEGER (0 .. maxInt)
041 *
042 * maxInt INTEGER ::= 2147483647 -- (2^^31 - 1) --
043 *
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 * @version $Rev: 764131 $, $Date: 2009-04-11 03:03:00 +0200 (Sam, 11 avr 2009) $,
046 */
047 public class AbandonRequestCodec extends LdapMessageCodec
048 {
049 /** The logger */
050 private static Logger log = LoggerFactory.getLogger( AbandonRequestCodec.class );
051
052 /** Speedup for logs */
053 private static final boolean IS_DEBUG = log.isDebugEnabled();
054
055 // ~ Instance fields
056 // ----------------------------------------------------------------------------
057
058 /** The abandoned message ID */
059 private int abandonedMessageId;
060
061
062 // ~ Constructors
063 // -------------------------------------------------------------------------------
064
065 /**
066 * Creates a new AbandonRequest object.
067 */
068 public AbandonRequestCodec()
069 {
070 super();
071 }
072
073
074 // ~ Methods
075 // ------------------------------------------------------------------------------------
076
077 /**
078 * Get the abandoned message ID
079 *
080 * @return Returns the abandoned MessageId.
081 */
082 public int getAbandonedMessageId()
083 {
084 return abandonedMessageId;
085 }
086
087
088 /**
089 * Get the message type
090 *
091 * @return Returns the type.
092 */
093 public int getMessageType()
094 {
095 return LdapConstants.ABANDON_REQUEST;
096 }
097
098
099 /**
100 * Set the abandoned message ID
101 *
102 * @param abandonedMessageId The abandoned messageID to set.
103 */
104 public void setAbandonedMessageId( int abandonedMessageId )
105 {
106 this.abandonedMessageId = abandonedMessageId;
107 }
108
109
110 /**
111 * Compute the AbandonRequest length
112 *
113 * AbandonRequest :
114 * 0x50 0x0(1..4) abandoned MessageId
115 *
116 * Length(AbandonRequest) = Length(0x50) + 1 + Length(abandoned MessageId)
117 */
118 public int computeLength()
119 {
120 int length = 1 + 1 + Value.getNbBytes( abandonedMessageId );
121
122 if ( IS_DEBUG )
123 {
124 log.debug( "Message length : {}", Integer.valueOf( length ) );
125 }
126
127 return length;
128 }
129
130
131 /**
132 * Encode the AbandonRequest message to a PDU.
133 *
134 * @param buffer The buffer where to put the PDU
135 * @return The PDU.
136 */
137 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
138 {
139 if ( buffer == null )
140 {
141 log.error( "Cannot put a PDU in a null buffer !" );
142 throw new EncoderException( "Cannot put a PDU in a null buffer !" );
143 }
144
145 try
146 {
147 // The tag
148 buffer.put( LdapConstants.ABANDON_REQUEST_TAG );
149
150 // The length. It has to be evaluated depending on
151 // the abandoned messageId value.
152 buffer.put( ( byte ) Value.getNbBytes( abandonedMessageId ) );
153
154 // The abandoned messageId
155 buffer.put( Value.getBytes( abandonedMessageId ) );
156 }
157 catch ( BufferOverflowException boe )
158 {
159 log.error( "The PDU buffer size is too small !" );
160 throw new EncoderException( "The PDU buffer size is too small !" );
161 }
162
163 return buffer;
164 }
165
166
167 /**
168 * Return a String representing an AbandonRequest
169 *
170 * @return A String representing the AbandonRequest
171 */
172 public String toString()
173 {
174
175 StringBuffer sb = new StringBuffer();
176
177 sb.append( " Abandon Request :\n" );
178 sb.append( " Message Id : " ).append( abandonedMessageId ).append( '\n' );
179
180 return sb.toString();
181 }
182 }