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.bootstrap;
021
022
023 import javax.naming.NamingException;
024
025 import org.apache.directory.server.schema.registries.OidRegistry;
026 import org.apache.directory.server.schema.registries.Registries;
027 import org.apache.directory.shared.ldap.entry.Value;
028 import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
029 import org.apache.directory.shared.ldap.exception.LdapNamingException;
030 import org.apache.directory.shared.ldap.message.ResultCodeEnum;
031 import org.apache.directory.shared.ldap.schema.Normalizer;
032 import org.apache.directory.shared.ldap.schema.syntaxes.NumericOidSyntaxChecker;
033
034
035 /**
036 * A name or numeric id normalizer. Needs an OID registry to operate properly.
037 * The OID registry is injected into this class after instantiation if a
038 * setRegistries(Registries) method is exposed.
039 *
040 *
041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
042 * @version $Rev$
043 */
044 public class NameOrNumericIdNormalizer implements Normalizer
045 {
046 // The serial UID
047 private static final long serialVersionUID = 1L;
048
049 private NumericOidSyntaxChecker checker = new NumericOidSyntaxChecker();
050
051 private transient OidRegistry registry;
052
053
054 /** A static instance of this normalizer */
055 public static final NameOrNumericIdNormalizer INSTANCE = new NameOrNumericIdNormalizer();
056
057
058 public NameOrNumericIdNormalizer( OidRegistry registry )
059 {
060 this.registry = registry;
061 }
062
063
064 /**
065 *
066 */
067 public NameOrNumericIdNormalizer()
068 {
069 // Nothing to do
070 }
071
072
073 /**
074 * {@inheritDoc}
075 */
076 public Value<?> normalize( Value<?> value ) throws NamingException
077 {
078 if ( value == null )
079 {
080 return null;
081 }
082
083 String strValue = value.getString();
084
085 if ( strValue.length() == 0 )
086 {
087 return new ClientStringValue( "" );
088 }
089
090 // if value is a numeric id then return it as is
091 if ( checker.isValidSyntax( strValue ) )
092 {
093 return value;
094 }
095
096 // if it is a name we need to do a lookup
097 if ( registry.hasOid( strValue ) )
098 {
099 return new ClientStringValue( registry.getOid( strValue ) );
100 }
101
102 // if all else fails
103 throw new LdapNamingException( "Encountered name based id of " + value
104 + " which was not found in the OID registry" , ResultCodeEnum.OTHER );
105 }
106
107
108 /**
109 * {@inheritDoc}
110 */
111 public String normalize( String value ) throws NamingException
112 {
113 if ( value == null )
114 {
115 return null;
116 }
117
118 if ( value.length() == 0 )
119 {
120 return value;
121 }
122
123 // if value is a numeric id then return it as is
124 if ( checker.isValidSyntax( value ) )
125 {
126 return value;
127 }
128
129 // if it is a name we need to do a lookup
130 if ( registry.hasOid( value ) )
131 {
132 return registry.getOid( value );
133 }
134
135 // if all else fails
136 throw new LdapNamingException( "Encountered name based id of " + value
137 + " which was not found in the OID registry" , ResultCodeEnum.OTHER );
138 }
139
140
141 public void setRegistries( Registries registries )
142 {
143 this.registry = registries.getOidRegistry();
144 }
145 }