View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.normalizers;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.exception.LdapException;
26  import org.apache.directory.api.ldap.model.name.Dn;
27  import org.apache.directory.api.ldap.model.schema.Normalizer;
28  import org.apache.directory.api.ldap.model.schema.PrepareString;
29  import org.apache.directory.api.ldap.model.schema.SchemaManager;
30  import org.apache.directory.api.util.Strings;
31  
32  
33  /**
34   * A normalizer for UniqueMember. We will get the Normilzed name of the DN
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  @SuppressWarnings("serial")
39  public class UniqueMemberNormalizer extends Normalizer
40  {
41      /** A reference to the schema manager used to normalize the Dn */
42      private transient SchemaManager schemaManager;
43  
44  
45      /**
46       * Creates a new UniqueMemberNormalizer instance
47       */
48      public UniqueMemberNormalizer()
49      {
50          super( SchemaConstants.UNIQUE_MEMBER_MATCH_MR_OID );
51      }
52  
53  
54      /**
55       * {@inheritDoc}
56       */
57      @Override
58      public String normalize( String value ) throws LdapException
59      {
60          return normalize( value, PrepareString.AssertionType.ATTRIBUTE_VALUE );
61      }
62  
63  
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public String normalize( String value, PrepareString.AssertionType assertionType ) throws LdapException
69      {
70          if ( Strings.isEmpty( value ) )
71          {
72              return null;
73          }
74  
75          // Let's see if we have an UID part
76          int sharpPos = value.lastIndexOf( '#' );
77  
78          if ( sharpPos != -1 )
79          {
80              // Now, check that we don't have another '#'
81              if ( value.indexOf( '#' ) != sharpPos )
82              {
83                  // Yes, we have one : this is not allowed, it should have been
84                  // escaped.
85                  return null;
86              }
87  
88              // This is an UID if the '#' is immediately
89              // followed by a BitString, except if the '#' is
90              // on the last position
91              String uid = value.substring( sharpPos + 1 );
92  
93              if ( sharpPos > 0 )
94              {
95                  Dn dn = new Dn( schemaManager, value.substring( 0, sharpPos ) );
96  
97                  return dn.getNormName() + '#' + uid;
98              }
99              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 }