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 */ 020package org.apache.directory.api.ldap.model.schema.syntaxCheckers; 021 022 023import org.apache.directory.api.asn1.util.Oid; 024import org.apache.directory.api.ldap.model.constants.SchemaConstants; 025import org.apache.directory.api.ldap.model.schema.SyntaxChecker; 026import org.apache.directory.api.util.Strings; 027import org.slf4j.Logger; 028import 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 */ 050@SuppressWarnings("serial") 051public class OidLenSyntaxChecker extends SyntaxChecker 052{ 053 /** A logger for this class */ 054 private static final Logger LOG = LoggerFactory.getLogger( OidLenSyntaxChecker.class ); 055 056 057 /** 058 * 059 * Creates a new instance of OidLenSyntaxChecker. 060 * 061 */ 062 public OidLenSyntaxChecker() 063 { 064 super( SchemaConstants.OID_LEN_SYNTAX ); 065 } 066 067 068 /** 069 * {@inheritDoc} 070 */ 071 public boolean isValidSyntax( Object value ) 072 { 073 String strValue = null; 074 075 if ( value == null ) 076 { 077 LOG.debug( "Syntax invalid for 'null'" ); 078 return false; 079 } 080 081 if ( value instanceof String ) 082 { 083 strValue = ( String ) value; 084 } 085 else if ( value instanceof byte[] ) 086 { 087 strValue = Strings.utf8ToString( ( byte[] ) value ); 088 } 089 else 090 { 091 strValue = value.toString(); 092 } 093 094 if ( strValue.length() == 0 ) 095 { 096 LOG.debug( "Syntax invalid for '{}'", value ); 097 return false; 098 } 099 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}