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.LdapConstants;
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 decoratedMessage the decorated AddRequest
046     */
047    public AbandonRequestDecorator( LdapApiService codec, AbandonRequest decoratedMessage )
048    {
049        super( codec, decoratedMessage );
050    }
051
052
053    //-------------------------------------------------------------------------
054    // The AbandonRequest methods
055    //-------------------------------------------------------------------------
056
057    /**
058     * {@inheritDoc}
059     */
060    public int getAbandoned()
061    {
062        return getDecorated().getAbandoned();
063    }
064
065
066    /**
067     * {@inheritDoc}
068     */
069    public AbandonRequest setAbandoned( int requestId )
070    {
071        getDecorated().setAbandoned( requestId );
072
073        return this;
074    }
075
076
077    /**
078     * {@inheritDoc}
079     */
080    public AbandonRequest setMessageId( int messageId )
081    {
082        super.setMessageId( messageId );
083
084        return this;
085    }
086
087
088    /**
089     * {@inheritDoc}
090     */
091    public AbandonRequest addControl( Control control )
092    {
093        return ( AbandonRequest ) super.addControl( control );
094    }
095
096
097    /**
098     * {@inheritDoc}
099     */
100    public AbandonRequest addAllControls( Control[] controls )
101    {
102        return ( AbandonRequest ) super.addAllControls( controls );
103    }
104
105
106    /**
107     * {@inheritDoc}
108     */
109    public AbandonRequest removeControl( Control control )
110    {
111        return ( AbandonRequest ) super.removeControl( control );
112    }
113
114
115    //-------------------------------------------------------------------------
116    // The Decorator methods
117    //-------------------------------------------------------------------------
118
119    /**
120     * Encode the Abandon protocolOp part
121     */
122    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
123    {
124        try
125        {
126            // The tag
127            buffer.put( LdapConstants.ABANDON_REQUEST_TAG );
128
129            // The length. It has to be evaluated depending on
130            // the abandoned messageId value.
131            buffer.put( ( byte ) BerValue.getNbBytes( getAbandoned() ) );
132
133            // The abandoned messageId
134            buffer.put( BerValue.getBytes( getAbandoned() ) );
135        }
136        catch ( BufferOverflowException boe )
137        {
138            String msg = I18n.err( I18n.ERR_04005 );
139            throw new EncoderException( msg );
140        }
141
142        return buffer;
143    }
144
145
146    /**
147     * Compute the AbandonRequest length 
148     * 
149     * AbandonRequest : 
150     * 0x50 0x0(1..4) abandoned MessageId 
151     * 
152     * Length(AbandonRequest) = Length(0x50) + 1 + Length(abandoned MessageId)
153     */
154    public int computeLength()
155    {
156        int length = 1 + 1 + BerValue.getNbBytes( getAbandoned() );
157
158        return length;
159    }
160}