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.trigger;
021
022
023import java.util.ArrayList;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.directory.api.ldap.model.constants.SchemaConstants;
028import org.apache.directory.api.ldap.model.entry.Entry;
029import org.apache.directory.api.ldap.model.entry.Modification;
030import org.apache.directory.api.ldap.model.exception.LdapException;
031import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
032import org.apache.directory.api.ldap.model.name.Dn;
033import org.apache.directory.api.ldap.trigger.StoredProcedureParameter;
034import org.apache.directory.server.core.api.CoreSession;
035import org.apache.directory.server.core.api.interceptor.context.LookupOperationContext;
036import org.apache.directory.server.core.api.interceptor.context.ModifyOperationContext;
037import org.apache.directory.server.core.api.interceptor.context.OperationContext;
038
039
040public class ModifyStoredProcedureParameterInjector extends AbstractStoredProcedureParameterInjector
041{
042    private Dn modifiedEntryName;
043    private List<Modification> modifications;
044    private Entry oldEntry;
045
046
047    public ModifyStoredProcedureParameterInjector( ModifyOperationContext opContext ) throws LdapException
048    {
049        super( opContext );
050        modifiedEntryName = opContext.getDn();
051        modifications = opContext.getModItems();
052        this.oldEntry = getEntry( opContext );
053        Map<Class<?>, MicroInjector> injectors = super.getInjectors();
054        injectors.put( StoredProcedureParameter.Modify_OBJECT.class, objectInjector );
055        injectors.put( StoredProcedureParameter.Modify_MODIFICATION.class, modificationInjector );
056        injectors.put( StoredProcedureParameter.Modify_OLD_ENTRY.class, oldEntryInjector );
057        injectors.put( StoredProcedureParameter.Modify_NEW_ENTRY.class, newEntryInjector );
058    }
059
060    MicroInjector objectInjector = new MicroInjector()
061    {
062        public Object inject( OperationContext opContext, StoredProcedureParameter param )
063            throws LdapInvalidDnException
064        {
065            // Return a safe copy constructed with user provided name.
066            return opContext.getSession().getDirectoryService().getDnFactory().create( modifiedEntryName.getName() );
067        }
068    };
069
070    MicroInjector modificationInjector = new MicroInjector()
071    {
072        public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws LdapException
073        {
074            List<Modification> newMods = new ArrayList<>();
075
076            for ( Modification mod : modifications )
077            {
078                newMods.add( mod.clone() );
079            }
080
081            return newMods;
082        }
083    };
084
085    MicroInjector oldEntryInjector = new MicroInjector()
086    {
087        public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws LdapException
088        {
089            return oldEntry;
090        }
091    };
092
093    MicroInjector newEntryInjector = new MicroInjector()
094    {
095        public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws LdapException
096        {
097            return getEntry( opContext );
098        }
099    };
100
101
102    private Entry getEntry( OperationContext opContext ) throws LdapException
103    {
104        /**
105         * Exclude operational attributes while doing lookup
106         * especially subentry related ones like "triggerExecutionSubentries".
107         */
108        CoreSession session = opContext.getSession();
109        LookupOperationContext lookupContext = new LookupOperationContext( session, modifiedEntryName,
110            SchemaConstants.ALL_USER_ATTRIBUTES_ARRAY );
111        lookupContext.setPartition( opContext.getPartition() );
112        lookupContext.setTransaction( opContext.getTransaction() );
113        
114        return session.getDirectoryService().getPartitionNexus().lookup( lookupContext );
115    }
116}