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.asn1.util.Oid;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
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 numeric oid and a length
33   * constraint according to RFC 4512.
34   * 
35   * From RFC 4512 :
36   * 
37   * noidlen    = numericoid [ LCURLY len RCURLY ]
38   * numericoid = number 1*( DOT number )
39   * len        = number
40   * number     = DIGIT | ( LDIGIT 1*DIGIT )
41   * DIGIT      = %x30 | LDIGIT                  ; "0"-"9"
42   * LDIGIT     = %x31-39                        ; "1"-"9"
43   * DOT        = %x2E                           ; period (".")
44   * LCURLY  = %x7B                              ; left curly brace "{"
45   * RCURLY  = %x7D                              ; right curly brace "}"
46   * 
47   *
48   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
49   */
50  @SuppressWarnings("serial")
51  public class OidLenSyntaxChecker extends SyntaxChecker
52  {
53      /** A logger for this class */
54      private static final Logger LOG = LoggerFactory.getLogger( OidLenSyntaxChecker.class );
55  
56  
57      /**
58       * 
59       * Creates a new instance of OidLenSyntaxChecker.
60       *
61       */
62      public OidLenSyntaxChecker()
63      {
64          super( SchemaConstants.OID_LEN_SYNTAX );
65      }
66  
67  
68      /**
69       * {@inheritDoc}
70       */
71      public boolean isValidSyntax( Object value )
72      {
73          String strValue = null;
74  
75          if ( value == null )
76          {
77              LOG.debug( "Syntax invalid for 'null'" );
78              return false;
79          }
80  
81          if ( value instanceof String )
82          {
83              strValue = ( String ) value;
84          }
85          else if ( value instanceof byte[] )
86          {
87              strValue = Strings.utf8ToString( ( byte[] ) value );
88          }
89          else
90          {
91              strValue = value.toString();
92          }
93  
94          if ( strValue.length() == 0 )
95          {
96              LOG.debug( "Syntax invalid for '{}'", value );
97              return false;
98          }
99  
100         // We are looking at the first position of the len part
101         int pos = strValue.indexOf( '{' );
102 
103         if ( pos < 0 )
104         {
105             // Not found ... but it may still be a valid OID
106             boolean result = Oid.isOid( strValue );
107 
108             if ( result )
109             {
110                 LOG.debug( "Syntax valid for '{}'", value );
111             }
112             else
113             {
114                 LOG.debug( "Syntax invalid for '{}'", value );
115             }
116 
117             return result;
118         }
119         else
120         {
121             // we should have a len value. First check that the OID is valid
122             String oid = strValue.substring( 0, pos );
123 
124             if ( !Oid.isOid( oid ) )
125             {
126                 LOG.debug( "Syntax invalid for '{}'", value );
127                 return false;
128             }
129 
130             String len = strValue.substring( pos );
131 
132             // We must have a lnumber and a '}' at the end
133             if ( len.charAt( len.length() - 1 ) != '}' )
134             {
135                 // No final '}'
136                 LOG.debug( "Syntax invalid for '{}'", value );
137                 return false;
138             }
139 
140             for ( int i = 1; i < len.length() - 1; i++ )
141             {
142                 switch ( len.charAt( i ) )
143                 {
144                     case '0':
145                     case '1':
146                     case '2':
147                     case '3':
148                     case '4':
149                     case '5':
150                     case '6':
151                     case '7':
152                     case '8':
153                     case '9':
154                         break;
155 
156                     default:
157                         LOG.debug( "Syntax invalid for '{}'", value );
158                         return false;
159                 }
160             }
161 
162             if ( ( len.charAt( 1 ) == '0' ) && len.length() > 3 )
163             {
164                 // A number can't start with a '0' unless it's the only
165                 // number
166                 LOG.debug( "Syntax invalid for '{}'", value );
167                 return false;
168             }
169 
170             LOG.debug( "Syntax valid for '{}'", value );
171             return true;
172         }
173     }
174 }