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 */ 020 package org.apache.directory.server.core.collective; 021 022 023 import java.util.List; 024 import java.util.Set; 025 026 import javax.naming.NamingException; 027 028 import org.apache.directory.server.core.interceptor.context.OperationContext; 029 import org.apache.directory.server.core.partition.ByPassConstants; 030 import org.apache.directory.server.core.partition.PartitionNexus; 031 import org.apache.directory.server.i18n.I18n; 032 import org.apache.directory.shared.ldap.constants.SchemaConstants; 033 import org.apache.directory.shared.ldap.entry.EntryAttribute; 034 import org.apache.directory.shared.ldap.entry.Modification; 035 import org.apache.directory.shared.ldap.entry.ModificationOperation; 036 import org.apache.directory.shared.ldap.entry.ServerEntry; 037 import org.apache.directory.shared.ldap.exception.LdapException; 038 import org.apache.directory.shared.ldap.exception.LdapInvalidAttributeTypeException; 039 import org.apache.directory.shared.ldap.exception.LdapSchemaViolationException; 040 import org.apache.directory.shared.ldap.message.ResultCodeEnum; 041 import org.apache.directory.shared.ldap.name.DN; 042 import org.apache.directory.shared.ldap.schema.AttributeType; 043 import org.apache.directory.shared.ldap.schema.SchemaManager; 044 import org.apache.directory.shared.ldap.schema.SchemaUtils; 045 046 047 /** 048 * Schema checking utilities specifically for operations on collective attributes. 049 * 050 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 051 * @version $Rev:$ 052 */ 053 public class CollectiveAttributesSchemaChecker 054 { 055 private PartitionNexus nexus = null; 056 private SchemaManager schemaManager = null; 057 058 public CollectiveAttributesSchemaChecker( PartitionNexus nexus, SchemaManager schemaManager ) 059 { 060 this.nexus = nexus; 061 this.schemaManager = schemaManager; 062 } 063 064 /* package scope*/ void checkAdd( DN normName, ServerEntry entry ) throws Exception 065 { 066 if ( entry.hasObjectClass( SchemaConstants.COLLECTIVE_ATTRIBUTE_SUBENTRY_OC ) ) 067 { 068 return; 069 } 070 071 if ( containsAnyCollectiveAttributes( entry ) ) 072 { 073 /* 074 * TODO: Replace the Exception and the ResultCodeEnum with the correct ones. 075 */ 076 throw new LdapSchemaViolationException( ResultCodeEnum.OTHER, I18n.err( I18n.ERR_241 ) ); 077 } 078 } 079 080 081 public void checkModify( OperationContext opContext, DN normName, List<Modification> mods ) throws Exception 082 { 083 ServerEntry originalEntry = opContext.lookup( normName, ByPassConstants.LOOKUP_BYPASS ); 084 ServerEntry targetEntry = (ServerEntry)SchemaUtils.getTargetEntry( mods, originalEntry ); 085 086 EntryAttribute targetObjectClasses = targetEntry.get( SchemaConstants.OBJECT_CLASS_AT ); 087 088 if ( targetObjectClasses.contains( SchemaConstants.COLLECTIVE_ATTRIBUTE_SUBENTRY_OC ) ) 089 { 090 return; 091 } 092 093 if ( addsAnyCollectiveAttributes( mods ) ) 094 { 095 /* 096 * TODO: Replace the Exception and the ResultCodeEnum with the correct ones. 097 */ 098 throw new LdapSchemaViolationException( ResultCodeEnum.OTHER, I18n.err( I18n.ERR_242 )); 099 } 100 } 101 102 103 private boolean addsAnyCollectiveAttributes( List<Modification> mods ) throws LdapException 104 { 105 for ( Modification mod:mods ) 106 { 107 // TODO: handle http://issues.apache.org/jira/browse/DIRSERVER-1198 108 EntryAttribute attr = mod.getAttribute(); 109 AttributeType attrType = attr.getAttributeType(); 110 111 if ( attrType == null ) 112 { 113 if ( !schemaManager.getAttributeTypeRegistry().contains( attr.getUpId() ) ) 114 { 115 throw new LdapInvalidAttributeTypeException(); 116 } 117 else 118 { 119 attrType = schemaManager.lookupAttributeTypeRegistry( attr.getUpId() ); 120 } 121 } 122 123 124 ModificationOperation modOp = mod.getOperation(); 125 126 if ( ( ( modOp == ModificationOperation.ADD_ATTRIBUTE ) || ( modOp == ModificationOperation.REPLACE_ATTRIBUTE ) ) && 127 attrType.isCollective() ) 128 { 129 return true; 130 } 131 } 132 133 return false; 134 } 135 136 137 private boolean containsAnyCollectiveAttributes( ServerEntry entry ) throws NamingException 138 { 139 Set<AttributeType> attributeTypes = entry.getAttributeTypes(); 140 141 for ( AttributeType attributeType:attributeTypes ) 142 { 143 if ( attributeType.isCollective() ) 144 { 145 return true; 146 } 147 } 148 149 return false; 150 } 151 }