Class SpaUtils

java.lang.Object
ru.vyarus.guicey.spa.filter.SpaUtils

public final class SpaUtils extends Object
Core SPA routes detection logic.
Since:
16.01.2019
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    doRedirect(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, String target)
    Perform server redirect into root page.
    static boolean
    isHtmlRequest(jakarta.servlet.http.HttpServletRequest req)
    Checks if provided request expects html response (by accept header).
    static boolean
    isRootPage(String currentPath, String rootPath)
    Note that root path is not the index page, but root mapping path, which will implicitly lead to index page.
    static boolean
    isSpaRoute(jakarta.servlet.http.HttpServletRequest req, Pattern noRedirect)
    Checks if request could be actually a client side route.
    static void
    noCache(jakarta.servlet.http.HttpServletResponse resp)
    Applies response header to prevent caching (because SPA page should not be cached).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isRootPage

      public static boolean isRootPage(String currentPath, String rootPath)
      Note that root path is not the index page, but root mapping path, which will implicitly lead to index page.
      Parameters:
      currentPath - current path
      rootPath - application root path
      Returns:
      true if provided path is application root path, false otherwise
    • isHtmlRequest

      public static boolean isHtmlRequest(jakarta.servlet.http.HttpServletRequest req)
      Checks if provided request expects html response (by accept header). Did not consider wildcard type (*/*)) as html request, because browser request resources (like fonts) with such type. Only direct text/html type is recognized (assuming human request).
      Parameters:
      req - request instance
      Returns:
      true if request expect html, false otherwise
    • isSpaRoute

      public static boolean isSpaRoute(jakarta.servlet.http.HttpServletRequest req, Pattern noRedirect)
      Checks if request could be actually a client side route. SPA route should be a html request (by accepted type) and not match to provided pattern (describing non-routing urls).
      Parameters:
      req - request instance
      noRedirect - no-redirect pattern
      Returns:
      true if request could be SPA route, false if not
    • noCache

      public static void noCache(jakarta.servlet.http.HttpServletResponse resp)
      Applies response header to prevent caching (because SPA page should not be cached).
      Parameters:
      resp - response instance
    • doRedirect

      public static void doRedirect(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse res, String target) throws IOException, jakarta.servlet.ServletException
      Perform server redirect into root page. Means that current request considered as SPA route and server should return index page as response (under the same url) so client could handle url as internal navigation.

      No cache header is applied to response to prevent index page caching (by this route).

      Parameters:
      req - request instance
      res - response instance
      target - spa root path
      Throws:
      IOException - on error
      jakarta.servlet.ServletException - on error