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 * https://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.i18n.I18n; 024import org.apache.directory.api.ldap.model.constants.SchemaConstants; 025import org.apache.directory.api.ldap.model.name.Dn; 026import org.apache.directory.api.ldap.model.name.Rdn; 027import org.apache.directory.api.ldap.model.schema.SyntaxChecker; 028import org.apache.directory.api.util.Strings; 029 030 031/** 032 * A SyntaxChecker which verifies that a value is a valid {@link Dn}. We just check 033 * that the {@link Dn} is valid, we don't need to verify each of the {@link Rdn} syntax. 034 * 035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 036 */ 037@SuppressWarnings("serial") 038public final class DnSyntaxChecker extends SyntaxChecker 039{ 040 /** 041 * A static instance of DnSyntaxChecker 042 */ 043 public static final DnSyntaxChecker INSTANCE = new DnSyntaxChecker( SchemaConstants.DN_SYNTAX ); 044 045 /** 046 * A static Builder for this class 047 */ 048 public static final class Builder extends SCBuilder<DnSyntaxChecker> 049 { 050 /** 051 * The Builder constructor 052 */ 053 private Builder() 054 { 055 super( SchemaConstants.DN_SYNTAX ); 056 } 057 058 059 /** 060 * Create a new instance of DnSyntaxChecker 061 * @return A new instance of DnSyntaxChecker 062 */ 063 @Override 064 public DnSyntaxChecker build() 065 { 066 return new DnSyntaxChecker( oid ); 067 } 068 } 069 070 071 /** 072 * Creates a new instance of DNSyntaxChecker. 073 * 074 * @param oid The OID to use for this SyntaxChecker 075 */ 076 private DnSyntaxChecker( String oid ) 077 { 078 super( oid ); 079 } 080 081 082 /** 083 * @return An instance of the Builder for this class 084 */ 085 public static Builder builder() 086 { 087 return new Builder(); 088 } 089 090 091 /** 092 * {@inheritDoc} 093 */ 094 @Override 095 public boolean isValidSyntax( Object value ) 096 { 097 String strValue; 098 099 if ( value == null ) 100 { 101 if ( LOG.isDebugEnabled() ) 102 { 103 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) ); 104 } 105 106 return false; 107 } 108 109 if ( value instanceof String ) 110 { 111 strValue = ( String ) value; 112 } 113 else if ( value instanceof byte[] ) 114 { 115 strValue = Strings.utf8ToString( ( byte[] ) value ); 116 } 117 else 118 { 119 strValue = value.toString(); 120 } 121 122 if ( strValue.length() == 0 ) 123 { 124 // TODO: this should be a false, but for 125 // some reason, the principal is empty in 126 // some cases. 127 if ( LOG.isDebugEnabled() ) 128 { 129 LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) ); 130 } 131 132 return true; 133 } 134 135 // Check that the value is a valid Dn 136 boolean result = Dn.isValid( strValue ); 137 138 if ( LOG.isDebugEnabled() ) 139 { 140 if ( result ) 141 { 142 LOG.debug( I18n.msg( I18n.MSG_13701_SYNTAX_VALID, value ) ); 143 } 144 else 145 { 146 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) ); 147 } 148 } 149 150 return result; 151 } 152}