View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   * 
19   */
20  package org.apache.directory.server.core.api.interceptor.context;
21  
22  
23  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
24  import org.apache.directory.api.ldap.model.message.ModifyDnRequest;
25  import org.apache.directory.api.ldap.model.message.controls.ManageDsaIT;
26  import org.apache.directory.api.ldap.model.name.Dn;
27  import org.apache.directory.api.ldap.model.name.Rdn;
28  import org.apache.directory.server.core.api.CoreSession;
29  import org.apache.directory.server.core.api.OperationEnum;
30  import org.apache.directory.server.i18n.I18n;
31  
32  
33  /**
34   * A RenameService context used for Interceptors. It contains all the informations
35   * needed for the modify Dn operation, and used by all the interceptors
36   * 
37   * This is used when the modifyDN is about changing the Rdn, not the base Dn.
38   *
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  public class RenameOperationContext extends AbstractChangeOperationContext
42  {
43      /** The new Rdn */
44      protected Rdn newRdn;
45  
46      /** Cached copy of the new Dn */
47      protected Dn newDn;
48  
49      /** The flag to remove the old Rdn Attribute  */
50      private boolean deleteOldRdn;
51  
52  
53      /**
54       * Creates a new instance of RenameOperationContext.
55       * 
56       * @param session The session to use
57       */
58      public RenameOperationContext( CoreSession session )
59      {
60          super( session );
61  
62          if ( session != null )
63          {
64              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.RENAME ) );
65          }
66      }
67  
68  
69      /**
70       * Creates a new instance of RenameOperationContext.
71       *
72       * @param session The session to use
73       * @param oldDn the dn of the entry before the rename
74       * @param newRdn the new Rdn to use for the target
75       * @param deleteOldRdn true if we delete the old Rdn value
76       */
77      public RenameOperationContext( CoreSession session, Dn oldDn, Rdn newRdn, boolean deleteOldRdn )
78      {
79          super( session, oldDn );
80          this.newRdn = newRdn;
81          this.deleteOldRdn = deleteOldRdn;
82  
83          if ( session != null )
84          {
85              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.RENAME ) );
86          }
87      }
88  
89      /**
90       * Creates a new instance of RenameOperationContext
91       *  
92       * @param session The session to use
93       * @param modifyDnRequest The ModDn operation to apply
94       */
95      public RenameOperationContext( CoreSession session, ModifyDnRequest modifyDnRequest )
96      {
97          super( session, modifyDnRequest.getName() );
98          this.newRdn = modifyDnRequest.getNewRdn();
99  
100         if ( session != null )
101         {
102             setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.RENAME ) );
103         }
104 
105         if ( newRdn == null )
106         {
107             throw new IllegalStateException( I18n.err( I18n.ERR_328, modifyDnRequest ) );
108         }
109 
110         this.deleteOldRdn = modifyDnRequest.getDeleteOldRdn();
111         this.requestControls = modifyDnRequest.getControls();
112 
113         if ( requestControls.containsKey( ManageDsaIT.OID ) )
114         {
115             ignoreReferral();
116         }
117         else
118         {
119             throwReferral();
120         }
121     }
122 
123 
124     /**
125      * @return The delete old Rdn flag
126      */
127     public boolean getDeleteOldRdn()
128     {
129         return deleteOldRdn;
130     }
131 
132 
133     /**
134      * Set the flag to delete the old Rdn
135      * @param deleteOldRdn the flag to set
136      */
137     public void setDelOldDn( boolean deleteOldRdn )
138     {
139         this.deleteOldRdn = deleteOldRdn;
140     }
141 
142 
143     /**
144      * @return The new Dn either computed if null or already computed
145      */
146     public Dn getNewDn()
147     {
148         return newDn;
149     }
150 
151 
152     /**
153      * @return The new Rdn
154      */
155     public Rdn getNewRdn()
156     {
157         return newRdn;
158     }
159 
160 
161     /**
162      * Set the new Rdn
163      * @param newRdn The new Rdn
164      */
165     public void setNewRdn( Rdn newRdn )
166     {
167         this.newRdn = newRdn;
168     }
169 
170 
171     /**
172      * Set the new Dn
173      * @param newDn The new Dn
174      */
175     public void setNewDn( Dn newDn )
176     {
177         this.newDn = newDn;
178     }
179 
180 
181     /**
182      * @return the operation name
183      */
184     public String getName()
185     {
186         return MessageTypeEnum.MODIFYDN_REQUEST.name();
187     }
188 
189 
190     /**
191      * @see Object#toString()
192      */
193     public String toString()
194     {
195         return "RenameContext for old Dn '" + getDn().getName() + "'" + ", new Rdn '" + newRdn + "'"
196             + ( deleteOldRdn ? ", delete old Rdn" : "" );
197     }
198 }