Class ResourcePattern

java.lang.Object
org.jgrapes.http.ResourcePattern

public class ResourcePattern extends Object
A resource pattern can be used to filter URIs.

A pattern looks
similar to a URI (scheme://host:port/path) with the following differences:

  • The scheme may be a single protocol name or a list of protocol names separated by commas, or an asterisk, which is matched by URIs with any scheme. The scheme part (scheme://) is optional in a pattern. Omitting it is equivalent to specifying an asterisk.

  • The host may be a host name or an IP address or an asterisk. Unless the value is an asterisk, filtered URIs must match the value literally.

  • The port may be a number or an asterisk. Unless the value is an asterisk, filtered URIs must match it.

  • Specifying a port (:port) is optional. If omitted, it is equivalent to specifying an asterisk.

  • If the scheme part is omitted, the host:port part may completely be left out as well, which is equivalent to specifying an asterisk for both host and port.

  • The optional path part consist of one or more path separated by commas.

    Each path consists of a sequence of names and asterisks separated by slashes or a vertical bar (“|”). A name must be matched by the corresponding path element of filtered URIs, an asterisk is matched by any corresponding path element (which, however, must exist in the filtered URI). The final element in the path of a pattern may be two asterisks (**), which matches any remaining path elements in the filtered URI.

    Using a vertical bar instead of a slash separates the path in a prefix part and the rest. The number of prefix segments is the value returned by the match methods.

    If a path ends with a vertical bar and the URI matched with the path does not end with a slash, a slash is appended to the URI before matching. This causes both /foo and /foo/ to match a path /foo|. This is usually intended. If you do want to treat /foo as a leaf, specify /foo,/foo| in your pattern.

  • Constructor Details

  • Method Details

    • pattern

      public String pattern()
      Returns:
      the pattern (string) that was used to create this resource pattern.
    • protocol

      public String protocol()
      Returns:
      the protocol value specified in the pattern or null
    • host

      public String host()
      Returns:
      the host value specified in the pattern or null
    • port

      public String port()
      Returns:
      the port value specified in the pattern or null
    • path

      public String path()
      Returns:
      the path value specified in the pattern or null
    • matches

      public int matches(URI resource)
      Matches the given resource URI against the pattern.
      Parameters:
      resource - the URI specifying the resource to match
      Returns:
      -1 if the resource does not match, else the number of prefix segments (which may be 0)
    • matches

      public int matches(URI resource, int pathSegs)
      Matches the given resource URI against the pattern, comparing at most the given number of path segments.
      Parameters:
      resource - the URI specifying the resource to match
      pathSegs - the maximum number of path segments to compare
      Returns:
      -1 if the resource does not match, else the number of prefix segments (which may be 0)
    • matches

      public static boolean matches(String pattern, URI resource) throws ParseException
      Matches the given pattern against the given resource URI.
      Parameters:
      pattern - the pattern to match
      resource - the URI specifying the resource to match
      Returns:
      true if the resource URI matches
      Throws:
      ParseException - if an invalid pattern is specified
    • matches

      public static boolean matches(String pattern, URI resource, int pathSegments) throws ParseException
      Matches the given pattern against the given resource URI, comparing at most the given number of path segments.
      Parameters:
      pattern - the pattern to match
      resource - the URI specifying the resource to match
      pathSegments - the maximum number of path segments to compare
      Returns:
      true if the resource URI matches
      Throws:
      ParseException - if an invalid pattern is specified
    • removeSegments

      public static String removeSegments(String path, int segments)
      Removes the given number of segments (and their trailing slashes) from the beginning of the path.

      Segments may be empty. This implies that invoking this method with a path that starts with a slash, the first removed segment is the empty segment preceding the slash and the starting slash. Put differently, invoking this method with an absolute path and 1 makes the path relative.

      Parameters:
      path - the path
      segments - the number of segments to remove
      Returns:
      the result
    • split

      public static String[] split(String path, int segments)
      Splits the given path in a prefix with the given number of segments and the rest.

      Like {removeSegments(String, int) but additionally returning the removed segments.

      Parameters:
      path - the path
      segments - the number of segments in the prefi
      Returns:
      the prefix and the rest
    • splitPath

      public Optional<String[]> splitPath(URI resource)
      If the URI matches, returns the path split according to the matched pattern (see split(String, int)).
      Parameters:
      resource - the resource
      Returns:
      the result.
    • pathRemainder

      public Optional<String> pathRemainder(URI resource)
      If the URI matches, returns the path without prefix as specified by the matched pattern (see split(String, int)).
      Parameters:
      resource - the resource
      Returns:
      the result.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object