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.ldap.constants.SchemaConstants; 024 import org.apache.directory.shared.ldap.name.DN; 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 valid Name and Optional UID. 033 * 034 * This element is a composition of two parts : a DN and an optional UID : 035 * NameAndOptionalUID = distinguishedName [ SHARP BitString ] 036 * 037 * Both part already have their syntax checkers, so we will just call them 038 * after having splitted the element in two ( if necessary) 039 * 040 * We just check that the DN is valid, we don't need to verify each of the RDN 041 * syntax. 042 * 043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 044 * @version $Rev$ 045 */ 046 public class NameAndOptionalUIDSyntaxChecker extends SyntaxChecker 047 { 048 /** A logger for this class */ 049 private static final Logger LOG = LoggerFactory.getLogger( NameAndOptionalUIDSyntaxChecker.class ); 050 051 /** The serialVersionUID */ 052 private static final long serialVersionUID = 1L; 053 054 /** 055 * Creates a new instance of NameAndOptionalUIDSyntaxChecker. 056 */ 057 public NameAndOptionalUIDSyntaxChecker() 058 { 059 super( SchemaConstants.NAME_AND_OPTIONAL_UID_SYNTAX ); 060 } 061 062 063 /** 064 * {@inheritDoc} 065 */ 066 public boolean isValidSyntax( Object value ) 067 { 068 String strValue = null; 069 070 if ( value == null ) 071 { 072 LOG.debug( "Syntax invalid for '{}'", value ); 073 return false; 074 } 075 076 if ( value instanceof String ) 077 { 078 strValue = ( String ) value; 079 } 080 else if ( value instanceof byte[] ) 081 { 082 strValue = StringTools.utf8ToString( ( byte[] ) value ); 083 } 084 else 085 { 086 strValue = value.toString(); 087 } 088 089 if ( strValue.length() == 0 ) 090 { 091 LOG.debug( "Syntax invalid for '{}'", value ); 092 return false; 093 } 094 095 // Let's see if we have an UID part 096 int sharpPos = strValue.lastIndexOf( '#' ); 097 098 if ( sharpPos != -1 ) 099 { 100 // Now, check that we don't have another '#' 101 if ( strValue.indexOf( '#' ) != sharpPos ) 102 { 103 // Yes, we have one : this is not allowed, it should have been 104 // escaped. 105 LOG.debug( "Syntax invalid for '{}'", value ); 106 return false; 107 } 108 109 // This is an UID if the '#' is immediatly 110 // followed by a BitString, except if the '#' is 111 // on the last position 112 // We shoould not find a 113 if ( BitStringSyntaxChecker.isValid( strValue.substring( sharpPos + 1 ) ) && 114 ( sharpPos < strValue.length() ) ) 115 { 116 // Ok, we have a BitString, now check the DN, 117 // except if the '#' is in first position 118 if ( sharpPos > 0 ) 119 { 120 boolean result = DN.isValid( strValue.substring( 0, sharpPos ) ); 121 122 if ( result ) 123 { 124 LOG.debug( "Syntax valid for '{}'", value ); 125 } 126 else 127 { 128 LOG.debug( "Syntax invalid for '{}'", value ); 129 } 130 131 return result; 132 133 } 134 else 135 { 136 // The DN must not be null ? 137 LOG.debug( "Syntax invalid for '{}'", value ); 138 return false; 139 } 140 } 141 else 142 { 143 // We have found a '#' but no UID part. 144 LOG.debug( "Syntax invalid for '{}'", value ); 145 return false; 146 } 147 } 148 else 149 { 150 // No UID, the strValue is a DN 151 // Check that the value is a valid DN 152 boolean result = DN.isValid( strValue ); 153 154 if ( result ) 155 { 156 LOG.debug( "Syntax valid for '{}'", value ); 157 } 158 else 159 { 160 LOG.debug( "Syntax invalid for '{}'", value ); 161 } 162 163 return result; 164 } 165 } 166 }