Class SpaUtils


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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void doRedirect​(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, java.lang.String target)
      Perform server redirect into root page.
      static boolean isHtmlRequest​(javax.servlet.http.HttpServletRequest req)
      Checks if provided request expects html response (by accept header).
      static boolean isRootPage​(java.lang.String currentPath, java.lang.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​(javax.servlet.http.HttpServletRequest req, java.util.regex.Pattern noRedirect)
      Checks if request could be actually a client side route.
      static void noCache​(javax.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 Detail

      • isRootPage

        public static boolean isRootPage​(java.lang.String currentPath,
                                         java.lang.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​(javax.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​(javax.servlet.http.HttpServletRequest req,
                                         java.util.regex.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​(javax.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​(javax.servlet.http.HttpServletRequest req,
                                      javax.servlet.http.HttpServletResponse res,
                                      java.lang.String target)
                               throws java.io.IOException,
                                      javax.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:
        java.io.IOException - on error
        javax.servlet.ServletException - on error