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