273 return "type=host_name (0), value=" + hostname; 274 } 275 276 /** 277 * Creates an {@link SNIMatcher} object for {@code SNIHostName}s. 278 * <P> 279 * This method can be used by a server to verify the acceptable 280 * {@code SNIHostName}s. For example, 281 * <pre> 282 * SNIMatcher matcher = 283 * SNIHostName.createSNIMatcher("www\\.example\\.com"); 284 * </pre> 285 * will accept the hostname "www.example.com". 286 * <pre> 287 * SNIMatcher matcher = 288 * SNIHostName.createSNIMatcher("www\\.example\\.(com|org)"); 289 * </pre> 290 * will accept hostnames "www.example.com" and "www.example.org". 291 * 292 * @param regex 293 * the <a href="{@docRoot}/java/util/regex/Pattern.html#sum"> 294 * regular expression pattern</a> 295 * representing the hostname(s) to match 296 * @return a {@code SNIMatcher} object for {@code SNIHostName}s 297 * @throws NullPointerException if {@code regex} is 298 * {@code null} 299 * @throws java.util.regex.PatternSyntaxException if the regular expression's 300 * syntax is invalid 301 */ 302 public static SNIMatcher createSNIMatcher(String regex) { 303 if (regex == null) { 304 throw new NullPointerException( 305 "The regular expression cannot be null"); 306 } 307 308 return new SNIHostNameMatcher(regex); 309 } 310 311 // check the validity of the string hostname 312 private void checkHostName() { 313 if (hostname.isEmpty()) { 314 throw new IllegalArgumentException( 315 "Server name value of host_name cannot be empty"); 316 } 317 318 if (hostname.endsWith(".")) { 319 throw new IllegalArgumentException( 320 "Server name value of host_name cannot have the trailing dot"); 321 } 322 } 323 324 private static final class SNIHostNameMatcher extends SNIMatcher { 325 326 // the compiled representation of a regular expression. 327 private final Pattern pattern; 328 329 /** 330 * Creates an SNIHostNameMatcher object. 331 * 332 * @param regex 333 * the <a href="{@docRoot}/java/util/regex/Pattern.html#sum"> 334 * regular expression pattern</a> 335 * representing the hostname(s) to match 336 * @throws NullPointerException if {@code regex} is 337 * {@code null} 338 * @throws PatternSyntaxException if the regular expression's syntax 339 * is invalid 340 */ 341 SNIHostNameMatcher(String regex) { 342 super(StandardConstants.SNI_HOST_NAME); 343 pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); 344 } 345 346 /** 347 * Attempts to match the given {@link SNIServerName}. 348 * 349 * @param serverName 350 * the {@link SNIServerName} instance on which this matcher 351 * performs match operations 352 * 353 * @return {@code true} if, and only if, the matcher matches the | 273 return "type=host_name (0), value=" + hostname; 274 } 275 276 /** 277 * Creates an {@link SNIMatcher} object for {@code SNIHostName}s. 278 * <P> 279 * This method can be used by a server to verify the acceptable 280 * {@code SNIHostName}s. For example, 281 * <pre> 282 * SNIMatcher matcher = 283 * SNIHostName.createSNIMatcher("www\\.example\\.com"); 284 * </pre> 285 * will accept the hostname "www.example.com". 286 * <pre> 287 * SNIMatcher matcher = 288 * SNIHostName.createSNIMatcher("www\\.example\\.(com|org)"); 289 * </pre> 290 * will accept hostnames "www.example.com" and "www.example.org". 291 * 292 * @param regex 293 * the <a href="{@docRoot}/java.base/java/util/regex/Pattern.html#sum"> 294 * regular expression pattern</a> 295 * representing the hostname(s) to match 296 * @return a {@code SNIMatcher} object for {@code SNIHostName}s 297 * @throws NullPointerException if {@code regex} is 298 * {@code null} 299 * @throws java.util.regex.PatternSyntaxException if the regular expression's 300 * syntax is invalid 301 */ 302 public static SNIMatcher createSNIMatcher(String regex) { 303 if (regex == null) { 304 throw new NullPointerException( 305 "The regular expression cannot be null"); 306 } 307 308 return new SNIHostNameMatcher(regex); 309 } 310 311 // check the validity of the string hostname 312 private void checkHostName() { 313 if (hostname.isEmpty()) { 314 throw new IllegalArgumentException( 315 "Server name value of host_name cannot be empty"); 316 } 317 318 if (hostname.endsWith(".")) { 319 throw new IllegalArgumentException( 320 "Server name value of host_name cannot have the trailing dot"); 321 } 322 } 323 324 private static final class SNIHostNameMatcher extends SNIMatcher { 325 326 // the compiled representation of a regular expression. 327 private final Pattern pattern; 328 329 /** 330 * Creates an SNIHostNameMatcher object. 331 * 332 * @param regex 333 * the <a href="{@docRoot}/java.base/java/util/regex/Pattern.html#sum"> 334 * regular expression pattern</a> 335 * representing the hostname(s) to match 336 * @throws NullPointerException if {@code regex} is 337 * {@code null} 338 * @throws PatternSyntaxException if the regular expression's syntax 339 * is invalid 340 */ 341 SNIHostNameMatcher(String regex) { 342 super(StandardConstants.SNI_HOST_NAME); 343 pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); 344 } 345 346 /** 347 * Attempts to match the given {@link SNIServerName}. 348 * 349 * @param serverName 350 * the {@link SNIServerName} instance on which this matcher 351 * performs match operations 352 * 353 * @return {@code true} if, and only if, the matcher matches the |