Class UrlRouter

  • All Implemented Interfaces:
    HandlerRouter, ValidatorRouter

    public class UrlRouter
    extends java.lang.Object
    implements HandlerRouter, ValidatorRouter
    This class provides an implementation to select a handler based on the url from which they were fetched.
    Author:
    Maksim Tkachenko, Ween Jiann Lee
    • Constructor Summary

      Constructors 
      Constructor Description
      UrlRouter()
      Constructs a url router without default handler.
      UrlRouter​(Handler defaultHandler)
      Constructs a url router with default handler.
    • Constructor Detail

      • UrlRouter

        public UrlRouter()
        Constructs a url router without default handler.
      • UrlRouter

        public UrlRouter​(Handler defaultHandler)
        Constructs a url router with default handler.
        Parameters:
        defaultHandler - default handler
    • Method Detail

      • register

        public final UrlRouter register​(java.util.regex.Pattern urlPattern,
                                        Handler handler)
        Adds a url pattern, and the handler to be used.

        Please note that the pattern must be an exact match of the url to work.

        Parameters:
        urlPattern - regex pattern of the url.
        handler - handler to which the fetched page should use.
        Returns:
        this.
      • register

        public final UrlRouter register​(java.util.regex.Pattern urlPattern,
                                        Validator validator)
        Adds a url pattern, and the handler to be used.

        Please note that the pattern must be an exact match of the url to work.

        Parameters:
        urlPattern - regex pattern of the url.
        validator - validator to which the fetched page should use.
        Returns:
        this.
      • register

        public final UrlRouter register​(java.util.regex.Pattern urlPattern,
                                        Handler handler,
                                        Validator validator)
        Adds a url pattern, and the handler to be used.

        Please note that the pattern must be an exact match of the url to work.

        Parameters:
        urlPattern - regex pattern of the url.
        handler - handler to which the fetched page should use.
        validator - validator to which the fetched page should use.
        Returns:
        this.
      • getHandler

        public final Handler getHandler​(Request request)
        Description copied from interface: HandlerRouter
        Returns the handler to be used for a specified request.

        This will only be used if a handler is not specified in when added to scheduler.

        Specified by:
        getHandler in interface HandlerRouter
        Parameters:
        request - request made
        Returns:
        the instance of handler to be used
      • getValidator

        public final Validator getValidator​(Request request)
        Description copied from interface: ValidatorRouter
        Returns the handler to be used for a specified request.

        This will only be used if a handler is not specified in when added to scheduler

        Specified by:
        getValidator in interface ValidatorRouter
        Parameters:
        request - request made
        Returns:
        the instance of handler to be used