View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
25  import org.apache.directory.api.ldap.model.name.Dn;
26  import org.apache.directory.api.ldap.model.name.Rdn;
27  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
28  import org.apache.directory.api.util.Strings;
29  
30  
31  /**
32   * A SyntaxChecker which verifies that a value is a valid {@link Dn}. We just check
33   * that the {@link Dn} is valid, we don't need to verify each of the {@link Rdn} syntax.
34   * 
35   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36   */
37  @SuppressWarnings("serial")
38  public final class DnSyntaxChecker extends SyntaxChecker
39  {
40      /**
41       * A static instance of DnSyntaxChecker
42       */
43      public static final DnSyntaxChecker INSTANCE = new DnSyntaxChecker( SchemaConstants.DN_SYNTAX );
44      
45      /**
46       * A static Builder for this class
47       */
48      public static final class Builder extends SCBuilder<DnSyntaxChecker>
49      {
50          /**
51           * The Builder constructor
52           */
53          private Builder()
54          {
55              super( SchemaConstants.DN_SYNTAX );
56          }
57          
58          
59          /**
60           * Create a new instance of DnSyntaxChecker
61           * @return A new instance of DnSyntaxChecker
62           */
63          @Override
64          public DnSyntaxChecker build()
65          {
66              return new DnSyntaxChecker( oid );
67          }
68      }
69  
70      
71      /**
72       * Creates a new instance of DNSyntaxChecker.
73       * 
74       * @param oid The OID to use for this SyntaxChecker
75       */
76      private DnSyntaxChecker( String oid )
77      {
78          super( oid );
79      }
80  
81      
82      /**
83       * @return An instance of the Builder for this class
84       */
85      public static Builder builder()
86      {
87          return new Builder();
88      }
89  
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public boolean isValidSyntax( Object value )
96      {
97          String strValue;
98  
99          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 }