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.shared.ldap.model.schema.syntaxCheckers; 021 022 023import org.apache.directory.shared.ldap.model.constants.SchemaConstants; 024import org.apache.directory.shared.ldap.model.name.Dn; 025import org.apache.directory.shared.ldap.model.schema.SyntaxChecker; 026import org.apache.directory.shared.util.Strings; 027import org.slf4j.Logger; 028import 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 */ 045@SuppressWarnings("serial") 046public class NameAndOptionalUIDSyntaxChecker extends SyntaxChecker 047{ 048 /** A logger for this class */ 049 private static final Logger LOG = LoggerFactory.getLogger( NameAndOptionalUIDSyntaxChecker.class ); 050 051 /** 052 * Creates a new instance of NameAndOptionalUIDSyntaxChecker. 053 */ 054 public NameAndOptionalUIDSyntaxChecker() 055 { 056 super( SchemaConstants.NAME_AND_OPTIONAL_UID_SYNTAX ); 057 } 058 059 060 /** 061 * {@inheritDoc} 062 */ 063 public boolean isValidSyntax( Object value ) 064 { 065 String strValue = null; 066 067 if ( value == null ) 068 { 069 LOG.debug( "Syntax invalid for 'null'" ); 070 return false; 071 } 072 073 if ( value instanceof String ) 074 { 075 strValue = ( String ) value; 076 } 077 else if ( value instanceof byte[] ) 078 { 079 strValue = Strings.utf8ToString((byte[]) value); 080 } 081 else 082 { 083 strValue = value.toString(); 084 } 085 086 if ( strValue.length() == 0 ) 087 { 088 LOG.debug( "Syntax invalid for '{}'", value ); 089 return false; 090 } 091 092 // Let's see if we have an UID part 093 int sharpPos = strValue.lastIndexOf( '#' ); 094 095 if ( sharpPos != -1 ) 096 { 097 // Now, check that we don't have another '#' 098 if ( strValue.indexOf( '#' ) != sharpPos ) 099 { 100 // Yes, we have one : this is not allowed, it should have been 101 // escaped. 102 LOG.debug( "Syntax invalid for '{}'", value ); 103 return false; 104 } 105 106 // This is an UID if the '#' is immediatly 107 // followed by a BitString, except if the '#' is 108 // on the last position 109 // We shoould not find a 110 if ( BitStringSyntaxChecker.isValid( strValue.substring( sharpPos + 1 ) ) 111 && ( sharpPos < strValue.length() ) ) 112 { 113 // Ok, we have a BitString, now check the Dn, 114 // except if the '#' is in first position 115 if ( sharpPos > 0 ) 116 { 117 boolean result = Dn.isValid(strValue.substring(0, sharpPos)); 118 119 if ( result ) 120 { 121 LOG.debug( "Syntax valid for '{}'", value ); 122 } 123 else 124 { 125 LOG.debug( "Syntax invalid for '{}'", value ); 126 } 127 128 return result; 129 130 } 131 else 132 { 133 // The Dn must not be null ? 134 LOG.debug( "Syntax invalid for '{}'", value ); 135 return false; 136 } 137 } 138 else 139 { 140 // We have found a '#' but no UID part. 141 LOG.debug( "Syntax invalid for '{}'", value ); 142 return false; 143 } 144 } 145 else 146 { 147 // No UID, the strValue is a Dn 148 // Check that the value is a valid Dn 149 boolean result = Dn.isValid(strValue); 150 151 if ( result ) 152 { 153 LOG.debug( "Syntax valid for '{}'", value ); 154 } 155 else 156 { 157 LOG.debug( "Syntax invalid for '{}'", value ); 158 } 159 160 return result; 161 } 162 } 163}