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.exception.LdapInvalidDnException;
24  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
25  import org.apache.directory.api.ldap.model.message.ModifyDnRequest;
26  import org.apache.directory.api.ldap.model.message.controls.ManageDsaIT;
27  import org.apache.directory.api.ldap.model.name.Dn;
28  import org.apache.directory.api.ldap.model.name.Rdn;
29  import org.apache.directory.server.core.api.CoreSession;
30  import org.apache.directory.server.core.api.OperationEnum;
31  import org.apache.directory.server.i18n.I18n;
32  
33  
34  /**
35   * A Move context used for Interceptors. It contains all the informations
36   * needed for the modify Dn operation, and used by all the interceptors
37   *
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class MoveOperationContext extends AbstractChangeOperationContext
41  {
42      /** The old superior */
43      private Dn oldSuperior;
44  
45      /** The entry Rdn */
46      private Rdn rdn;
47  
48      /** The newSuperior Dn */
49      private Dn newSuperior;
50  
51      /** The New target Dn */
52      private Dn newDn;
53  
54  
55      /**
56       * Creates a new instance of MoveOperationContext.
57       * 
58       * @param session The session to use
59       */
60      public MoveOperationContext( CoreSession session )
61      {
62          super( session );
63  
64          if ( session != null )
65          {
66              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MOVE ) );
67          }
68      }
69  
70  
71      /**
72       * Creates a new instance of MoveOperationContext.
73       * 
74       * @param session The session to use
75       * @param oldDn the original source entry Dn to be moved and renamed
76       * @param newSuperior the new entry superior of the target after the move
77       */
78      public MoveOperationContext( CoreSession session, Dn oldDn, Dn newSuperior )
79      {
80          super( session, oldDn );
81          this.newSuperior = newSuperior;
82          oldSuperior = oldDn.getParent();
83          rdn = oldDn.getRdn().clone();
84  
85          if ( session != null )
86          {
87              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MOVE ) );
88          }
89  
90          try
91          {
92              newDn = newSuperior.add( rdn );
93          }
94          catch ( LdapInvalidDnException lide )
95          {
96              throw new IllegalArgumentException( lide.getMessage(), lide );
97          }
98      }
99  
100 
101     /**
102      * Create a new instanc eof MoveOperationContext
103      *  
104      * @param session The session to use
105      * @param modifyDnRequest The ModDN operation to apply
106      */
107     public MoveOperationContext( CoreSession session, ModifyDnRequest modifyDnRequest )
108     {
109         super( session, modifyDnRequest.getName() );
110         this.newSuperior = modifyDnRequest.getNewSuperior();
111 
112         if ( session != null )
113         {
114             setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MOVE ) );
115         }
116 
117         if ( newSuperior == null )
118         {
119             throw new IllegalArgumentException( I18n.err( I18n.ERR_326_NEW_SUPERIROR_CANNOT_BE_NULL, modifyDnRequest ) );
120         }
121 
122         this.requestControls = modifyDnRequest.getControls();
123 
124         if ( modifyDnRequest.getNewRdn() != null )
125         {
126             throw new IllegalArgumentException( I18n.err( I18n.ERR_327_MOVE_AND_RENAME_OPERATION, modifyDnRequest ) );
127         }
128 
129         if ( requestControls.containsKey( ManageDsaIT.OID ) )
130         {
131             ignoreReferral();
132         }
133         else
134         {
135             throwReferral();
136         }
137 
138         oldSuperior = modifyDnRequest.getName().getParent();
139         rdn = modifyDnRequest.getName().getRdn().clone();
140 
141         try
142         {
143             newDn = newSuperior.add( rdn );
144         }
145         catch ( LdapInvalidDnException lide )
146         {
147             throw new IllegalArgumentException( lide.getMessage(), lide );
148         }
149     }
150 
151 
152     /**
153      *  @return The oldSuperior Dn
154      */
155     public Dn getOldSuperior()
156     {
157         return oldSuperior;
158     }
159 
160 
161     /**
162      * @param oldSuperior the oldSuperior to set
163      */
164     public void setOldSuperior( Dn oldSuperior )
165     {
166         this.oldSuperior = oldSuperior;
167     }
168 
169 
170     /**
171      *  @return The newSuperior Dn
172      */
173     public Dn getNewSuperior()
174     {
175         return newSuperior;
176     }
177 
178 
179     /**
180      * @param newSuperior the newSuperior to set
181      */
182     public void setNewSuperior( Dn newSuperior )
183     {
184         this.newSuperior = newSuperior;
185     }
186 
187 
188     /**
189      *  @return The Rdn
190      */
191     public Rdn getRdn()
192     {
193         return rdn;
194     }
195 
196 
197     /**
198      * @param rdn the rdn to set
199      */
200     public void setRdn( Rdn rdn )
201     {
202         this.rdn = rdn;
203     }
204 
205 
206     /**
207      *  @return The new Dn
208      */
209     public Dn getNewDn()
210     {
211         return newDn;
212     }
213 
214 
215     /**
216      * @param newDn the newDn to set
217      */
218     public void setNewDn( Dn newDn )
219     {
220         this.newDn = newDn;
221     }
222 
223 
224     /**
225      * @return the operation name
226      */
227     @Override
228     public String getName()
229     {
230         return MessageTypeEnum.MODIFYDN_REQUEST.name();
231     }
232 
233 
234     /**
235      * @see Object#toString()
236      */
237     @Override
238     public String toString()
239     {
240         return "ReplaceContext for old Dn '" + getDn().getName() + "'" + ", newSuperior '" + newSuperior + "'";
241     }
242 }