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.gracefulDisconnect;
021
022
023import java.nio.ByteBuffer;
024import java.util.Collection;
025import java.util.Iterator;
026
027import org.apache.directory.api.asn1.DecoderException;
028import org.apache.directory.api.asn1.ber.Asn1Decoder;
029import org.apache.directory.api.asn1.ber.tlv.BerValue;
030import org.apache.directory.api.asn1.util.Asn1Buffer;
031import org.apache.directory.api.ldap.codec.api.AbstractExtendedOperationFactory;
032import org.apache.directory.api.ldap.codec.api.ExtendedOperationFactory;
033import org.apache.directory.api.ldap.codec.api.LdapApiService;
034import org.apache.directory.api.ldap.extras.extended.gracefulDisconnect.GracefulDisconnectResponse;
035import org.apache.directory.api.ldap.extras.extended.gracefulDisconnect.GracefulDisconnectResponseImpl;
036import org.apache.directory.api.ldap.model.message.ExtendedRequest;
037import org.apache.directory.api.ldap.model.message.ExtendedResponse;
038
039
040/**
041 * An {@link ExtendedOperationFactory} for creating cancel extended request response 
042 * pairs.
043 *
044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
045 */
046public class GracefulDisconnectFactory extends AbstractExtendedOperationFactory
047{
048    /**
049     * Creates a new instance of GracefulDisconnectFactory.
050     *
051     * @param codec The codec for this factory.
052     */
053    public GracefulDisconnectFactory( LdapApiService codec )
054    {
055        super( codec, GracefulDisconnectResponse.EXTENSION_OID );
056    }
057
058
059    /**
060     * {@inheritDoc}
061     */
062    @Override
063    public ExtendedRequest newRequest()
064    {
065        // Nothing to do (there's no request associated to GracefulDisconnectResponse)
066        return null;
067    }
068
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    public GracefulDisconnectResponse newResponse()
075    {
076        return new GracefulDisconnectResponseImpl();
077    }
078
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    public GracefulDisconnectResponse newResponse( byte[] encodedValue ) throws DecoderException
085    {
086        GracefulDisconnectResponse gracefulDisconnectResponse = new GracefulDisconnectResponseImpl();
087        decodeValue( gracefulDisconnectResponse, encodedValue );
088        
089        return gracefulDisconnectResponse;
090    }
091
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    public void decodeValue( ExtendedResponse extendedResponse, byte[] requestValue ) throws DecoderException
098    {
099        ByteBuffer bb = ByteBuffer.wrap( requestValue );
100        GracefulDisconnectResponseContainer container = new GracefulDisconnectResponseContainer();
101        container.setGracefulDisconnectResponse( ( GracefulDisconnectResponse ) extendedResponse ); 
102        Asn1Decoder.decode( bb, container );
103    }
104
105    
106    private void encodeUrls( Asn1Buffer buffer, Iterator<String> urls )
107    {
108        if ( urls.hasNext() )
109        {
110            String url = urls.next();
111            
112            encodeUrls( buffer, urls );
113            
114            BerValue.encodeOctetString( buffer, url );
115        }
116    }
117
118
119    /**
120     * {@inheritDoc}
121     */
122    @Override
123    public void encodeValue( Asn1Buffer buffer, ExtendedResponse extendedResponse )
124    {
125        int start  = buffer.getPos();
126        GracefulDisconnectResponse gracefulDisconnectResponse = ( GracefulDisconnectResponse ) extendedResponse;
127        
128        // The URLs if any
129        if ( gracefulDisconnectResponse.getReplicatedContexts() != null )
130        {
131            Collection<String> urls = gracefulDisconnectResponse.getReplicatedContexts().getLdapUrls();
132            
133            if ( urls.size() != 0 )
134            {
135                encodeUrls( buffer, urls.iterator() );
136
137                // The URLs sequence
138                BerValue.encodeSequence( buffer, start );
139            }
140        }
141        
142        // The delay, if any
143        if ( gracefulDisconnectResponse.getDelay() != 0 )
144        {
145            BerValue.encodeInteger( buffer,
146                ( byte ) GracefulActionConstants.GRACEFUL_ACTION_DELAY_TAG,
147                gracefulDisconnectResponse.getDelay() );
148        }
149
150        // The timeOffline, if any
151        if ( gracefulDisconnectResponse.getTimeOffline() != 0 )
152        {
153            BerValue.encodeInteger( buffer, gracefulDisconnectResponse.getTimeOffline() );
154        }
155        
156        // The sequence
157        BerValue.encodeSequence( buffer, start );
158    }
159}