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.ArrayList;
24  import java.util.List;
25  
26  import org.apache.directory.api.ldap.model.entry.Attribute;
27  import org.apache.directory.api.ldap.model.entry.DefaultModification;
28  import org.apache.directory.api.ldap.model.entry.Entry;
29  import org.apache.directory.api.ldap.model.entry.Modification;
30  import org.apache.directory.api.ldap.model.entry.ModificationOperation;
31  import org.apache.directory.api.ldap.model.exception.LdapException;
32  import org.apache.directory.api.ldap.model.exception.LdapOperationErrorException;
33  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
34  import org.apache.directory.api.ldap.model.message.ModifyRequest;
35  import org.apache.directory.api.ldap.model.message.controls.ManageDsaIT;
36  import org.apache.directory.api.ldap.model.name.Dn;
37  import org.apache.directory.server.core.api.CoreSession;
38  import org.apache.directory.server.core.api.OperationEnum;
39  import org.apache.directory.server.core.api.entry.ServerEntryUtils;
40  
41  
42  /**
43   * A Modify context used for Interceptors. It contains all the informations
44   * needed for the modify operation, and used by all the interceptors
45   * 
46   * This context can use either Attributes or ModificationItem, but not both.
47   *
48   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
49   */
50  public class ModifyOperationContext extends AbstractChangeOperationContext
51  {
52      /** The modification items */
53      private List<Modification> modItems;
54  
55      /** The entry after being renamed and altered for rdn attributes */
56      private Entry alteredEntry;
57  
58  
59      /**
60       * Creates a new instance of ModifyOperationContext.
61       * 
62       * @param session The session to use
63       */
64      public ModifyOperationContext( CoreSession session )
65      {
66          super( session );
67  
68          if ( session != null )
69          {
70              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MODIFY ) );
71          }
72      }
73  
74  
75      /**
76       * Creates a new instance of ModifyOperationContext.
77       *
78       * @param session The session to use
79       * @param dn the dn of the entry to be modified
80       * @param modItems the modifications to be performed on the entry
81       */
82      public ModifyOperationContext( CoreSession session, Dn dn, List<Modification> modItems )
83      {
84          super( session, dn );
85  
86          if ( session != null )
87          {
88              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MODIFY ) );
89          }
90  
91          this.modItems = modItems;
92      }
93  
94  
95      /**
96       * Creates a new instance of ModifyOperationContext.
97       * 
98       * @param session The session to use
99       * @param modifyRequest The ModifyRequest to process
100      * @throws LdapException If the modify failed
101      */
102     public ModifyOperationContext( CoreSession session, ModifyRequest modifyRequest ) throws LdapException
103     {
104         super( session, modifyRequest.getName() );
105 
106         if ( session != null )
107         {
108             setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.MODIFY ) );
109         }
110         else
111         {
112             throw new LdapOperationErrorException( "Cannot proceed the operation with no session" );
113         }
114 
115         modItems = ServerEntryUtils.toServerModification( modifyRequest.getModifications().toArray(
116             new DefaultModification[0] ), session.getDirectoryService().getSchemaManager() );
117 
118         requestControls = modifyRequest.getControls();
119 
120         if ( requestControls.containsKey( ManageDsaIT.OID ) )
121         {
122             ignoreReferral();
123         }
124         else
125         {
126             throwReferral();
127         }
128     }
129 
130 
131     /**
132      * Set the modified attributes
133      * @param modItems The modified attributes
134      */
135     public void setModItems( List<Modification> modItems )
136     {
137         this.modItems = modItems;
138     }
139 
140 
141     /**
142      * @return The modifications
143      */
144     public List<Modification> getModItems()
145     {
146         return modItems;
147     }
148 
149 
150     public static List<Modification> createModItems( Entry serverEntry, ModificationOperation modOp )
151     {
152         List<Modification> items = new ArrayList<>( serverEntry.size() );
153 
154         for ( Attribute attribute : serverEntry )
155         {
156             items.add( new DefaultModification( modOp, attribute ) );
157         }
158 
159         return items;
160     }
161 
162 
163     /**
164      * @return the operation name
165      */
166     public String getName()
167     {
168         return MessageTypeEnum.MODIFY_REQUEST.name();
169     }
170 
171 
172     /**
173      * Returns the entry after it has been renamed and potentially changed for
174      * Rdn alterations.
175      *
176      * @return the new renamed entry
177      */
178     public Entry getAlteredEntry()
179     {
180         return alteredEntry;
181     }
182 
183 
184     /**
185      * Set the modified entry once the operation has been proceced
186      * on the backend.
187      *
188      * @param alteredEntry The modified entry
189      */
190     public void setAlteredEntry( Entry alteredEntry )
191     {
192         this.alteredEntry = alteredEntry;
193     }
194 
195 
196     /**
197      * @see Object#toString()
198      */
199     public String toString()
200     {
201         StringBuilder sb = new StringBuilder();
202 
203         sb.append( "ModifyContext for Dn '" ).append( getDn().getName() ).append( "', modifications :\n" );
204 
205         if ( modItems != null )
206         {
207             for ( Modification mod : modItems )
208             {
209                 sb.append( mod ).append( '\n' );
210             }
211         }
212 
213         return sb.toString();
214     }
215 }