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 * https://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.api.ldap.model.schema.normalizers; 021 022 023import org.apache.directory.api.i18n.I18n; 024import org.apache.directory.api.ldap.model.constants.SchemaConstants; 025import org.apache.directory.api.ldap.model.exception.LdapException; 026import org.apache.directory.api.ldap.model.name.Dn; 027import org.apache.directory.api.ldap.model.schema.Normalizer; 028import org.apache.directory.api.ldap.model.schema.PrepareString; 029import org.apache.directory.api.ldap.model.schema.SchemaManager; 030import org.apache.directory.api.util.Strings; 031 032 033/** 034 * A normalizer for UniqueMember. We will get the Normilzed name of the DN 035 * 036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 037 */ 038@SuppressWarnings("serial") 039public class UniqueMemberNormalizer extends Normalizer 040{ 041 /** A reference to the schema manager used to normalize the Dn */ 042 private transient SchemaManager schemaManager; 043 044 045 /** 046 * Creates a new UniqueMemberNormalizer instance 047 */ 048 public UniqueMemberNormalizer() 049 { 050 super( SchemaConstants.UNIQUE_MEMBER_MATCH_MR_OID ); 051 } 052 053 054 /** 055 * {@inheritDoc} 056 */ 057 @Override 058 public String normalize( String value ) throws LdapException 059 { 060 return normalize( value, PrepareString.AssertionType.ATTRIBUTE_VALUE ); 061 } 062 063 064 /** 065 * {@inheritDoc} 066 */ 067 @Override 068 public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException 069 { 070 if ( Strings.isEmpty( value ) ) 071 { 072 return null; 073 } 074 075 // Let's see if we have an UID part 076 int sharpPos = value.lastIndexOf( '#' ); 077 078 if ( sharpPos != -1 ) 079 { 080 // Now, check that we don't have another '#' 081 if ( value.indexOf( '#' ) != sharpPos ) 082 { 083 // Yes, we have one : this is not allowed, it should have been 084 // escaped. 085 return null; 086 } 087 088 // This is an UID if the '#' is immediately 089 // followed by a BitString, except if the '#' is 090 // on the last position 091 String uid = value.substring( sharpPos + 1 ); 092 093 if ( sharpPos > 0 ) 094 { 095 Dn dn = new Dn( schemaManager, value.substring( 0, sharpPos ) ); 096 097 return dn.getNormName() + '#' + uid; 098 } 099 else 100 { 101 throw new IllegalStateException( 102 I18n.err( I18n.ERR_13725_CANNOT_HANDLE_NAME_AND_OPTIONAL_UID_NORM, value.getClass() ) ); 103 } 104 } 105 else 106 { 107 // No UID, the strValue is a Dn 108 // Return the normalized Dn 109 return new Dn( schemaManager, value ).getNormName(); 110 } 111 } 112 113 114 /** 115 * {@inheritDoc} 116 */ 117 @Override 118 public void setSchemaManager( SchemaManager schemaManager ) 119 { 120 this.schemaManager = schemaManager; 121 } 122}