Class FkRegex

  • All Implemented Interfaces:
    Fork

    public final class FkRegex
    extends Object
    implements Fork
    Fork by regular expression pattern.

    Use this class in combination with TkFork, for example:

     Take take = new TkFork(
       new FkRegex("/home", new TkHome()),
       new FkRegex("/account", new TkAccount())
     );

    Each instance of FkRegex is being asked only once by TkFork whether the request is good enough to be processed. If the request is suitable for this particular fork, it will return the relevant Take.

    Also, keep in mind that the second argument of the constructor may be of type TkRegex and accept an instance of RqRegex, which makes it very convenient to reuse regular expression matcher, for example:

     Take take = new TkFork(
       new FkRegex(
         "/file(.*)",
         new Target<RqRegex>() {
           @Override
           public Response act(final RqRegex req) {
             // Here we immediately getting access to the
             // matcher that was used during parsing of
             // the incoming request
             final String file = req.matcher().group(1);
           }
         }
       )
     );

    The class is immutable and thread-safe.

    Since:
    0.4
    See Also:
    TkFork, TkRegex
    • Constructor Detail

      • FkRegex

        public FkRegex​(String ptn,
                       String text)
        Ctor.
        Parameters:
        ptn - Pattern
        text - Text
      • FkRegex

        public FkRegex​(String ptn,
                       Response rsp)
        Ctor.
        Parameters:
        ptn - Pattern
        rsp - Response
        Since:
        0.16
      • FkRegex

        public FkRegex​(Pattern ptn,
                       Response rsp)
        Ctor.
        Parameters:
        ptn - Pattern
        rsp - Response
        Since:
        0.16
      • FkRegex

        public FkRegex​(String ptn,
                       Take tke)
        Ctor.
        Parameters:
        ptn - Pattern
        tke - Take
      • FkRegex

        public FkRegex​(Pattern ptn,
                       Take tke)
        Ctor.
        Parameters:
        ptn - Pattern
        tke - Take
      • FkRegex

        public FkRegex​(String ptn,
                       TkRegex tke)
        Ctor.
        Parameters:
        ptn - Pattern
        tke - Take
      • FkRegex

        public FkRegex​(Pattern ptn,
                       TkRegex tke)
        Ctor.
        Parameters:
        ptn - Pattern
        tke - Take
      • FkRegex

        public FkRegex​(Pattern ptn,
                       org.cactoos.Scalar<TkRegex> tke)
        Ctor.
        Parameters:
        ptn - Pattern
        tke - Take
        Since:
        1.4
    • Method Detail

      • setRemoveTrailingSlash

        public FkRegex setRemoveTrailingSlash​(boolean enabled)
        Allows disabling the standard way for handling trailing slashes.
        Parameters:
        enabled - Enables/Disables the removal of a trailing slash.
        Returns:
        FkRegex
      • route

        public Opt<Response> route​(Request req)
                            throws Exception
        Description copied from interface: Fork
        Process this request or ignore it.
        Specified by:
        route in interface Fork
        Parameters:
        req - Request
        Returns:
        Non-empty list of responses if it was processed
        Throws:
        Exception - If fails