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.schema;
021
022
023 import org.apache.directory.server.schema.registries.AttributeTypeRegistry;
024 import org.apache.directory.server.schema.registries.Registries;
025 import org.apache.directory.shared.ldap.entry.Value;
026 import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
027 import org.apache.directory.shared.ldap.name.LdapDN;
028 import org.apache.directory.shared.ldap.schema.Normalizer;
029 import org.apache.directory.shared.ldap.util.StringTools;
030
031 import javax.naming.NamingException;
032
033
034 /**
035 * A noirmalizer for UniqueMember
036 *
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 * @version $Rev$
039 */
040 public class NameAndOptionalUIDNormalizer implements Normalizer
041 {
042 // The serial UID
043 private static final long serialVersionUID = 1L;
044
045 private AttributeTypeRegistry attrRegistry;
046
047 public final static NameAndOptionalUIDNormalizer INSTANCE = new NameAndOptionalUIDNormalizer();
048
049 public NameAndOptionalUIDNormalizer()
050 {
051 // Do nothing
052 }
053
054 public NameAndOptionalUIDNormalizer( AttributeTypeRegistry attrRegistry )
055 {
056 this.attrRegistry = attrRegistry;
057 }
058
059
060 public void setRegistries( Registries registries )
061 {
062 this.attrRegistry = registries.getAttributeTypeRegistry();
063 }
064
065
066 public Value<?> normalize( Value<?> value ) throws NamingException
067 {
068 String nameAndUid = value.getString();
069
070 if ( nameAndUid.length() == 0 )
071 {
072 return null;
073 }
074
075 // Let's see if we have an UID part
076 int sharpPos = nameAndUid.lastIndexOf( '#' );
077
078 if ( sharpPos != -1 )
079 {
080 // Now, check that we don't have another '#'
081 if ( nameAndUid.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 = nameAndUid.substring( sharpPos + 1 );
092
093 if ( sharpPos > 0 )
094 {
095 LdapDN dn = new LdapDN( nameAndUid.substring( 0, sharpPos ) );
096
097 dn.normalize( attrRegistry.getNormalizerMapping() );
098
099 return new ClientStringValue( dn.getNormName() + '#' + uid );
100 }
101 else
102 {
103 throw new IllegalStateException( "I do not know how to handle NameAndOptionalUID normalization with objects of class: "
104 + value.getClass() );
105 }
106 }
107 else
108 {
109 // No UID, the strValue is a DN
110 // Return the normalized DN
111 return new ClientStringValue( new LdapDN( nameAndUid ).getNormName() );
112 }
113 }
114
115
116 public String normalize( String value ) throws NamingException
117 {
118 if ( StringTools.isEmpty( value ) )
119 {
120 return null;
121 }
122
123 // Let's see if we have an UID part
124 int sharpPos = value.lastIndexOf( '#' );
125
126 if ( sharpPos != -1 )
127 {
128 // Now, check that we don't have another '#'
129 if ( value.indexOf( '#' ) != sharpPos )
130 {
131 // Yes, we have one : this is not allowed, it should have been
132 // escaped.
133 return null;
134 }
135
136 // This is an UID if the '#' is immediatly
137 // followed by a BitString, except if the '#' is
138 // on the last position
139 String uid = value.substring( sharpPos + 1 );
140
141 if ( sharpPos > 0 )
142 {
143 LdapDN dn = new LdapDN( value.substring( 0, sharpPos ) );
144
145 dn.normalize( attrRegistry.getNormalizerMapping() );
146
147 return dn.getNormName() + '#' + uid;
148 }
149 else
150 {
151 throw new IllegalStateException( "I do not know how to handle NameAndOptionalUID normalization with objects of class: "
152 + value.getClass() );
153 }
154 }
155 else
156 {
157 // No UID, the strValue is a DN
158 // Return the normalized DN
159 return new LdapDN( value ).getNormName();
160 }
161 }
162 }