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 */
020package org.apache.directory.api.ldap.codec.decorators;
021
022
023import java.nio.BufferOverflowException;
024import java.nio.ByteBuffer;
025
026import org.apache.directory.api.asn1.EncoderException;
027import org.apache.directory.api.i18n.I18n;
028import org.apache.directory.api.ldap.codec.api.LdapApiService;
029import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
030import org.apache.directory.api.ldap.model.message.Control;
031import org.apache.directory.api.ldap.model.message.UnbindRequest;
032
033
034/**
035 * A decorator for the LdapResultResponse message
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class UnbindRequestDecorator extends RequestDecorator<UnbindRequest> implements UnbindRequest
040{
041    /**
042     * Makes Request a MessageDecorator.
043     *
044     * @param codec The LDAP service instance
045     * @param decoratedMessage the decorated message
046     */
047    public UnbindRequestDecorator( LdapApiService codec, UnbindRequest decoratedMessage )
048    {
049        super( codec, decoratedMessage );
050    }
051
052
053    /**
054     * {@inheritDoc}
055     */
056    @Override
057    public UnbindRequest setMessageId( int messageId )
058    {
059        super.setMessageId( messageId );
060
061        return this;
062    }
063
064
065    /**
066     * {@inheritDoc}
067     */
068    @Override
069    public UnbindRequest addControl( Control control )
070    {
071        return ( UnbindRequest ) super.addControl( control );
072    }
073
074
075    /**
076     * {@inheritDoc}
077     */
078    @Override
079    public UnbindRequest addAllControls( Control[] controls )
080    {
081        return ( UnbindRequest ) super.addAllControls( controls );
082    }
083
084
085    /**
086     * {@inheritDoc}
087     */
088    @Override
089    public UnbindRequest removeControl( Control control )
090    {
091        return ( UnbindRequest ) super.removeControl( control );
092    }
093
094
095    //-------------------------------------------------------------------------
096    // The Decorator methods
097    //-------------------------------------------------------------------------
098
099    /**
100     * Compute the UnBindRequest length 
101     * <br>
102     * UnBindRequest :
103     * <pre> 
104     * 0x42 00
105     * </pre>
106     */
107    @Override
108    public int computeLength()
109    {
110        // Always 2
111        return 2;
112    }
113
114
115    /**
116     * Encode the Unbind protocolOp part
117     */
118    @Override
119    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
120    {
121        try
122        {
123            // The tag
124            buffer.put( LdapCodecConstants.UNBIND_REQUEST_TAG );
125
126            // The length is always null.
127            buffer.put( ( byte ) 0 );
128        }
129        catch ( BufferOverflowException boe )
130        {
131            String msg = I18n.err( I18n.ERR_04005 );
132            throw new EncoderException( msg, boe );
133        }
134
135        return buffer;
136    }
137}