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.asn1.ber.tlv.BerValue;
028import org.apache.directory.api.i18n.I18n;
029import org.apache.directory.api.ldap.codec.api.LdapApiService;
030import org.apache.directory.api.ldap.codec.api.LdapCodecConstants;
031import org.apache.directory.api.ldap.model.message.AbandonRequest;
032import org.apache.directory.api.ldap.model.message.Control;
033
034
035/**
036 * A decorator for the AddRequest message
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public final class AbandonRequestDecorator extends RequestDecorator<AbandonRequest> implements AbandonRequest
041{
042    /**
043     * Makes a AddRequest a MessageDecorator.
044     *
045     * @param codec The LDAP service instance
046     * @param decoratedMessage the decorated AddRequest
047     */
048    public AbandonRequestDecorator( LdapApiService codec, AbandonRequest decoratedMessage )
049    {
050        super( codec, decoratedMessage );
051    }
052
053
054    //-------------------------------------------------------------------------
055    // The AbandonRequest methods
056    //-------------------------------------------------------------------------
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public int getAbandoned()
063    {
064        return getDecorated().getAbandoned();
065    }
066
067
068    /**
069     * {@inheritDoc}
070     */
071    @Override
072    public AbandonRequest setAbandoned( int requestId )
073    {
074        getDecorated().setAbandoned( requestId );
075
076        return this;
077    }
078
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public AbandonRequest setMessageId( int messageId )
085    {
086        super.setMessageId( messageId );
087
088        return this;
089    }
090
091
092    /**
093     * {@inheritDoc}
094     */
095    @Override
096    public AbandonRequest addControl( Control control )
097    {
098        return ( AbandonRequest ) super.addControl( control );
099    }
100
101
102    /**
103     * {@inheritDoc}
104     */
105    @Override
106    public AbandonRequest addAllControls( Control[] controls )
107    {
108        return ( AbandonRequest ) super.addAllControls( controls );
109    }
110
111
112    /**
113     * {@inheritDoc}
114     */
115    @Override
116    public AbandonRequest removeControl( Control control )
117    {
118        return ( AbandonRequest ) super.removeControl( control );
119    }
120
121
122    //-------------------------------------------------------------------------
123    // The Decorator methods
124    //-------------------------------------------------------------------------
125
126    /**
127     * Encode the Abandon protocolOp part
128     */
129    @Override
130    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
131    {
132        try
133        {
134            // The tag
135            buffer.put( LdapCodecConstants.ABANDON_REQUEST_TAG );
136
137            // The length. It has to be evaluated depending on
138            // the abandoned messageId value.
139            buffer.put( ( byte ) BerValue.getNbBytes( getAbandoned() ) );
140
141            // The abandoned messageId
142            buffer.put( BerValue.getBytes( getAbandoned() ) );
143        }
144        catch ( BufferOverflowException boe )
145        {
146            String msg = I18n.err( I18n.ERR_04005 );
147            throw new EncoderException( msg, boe );
148        }
149
150        return buffer;
151    }
152
153
154    /**
155     * Compute the AbandonRequest length 
156     * <br>
157     * AbandonRequest :
158     * <pre> 
159     * 0x50 0x0(1..4) abandoned MessageId 
160     * 
161     * Length(AbandonRequest) = Length(0x50) + 1 + Length(abandoned MessageId)
162     * </pre>
163     */
164    @Override
165    public int computeLength()
166    {
167        return 1 + 1 + BerValue.getNbBytes( getAbandoned() );
168    }
169}