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.syncRequestValue.SyncRequestValueControlCodec;
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 implements the Sunc Request Control, as described by RFC 4533.
030     * The structure for this control is :
031     * 
032     * syncRequestValue ::= SEQUENCE {
033     *     mode ENUMERATED {
034     *         -- 0 unused
035     *         refreshOnly       (1),
036     *         -- 2 reserved
037     *         refreshAndPersist (3)
038     *     },
039     *     cookie     syncCookie OPTIONAL,
040     *     reloadHint BOOLEAN DEFAULT FALSE
041     * }
042     * 
043     * This control OID is 1.3.6.1.4.1.4203.1.9.1.1
044     * @see <a href="http://www.faqs.org/rfcs/rfc4533.html">RFC 4533</a>
045     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
046     * @version $Rev: $
047     *
048     */
049    public class SyncRequestValueControl extends InternalAbstractControl
050    {
051        /** As this class is serializable, defined its serialVersionUID */ 
052        private static final long serialVersionUID = 1L;
053    
054        /** The Logger for this class */
055        private static final Logger LOG = LoggerFactory.getLogger( SyncRequestValueControl.class );
056    
057        /** This control OID */
058        public static final String CONTROL_OID = "1.3.6.1.4.1.4203.1.9.1.1";
059    
060        /** The synchronization type */
061        private SynchronizationModeEnum mode;
062        
063        /** The cookie */
064        private byte[] cookie;
065        
066        /** The reloadHint flag */
067        private boolean reloadHint;
068    
069        
070        /**
071         * @return the mode
072         */
073        public SynchronizationModeEnum getMode()
074        {
075            return mode;
076        }
077    
078        
079        /**
080         * @param syncMode the mode to set
081         */
082        public void setMode( SynchronizationModeEnum mode )
083        {
084            this.mode = mode;
085        }
086    
087        
088        /**
089         * @return the cookie
090         */
091        public byte[] getCookie()
092        {
093            return cookie;
094        }
095    
096        
097        /**
098         * @param syncCookie the syncCookie to set
099         */
100        public void setCookie( byte[] cookie )
101        {
102            this.cookie = cookie;
103        }
104    
105        
106        /**
107         * @return the reloadHint
108         */
109        public boolean isReloadHint()
110        {
111            return reloadHint;
112        }
113    
114        
115        /**
116         * @param reloadHint the reloadHint to set
117         */
118        public void setReloadHint( boolean reloadHint )
119        {
120            this.reloadHint = reloadHint;
121        }
122        
123        
124        /**
125         * {@inheritDoc}
126         */
127        @Override
128        public String getID()
129        {
130            return CONTROL_OID;
131        }
132    
133    
134        /**
135         * {@inheritDoc}
136         */
137        public byte[] getEncodedValue()
138        {
139            SyncRequestValueControlCodec syncRequestValueCtlCodec = new SyncRequestValueControlCodec();
140            syncRequestValueCtlCodec.setMode( mode );
141            syncRequestValueCtlCodec.setCookie( cookie );
142            syncRequestValueCtlCodec.setReloadHint( reloadHint );
143    
144            try
145            {
146                return syncRequestValueCtlCodec.encode( null ).array();
147            }
148            catch ( EncoderException e )
149            {
150                LOG.error( "Failed to encode syncRequestValue control", e );
151                throw new IllegalStateException( "Failed to encode control with encoder.", e );
152            }
153        }
154    }