001/* 002 * Copyright 2008-2020 Ping Identity Corporation 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright 2008-2020 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-2020 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.StaticUtils; 044import com.unboundid.util.ThreadSafety; 045import com.unboundid.util.ThreadSafetyLevel; 046 047import static com.unboundid.ldap.matchingrules.MatchingRuleMessages.*; 048 049 050 051/** 052 * This class provides an implementation of a matching rule that may be used for 053 * telephone numbers. It will accept values with any ASCII printable character. 054 * When making comparisons, spaces and dashes will be ignored. 055 */ 056@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE) 057public final class TelephoneNumberMatchingRule 058 extends SimpleMatchingRule 059{ 060 /** 061 * The singleton instance that will be returned from the {@code getInstance} 062 * method. 063 */ 064 private static final TelephoneNumberMatchingRule INSTANCE = 065 new TelephoneNumberMatchingRule(); 066 067 068 069 /** 070 * The name for the telephoneNumberMatch equality matching rule. 071 */ 072 public static final String EQUALITY_RULE_NAME = "telephoneNumberMatch"; 073 074 075 076 /** 077 * The name for the telephoneNumberMatch equality matching rule, formatted in 078 * all lowercase characters. 079 */ 080 static final String LOWER_EQUALITY_RULE_NAME = 081 StaticUtils.toLowerCase(EQUALITY_RULE_NAME); 082 083 084 085 /** 086 * The OID for the telephoneNumberMatch equality matching rule. 087 */ 088 public static final String EQUALITY_RULE_OID = "2.5.13.20"; 089 090 091 092 /** 093 * The name for the telephoneNumberSubstringsMatch substring matching rule. 094 */ 095 public static final String SUBSTRING_RULE_NAME = 096 "telephoneNumberSubstringsMatch"; 097 098 099 100 /** 101 * The name for the telephoneNumberSubstringsMatch substring matching rule, 102 * formatted in all lowercase characters. 103 */ 104 static final String LOWER_SUBSTRING_RULE_NAME = 105 StaticUtils.toLowerCase(SUBSTRING_RULE_NAME); 106 107 108 109 /** 110 * The OID for the telephoneNumberSubstringsMatch substring matching rule. 111 */ 112 public static final String SUBSTRING_RULE_OID = "2.5.13.21"; 113 114 115 116 /** 117 * The serial version UID for this serializable class. 118 */ 119 private static final long serialVersionUID = -5463096544849211252L; 120 121 122 123 /** 124 * Creates a new instance of this telephone number matching rule. 125 */ 126 public TelephoneNumberMatchingRule() 127 { 128 // No implementation is required. 129 } 130 131 132 133 /** 134 * Retrieves a singleton instance of this matching rule. 135 * 136 * @return A singleton instance of this matching rule. 137 */ 138 public static TelephoneNumberMatchingRule getInstance() 139 { 140 return INSTANCE; 141 } 142 143 144 145 /** 146 * {@inheritDoc} 147 */ 148 @Override() 149 public String getEqualityMatchingRuleName() 150 { 151 return EQUALITY_RULE_NAME; 152 } 153 154 155 156 /** 157 * {@inheritDoc} 158 */ 159 @Override() 160 public String getEqualityMatchingRuleOID() 161 { 162 return EQUALITY_RULE_OID; 163 } 164 165 166 167 /** 168 * {@inheritDoc} 169 */ 170 @Override() 171 public String getOrderingMatchingRuleName() 172 { 173 return null; 174 } 175 176 177 178 /** 179 * {@inheritDoc} 180 */ 181 @Override() 182 public String getOrderingMatchingRuleOID() 183 { 184 return null; 185 } 186 187 188 189 /** 190 * {@inheritDoc} 191 */ 192 @Override() 193 public String getSubstringMatchingRuleName() 194 { 195 return SUBSTRING_RULE_NAME; 196 } 197 198 199 200 /** 201 * {@inheritDoc} 202 */ 203 @Override() 204 public String getSubstringMatchingRuleOID() 205 { 206 return SUBSTRING_RULE_OID; 207 } 208 209 210 211 /** 212 * {@inheritDoc} 213 */ 214 @Override() 215 public int compareValues(final ASN1OctetString value1, 216 final ASN1OctetString value2) 217 throws LDAPException 218 { 219 throw new LDAPException(ResultCode.INAPPROPRIATE_MATCHING, 220 ERR_TELEPHONE_NUMBER_ORDERING_MATCHING_NOT_SUPPORTED.get()); 221 } 222 223 224 225 /** 226 * {@inheritDoc} 227 */ 228 @Override() 229 public ASN1OctetString normalize(final ASN1OctetString value) 230 throws LDAPException 231 { 232 final byte[] valueBytes = value.getValue(); 233 final StringBuilder buffer = new StringBuilder(); 234 for (int i=0; i < valueBytes.length; i++) 235 { 236 switch (valueBytes[i]) 237 { 238 case ' ': 239 case '-': 240 // These should be ignored. 241 break; 242 243 case '\'': 244 case '(': 245 case ')': 246 case '+': 247 case ',': 248 case '.': 249 case '=': 250 case '/': 251 case ':': 252 case '?': 253 // These should be retained. 254 buffer.append((char) valueBytes[i]); 255 break; 256 257 default: 258 final byte b = valueBytes[i]; 259 if (((b >= '0') && (b <= '9')) || 260 ((b >= 'a') && (b <= 'z')) || 261 ((b >= 'A') && (b <= 'Z'))) 262 { 263 // These should be retained. 264 buffer.append((char) valueBytes[i]); 265 break; 266 } 267 268 throw new LDAPException(ResultCode.INVALID_ATTRIBUTE_SYNTAX, 269 ERR_TELEPHONE_NUMBER_INVALID_CHARACTER.get(i)); 270 } 271 } 272 273 return new ASN1OctetString(buffer.toString()); 274 } 275 276 277 278 /** 279 * {@inheritDoc} 280 */ 281 @Override() 282 public ASN1OctetString normalizeSubstring(final ASN1OctetString value, 283 final byte substringType) 284 throws LDAPException 285 { 286 return normalize(value); 287 } 288}