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.shared.ldap.schema.syntaxCheckers;
021    
022    
023    import org.apache.directory.shared.asn1.primitives.OID;
024    import org.apache.directory.shared.ldap.constants.SchemaConstants;
025    import org.apache.directory.shared.ldap.schema.SyntaxChecker;
026    import org.apache.directory.shared.ldap.util.StringTools;
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    
030    
031    /**
032     * A SyntaxChecker which verifies that a value is a numeric oid and a length
033     * constraint according to RFC 4512.
034     * 
035     * From RFC 4512 :
036     * 
037     * noidlen    = numericoid [ LCURLY len RCURLY ]
038     * numericoid = number 1*( DOT number )
039     * len        = number
040     * number     = DIGIT | ( LDIGIT 1*DIGIT )
041     * DIGIT      = %x30 | LDIGIT                  ; "0"-"9"
042     * LDIGIT     = %x31-39                        ; "1"-"9"
043     * DOT        = %x2E                           ; period (".")
044     * LCURLY  = %x7B                              ; left curly brace "{"
045     * RCURLY  = %x7D                              ; right curly brace "}"
046     * 
047     *
048     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
049     * @version $Rev$
050     */
051    public class OidLenSyntaxChecker extends SyntaxChecker
052    {
053        /** A logger for this class */
054        private static final Logger LOG = LoggerFactory.getLogger( OidLenSyntaxChecker.class );
055    
056        /** The serialVersionUID */
057        private static final long serialVersionUID = 1L;
058    
059        /**
060         * 
061         * Creates a new instance of OidLenSyntaxChecker.
062         *
063         */
064        public OidLenSyntaxChecker()
065        {
066            super( SchemaConstants.OID_LEN_SYNTAX );
067        }
068        
069    
070        /**
071         * {@inheritDoc}
072         */
073        public boolean isValidSyntax( Object value )
074        {
075            String strValue = null;
076    
077            if ( value == null )
078            {
079                LOG.debug( "Syntax invalid for '{}'", value );
080                return false;
081            }
082            
083            if ( value instanceof String )
084            {
085                strValue = ( String ) value;
086            }
087            else if ( value instanceof byte[] )
088            {
089                strValue = StringTools.utf8ToString( ( byte[] ) value ); 
090            }
091            else
092            {
093                strValue = value.toString();
094            }
095    
096            if ( strValue.length() == 0 )
097            {
098                LOG.debug( "Syntax invalid for '{}'", value );
099                return false;
100            }
101            
102            // We are looking at the first position of the len part
103            int pos = strValue.indexOf( '{' );
104                
105            if ( pos < 0 )
106            {
107                // Not found ... but it may still be a valid OID
108                boolean result = OID.isOID( strValue );
109                
110                if ( result )
111                {
112                    LOG.debug( "Syntax valid for '{}'", value );
113                }
114                else
115                {
116                    LOG.debug( "Syntax invalid for '{}'", value );
117                }
118                
119                return result;
120            }
121            else
122            {
123                // we should have a len value. First check that the OID is valid
124                String oid = strValue.substring( 0, pos );
125                
126                if ( !OID.isOID( oid ) )
127                {
128                    LOG.debug( "Syntax invalid for '{}'", value );
129                    return false;
130                }
131                
132                String len = strValue.substring( pos );
133                
134                // We must have a lnumber and a '}' at the end
135                if ( len.charAt( len.length() -1 ) != '}' )
136                {
137                    // No final '}'
138                    LOG.debug( "Syntax invalid for '{}'", value );
139                    return false;
140                }
141                
142                for ( int i = 1; i < len.length() - 1; i++ )
143                {
144                    switch ( len.charAt(i) )
145                    {
146                        case '0': case '1': case '2' : case '3': case '4':
147                        case '5': case '6': case '7' : case '8': case '9':
148                            break;
149                            
150                        default: 
151                            LOG.debug( "Syntax invalid for '{}'", value );
152                            return false;
153                    }
154                }
155                
156                if ( ( len.charAt( 1 ) == '0' ) && len.length() > 3 )
157                {
158                    // A number can't start with a '0' unless it's the only
159                    // number
160                    LOG.debug( "Syntax invalid for '{}'", value );
161                    return false;
162                }
163                
164                LOG.debug( "Syntax valid for '{}'", value );
165                return true;
166            }
167        }
168    }