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.interceptor.context;
21  
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import javax.naming.NamingException;
27  
28  import org.apache.directory.shared.ldap.entry.EntryAttribute;
29  import org.apache.directory.server.core.CoreSession;
30  import org.apache.directory.server.core.entry.ClonedServerEntry;
31  import org.apache.directory.server.core.entry.ServerEntry;
32  import org.apache.directory.server.core.entry.ServerEntryUtils;
33  import org.apache.directory.server.core.entry.ServerModification;
34  import org.apache.directory.shared.ldap.entry.Modification;
35  import org.apache.directory.shared.ldap.entry.ModificationOperation;
36  import org.apache.directory.shared.ldap.entry.client.ClientModification;
37  import org.apache.directory.shared.ldap.message.MessageTypeEnum;
38  import org.apache.directory.shared.ldap.message.ModifyRequest;
39  import org.apache.directory.shared.ldap.name.LdapDN;
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   * @version $Rev$, $Date$
50   */
51  public class ModifyOperationContext extends AbstractChangeOperationContext
52  {
53      /** The modification items */
54      private List<Modification> modItems;
55      
56      /** Cloned entry that is modified */
57      private ClonedServerEntry entry;
58  
59  
60      /**
61       * Creates a new instance of ModifyOperationContext.
62       */
63      public ModifyOperationContext( CoreSession session )
64      {
65          super( session );
66      }
67  
68  
69      /**
70       * Creates a new instance of ModifyOperationContext.
71       *
72       * @param dn the dn of the entry to be modified
73       * @param modItems the modifications to be performed on the entry
74       */
75      public ModifyOperationContext( CoreSession session, LdapDN dn, List<Modification> modItems )
76      {
77          super( session, dn );
78  
79          this.modItems = modItems;
80      }
81  
82  
83      public ModifyOperationContext( CoreSession session, ModifyRequest modifyRequest ) throws Exception
84      {
85          super( session, modifyRequest.getName() );
86          this.modItems = ServerEntryUtils.toServerModification( 
87              modifyRequest.getModificationItems().toArray( new ClientModification[0]), 
88              session.getDirectoryService().getRegistries().getAttributeTypeRegistry() );
89          this.requestControls = modifyRequest.getControls();
90      }
91  
92  
93      /**
94       * Set the modified attributes
95       * @param modItems The modified attributes
96       */
97      public void setModItems( List<Modification> modItems )
98      {
99          this.modItems = modItems;
100     }
101 
102 
103     /**
104      * @return The modifications
105      */
106     public List<Modification> getModItems() 
107     {
108         return modItems;
109     }
110 
111 
112     public static List<Modification> createModItems( ServerEntry serverEntry, ModificationOperation modOp ) throws NamingException
113     {
114         List<Modification> items = new ArrayList<Modification>( serverEntry.size() );
115         
116         for ( EntryAttribute attribute:serverEntry )
117         {
118             items.add( new ServerModification( modOp, attribute ) );
119         }
120 
121         return items;
122     }
123 
124 
125     /**
126      * @return the operation name
127      */
128     public String getName()
129     {
130         return MessageTypeEnum.MODIFY_REQUEST.name();
131     }
132 
133     
134     public void setEntry( ClonedServerEntry entry )
135     {
136         this.entry = entry;
137     }
138     
139     
140     public ClonedServerEntry getEntry()
141     {
142         return entry;
143     }
144 
145     
146     /**
147      * @see Object#toString()
148      */
149     public String toString()
150     {
151         StringBuilder sb = new StringBuilder();
152         
153         sb.append("ModifyContext for DN '").append( getDn().getUpName() ).append( "', modifications :\n" );
154         
155         for ( Modification mod:modItems )
156         {
157             sb.append( mod ).append( '\n' );
158         }
159         
160         return sb.toString();
161     }
162 }