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.server.core.interceptor.context;
021    
022    
023    import org.apache.directory.server.core.CoreSession;
024    import org.apache.directory.server.core.entry.ClonedServerEntry;
025    import org.apache.directory.server.i18n.I18n;
026    import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
027    import org.apache.directory.shared.ldap.codec.controls.ManageDsaITControl;
028    import org.apache.directory.shared.ldap.message.internal.InternalModifyDnRequest;
029    import org.apache.directory.shared.ldap.name.DN;
030    import org.apache.directory.shared.ldap.name.RDN;
031    
032    
033    /**
034     * A RenameService context used for Interceptors. It contains all the informations
035     * needed for the modify DN operation, and used by all the interceptors
036     * 
037     * This is used when the modifyDN is about changing the RDN, not the base DN.
038     *
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev$, $Date$
041     */
042    public class RenameOperationContext extends AbstractChangeOperationContext
043    {
044        /** The new RDN */
045        private RDN newRdn;
046    
047        /** Cached copy of the new DN */
048        private DN newDn;
049    
050        /** The flag to remove the old DN Attribute  */
051        private boolean delOldDn;
052    
053        /** The entry after being renamed and altered for rdn attributes */ 
054        private ClonedServerEntry alteredEntry;
055        
056    
057        /**
058         * Creates a new instance of RenameOperationContext.
059         */
060        public RenameOperationContext( CoreSession session )
061        {
062            super( session );
063        }
064    
065    
066        /**
067         * Creates a new instance of RenameOperationContext.
068         *
069         * @param oldDn the dn of the entry before the rename
070         * @param newRdn the new RDN to use for the target
071         * @param delOldDn true if we delete the old RDN value
072         */
073        public RenameOperationContext( CoreSession session, DN oldDn, RDN newRdn, boolean delOldDn )
074        {
075            super( session, oldDn );
076            this.newRdn = newRdn;
077            this.delOldDn = delOldDn;
078        }
079    
080    
081        public RenameOperationContext( CoreSession session, InternalModifyDnRequest modifyDnRequest )
082        {
083            super( session, modifyDnRequest.getName() );
084            this.newRdn = modifyDnRequest.getNewRdn();
085            
086            if ( newRdn == null )
087            {
088                throw new IllegalStateException( I18n.err( I18n.ERR_328, modifyDnRequest ) );
089            }
090            
091            this.delOldDn = modifyDnRequest.getDeleteOldRdn();
092            this.requestControls = modifyDnRequest.getControls();
093            
094            if ( requestControls.containsKey( ManageDsaITControl.CONTROL_OID ) )
095            {
096                ignoreReferral();
097            }
098            else
099            {
100                throwReferral();
101            }
102        }
103    
104    
105        /**
106         * @return The delete old DN flag
107         */
108        public boolean getDelOldDn() 
109        {
110            return delOldDn;
111        }
112    
113    
114        /**
115         * Set the flag to delete the old DN
116         * @param delOldDn the flag to set
117         */
118        public void setDelOldDn( boolean delOldDn ) 
119        {
120            this.delOldDn = delOldDn;
121        }
122    
123    
124        /**
125         * @return The new DN either computed if null or already computed
126         */
127        public DN getNewDn() throws Exception
128        {
129            return newDn;
130        }
131    
132    
133        /**
134         * @return The new RDN
135         */
136        public RDN getNewRdn()
137        {
138            return newRdn;
139        }
140    
141    
142        /**
143         * Set the new RDN
144         * @param newRdn The new RDN
145         */
146        public void setNewRdn( RDN newRdn )
147        {
148            this.newRdn = newRdn;
149        }
150    
151    
152        /**
153         * Set the new DN
154         * @param newDn The new DN
155         */
156        public void setNewDn( DN newDn )
157        {
158            this.newDn = newDn;
159        }
160    
161    
162        /**
163         * @return the operation name
164         */
165        public String getName()
166        {
167            return MessageTypeEnum.MODIFYDN_REQUEST.name();
168        }
169        
170        
171        /**
172         * Returns the entry after it has been renamed and potentially changed for 
173         * RDN alterations.
174         *
175         * @return the new renamed entry
176         */
177        public ClonedServerEntry getAlteredEntry()
178        {
179            return alteredEntry;
180        }
181    
182        
183        /**
184         * Set the modified entry once the operation has been proceced
185         * on the backend.
186         *
187         * @param alteredEntry The modified entry
188         */
189        public void setAlteredEntry( ClonedServerEntry alteredEntry ) 
190        {
191            this.alteredEntry = alteredEntry;
192        }
193        
194        
195        /**
196         * @see Object#toString()
197         */
198        public String toString()
199        {
200            return "RenameContext for old DN '" + getDn().getName() + "'" +
201            ", new RDN '" + newRdn + "'" +
202            ( delOldDn ? ", delete old Dn" : "" ) ; 
203        }
204    }