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 java.util.List;
24  import java.util.Map;
25  
26  import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
27  import org.apache.directory.api.ldap.model.message.ModifyDnRequest;
28  import org.apache.directory.api.ldap.model.message.controls.ManageDsaIT;
29  import org.apache.directory.api.ldap.model.name.Dn;
30  import org.apache.directory.api.ldap.model.name.Rdn;
31  import org.apache.directory.server.core.api.CoreSession;
32  import org.apache.directory.server.core.api.OperationEnum;
33  import org.apache.directory.server.i18n.I18n;
34  
35  
36  /**
37   * A Move And Rename context used for Interceptors. It contains all the informations
38   * needed for the modify Dn operation, and used by all the interceptors
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public class MoveAndRenameOperationContext extends RenameOperationContext
43  {
44      /** The new superior Dn */
45      private Dn newSuperiorDn;
46  
47      /** The map of modified AVAs */
48      private Map<String, List<ModDnAva>> modifiedAvas;
49  
50      /**
51       * Creates a new instance of MoveAndRenameOperationContext.
52       * 
53       * @param session The session to use
54       */
55      public MoveAndRenameOperationContext( CoreSession session )
56      {
57          super( session );
58  
59          if ( session != null )
60          {
61              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MOVE_AND_RENAME ) );
62          }
63      }
64  
65  
66      /**
67       * Creates a new instance of MoveAndRenameOperationContext.
68       *
69       * @param session The session to use
70       * @param oldDn the original source entry Dn to be moved and renamed
71       * @param newSuperiorDn the new entry superior of the target after the move
72       * @param newRdn the new rdn to use for the target once renamed
73       * @param delOldRdn true if the old rdn value is deleted, false otherwise
74       */
75      public MoveAndRenameOperationContext( CoreSession session, Dn oldDn, Dn newSuperiorDn, Rdn newRdn, boolean delOldRdn )
76      {
77          super( session, oldDn, newRdn, delOldRdn );
78          this.newSuperiorDn = newSuperiorDn;
79  
80          if ( session != null )
81          {
82              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MOVE_AND_RENAME ) );
83          }
84  
85          try
86          {
87              newDn = newSuperiorDn.add( newRdn );
88          }
89          catch ( LdapInvalidDnException lide )
90          {
91              throw new IllegalArgumentException( lide.getMessage(), lide );
92          }
93      }
94  
95  
96      public MoveAndRenameOperationContext( CoreSession session, ModifyDnRequest modifyDnRequest )
97      {
98          // super sets the newRdn and the delOldRdn members and tests
99          super( session, modifyDnRequest );
100         newSuperiorDn = modifyDnRequest.getNewSuperior();
101         
102         if ( !newSuperiorDn.isSchemaAware() )
103         {
104             try
105             {
106                 newSuperiorDn = new Dn( session.getDirectoryService().getSchemaManager(), newSuperiorDn );
107             }
108             catch ( LdapInvalidDnException lide )
109             {
110                 throw new IllegalStateException( I18n.err( I18n.ERR_325, modifyDnRequest ), lide );
111             }
112         }
113 
114         if ( session != null )
115         {
116             setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MOVE_AND_RENAME ) );
117         }
118 
119         if ( newSuperiorDn == null )
120         {
121             throw new IllegalStateException( I18n.err( I18n.ERR_325, modifyDnRequest ) );
122         }
123 
124         if ( requestControls.containsKey( ManageDsaIT.OID ) )
125         {
126             ignoreReferral();
127         }
128         else
129         {
130             throwReferral();
131         }
132 
133         try
134         {
135             newDn = newSuperiorDn.add( newRdn );
136         }
137         catch ( LdapInvalidDnException lide )
138         {
139             throw new IllegalStateException( I18n.err( I18n.ERR_325, modifyDnRequest ), lide );
140         }
141     }
142 
143 
144     /**
145      *  @return The new superior Dn
146      */
147     public Dn getNewSuperiorDn()
148     {
149         return newSuperiorDn;
150     }
151 
152 
153     /**
154      * Set the new Superior Dn
155      *
156      * @param newSuperiorDn The new Superior Dn
157      */
158     public void setNewSuperiorDn( Dn newSuperiorDn )
159     {
160         this.newSuperiorDn = newSuperiorDn;
161     }
162 
163 
164     /**
165      * @see Object#toString()
166      */
167     @Override
168     public String toString()
169     {
170         return "ReplaceContext for old Dn '" + getDn().getName() + "' : " + newDn;
171     }
172 
173 
174     /**
175      * @return the modifiedAvas
176      */
177     public Map<String, List<ModDnAva>> getModifiedAvas()
178     {
179         return modifiedAvas;
180     }
181 
182 
183     /**
184      * @param modifiedAvas the modifiedAvas to set
185      */
186     public void setModifiedAvas( Map<String, List<ModDnAva>> modifiedAvas )
187     {
188         this.modifiedAvas = modifiedAvas;
189     }
190 }