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 java.util.ArrayList;
024    import java.util.List;
025    
026    import javax.naming.NamingException;
027    
028    import org.apache.directory.server.core.CoreSession;
029    import org.apache.directory.server.core.entry.ClonedServerEntry;
030    import org.apache.directory.server.core.entry.ServerEntryUtils;
031    import org.apache.directory.shared.ldap.codec.MessageTypeEnum;
032    import org.apache.directory.shared.ldap.codec.controls.ManageDsaITControl;
033    import org.apache.directory.shared.ldap.entry.EntryAttribute;
034    import org.apache.directory.shared.ldap.entry.Modification;
035    import org.apache.directory.shared.ldap.entry.ModificationOperation;
036    import org.apache.directory.shared.ldap.entry.ServerEntry;
037    import org.apache.directory.shared.ldap.entry.ServerModification;
038    import org.apache.directory.shared.ldap.entry.client.ClientModification;
039    import org.apache.directory.shared.ldap.message.internal.InternalModifyRequest;
040    import org.apache.directory.shared.ldap.name.DN;
041    
042    
043    /**
044     * A Modify context used for Interceptors. It contains all the informations
045     * needed for the modify operation, and used by all the interceptors
046     * 
047     * This context can use either Attributes or ModificationItem, but not both.
048     *
049     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
050     * @version $Rev$, $Date$
051     */
052    public class ModifyOperationContext extends AbstractChangeOperationContext
053    {
054        /** The modification items */
055        private List<Modification> modItems;
056        
057        /** The entry after being renamed and altered for rdn attributes */ 
058        private ClonedServerEntry alteredEntry;
059        
060        /**
061         * Creates a new instance of ModifyOperationContext.
062         */
063        public ModifyOperationContext( CoreSession session )
064        {
065            super( session );
066        }
067    
068    
069        /**
070         * Creates a new instance of ModifyOperationContext.
071         *
072         * @param dn the dn of the entry to be modified
073         * @param modItems the modifications to be performed on the entry
074         */
075        public ModifyOperationContext( CoreSession session, DN dn, List<Modification> modItems )
076        {
077            super( session, dn );
078    
079            this.modItems = modItems;
080        }
081    
082    
083        public ModifyOperationContext( CoreSession session, InternalModifyRequest modifyRequest ) throws Exception
084        {
085            super( session, modifyRequest.getName() );
086            
087            modItems = ServerEntryUtils.toServerModification( 
088                modifyRequest.getModificationItems().toArray( new ClientModification[0] ), 
089                session.getDirectoryService().getSchemaManager() );
090            
091            requestControls = modifyRequest.getControls();
092    
093            if ( requestControls.containsKey( ManageDsaITControl.CONTROL_OID ) )
094            {
095                ignoreReferral();
096            }
097            else
098            {
099                throwReferral();
100            }
101        }
102    
103    
104        /**
105         * Set the modified attributes
106         * @param modItems The modified attributes
107         */
108        public void setModItems( List<Modification> modItems )
109        {
110            this.modItems = modItems;
111        }
112    
113    
114        /**
115         * @return The modifications
116         */
117        public List<Modification> getModItems() 
118        {
119            return modItems;
120        }
121    
122    
123        public static List<Modification> createModItems( ServerEntry serverEntry, ModificationOperation modOp ) throws NamingException
124        {
125            List<Modification> items = new ArrayList<Modification>( serverEntry.size() );
126            
127            for ( EntryAttribute attribute:serverEntry )
128            {
129                items.add( new ServerModification( modOp, attribute ) );
130            }
131    
132            return items;
133        }
134    
135    
136        /**
137         * @return the operation name
138         */
139        public String getName()
140        {
141            return MessageTypeEnum.MODIFY_REQUEST.name();
142        }
143    
144        
145        /**
146         * Returns the entry after it has been renamed and potentially changed for 
147         * Rdn alterations.
148         *
149         * @return the new renamed entry
150         */
151        public ClonedServerEntry getAlteredEntry()
152        {
153            return alteredEntry;
154        }
155    
156        
157        /**
158         * Set the modified entry once the operation has been proceced
159         * on the backend.
160         *
161         * @param alteredEntry The modified entry
162         */
163        public void setAlteredEntry( ClonedServerEntry alteredEntry ) 
164        {
165            this.alteredEntry = alteredEntry;
166        }
167    
168        
169        /**
170         * @see Object#toString()
171         */
172        public String toString()
173        {
174            StringBuilder sb = new StringBuilder();
175            
176            sb.append("ModifyContext for DN '").append( getDn().getName() ).append( "', modifications :\n" );
177            
178            if ( modItems != null )
179            {
180                for ( Modification mod:modItems )
181                {
182                    sb.append( mod ).append( '\n' );
183                }
184            }
185            
186            return sb.toString();
187        }
188    }