001/* 002 * Copyright 2008-2022 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2008-2022 Ping Identity Corporation 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020/* 021 * Copyright (C) 2008-2022 Ping Identity Corporation 022 * 023 * This program is free software; you can redistribute it and/or modify 024 * it under the terms of the GNU General Public License (GPLv2 only) 025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 026 * as published by the Free Software Foundation. 027 * 028 * This program is distributed in the hope that it will be useful, 029 * but WITHOUT ANY WARRANTY; without even the implied warranty of 030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 031 * GNU General Public License for more details. 032 * 033 * You should have received a copy of the GNU General Public License 034 * along with this program; if not, see <http://www.gnu.org/licenses>. 035 */ 036package com.unboundid.ldap.matchingrules; 037 038 039 040import com.unboundid.asn1.ASN1OctetString; 041import com.unboundid.ldap.sdk.LDAPException; 042import com.unboundid.ldap.sdk.ResultCode; 043import com.unboundid.util.NotNull; 044import com.unboundid.util.Nullable; 045import com.unboundid.util.StaticUtils; 046import com.unboundid.util.ThreadSafety; 047import com.unboundid.util.ThreadSafetyLevel; 048 049import static com.unboundid.ldap.matchingrules.MatchingRuleMessages.*; 050 051 052 053/** 054 * This class provides an implementation of a matching rule that may be used for 055 * telephone numbers. It will accept values with any ASCII printable character. 056 * When making comparisons, spaces and dashes will be ignored. 057 */ 058@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 059public final class TelephoneNumberMatchingRule 060 extends SimpleMatchingRule 061{ 062 /** 063 * The singleton instance that will be returned from the {@code getInstance} 064 * method. 065 */ 066 @NotNull private static final TelephoneNumberMatchingRule INSTANCE = 067 new TelephoneNumberMatchingRule(); 068 069 070 071 /** 072 * The name for the telephoneNumberMatch equality matching rule. 073 */ 074 @NotNull public static final String EQUALITY_RULE_NAME = 075 "telephoneNumberMatch"; 076 077 078 079 /** 080 * The name for the telephoneNumberMatch equality matching rule, formatted in 081 * all lowercase characters. 082 */ 083 @NotNull static final String LOWER_EQUALITY_RULE_NAME = 084 StaticUtils.toLowerCase(EQUALITY_RULE_NAME); 085 086 087 088 /** 089 * The OID for the telephoneNumberMatch equality matching rule. 090 */ 091 @NotNull public static final String EQUALITY_RULE_OID = "2.5.13.20"; 092 093 094 095 /** 096 * The name for the telephoneNumberSubstringsMatch substring matching rule. 097 */ 098 @NotNull public static final String SUBSTRING_RULE_NAME = 099 "telephoneNumberSubstringsMatch"; 100 101 102 103 /** 104 * The name for the telephoneNumberSubstringsMatch substring matching rule, 105 * formatted in all lowercase characters. 106 */ 107 @NotNull static final String LOWER_SUBSTRING_RULE_NAME = 108 StaticUtils.toLowerCase(SUBSTRING_RULE_NAME); 109 110 111 112 /** 113 * The OID for the telephoneNumberSubstringsMatch substring matching rule. 114 */ 115 @NotNull public static final String SUBSTRING_RULE_OID = "2.5.13.21"; 116 117 118 119 /** 120 * The serial version UID for this serializable class. 121 */ 122 private static final long serialVersionUID = -5463096544849211252L; 123 124 125 126 /** 127 * Creates a new instance of this telephone number matching rule. 128 */ 129 public TelephoneNumberMatchingRule() 130 { 131 // No implementation is required. 132 } 133 134 135 136 /** 137 * Retrieves a singleton instance of this matching rule. 138 * 139 * @return A singleton instance of this matching rule. 140 */ 141 @NotNull() 142 public static TelephoneNumberMatchingRule getInstance() 143 { 144 return INSTANCE; 145 } 146 147 148 149 /** 150 * {@inheritDoc} 151 */ 152 @Override() 153 @NotNull() 154 public String getEqualityMatchingRuleName() 155 { 156 return EQUALITY_RULE_NAME; 157 } 158 159 160 161 /** 162 * {@inheritDoc} 163 */ 164 @Override() 165 @NotNull() 166 public String getEqualityMatchingRuleOID() 167 { 168 return EQUALITY_RULE_OID; 169 } 170 171 172 173 /** 174 * {@inheritDoc} 175 */ 176 @Override() 177 @Nullable() 178 public String getOrderingMatchingRuleName() 179 { 180 return null; 181 } 182 183 184 185 /** 186 * {@inheritDoc} 187 */ 188 @Override() 189 @Nullable() 190 public String getOrderingMatchingRuleOID() 191 { 192 return null; 193 } 194 195 196 197 /** 198 * {@inheritDoc} 199 */ 200 @Override() 201 @NotNull() 202 public String getSubstringMatchingRuleName() 203 { 204 return SUBSTRING_RULE_NAME; 205 } 206 207 208 209 /** 210 * {@inheritDoc} 211 */ 212 @Override() 213 @NotNull() 214 public String getSubstringMatchingRuleOID() 215 { 216 return SUBSTRING_RULE_OID; 217 } 218 219 220 221 /** 222 * {@inheritDoc} 223 */ 224 @Override() 225 public int compareValues(@NotNull final ASN1OctetString value1, 226 @NotNull final ASN1OctetString value2) 227 throws LDAPException 228 { 229 throw new LDAPException(ResultCode.INAPPROPRIATE_MATCHING, 230 ERR_TELEPHONE_NUMBER_ORDERING_MATCHING_NOT_SUPPORTED.get()); 231 } 232 233 234 235 /** 236 * {@inheritDoc} 237 */ 238 @Override() 239 @NotNull() 240 public ASN1OctetString normalize(@NotNull final ASN1OctetString value) 241 throws LDAPException 242 { 243 final byte[] valueBytes = value.getValue(); 244 final StringBuilder buffer = new StringBuilder(); 245 for (int i=0; i < valueBytes.length; i++) 246 { 247 switch (valueBytes[i]) 248 { 249 case ' ': 250 case '-': 251 // These should be ignored. 252 break; 253 254 case '\'': 255 case '(': 256 case ')': 257 case '+': 258 case ',': 259 case '.': 260 case '=': 261 case '/': 262 case ':': 263 case '?': 264 // These should be retained. 265 buffer.append((char) valueBytes[i]); 266 break; 267 268 default: 269 final byte b = valueBytes[i]; 270 if (((b >= '0') && (b <= '9')) || 271 ((b >= 'a') && (b <= 'z')) || 272 ((b >= 'A') && (b <= 'Z'))) 273 { 274 // These should be retained. 275 buffer.append((char) valueBytes[i]); 276 break; 277 } 278 279 throw new LDAPException(ResultCode.INVALID_ATTRIBUTE_SYNTAX, 280 ERR_TELEPHONE_NUMBER_INVALID_CHARACTER.get(i)); 281 } 282 } 283 284 return new ASN1OctetString(buffer.toString()); 285 } 286 287 288 289 /** 290 * {@inheritDoc} 291 */ 292 @Override() 293 @NotNull() 294 public ASN1OctetString normalizeSubstring( 295 @NotNull final ASN1OctetString value, 296 final byte substringType) 297 throws LDAPException 298 { 299 return normalize(value); 300 } 301}