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.unbind;
021    
022    
023    import java.nio.BufferOverflowException;
024    import java.nio.ByteBuffer;
025    
026    import org.apache.directory.shared.asn1.codec.EncoderException;
027    import org.apache.directory.shared.ldap.codec.LdapConstants;
028    import org.apache.directory.shared.ldap.codec.LdapMessageCodec;
029    
030    
031    /**
032     * A UnBindRequest ldapObject. 
033     * 
034     * Its syntax is : 
035     * UnbindRequest ::= [APPLICATION 2] NULL 
036     * 
037     * This ldapObject is empty.
038     * 
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev: 764131 $, $Date: 2009-04-11 03:03:00 +0200 (Sam, 11 avr 2009) $, 
041     */
042    public class UnBindRequestCodec extends LdapMessageCodec
043    {
044        // ~ Instance fields
045        // ----------------------------------------------------------------------------
046    
047        // ~ Constructors
048        // -------------------------------------------------------------------------------
049    
050        /**
051         * Creates a new BindRequest object.
052         */
053        public UnBindRequestCodec()
054        {
055            super();
056        }
057    
058    
059        // ~ Methods
060        // ------------------------------------------------------------------------------------
061    
062        /**
063         * Get the message type
064         * 
065         * @return Returns the type.
066         */
067        public int getMessageType()
068        {
069            return LdapConstants.UNBIND_REQUEST;
070        }
071    
072    
073        /**
074         * Compute the UnBindRequest length 
075         * 
076         * UnBindRequest : 
077         * 0x42 00
078         */
079        public int computeLength()
080        {
081            return 2; // Always 2
082        }
083    
084    
085        /**
086         * Encode the UnbindRequest message to a PDU.
087         * 
088         * @param buffer The buffer where to put the PDU
089         * @return The PDU.
090         */
091        public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
092        {
093            if ( buffer == null )
094            {
095                throw new EncoderException( "Cannot put a PDU in a null buffer !" );
096            }
097    
098            try
099            {
100                // The tag
101                buffer.put( LdapConstants.UNBIND_REQUEST_TAG );
102    
103                // The length is always null.
104                buffer.put( ( byte ) 0 );
105            }
106            catch ( BufferOverflowException boe )
107            {
108                throw new EncoderException( "The PDU buffer size is too small !" );
109            }
110    
111            return buffer;
112        }
113    
114    
115        /**
116         * Get a String representation of a UnBindRequest
117         * 
118         * @return A UnBindRequest String
119         */
120        public String toString()
121        {
122    
123            StringBuffer sb = new StringBuffer();
124    
125            sb.append( "    UnBind Request\n" );
126    
127            return sb.toString();
128        }
129    }