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.schema.SyntaxChecker;
26  import org.apache.directory.api.util.Strings;
27  
28  
29  /**
30   * A SyntaxChecker which verifies that a value is a NisNetGroupTriple according to 
31   * RFC 2307 :
32   * <pre>
33   * nisnetgrouptriple = "(" hostname "," username "," domainname ")"
34   *      hostname          = "" / "-" / keystring
35   *      username          = "" / "-" / keystring
36   *      domainname        = "" / "-" / keystring
37   * </pre>
38   * 
39   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
40   */
41  @SuppressWarnings("serial")
42  public final class NisNetgroupTripleSyntaxChecker extends SyntaxChecker
43  {
44      /**
45       * A static instance of NisNetGroupTripleChecker
46       */
47      public static final NisNetgroupTripleSyntaxChecker INSTANCE = 
48          new NisNetgroupTripleSyntaxChecker( SchemaConstants.NIS_NETGROUP_TRIPLE_SYNTAX );
49      
50      /**
51       * A static Builder for this class
52       */
53      public static final class Builder extends SCBuilder<NisNetgroupTripleSyntaxChecker>
54      {
55          /**
56           * The Builder constructor
57           */
58          private Builder()
59          {
60              super( SchemaConstants.NIS_NETGROUP_TRIPLE_SYNTAX );
61          }
62          
63          
64          /**
65           * Create a new instance of NisNetgroupTripleSyntaxChecker
66           * @return A new instance of NisNetgroupTripleSyntaxChecker
67           */
68          @Override
69          public NisNetgroupTripleSyntaxChecker build()
70          {
71              return new NisNetgroupTripleSyntaxChecker( oid );
72          }
73      }
74  
75      
76      /**
77       * Creates a new instance of NisNetgroupTripleSyntaxChecker.
78       * 
79       * @param oid The OID to use for this SyntaxChecker
80       */
81      private NisNetgroupTripleSyntaxChecker( String oid )
82      {
83          super( oid );
84      }
85  
86      
87      /**
88       * @return An instance of the Builder for this class
89       */
90      public static Builder builder()
91      {
92          return new Builder();
93      }
94      
95      
96      private int parseKeyString( String strValue, int pos )
97      {
98          char c = strValue.charAt( pos );
99          
100         // The end of the keyString
101         if ( ( c == ',' ) || ( c == ')' ) )
102         {
103             return pos;
104         }
105         
106         // We must have a first alphabetic char
107         if ( Character.isUpperCase( c ) || Character.isLowerCase( c ) )
108         {
109             pos++;
110         }
111         else
112         {
113             return -1;
114         }
115         
116         try 
117         { 
118             c = strValue.charAt( pos );
119             
120             while ( ( c != ',' ) && ( c != ')' ) )
121             {
122                 if ( Character.isUpperCase( c ) || Character.isLowerCase( c ) || Character.isDigit( c )
123                     || ( c == '-' ) || ( c == ';' ) || ( c == '_' ) )
124                 {
125                     pos++;
126                 }
127                 else
128                 {
129                     return -1;
130                 }
131 
132                 c = strValue.charAt( pos );
133             }
134             
135             return pos;
136         }
137         catch ( IndexOutOfBoundsException ioobe )
138         {
139             return -1;
140         }
141     }
142     
143 
144     /**
145      * {@inheritDoc}
146      */
147     @Override
148     public boolean isValidSyntax( Object value )
149     {
150         String strValue;
151 
152         if ( value == null )
153         {
154             if ( LOG.isDebugEnabled() )
155             {
156                 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, "null" ) );
157             }
158             
159             return false;
160         }
161 
162         if ( value instanceof String )
163         {
164             strValue = ( String ) value;
165         }
166         else if ( value instanceof byte[] )
167         {
168             strValue = Strings.utf8ToString( ( byte[] ) value );
169         }
170         else
171         {
172             strValue = value.toString();
173         }
174 
175         // The  nisNetGroup must at least contain a '(', 2 ',' and a ')'
176         if ( strValue.length() < 4 )
177         {
178             if ( LOG.isDebugEnabled() )
179             {
180                 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
181             }
182             
183             return false;
184         }
185 
186         // The hostname
187         int pos = parseKeyString( strValue, 1 );
188         
189         if ( pos == -1 )
190         {
191             if ( LOG.isDebugEnabled() )
192             {
193                 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
194             }
195             
196             return false;
197         }
198         
199         if ( strValue.charAt( pos ) != ',' )
200         {
201             if ( LOG.isDebugEnabled() )
202             {
203                 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
204             }
205             
206             return false;
207         }
208         else
209         {
210             pos++;
211         }
212 
213         // The username
214         pos = parseKeyString( strValue, pos );
215         
216         if ( pos == -1 )
217         {
218             if ( LOG.isDebugEnabled() )
219             {
220                 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
221             }
222             
223             return false;
224         }
225         
226         if ( strValue.charAt( pos ) != ',' )
227         {
228             if ( LOG.isDebugEnabled() )
229             {
230                 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
231             }
232             
233             return false;
234         }
235         else
236         {
237             pos++;
238         }
239 
240         // The domainname
241         pos = parseKeyString( strValue, pos );
242         
243         if ( pos == -1 )
244         {
245             if ( LOG.isDebugEnabled() )
246             {
247                 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
248             }
249             
250             return false;
251         }
252         
253         if ( strValue.charAt( pos ) != ')' )
254         {
255             if ( LOG.isDebugEnabled() )
256             {
257                 LOG.debug( I18n.err( I18n.ERR_13210_SYNTAX_INVALID, value ) );
258             }
259             
260             return false;
261         }
262 
263         return true;
264     }
265 }