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    
021    package org.apache.directory.shared.ldap.ldif;
022    
023    import javax.naming.ldap.Control;
024    
025    import org.apache.directory.shared.asn1.primitives.OID;
026    import org.apache.directory.shared.ldap.util.StringTools;
027    
028    /**
029     * The LdifControl class stores a control defined for an entry found in a ldif
030     * file.
031     * 
032     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033     * @version $Rev$, $Date$
034     */
035    public class LdifControl implements Control
036    {
037        private static final long serialVersionUID = 1L;
038    
039        /** The control OID */
040        private OID oid;
041    
042        /** The control criticality */
043        private boolean criticality;
044    
045        /** The control BER encoded value */
046        private byte[] value;
047    
048        /**
049         * Create a new Control
050         * 
051         * @param oid
052         *            OID of the created control
053         */
054        public LdifControl( OID oid )
055        {
056            this.oid = oid;
057            criticality = false;
058            value = null;
059        }
060    
061        /**
062         * Returns the criticality of the current control
063         * @return <code>true</code> if the control is critical
064         */
065        public boolean isCritical()
066        {
067            return criticality;
068        }
069    
070        /**
071         * Set the criticality
072         * 
073         * @param criticality
074         *            True or false.
075         */
076        public void setCriticality( boolean criticality )
077        {
078            this.criticality = criticality;
079        }
080    
081        /**
082         * Return the control's OID as a String
083         * @return The control's OID
084         */
085        public String getID()
086        {
087            return oid.toString();
088        }
089    
090        /**
091         * Set the control's OID
092         * 
093         * @param oid The control's OID
094         */
095        public void setOid( OID oid )
096        {
097            this.oid = oid;
098        }
099    
100        /**
101         * Returns the BER encoded value of the control
102         * @return the BER encoded value
103         */
104        public byte[] getEncodedValue()
105        {
106            if ( value == null )
107            {
108                return null;
109            }
110    
111            final byte[] copy = new byte[ value.length ];
112            System.arraycopy( value, 0, copy, 0, value.length );
113            return copy;
114        }
115    
116        /**
117         * Set the BER encoded value of the control
118         * 
119         * @param value
120         *            BER encodec value
121         */
122        public void setValue( byte[] value )
123        {
124            if ( value != null )
125            {
126                this.value = new byte[ value.length ];
127                System.arraycopy( value, 0, this.value, 0, value.length );
128            } else {
129                this.value = null;
130            }
131        }
132    
133        public String toString()
134        {
135            return "LdifControl : {" + oid.toString() + ", " + criticality + ", " + StringTools.dumpBytes( value ) + "}";
136        }
137    }