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