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.controls.search.entryChange;
021
022
023import org.apache.directory.api.asn1.DecoderException;
024import org.apache.directory.api.asn1.ber.tlv.BerValue;
025import org.apache.directory.api.asn1.util.Asn1Buffer;
026import org.apache.directory.api.ldap.codec.api.AbstractControlFactory;
027import org.apache.directory.api.ldap.codec.api.ControlFactory;
028import org.apache.directory.api.ldap.codec.api.LdapApiService;
029import org.apache.directory.api.ldap.model.message.Control;
030import org.apache.directory.api.ldap.model.message.controls.EntryChange;
031import org.apache.directory.api.ldap.model.message.controls.EntryChangeImpl;
032
033
034/**
035 * A {@link ControlFactory} for {@link EntryChange} controls.
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 * @version $Rev$, $Date$
039 */
040public class EntryChangeFactory extends AbstractControlFactory<EntryChange>
041{
042    /** Default value when no change number is provided */
043    public static final int UNDEFINED_CHANGE_NUMBER = -1;
044
045    /**
046     * Creates a new instance of EntryChangeFactory.
047     *
048     * @param codec The LDAP codec.
049     */
050    public EntryChangeFactory( LdapApiService codec )
051    {
052        super( codec, EntryChange.OID );
053    }
054
055
056    /**
057     * {@inheritDoc}
058     */
059    @Override
060    public EntryChange newControl()
061    {
062        return new EntryChangeImpl();
063    }
064
065
066    /**
067     * {@inheritDoc}
068     */
069    @Override
070    public void encodeValue( Asn1Buffer buffer, Control control )
071    {
072        int start = buffer.getPos();
073
074        EntryChange entryChange = ( EntryChange ) control;
075
076        // The changeNumber
077        if ( entryChange.getChangeNumber() != UNDEFINED_CHANGE_NUMBER )
078        {
079            BerValue.encodeInteger( buffer, entryChange.getChangeNumber() );
080        }
081
082        // The previous DN if any
083        if ( entryChange.getPreviousDn() != null )
084        {
085            BerValue.encodeOctetString( buffer, entryChange.getPreviousDn().getName() );
086        }
087
088        // The change type
089        BerValue.encodeEnumerated( buffer, entryChange.getChangeType().getValue() );
090
091        // The EntryChangeNotification sequence
092        BerValue.encodeSequence( buffer, start );
093    }
094
095
096    /**
097     * {@inheritDoc}
098     */
099    @Override
100    public void decodeValue( Control control, byte[] controlBytes ) throws DecoderException
101    {
102        decodeValue( new EntryChangeContainer( control ), control, controlBytes );
103    }
104}