public final class IPFilter extends Object
An IPFilter
instance is created using a so-called
filter expression. This filter expression specifies the IP address
and mask to use for matching a subject IP address.
IPv4 filters will only accept IPv4 addresses and IPv6 filters will only accept IPv6 addresses.
An IPFilter
object is
created and used as follows:
IPFilter filter = IPFilter.parseFilter("10.0.0.0/24");
if (filter.match("10.0.0.1")) {
// IP is granted access
} else {
// IP is denied access
}
IPFilter filter = IPFilter.parseFilter("3FFE:200::/32");
if (filter.match("1fff:0:a88:85a3::ac1f:8001")) {
// IP is granted access
} else {
// IP is denied access
}
Modifier and Type | Method and Description |
---|---|
String |
getBaseIP()
Returns the base IP address.
|
String |
getExpression()
Returns the filter expression.
|
int |
getMask()
Returns the mask.
|
boolean |
match(String ipString)
Determines if the specified IP address is authorized.
|
static IPFilter |
parseIPFilter(String expression)
Creates an
IPFilter object for the specified filter
expression. |
String |
toString()
Returns a textual representation of this filter.
|
public static final IPFilter parseIPFilter(String expression) throws IllegalArgumentException, ParseException
IPFilter
object for the specified filter
expression. The expression consists of a base IP address and a bit
count. The bit count indicates how many bits in an IP address must match
the bits in the base IP address.expression
- the filter expression, cannot be null
and must match
the format for a filter expression.
if no mask is passed 32 is assumed for IPv4 addresses and 128
for IPv6 addresses.IPFilter
object, never
null
.IllegalArgumentException
- if expression == null
.ParseException
- if expression
does not match the specified format.public final String getExpression()
null
.public final String getBaseIP()
a.a.a.a/n
,
where a is a number between 0 and 255, with no leading
zeroes; never null
.public final int getMask()
public final boolean match(String ipString) throws IllegalArgumentException, ParseException
Note IPv6 addresses are only accepted by IPv6 ACLs and IPv4 addresses are only accepted by IPv4 ACLs.
ipString
- the IP address of which must be determined if it is authorized,
cannot be null
.true
if the IP address is authorized to access the
protected resource, otherwise false
.IllegalArgumentException
- if ipString == null
.ParseException
- if ip
does not match the specified format.See http://www.xins.org/.