1/*
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package javax.net.ssl;
27
28/**
29 * Instances of this class represent a matcher that performs match
30 * operations on an {@link SNIServerName} instance.
31 * <P>
32 * Servers can use Server Name Indication (SNI) information to decide if
33 * specific {@link SSLSocket} or {@link SSLEngine} instances should accept
34 * a connection.  For example, when multiple "virtual" or "name-based"
35 * servers are hosted on a single underlying network address, the server
36 * application can use SNI information to determine whether this server is
37 * the exact server that the client wants to access.  Instances of this
38 * class can be used by a server to verify the acceptable server names of
39 * a particular type, such as host names.
40 * <P>
41 * {@code SNIMatcher} objects are immutable.  Subclasses should not provide
42 * methods that can change the state of an instance once it has been created.
43 *
44 * @see SNIServerName
45 * @see SNIHostName
46 * @see SSLParameters#getSNIMatchers()
47 * @see SSLParameters#setSNIMatchers(Collection)
48 *
49 * @since 1.8
50 */
51public abstract class SNIMatcher {
52
53    // the type of the server name that this matcher performs on
54    private final int type;
55
56    /**
57     * Creates an {@code SNIMatcher} using the specified server name type.
58     *
59     * @param  type
60     *         the type of the server name that this matcher performs on
61     *
62     * @throws IllegalArgumentException if {@code type} is not in the range
63     *         of 0 to 255, inclusive.
64     */
65    protected SNIMatcher(int type) {
66        if (type < 0) {
67            throw new IllegalArgumentException(
68                "Server name type cannot be less than zero");
69        } else if (type > 255) {
70            throw new IllegalArgumentException(
71                "Server name type cannot be greater than 255");
72        }
73
74        this.type = type;
75    }
76
77    /**
78     * Returns the server name type of this {@code SNIMatcher} object.
79     *
80     * @return the server name type of this {@code SNIMatcher} object.
81     *
82     * @see SNIServerName
83     */
84    public final int getType() {
85        return type;
86    }
87
88    /**
89     * Attempts to match the given {@link SNIServerName}.
90     *
91     * @param  serverName
92     *         the {@link SNIServerName} instance on which this matcher
93     *         performs match operations
94     *
95     * @return {@code true} if, and only if, the matcher matches the
96     *         given {@code serverName}
97     *
98     * @throws NullPointerException if {@code serverName} is {@code null}
99     * @throws IllegalArgumentException if {@code serverName} is
100     *         not of the given server name type of this matcher
101     *
102     * @see SNIServerName
103     */
104    public abstract boolean matches(SNIServerName serverName);
105}
106