Class AddressFilter
- java.lang.Object
-
- net.sf.eBus.config.AddressFilter
-
public final class AddressFilter extends Object
Stores a set ofInetSocketAddresses and determines if a given address passes the filter. An address passes if:- The filter contains both the host and port or
- The filter contains the host and a zero port. A zero port means that the client may bind to any port on the host.
This filter acts as a positive filter. Only clients whose address is listed may connect. eBus does not use a negative filter where all clients are accepted except those listed.
An address filter may be defined in text for storage purposes. The format is
<address>[',' <address>]*where <address> format is either(host | IP address) ':' portor just(host | IP address)andportis assumed to be zero.net.sf.eBus.client.EServercan be configured to use an address filter, restricting accepted connections to those specified by the filter.An empty address filter means there is not restriction on client location and all client connections are accepted.
As of eBus release 5.2.0, an address filter may be loaded from a
com.typesafeHOCON configuration.In eBus release 5.4.0, methods
getAllowedRemoteAddresses()andsetAllowedRemoteAddresses(List)were added which means this class implements the Java Bean interface. This means the class can be loaded by the typesafeConfigBeanFactory.create(com.typesafe.config.Config, Class)method. The filter addresses are defined in a property namedallowedRemoteAddressesas a typesafe string array. For example:allowedRemoteAddresses = [ "192.168.3.4:12345", "223.128.33.48", "196.255.254.8:54321" ]- Author:
- Charles Rapp
-
-
Constructor Summary
Constructors Constructor Description AddressFilter()Creates an empty address filter.AddressFilter(Collection<InetSocketAddress> c)Creates a new address filter containing those elements in the specified collection.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(InetSocketAddress address)Adds the address to the filter set.booleanaddAll(Collection<InetSocketAddress> c)Adds all the addresses in the collection to this set.voidclear()Removes all addresses from this filter.booleancontains(InetSocketAddress address)Returnstrueif this address filter contains the specified address; otherwise returnsfalse.booleanequals(Object o)Compares the given object with this filter for equality.List<String>getAllowedRemoteAddresses()Returns address filter as a string list.inthashCode()Returns the hash code value for this set.booleanisEmpty()Returnstrueif this address filter is empty.static AddressFilterload(com.typesafe.config.Config config, String key)Returns an address filter extracted from the given JSON configuration.static AddressFilterparse(String s)Returns the address filter parsed from the given text.static InetSocketAddressparseAddress(String s)Returns an IP address and port from the given string.booleanpasses(InetSocketAddress iAddress)ReturnstrueifiAddressis acceptable to this filter andfalseotherwise.booleanremove(InetSocketAddress address)Removes the specified address from this filter if it is present.booleanremoveAll(Collection<InetSocketAddress> c)Removes all the specified addresses from this filter.voidsetAllowedRemoteAddresses(List<String> addresses)Sets the allowed remote addresses list.intsize()Returns the address filter size.StringtoString()Returns the address filter in text, formatted for storage inEConfigure.
-
-
-
Constructor Detail
-
AddressFilter
public AddressFilter()
Creates an empty address filter.
-
AddressFilter
public AddressFilter(Collection<InetSocketAddress> c)
Creates a new address filter containing those elements in the specified collection.- Parameters:
c- collection whose elements will comprise the new address filter.
-
-
Method Detail
-
equals
public boolean equals(Object o)
Compares the given object with this filter for equality. Returnstrueifois a non-null AddressFilterwith the same size and addresses asthisfilter; otherwise, returnsfalse.
-
hashCode
public int hashCode()
Returns the hash code value for this set. The hash code of a set is defined to be the sum of the hash codes of the elements in the set, where the hash code of anullelement is defined to be zero. This ensures thats1.equals(s2)implies thats1.hashCode()==s2.hashCode()for any two setss1ands2, as required by the general contract ofObject.hashCode().
-
toString
public String toString()
Returns the address filter in text, formatted for storage inEConfigure.
-
isEmpty
public boolean isEmpty()
Returnstrueif this address filter is empty.- Returns:
trueif this address filter is empty.
-
size
public int size()
Returns the address filter size.- Returns:
- the address filter size.
-
contains
public boolean contains(InetSocketAddress address)
Returnstrueif this address filter contains the specified address; otherwise returnsfalse.- Parameters:
address- look for this object in the filter set.- Returns:
trueifaddressis in the filter.
-
getAllowedRemoteAddresses
public List<String> getAllowedRemoteAddresses()
Returns address filter as a string list. If the address filter is empty, then returns an empty list. Does notnull.This method is part of the Java Beans implementation to allow this class to be loaded using the typesafe
ConfigBeanFactory.- Returns:
- filter addresses as a string list.
-
setAllowedRemoteAddresses
public void setAllowedRemoteAddresses(List<String> addresses) throws ParseException
Sets the allowed remote addresses list. An empty list means that all remote addresses are allowed.- Parameters:
addresses- clients are allowed to connection from these remote addresses.This method is part of the Java Beans implementation to allow this class to be loaded using the typesafe
ConfigBeanFactory.- Throws:
ParseException- ifaddressescontains an improperly formatted Internet address.
-
add
public boolean add(InetSocketAddress address)
Adds the address to the filter set.- Parameters:
address- the address added to this filter.- Returns:
trueif this filter did not already containaddress.- Throws:
NullPointerException- ifaddressisnull.
-
addAll
public boolean addAll(Collection<InetSocketAddress> c)
Adds all the addresses in the collection to this set.- Parameters:
c- the addresses to be added to this collection.- Returns:
trueif the filter changed as a result of this call.- Throws:
NullPointerException- if eithercisnullor contains anullelement.
-
remove
public boolean remove(InetSocketAddress address)
Removes the specified address from this filter if it is present. Returnstrueif the filter contained this address.- Parameters:
address- remove this address if present.- Returns:
trueif the address was removed.- Throws:
NullPointerException- ifaddressisnull.
-
removeAll
public boolean removeAll(Collection<InetSocketAddress> c)
Removes all the specified addresses from this filter. Returnstrueif this filter was changed by this operation.- Parameters:
c- addresses to be removed.- Returns:
trueif this filter was changed by this operation.- Throws:
NullPointerException- ifcisnullor contains anulladdress.
-
clear
public void clear()
Removes all addresses from this filter.
-
passes
public boolean passes(InetSocketAddress iAddress)
ReturnstrueifiAddressis acceptable to this filter andfalseotherwise. If this filteris empty, then returnstrue; that is, if no addresses are defined, then all client connections are allowed.- Parameters:
iAddress- Check if this address is in the filter.- Returns:
trueifiAddressis acceptable to this filter andfalseotherwise.
-
parse
public static AddressFilter parse(String s) throws ParseException
Returns the address filter parsed from the given text. The filter addresses are comma-separated and the filter address must be formatted either "host:port" or just "host" (and port is assumed to be zero).Returns a non-
nullempty address filter if not defined.- Parameters:
s- the address filter text.- Returns:
- the parsed address filter.
- Throws:
ParseException- ifsis either improperly formatted or contains an invalid filter address.
-
parseAddress
public static InetSocketAddress parseAddress(String s) throws ParseException
Returns an IP address and port from the given string. The string format must be either "host:port" or "host". If a port is no provided, it is assumed to be zero.- Parameters:
s- parse this host and port text.- Returns:
- the IP address and port.
- Throws:
ParseException- ifshas an invalid format, references an unknown host address, or contains an invalid port.
-
load
public static AddressFilter load(com.typesafe.config.Config config, String key)
Returns an address filter extracted from the given JSON configuration. The filter addresses are defined as a JSON string list and the filter address must be formatted either "host:port" or just "host" and port is assumed to be zero.Returns a non-
nullempty address filter if not defined.- Parameters:
config- JSON configuration containing address filter.key- address filter list is stored under this key.- Returns:
- parsed address filter.
- Throws:
com.typesafe.config.ConfigException- ifconfigcontains an improperly formatted filter.
-
-