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   *    http://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.syntaxCheckers;
21  
22  
23  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
24  import org.apache.directory.api.ldap.model.name.Dn;
25  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
26  import org.apache.directory.api.util.Strings;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  
31  /**
32   * A SyntaxChecker which verifies that a value is a valid Name and Optional UID.
33   * 
34   * This element is a composition of two parts : a Dn and an optional UID :
35   * NameAndOptionalUID = distinguishedName [ SHARP BitString ]
36   * 
37   * Both part already have their syntax checkers, so we will just call them
38   * after having splitted the element in two ( if necessary)
39   * 
40   * We just check that the Dn is valid, we don't need to verify each of the Rdn
41   * syntax.
42   * 
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   */
45  @SuppressWarnings("serial")
46  public class NameAndOptionalUIDSyntaxChecker extends SyntaxChecker
47  {
48      /** A logger for this class */
49      private static final Logger LOG = LoggerFactory.getLogger( NameAndOptionalUIDSyntaxChecker.class );
50  
51  
52      /**
53       * Creates a new instance of NameAndOptionalUIDSyntaxChecker.
54       */
55      public NameAndOptionalUIDSyntaxChecker()
56      {
57          super( SchemaConstants.NAME_AND_OPTIONAL_UID_SYNTAX );
58      }
59  
60  
61      /**
62       * {@inheritDoc}
63       */
64      public boolean isValidSyntax( Object value )
65      {
66          String strValue = null;
67  
68          if ( value == null )
69          {
70              LOG.debug( "Syntax invalid for 'null'" );
71              return false;
72          }
73  
74          if ( value instanceof String )
75          {
76              strValue = ( String ) value;
77          }
78          else if ( value instanceof byte[] )
79          {
80              strValue = Strings.utf8ToString( ( byte[] ) value );
81          }
82          else
83          {
84              strValue = value.toString();
85          }
86  
87          if ( strValue.length() == 0 )
88          {
89              LOG.debug( "Syntax invalid for '{}'", value );
90              return false;
91          }
92  
93          // Let's see if we have an UID part
94          int sharpPos = strValue.lastIndexOf( '#' );
95  
96          if ( sharpPos != -1 )
97          {
98              // Now, check that we don't have another '#'
99              if ( strValue.indexOf( '#' ) != sharpPos )
100             {
101                 // Yes, we have one : this is not allowed, it should have been
102                 // escaped.
103                 LOG.debug( "Syntax invalid for '{}'", value );
104                 return false;
105             }
106 
107             // This is an UID if the '#' is immediatly
108             // followed by a BitString, except if the '#' is
109             // on the last position
110             // We shoould not find a
111             if ( BitStringSyntaxChecker.isValid( strValue.substring( sharpPos + 1 ) )
112                 && ( sharpPos < strValue.length() ) )
113             {
114                 // Ok, we have a BitString, now check the Dn,
115                 // except if the '#' is in first position
116                 if ( sharpPos > 0 )
117                 {
118                     boolean result = Dn.isValid( strValue.substring( 0, sharpPos ) );
119 
120                     if ( result )
121                     {
122                         LOG.debug( "Syntax valid for '{}'", value );
123                     }
124                     else
125                     {
126                         LOG.debug( "Syntax invalid for '{}'", value );
127                     }
128 
129                     return result;
130 
131                 }
132                 else
133                 {
134                     // The Dn must not be null ?
135                     LOG.debug( "Syntax invalid for '{}'", value );
136                     return false;
137                 }
138             }
139             else
140             {
141                 // We have found a '#' but no UID part.
142                 LOG.debug( "Syntax invalid for '{}'", value );
143                 return false;
144             }
145         }
146         else
147         {
148             // No UID, the strValue is a Dn
149             // Check that the value is a valid Dn
150             boolean result = Dn.isValid( strValue );
151 
152             if ( result )
153             {
154                 LOG.debug( "Syntax valid for '{}'", value );
155             }
156             else
157             {
158                 LOG.debug( "Syntax invalid for '{}'", value );
159             }
160 
161             return result;
162         }
163     }
164 }