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.extras.extended.ads_impl.cancel;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.DecoderException;
026import org.apache.directory.api.asn1.ber.Asn1Decoder;
027import org.apache.directory.api.asn1.ber.tlv.BerValue;
028import org.apache.directory.api.asn1.util.Asn1Buffer;
029import org.apache.directory.api.ldap.codec.api.AbstractExtendedOperationFactory;
030import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
031import org.apache.directory.api.ldap.codec.api.LdapApiService;
032import org.apache.directory.api.ldap.extras.extended.cancel.CancelRequest;
033import org.apache.directory.api.ldap.extras.extended.cancel.CancelRequestImpl;
034import org.apache.directory.api.ldap.extras.extended.cancel.CancelResponse;
035import org.apache.directory.api.ldap.extras.extended.cancel.CancelResponseImpl;
036import org.apache.directory.api.ldap.model.message.ExtendedRequest;
037
038
039/**
040 * An {@link ExtendedOperationFactory} for creating cancel extended request response 
041 * pairs.
042 *
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 */
045public class CancelFactory extends AbstractExtendedOperationFactory
046{
047    /**
048     * Creates a new instance of CancelFactory.
049     *
050     * @param codec The codec for this factory.
051     */
052    public CancelFactory( LdapApiService codec )
053    {
054        super( codec, CancelRequest.EXTENSION_OID );
055    }
056
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public CancelRequest newRequest()
063    {
064        return new CancelRequestImpl();
065    }
066
067
068    /**
069     * {@inheritDoc}
070     */
071    @Override
072    public CancelRequest newRequest( byte[] encodedValue ) throws DecoderException
073    {
074        CancelRequest cancelRequest = new CancelRequestImpl();
075        decodeValue( cancelRequest, encodedValue );
076
077        return cancelRequest;
078    }
079
080
081    /**
082     * {@inheritDoc}
083     */
084    @Override
085    public CancelResponse newResponse()
086    {
087        return new CancelResponseImpl();
088    }
089
090
091    /**
092     * {@inheritDoc}
093     */
094    @Override
095    public void decodeValue( ExtendedRequest extendedRequest, byte[] requestValue ) throws DecoderException
096    {
097        ByteBuffer bb = ByteBuffer.wrap( requestValue );
098        CancelRequestContainer container = new CancelRequestContainer();
099        container.setCancelRequest( ( CancelRequest ) extendedRequest ); 
100        Asn1Decoder.decode( bb, container );
101    }
102
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public void encodeValue( Asn1Buffer buffer, ExtendedRequest extendedRequest )
109    {
110        int start  = buffer.getPos();
111        CancelRequest cancelRequest = ( CancelRequest ) extendedRequest;
112        
113        // the ID
114        BerValue.encodeInteger( buffer, cancelRequest.getCancelId() );
115        
116        // The sequence
117        BerValue.encodeSequence( buffer, start );
118        
119    }
120}