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.security.Principal; 024import java.util.ArrayList; 025import java.util.HashMap; 026import java.util.Iterator; 027import java.util.List; 028import java.util.Map; 029 030import org.apache.directory.api.ldap.model.constants.SchemaConstants; 031import org.apache.directory.api.ldap.model.exception.LdapException; 032import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException; 033import org.apache.directory.api.ldap.model.name.Dn; 034import org.apache.directory.api.ldap.trigger.StoredProcedureParameter; 035import org.apache.directory.api.ldap.trigger.StoredProcedureParameter.Generic_LDAP_CONTEXT; 036import org.apache.directory.server.core.api.CoreSession; 037import org.apache.directory.server.core.api.interceptor.context.LookupOperationContext; 038import org.apache.directory.server.core.api.interceptor.context.OperationContext; 039 040 041public abstract class AbstractStoredProcedureParameterInjector implements StoredProcedureParameterInjector 042{ 043 private OperationContext opContext; 044 private Map<Class<?>, MicroInjector> injectors; 045 046 047 MicroInjector operationPrincipalInjector = new MicroInjector() 048 { 049 @Override 050 public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws LdapException 051 { 052 return getOperationPrincipal(); 053 } 054 }; 055 056 MicroInjector ldapContextInjector = new MicroInjector() 057 { 058 @Override 059 public Object inject( OperationContext opContext, StoredProcedureParameter param ) throws LdapException 060 { 061 Generic_LDAP_CONTEXT ldapCtxParam = ( Generic_LDAP_CONTEXT ) param; 062 Dn ldapCtxName = ldapCtxParam.getCtxName(); 063 064 CoreSession session = opContext.getSession(); 065 LookupOperationContext lookupContext = 066 new LookupOperationContext( session, ldapCtxName, SchemaConstants.ALL_ATTRIBUTES_ARRAY ); 067 lookupContext.setPartition( opContext.getPartition() ); 068 lookupContext.setTransaction( opContext.getTransaction() ); 069 070 return session.getDirectoryService().getPartitionNexus().lookup( lookupContext ); 071 } 072 }; 073 074 075 public AbstractStoredProcedureParameterInjector( OperationContext opContext ) 076 { 077 this.opContext = opContext; 078 injectors = new HashMap<>(); 079 injectors.put( StoredProcedureParameter.Generic_OPERATION_PRINCIPAL.class, operationPrincipalInjector ); 080 injectors.put( StoredProcedureParameter.Generic_LDAP_CONTEXT.class, ldapContextInjector ); 081 } 082 083 084 protected Dn getOperationPrincipal() throws LdapInvalidDnException 085 { 086 Principal principal = opContext.getSession().getEffectivePrincipal(); 087 return opContext.getSession().getDirectoryService().getDnFactory().create( principal.getName() ); 088 } 089 090 091 protected Map<Class<?>, MicroInjector> getInjectors() 092 { 093 return injectors; 094 } 095 096 097 public OperationContext getOperationContext() 098 { 099 return opContext; 100 } 101 102 103 public void setOperationContext( OperationContext invocation ) 104 { 105 this.opContext = invocation; 106 } 107 108 109 @Override 110 public final List<Object> getArgumentsToInject( OperationContext opContext, 111 List<StoredProcedureParameter> parameterList ) throws LdapException 112 { 113 List<Object> arguments = new ArrayList<>(); 114 115 Iterator<StoredProcedureParameter> it = parameterList.iterator(); 116 117 while ( it.hasNext() ) 118 { 119 StoredProcedureParameter spParameter = it.next(); 120 MicroInjector injector = injectors.get( spParameter.getClass() ); 121 arguments.add( injector.inject( opContext, spParameter ) ); 122 } 123 124 return arguments; 125 } 126}