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     */
020    package org.apache.directory.shared.ldap.message.control.replication;
021    
022    import org.apache.directory.shared.asn1.codec.EncoderException;
023    import org.apache.directory.shared.ldap.codec.controls.replication.syncInfoValue.SyncInfoValueControlCodec;
024    import org.apache.directory.shared.ldap.message.control.InternalAbstractControl;
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    
028    /**
029     * This class implement the SyncInfoValue interface, and represent the first
030     * choice : newcookie.
031     * The structure for this control is :
032     * 
033     * syncInfoValue ::= CHOICE {
034     *     newcookie      [0] syncCookie,
035     * ...
036     * }
037     *
038     * @see <a href="http://www.faqs.org/rfcs/rfc4533.html">RFC 4533</a>
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev: $
041     *
042     */
043    public class SyncInfoValueNewCookieControl extends InternalAbstractControl implements SyncInfoValueControl 
044    {
045        /** As this class is serializable, defined its serialVersionUID */ 
046        private static final long serialVersionUID = 1L;
047    
048        /** The Logger for this class */
049        private static final Logger LOG = LoggerFactory.getLogger( SyncInfoValueNewCookieControl.class );
050    
051        /** The new cookie */
052        private byte[] newCookie;
053        
054        
055        /**
056         * {@inheritDoc}
057         */
058        public byte[] getCookie()
059        {
060            return newCookie;
061        }
062        
063        
064        /**
065         * {@inheritDoc}
066         */
067        public void setCookie( byte[] newCookie )
068        {
069            this.newCookie = newCookie;
070        }
071    
072        
073        /**
074         * {@inheritDoc}
075         */
076        @Override
077        public String getID()
078        {
079            return CONTROL_OID;
080        }
081    
082    
083        /**
084         * {@inheritDoc}
085         */
086        public byte[] getEncodedValue()
087        {
088            SyncInfoValueControlCodec syncInfoValueCtlCodec = new SyncInfoValueControlCodec( SynchronizationInfoEnum.NEW_COOKIE );
089            syncInfoValueCtlCodec.setCookie( newCookie );
090    
091            try
092            {
093                return syncInfoValueCtlCodec.encode( null ).array();
094            }
095            catch ( EncoderException e )
096            {
097                LOG.error( "Failed to encode syncInfoValue control", e );
098                throw new IllegalStateException( "Failed to encode control with encoder.", e );
099            }
100        }
101    }