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.syntaxCheckers;
21  
22  
23  import org.apache.directory.api.asn1.util.Oid;
24  import org.apache.directory.api.i18n.I18n;
25  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
26  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
27  import org.apache.directory.api.util.Strings;
28  
29  
30  /**
31   * A SyntaxChecker which verifies that a value is a numeric oid and a length
32   * constraint according to RFC 4512.
33   * <p>
34   * From RFC 4512 :
35   * <pre>
36   * noidlen    = numericoid [ LCURLY len RCURLY ]
37   * numericoid = number 1*( DOT number )
38   * len        = number
39   * number     = DIGIT | ( LDIGIT 1*DIGIT )
40   * DIGIT      = %x30 | LDIGIT                  ; "0"-"9"
41   * LDIGIT     = %x31-39                        ; "1"-"9"
42   * DOT        = %x2E                           ; period (".")
43   * LCURLY  = %x7B                              ; left curly brace "{"
44   * RCURLY  = %x7D                              ; right curly brace "}"
45   * </pre>
46   *
47   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
48   */
49  @SuppressWarnings("serial")
50  public final class OidLenSyntaxChecker extends SyntaxChecker
51  {
52      /**
53       * A static instance of OidLenSyntaxChecker
54       */
55      public static final OidLenSyntaxChecker INSTANCE = 
56          new OidLenSyntaxChecker( SchemaConstants.OID_LEN_SYNTAX );
57      
58      /**
59       * A static Builder for this class
60       */
61      public static final class Builder extends SCBuilder<OidLenSyntaxChecker>
62      {
63          /**
64           * The Builder constructor
65           */
66          private Builder()
67          {
68              super( SchemaConstants.OID_LEN_SYNTAX );
69          }
70          
71          
72          /**
73           * Create a new instance of OidLenSyntaxChecker
74           * @return A new instance of OidLenSyntaxChecker
75           */
76          @Override
77          public OidLenSyntaxChecker build()
78          {
79              return new OidLenSyntaxChecker( oid );
80          }
81      }
82  
83      
84      /**
85       * 
86       * Creates a new instance of OidLenSyntaxChecker.
87       * 
88       * @param oid The OID
89       */
90      private OidLenSyntaxChecker( String oid )
91      {
92          super( oid );
93      }
94  
95      
96      /**
97       * @return An instance of the Builder for this class
98       */
99      public static Builder builder()
100     {
101         return new Builder();
102     }
103 
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public boolean isValidSyntax( Object value )
110     {
111         String strValue;
112 
113         if ( value == null )
114         {
115             if ( LOG.isDebugEnabled() )
116             {
117                 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
118             }
119             
120             return false;
121         }
122 
123         if ( value instanceof String )
124         {
125             strValue = ( String ) value;
126         }
127         else if ( value instanceof byte[] )
128         {
129             strValue = Strings.utf8ToString( ( byte[] ) value );
130         }
131         else
132         {
133             strValue = value.toString();
134         }
135 
136         if ( strValue.length() == 0 )
137         {
138             if ( LOG.isDebugEnabled() )
139             {
140                 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
141             }
142             
143             return false;
144         }
145 
146         // We are looking at the first position of the len part
147         int pos = strValue.indexOf( '{' );
148 
149         if ( pos < 0 )
150         {
151             // Not found ... but it may still be a valid OID
152             boolean result = Oid.isOid( strValue );
153 
154             if ( LOG.isDebugEnabled() )
155             {
156                 if ( result )
157                 {
158                     LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
159                 }
160                 else
161                 {
162                     LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
163                 }
164             }
165                 
166             return result;
167         }
168         else
169         {
170             // we should have a len value. First check that the OID is valid
171             String oid = strValue.substring( 0, pos );
172 
173             if ( !Oid.isOid( oid ) )
174             {
175                 if ( LOG.isDebugEnabled() )
176                 {
177                     LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
178                 }
179                 
180                 return false;
181             }
182 
183             String len = strValue.substring( pos );
184 
185             // We must have a number and a '}' at the end
186             if ( len.charAt( len.length() - 1 ) != '}' )
187             {
188                 // No final '}'
189                 if ( LOG.isDebugEnabled() )
190                 {
191                     LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
192                 }
193                 
194                 return false;
195             }
196 
197             for ( int i = 1; i < len.length() - 1; i++ )
198             {
199                 switch ( len.charAt( i ) )
200                 {
201                     case '0':
202                     case '1':
203                     case '2':
204                     case '3':
205                     case '4':
206                     case '5':
207                     case '6':
208                     case '7':
209                     case '8':
210                     case '9':
211                         break;
212 
213                     default:
214                         if ( LOG.isDebugEnabled() )
215                         {
216                             LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
217                         }
218                         
219                         return false;
220                 }
221             }
222 
223             if ( ( len.charAt( 1 ) == '0' ) && len.length() > 3 )
224             {
225                 // A number can't start with a '0' unless it's the only
226                 // number
227                 if ( LOG.isDebugEnabled() )
228                 {
229                     LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
230                 }
231                 
232                 return false;
233             }
234 
235             if ( LOG.isDebugEnabled() )
236             {
237                 LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) );
238             }
239             
240             return true;
241         }
242     }
243 }