Class AddressFilter

  • All Implemented Interfaces:
    Serializable

    public final class AddressFilter
    extends Object
    implements Serializable
    Stores a set of InetSocketAddresses and determines if a given address passes the filter. An address passes if:
    1. The filter contains both the host and port or
    2. 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) ':' port or just (host | IP address) and port is assumed to be zero.

    net.sf.eBus.client.EServer can 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.typesafe HOCON configuration.

    In eBus release 5.4.0, methods getAllowedRemoteAddresses() and setAllowedRemoteAddresses(List) were added which means this class implements the Java Bean interface. This means the class can be loaded by the typesafe ConfigBeanFactory.create(com.typesafe.config.Config, Class) method. The filter addresses are defined in a property named allowedRemoteAddresses as a typesafe string array. For example:

    allowedRemoteAddresses = [
        "192.168.3.4:12345",
        "223.128.33.48",
        "196.255.254.8:54321"
    ]
    Author:
    Charles Rapp
    See Also:
    Serialized Form
    • 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. Returns true if o is a non-null AddressFilter with the same size and addresses as this filter; otherwise, returns false.
        Overrides:
        equals in class Object
        Parameters:
        o - comparison object.
        Returns:
        true if o is equal to this address filter.
      • 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 a null element is defined to be zero. This ensures that s1.equals(s2) implies that s1.hashCode()==s2.hashCode() for any two sets s1 and s2, as required by the general contract of Object.hashCode().
        Overrides:
        hashCode in class Object
        Returns:
        the hash code value for this address filter.
      • toString

        public String toString()
        Returns the address filter in text, formatted for storage in EConfigure.
        Overrides:
        toString in class Object
        Returns:
        address filter in text format.
      • isEmpty

        public boolean isEmpty()
        Returns true if this address filter is empty.
        Returns:
        true if 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)
        Returns true if this address filter contains the specified address; otherwise returns false.
        Parameters:
        address - look for this object in the filter set.
        Returns:
        true if address is 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 not null.

        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 - if addresses contains 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:
        true if this filter did not already contain address.
        Throws:
        NullPointerException - if address is null.
      • 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:
        true if the filter changed as a result of this call.
        Throws:
        NullPointerException - if either c is null or contains a null element.
      • remove

        public boolean remove​(InetSocketAddress address)
        Removes the specified address from this filter if it is present. Returns true if the filter contained this address.
        Parameters:
        address - remove this address if present.
        Returns:
        true if the address was removed.
        Throws:
        NullPointerException - if address is null.
      • removeAll

        public boolean removeAll​(Collection<InetSocketAddress> c)
        Removes all the specified addresses from this filter. Returns true if this filter was changed by this operation.
        Parameters:
        c - addresses to be removed.
        Returns:
        true if this filter was changed by this operation.
        Throws:
        NullPointerException - if c is null or contains a null address.
      • clear

        public void clear()
        Removes all addresses from this filter.
      • passes

        public boolean passes​(InetSocketAddress iAddress)
        Returns true if iAddress is acceptable to this filter and false otherwise. If this filter is empty, then returns true; that is, if no addresses are defined, then all client connections are allowed.
        Parameters:
        iAddress - Check if this address is in the filter.
        Returns:
        true if iAddress is acceptable to this filter and false otherwise.
      • 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-null empty address filter if not defined.

        Parameters:
        s - the address filter text.
        Returns:
        the parsed address filter.
        Throws:
        ParseException - if s is 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 - if s has 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-null empty 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 - if config contains an improperly formatted filter.