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 org.apache.directory.api.ldap.model.entry.DefaultEntry;
24  import org.apache.directory.api.ldap.model.entry.Entry;
25  import org.apache.directory.api.ldap.model.exception.LdapException;
26  import org.apache.directory.api.ldap.model.exception.LdapOperationErrorException;
27  import org.apache.directory.api.ldap.model.message.AddRequest;
28  import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
29  import org.apache.directory.api.ldap.model.message.controls.ManageDsaIT;
30  import org.apache.directory.api.ldap.model.name.Dn;
31  import org.apache.directory.server.core.api.CoreSession;
32  import org.apache.directory.server.core.api.OperationEnum;
33  import org.apache.directory.server.core.api.entry.ClonedServerEntry;
34  
35  
36  /**
37   * A Add context used for Interceptors. It contains all the informations
38   * needed for the add 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 AddOperationContext extends AbstractChangeOperationContext
43  {
44      /**
45       * Creates a new instance of AddOperationContext.
46       * 
47       * @param session the current Session
48       */
49      public AddOperationContext( CoreSession session )
50      {
51          super( session );
52  
53          if ( session != null )
54          {
55              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.ADD ) );
56          }
57      }
58  
59  
60      /**
61       * Creates a new instance of AddOperationContext.
62       * 
63       * @param session the current Session
64       * @param dn the name of the entry being added
65       */
66      public AddOperationContext( CoreSession session, Dn dn )
67      {
68          super( session, dn );
69  
70          if ( session != null )
71          {
72              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.ADD ) );
73          }
74      }
75  
76  
77      /**
78       * Creates a new instance of AddOperationContext.
79       * 
80       * @param session the current Session
81       * @param entry the entry being added
82       */
83      public AddOperationContext( CoreSession session, Entry entry )
84      {
85          super( session, entry.getDn() );
86          this.entry = new ClonedServerEntry( entry );
87          if ( session != null )
88          {
89              setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.ADD ) );
90          }
91      }
92  
93  
94      /**
95       * Creates a new instance of AddOperationContext.
96       *
97       * @param session the current Session
98       * @param dn the name of the entry being added
99       * @param entry the entry being added
100      */
101     public AddOperationContext( CoreSession session, Dn dn, Entry entry )
102     {
103         super( session, dn );
104 
105         if ( session != null )
106         {
107             setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.ADD ) );
108         }
109 
110         this.entry = new ClonedServerEntry( entry );
111     }
112 
113 
114     /**
115      * Creates a new instance of AddOperationContext.
116      * 
117      * @param session The session to use
118      * @param addRequest The Add operation to process
119      * @throws LdapException If the Add operation failed
120      */
121     public AddOperationContext( CoreSession session, AddRequest addRequest ) throws LdapException
122     {
123         super( session );
124 
125         if ( session != null )
126         {
127             setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.ADD ) );
128         }
129         else
130         {
131             throw new LdapOperationErrorException( "No session to proceed the operation" );
132         }
133 
134         Entry addEntry = addRequest.getEntry();
135 
136         if ( addEntry.isSchemaAware() )
137         {
138             entry = new ClonedServerEntry( addEntry );
139         }
140         else
141         {
142             entry = new ClonedServerEntry(
143                 new DefaultEntry( session.getDirectoryService().getSchemaManager(), addRequest.getEntry() ) );
144         }
145 
146         dn = addRequest.getEntry().getDn();
147         requestControls = addRequest.getControls();
148 
149         if ( requestControls.containsKey( ManageDsaIT.OID ) )
150         {
151             ignoreReferral();
152         }
153         else
154         {
155             throwReferral();
156         }
157     }
158 
159 
160     /**
161      * @return the operation name
162      */
163     public String getName()
164     {
165         return MessageTypeEnum.ADD_REQUEST.name();
166     }
167 
168 
169     /**
170      * @see Object#toString()
171      */
172     public String toString()
173     {
174         return "AddContext for Dn '" + getDn().getName() + "'" + ", added entry: " + entry;
175     }
176 }