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