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.schema.SyntaxChecker; 025 import org.apache.directory.shared.ldap.util.StringTools; 026 import org.slf4j.Logger; 027 import org.slf4j.LoggerFactory; 028 029 030 /** 031 * A SyntaxChecker which verifies that a value is a Telex Number according to 032 * RFC 4517 : 033 * 034 * telex-number = actual-number DOLLAR country-code DOLLAR answerback 035 * actual-number = PrintableString 036 * country-code = PrintableString 037 * answerback = PrintableString 038 * 039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 040 * @version $Rev$ 041 */ 042 public class TelexNumberSyntaxChecker extends SyntaxChecker 043 { 044 /** A logger for this class */ 045 private static final Logger LOG = LoggerFactory.getLogger( TelexNumberSyntaxChecker.class ); 046 047 /** The serialVersionUID */ 048 private static final long serialVersionUID = 1L; 049 050 /** 051 * Creates a new instance of TelexNumberSyntaxChecker. 052 */ 053 public TelexNumberSyntaxChecker() 054 { 055 super( SchemaConstants.TELEX_NUMBER_SYNTAX ); 056 } 057 058 059 /** 060 * {@inheritDoc} 061 */ 062 public boolean isValidSyntax( Object value ) 063 { 064 String strValue = null; 065 066 if ( value == null ) 067 { 068 LOG.debug( "Syntax invalid for '{}'", value ); 069 return false; 070 } 071 072 if ( value instanceof String ) 073 { 074 strValue = ( String ) value; 075 } 076 else if ( value instanceof byte[] ) 077 { 078 strValue = StringTools.utf8ToString( ( byte[] ) value ); 079 } 080 else 081 { 082 strValue = value.toString(); 083 } 084 085 if ( strValue.length() == 0 ) 086 { 087 LOG.debug( "Syntax invalid for '{}'", value ); 088 return false; 089 } 090 091 // Search for the first '$' separator 092 int dollar = strValue.indexOf( '$' ); 093 094 // We must have one, and not on first position 095 if ( dollar <= 0 ) 096 { 097 // No '$' => error 098 LOG.debug( "Syntax invalid for '{}'", value ); 099 return false; 100 } 101 102 String actualNumber = strValue.substring( 0, dollar ); 103 104 // The actualNumber must not be empty 105 if ( actualNumber.length() == 0 ) 106 { 107 LOG.debug( "Syntax invalid for '{}'", value ); 108 return false; 109 } 110 111 // The actual number should be a PrintableString 112 if ( ! StringTools.isPrintableString( actualNumber ) ) 113 { 114 LOG.debug( "Syntax invalid for '{}'", value ); 115 return false; 116 } 117 118 // Search for the second separator 119 int dollar2 = strValue.indexOf( '$', dollar + 1 ); 120 121 // We must have one 122 if ( dollar2 == -1 ) 123 { 124 // No '$' => error 125 LOG.debug( "Syntax invalid for '{}'", value ); 126 return false; 127 } 128 129 String countryCode = strValue.substring( dollar + 1, dollar2 ); 130 131 // The countryCode must not be empty 132 if ( countryCode.length() == 0 ) 133 { 134 LOG.debug( "Syntax invalid for '{}'", value ); 135 return false; 136 } 137 138 // The country Code should be a PrintableString 139 if ( ! StringTools.isPrintableString( countryCode ) ) 140 { 141 LOG.debug( "Syntax invalid for '{}'", value ); 142 return false; 143 } 144 145 // Now, check for the answerBack 146 if ( dollar2 + 1 == strValue.length() ) 147 { 148 // The last string should not be null 149 LOG.debug( "Syntax invalid for '{}'", value ); 150 return false; 151 } 152 153 String answerBack = strValue.substring( dollar2 + 1 ); 154 155 // The answerBack should be a PrintableString 156 if ( ! StringTools.isPrintableString( answerBack ) ) 157 { 158 LOG.debug( "Syntax invalid for '{}'", value ); 159 return false; 160 } 161 162 // Check that the mailboxType is a PrintableString 163 boolean result = StringTools.isPrintableString( answerBack ); 164 165 if ( result ) 166 { 167 LOG.debug( "Syntax valid for '{}'", value ); 168 } 169 else 170 { 171 LOG.debug( "Syntax invalid for '{}'", value ); 172 } 173 174 return result; 175 } 176 }