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.schema.SyntaxChecker; 025import org.apache.directory.shared.util.Strings; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028 029 030/** 031 * A SyntaxChecker which verifies that a value is a TeletexTerminalIdentifier according to 032 * RFC 4517 : 033 * 034 * teletex-id = ttx-term *(DOLLAR ttx-param) 035 * ttx-term = PrintableString ; terminal identifier 036 * ttx-param = ttx-key COLON ttx-value ; parameter 037 * ttx-key = "graphic" | "control" | "misc" | "page" | "private" 038 * ttx-value = *ttx-value-octet 039 * 040 * ttx-value-octet = %x00-23 | (%x5C "24") | %x25-5B | (%x5C "5C") | %x5D-FF 041 * 042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 043 */ 044@SuppressWarnings("serial") 045public class TeletexTerminalIdentifierSyntaxChecker extends SyntaxChecker 046{ 047 /** A logger for this class */ 048 private static final Logger LOG = LoggerFactory.getLogger( TeletexTerminalIdentifierSyntaxChecker.class ); 049 050 /** 051 * Creates a new instance of TeletexTerminalIdentifier. 052 */ 053 public TeletexTerminalIdentifierSyntaxChecker() 054 { 055 super( SchemaConstants.TELETEX_TERMINAL_IDENTIFIER_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 'null'" ); 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 = Strings.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 String terminalIdentifier = ( ( dollar == -1 ) ? strValue : strValue.substring( 0, dollar ) ); 095 096 if ( terminalIdentifier.length() == 0 ) 097 { 098 // It should not be null 099 LOG.debug( "Syntax invalid for '{}'", value ); 100 return false; 101 } 102 103 if ( !Strings.isPrintableString(terminalIdentifier) ) 104 { 105 // It's not a valid PrintableString 106 LOG.debug( "Syntax invalid for '{}'", value ); 107 return false; 108 } 109 110 if ( dollar == -1 ) 111 { 112 // No ttx-param : let's get out 113 LOG.debug( "Syntax valid for '{}'", value ); 114 return true; 115 } 116 117 // Ok, now let's deal withh optional ttx-params 118 String[] ttxParams = strValue.substring( dollar + 1 ).split( "\\$" ); 119 120 if ( ttxParams.length == 0 ) 121 { 122 LOG.debug( "Syntax invalid for '{}'", value ); 123 return false; 124 } 125 126 for ( String ttxParam:ttxParams ) 127 { 128 int colon = ttxParam.indexOf( ':' ); 129 130 if ( colon == -1 ) 131 { 132 // we must have a ':' separator 133 LOG.debug( "Syntax invalid for '{}'", value ); 134 return false; 135 } 136 137 String key = ttxParam.substring( 0, colon ); 138 139 if ( key.startsWith( "graphic" ) 140 || key.startsWith( "control" ) 141 || key.startsWith( "misc" ) 142 || key.startsWith( "page" ) 143 || key.startsWith( "private" ) ) 144 { 145 if ( colon + 1 == ttxParam.length() ) 146 { 147 LOG.debug( "Syntax invalid for '{}'", value ); 148 return false; 149 } 150 151 boolean hasEsc = false; 152 153 for ( byte b: Strings.getBytesUtf8(ttxParam) ) 154 { 155 switch ( b ) 156 { 157 case 0x24 : 158 // '$' is not accepted 159 LOG.debug( "Syntax invalid for '{}'", value ); 160 return false; 161 162 case 0x5c : 163 if ( hasEsc ) 164 { 165 // two following \ are not accepted 166 LOG.debug( "Syntax invalid for '{}'", value ); 167 return false; 168 } 169 else 170 { 171 hasEsc = true; 172 } 173 174 continue; 175 176 case '2' : 177 continue; 178 179 case '4' : 180 // We have found a "\24" 181 hasEsc = false; 182 continue; 183 184 case '5' : 185 continue; 186 187 case 'c' : 188 case 'C' : 189 // We have found a "\5c" or a "\5C" 190 hasEsc = false; 191 continue; 192 193 default : 194 if ( hasEsc ) 195 { 196 // A \ should be followed by "24" or "5c" or "5C" 197 return false; 198 } 199 200 continue; 201 } 202 } 203 } 204 else 205 { 206 LOG.debug( "Syntax invalid for '{}'", value ); 207 return false; 208 } 209 } 210 211 LOG.debug( "Syntax valid for '{}'", value ); 212 return true; 213 } 214}