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.entry.DefaultEntry;
024import org.apache.directory.api.ldap.model.entry.Entry;
025import org.apache.directory.api.ldap.model.exception.LdapException;
026import org.apache.directory.api.ldap.model.exception.LdapOperationErrorException;
027import org.apache.directory.api.ldap.model.message.AddRequest;
028import org.apache.directory.api.ldap.model.message.MessageTypeEnum;
029import org.apache.directory.api.ldap.model.message.controls.ManageDsaIT;
030import org.apache.directory.api.ldap.model.name.Dn;
031import org.apache.directory.server.core.api.CoreSession;
032import org.apache.directory.server.core.api.OperationEnum;
033import org.apache.directory.server.core.api.entry.ClonedServerEntry;
034
035
036/**
037 * A Add context used for Interceptors. It contains all the informations
038 * needed for the add operation, and used by all the interceptors
039 *
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042public class AddOperationContext extends AbstractChangeOperationContext
043{
044    /**
045     * Creates a new instance of AddOperationContext.
046     * 
047     * @param session the current Session
048     */
049    public AddOperationContext( CoreSession session )
050    {
051        super( session );
052
053        if ( session != null )
054        {
055            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.ADD ) );
056        }
057    }
058
059
060    /**
061     * Creates a new instance of AddOperationContext.
062     * 
063     * @param session the current Session
064     * @param dn the name of the entry being added
065     */
066    public AddOperationContext( CoreSession session, Dn dn )
067    {
068        super( session, dn );
069
070        if ( session != null )
071        {
072            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.ADD ) );
073        }
074    }
075
076
077    /**
078     * Creates a new instance of AddOperationContext.
079     * 
080     * @param session the current Session
081     * @param entry the entry being added
082     */
083    public AddOperationContext( CoreSession session, Entry entry )
084    {
085        super( session, entry.getDn() );
086        this.entry = new ClonedServerEntry( entry );
087        if ( session != null )
088        {
089            setInterceptors( session.getDirectoryService().getInterceptors( OperationEnum.ADD ) );
090        }
091    }
092
093
094    /**
095     * Creates a new instance of AddOperationContext.
096     *
097     * @param session the current Session
098     * @param dn the name of the entry being added
099     * @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}