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.extras.extended.ads_impl.cancel;
021
022
023import org.apache.directory.shared.asn1.DecoderException;
024import org.apache.directory.shared.asn1.EncoderException;
025import org.apache.directory.shared.i18n.I18n;
026import org.apache.directory.shared.ldap.codec.api.ExtendedRequestDecorator;
027import org.apache.directory.shared.ldap.codec.api.LdapApiService;
028import org.apache.directory.shared.ldap.extras.extended.CancelRequest;
029import org.apache.directory.shared.ldap.extras.extended.CancelResponse;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033
034/**
035 * A Decorator for CancelRequests.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class CancelRequestDecorator extends ExtendedRequestDecorator<CancelRequest,CancelResponse> implements CancelRequest
040{
041    private static final Logger LOG = LoggerFactory.getLogger( CancelRequestDecorator.class );
042    
043    
044    public CancelRequestDecorator( LdapApiService codec, CancelRequest decoratedMessage )
045    {
046        super( codec, decoratedMessage );
047    }
048
049
050    public int getCancelId()
051    {
052        return getDecorated().getCancelId();
053    }
054
055
056    public void setCancelId( int cancelId )
057    {
058        if ( cancelId == getCancelId() )
059        {
060            return;
061        }
062        
063        this.requestValue = null;
064        getDecorated().setCancelId( cancelId );
065    }
066
067    
068    /**
069     * {@inheritDoc}
070     */
071    @Override
072    public byte[] getRequestValue()
073    {
074        if ( requestValue == null )
075        {
076            try
077            {
078                Cancel cancel = new Cancel();
079                cancel.setCancelId( getDecorated().getCancelId() );
080    
081                requestValue = cancel.encode().array();
082            }
083            catch ( EncoderException e )
084            {
085                LOG.error( I18n.err( I18n.ERR_04164 ), e );
086                throw new RuntimeException( e );
087            }
088        }
089    
090        return requestValue;
091    }
092
093    
094    /**
095     * Sets the extended request's <b>requestValue</b> portion of the PDU.
096     *
097     * @param payload byte array of data encapsulating ext. req. parameters
098     */
099    @Override
100    public void setRequestValue( byte[] requestValue )
101    {
102        CancelDecoder decoder = new CancelDecoder();
103    
104        try
105        {
106            Cancel cancel = ( Cancel ) decoder.decode( requestValue );
107    
108            if ( requestValue != null )
109            {
110                this.requestValue = new byte[requestValue.length];
111                System.arraycopy( requestValue, 0, this.requestValue, 0, requestValue.length );
112            }
113            else
114            {
115                this.requestValue = null;
116            }
117            
118            getDecorated().setCancelId( cancel.getCancelId() );
119        }
120        catch ( DecoderException e )
121        {
122            LOG.error( I18n.err( I18n.ERR_04165 ), e );
123            throw new RuntimeException( e );
124        }
125    }
126}