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 */
020package org.apache.directory.server.core.api.interceptor.context;
021
022
023import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
024import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
025import org.apache.directory.api.ldap.model.message.ModifyDnRequest;
026import org.apache.directory.api.ldap.model.message.controls.ManageDsaIT;
027import org.apache.directory.api.ldap.model.name.Dn;
028import org.apache.directory.api.ldap.model.name.Rdn;
029import org.apache.directory.server.core.api.CoreSession;
030import org.apache.directory.server.core.api.OperationEnum;
031import org.apache.directory.server.i18n.I18n;
032
033
034/**
035 * A Move context used for Interceptors. It contains all the informations
036 * needed for the modify Dn operation, and used by all the interceptors
037 *
038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
039 */
040public class MoveOperationContext extends AbstractChangeOperationContext
041{
042    /** The old superior */
043    private Dn oldSuperior;
044
045    /** The entry Rdn */
046    private Rdn rdn;
047
048    /** The newSuperior Dn */
049    private Dn newSuperior;
050
051    /** The New target Dn */
052    private Dn newDn;
053
054
055    /**
056     * Creates a new instance of MoveOperationContext.
057     * 
058     * @param session The session to use
059     */
060    public MoveOperationContext( CoreSession session )
061    {
062        super( session );
063
064        if ( session != null )
065        {
066            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MOVE ) );
067        }
068    }
069
070
071    /**
072     * Creates a new instance of MoveOperationContext.
073     * 
074     * @param session The session to use
075     * @param oldDn the original source entry Dn to be moved and renamed
076     * @param newSuperior the new entry superior of the target after the move
077     */
078    public MoveOperationContext( CoreSession session, Dn oldDn, Dn newSuperior )
079    {
080        super( session, oldDn );
081        this.newSuperior = newSuperior;
082        oldSuperior = oldDn.getParent();
083        rdn = oldDn.getRdn().clone();
084
085        if ( session != null )
086        {
087            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MOVE ) );
088        }
089
090        try
091        {
092            newDn = newSuperior.add( rdn );
093        }
094        catch ( LdapInvalidDnException lide )
095        {
096            throw new IllegalArgumentException( lide.getMessage(), lide );
097        }
098    }
099
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}