@Override
  public void doFilter(
      ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
      throws IOException, ServletException {
    boolean doFilterChain = true;
    Content content = null;

    if (servletRequest instanceof HttpServletRequest) {
      HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
      HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
      String servletPath = httpServletRequest.getServletPath();
      Page page = PageFilter.getPage(httpServletRequest);
      UrlKeyPageModification.AppPageKeys appPageKey = null;
      HandlerResult handlerResult = null;
      PageInfo pageInfo = new PageInfo();

      if (null == servletPath) {
      } else if (servletPath.startsWith("/school/")) {
        // use regular processing if the request yields a page
        // otherwise, if no page would be returned, use this code
        if (null == page) {
          String[] pathParts = servletPath.split("/");
          String keyInPath = pathParts.length > 2 ? pathParts[2] : null;
          String action = pathParts.length > 3 ? pathParts[3] : null;
          School school = null;

          if (!isBlank(keyInPath)) {
            if (!isBlank(keyInPath)) {
              Query<School> schoolQuery = School.findSchoolByKeyQuery(keyInPath.toLowerCase());
              school = getDisplayQuery(schoolQuery).first();
            }

            content = school;

            if (!isBlank(content)) {
              /*-- get the appPageKey based on the action --*/
              if (isBlank(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.School;
              } else if (FOLLOWERS_PATH.equals(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.SchoolFollowers;
              } else if (NEWS_PATH.equals(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.SchoolNews;
              } else if (MEDIA_PATH.equals(action)) {
                appPageKey = AppPageKeys.SchoolMedia;
                UrlKeyPageModification.AppPageKeys videoPageKey = AppPageKeys.SchoolVideo;
                UrlKeyPageModification.AppPageKeys galleryPageKey = AppPageKeys.SchoolGallery;

                String assetId = null;
                String galleryId = null;

                if (pathParts.length > 4) {
                  galleryId = pathParts[4];
                  servletRequest.setAttribute(DynamicMediaGallery.GALLERY_ID_ATTRIBUTE, galleryId);
                  appPageKey = galleryPageKey;

                  DynamicMediaGallery gallery =
                      Query.findUnique(DynamicMediaGallery.class, "id", galleryId);
                  if (!(gallery instanceof DateRangeGallery || gallery instanceof EventGallery)) {
                    page = null;
                    appPageKey = null;
                  }

                  if (null != gallery) {
                    servletRequest.setAttribute(GALLERY_ATTRIBUTE, gallery);
                  }

                  if (gallery instanceof VideoGallery) {
                    appPageKey = videoPageKey;
                  }

                  if (pathParts.length > 5) {
                    assetId = pathParts[5];

                    servletRequest.setAttribute(
                        DynamicMediaGallery.GALLERY_ASSET_ID_ATTRIBUTE, assetId);
                  }
                }
              } else if (SCORES_SCHEDULES_PATH.equals(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.SchoolScoresSchedules;
              } else {
                // possible a team request
                String sportName = action;
                String levelName = pathParts.length > 4 ? pathParts[4] : null;
                String genderName = pathParts.length > 5 ? pathParts[5] : null;
                page = AppPageKeys.School.getPage();

                if ("All".equals(action) && "All".equals(levelName) && "All".equals(genderName)) {
                  page = AppPageKeys.School.getPage();
                } else if (!isBlank(sportName) && !isBlank(levelName) && !isBlank(genderName)) {
                  try {
                    Sport sport = Sport.valueOf(sportName.toUpperCase());
                    SportLevel level = SportLevel.valueOf(levelName.toUpperCase());
                    SportGender gender = SportGender.valueOf(genderName.toUpperCase());

                    Query<Team> teamQuery =
                        Query.from(Team.class)
                            .where(
                                "sport = ? && gender = ? && level = ? && school = ?",
                                sport,
                                gender,
                                level,
                                school);
                    getDisplayQuery(teamQuery);

                    Team team = teamQuery.first();

                    if (null != team) {
                      content = team;
                      page = AppPageKeys.Team.getPage();
                    }
                  } catch (Exception e) {
                    logger.debug("failed to get team from school request: " + servletPath, e);
                  }
                }
              }
            }
          }
        }
      } else if (servletPath.startsWith("/team/")) {
        // use regular processing if the request yields a page
        // otherwise, if no page would be returned, use this code
        if (null == page) {
          String[] pathParts = servletPath.split("/");
          String schoolNaturalKey = pathParts.length > 2 ? pathParts[2] : null;
          String teamDiscriminator = pathParts.length > 3 ? pathParts[3] : null;
          String action = pathParts.length > 4 ? pathParts[4] : null;
          Team team = null;
          String teamUrlNaturalKey = null;

          if (!isBlank(schoolNaturalKey) && !isBlank(teamDiscriminator)) {
            teamUrlNaturalKey = schoolNaturalKey + "/" + teamDiscriminator;

            Query<Team> teamQuery =
                Query.from(Team.class).where("naturalKey = ?", teamUrlNaturalKey.toLowerCase());

            getDisplayQuery(teamQuery);

            content = team = teamQuery.first();

            if (!isBlank(content)) {
              /*-- get the appPageKey based on the action --*/
              if (isBlank(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.Team;
              } else if (FOLLOWERS_PATH.equals(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.TeamFollowers;
              } else if (NEWS_PATH.equals(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.TeamNews;
              } else if (MEDIA_PATH.equals(action)) {
                appPageKey = AppPageKeys.TeamMedia;
                UrlKeyPageModification.AppPageKeys videoPageKey = AppPageKeys.TeamVideo;
                UrlKeyPageModification.AppPageKeys galleryPageKey = AppPageKeys.TeamGallery;

                String assetId = null;
                String galleryId = null;

                if (pathParts.length > 5) {
                  galleryId = pathParts[5];
                  servletRequest.setAttribute(DynamicMediaGallery.GALLERY_ID_ATTRIBUTE, galleryId);
                  appPageKey = galleryPageKey;

                  DynamicMediaGallery gallery =
                      Query.findUnique(DynamicMediaGallery.class, "id", galleryId);
                  if (!(gallery instanceof DateRangeGallery || gallery instanceof EventGallery)) {
                    page = null;
                    appPageKey = null;
                  }

                  if (null != gallery) {
                    servletRequest.setAttribute(GALLERY_ATTRIBUTE, gallery);
                  }

                  if (gallery instanceof VideoGallery) {
                    appPageKey = videoPageKey;
                  }

                  if (pathParts.length > 6) {
                    assetId = pathParts[6];

                    servletRequest.setAttribute(
                        DynamicMediaGallery.GALLERY_ASSET_ID_ATTRIBUTE, assetId);
                  }
                }
              } else if (SCORES_SCHEDULES_PATH.equals(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.TeamScoresSchedules;
              } else if (STATS_PATH.equals(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.TeamStats;
              } else if ("roster".equals(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.TeamRoster;
              }
            }
          }
        }
      } else if (servletPath.startsWith("/athlete/")) {
        // use regular processing if the request yields a page
        // otherwise, if no page would be returned, use this code
        if (null == page) {
          String[] pathParts = servletPath.split("/");
          String schoolNaturalKey = pathParts.length > 2 ? pathParts[2] : null;
          String fullName = pathParts.length > 3 ? pathParts[3] : null;
          String id = pathParts.length > 4 ? pathParts[4] : null;
          String action = pathParts.length > 5 ? pathParts[5] : null;
          Athlete athlete = null;
          String athleteUrlNaturalKey = null;

          if (!isBlank(schoolNaturalKey) && !isBlank(fullName)) {
            athleteUrlNaturalKey = schoolNaturalKey + "/" + fullName;

            Query<Athlete> athleteQuery = Query.from(Athlete.class).where("id = ?", id);

            getDisplayQuery(athleteQuery);

            content = athlete = athleteQuery.first();
            if (!isBlank(content)) {
              /*-- get the appPageKey based on the action --*/
              if (isBlank(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.Athlete;
              } else if (FOLLOWERS_PATH.equals(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.AthleteFollowers;
              } else if (NEWS_PATH.equals(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.AthleteNews;
              } else if (MEDIA_PATH.equals(action)) {
                appPageKey = AppPageKeys.AthleteMedia;
                UrlKeyPageModification.AppPageKeys videoPageKey = AppPageKeys.AthleteVideo;
                UrlKeyPageModification.AppPageKeys galleryPageKey = AppPageKeys.AthleteGallery;

                String assetId = null;
                String galleryId = null;

                if (pathParts.length > 6) {
                  galleryId = pathParts[6];
                  servletRequest.setAttribute(DynamicMediaGallery.GALLERY_ID_ATTRIBUTE, galleryId);
                  appPageKey = galleryPageKey;

                  DynamicMediaGallery gallery =
                      Query.findUnique(DynamicMediaGallery.class, "id", galleryId);
                  if (!(gallery instanceof DateRangeGallery || gallery instanceof EventGallery)) {
                    page = null;
                    appPageKey = null;
                  }

                  if (null != gallery) {
                    servletRequest.setAttribute(GALLERY_ATTRIBUTE, gallery);
                  }

                  if (gallery instanceof VideoGallery) {
                    appPageKey = videoPageKey;
                  }

                  if (pathParts.length > 7) {
                    assetId = pathParts[7];

                    servletRequest.setAttribute(
                        DynamicMediaGallery.GALLERY_ASSET_ID_ATTRIBUTE, assetId);
                  }
                }
              } else if (SCORES_SCHEDULES_PATH.equals(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.AthleteScoresSchedules;
              } else if (STATS_PATH.equals(action)) {
                appPageKey = UrlKeyPageModification.AppPageKeys.AthleteStats;
              }
            }
          }
        }
      } else if (servletPath.startsWith("/user/")) {
        // use regular processing if the request yields a page
        // otherwise, if no page would be returned, use this code
        if (null == page) {
          String[] pathParts = servletPath.split("/");
          String id = pathParts.length > 2 ? pathParts[2] : null;
          String action = pathParts.length > 3 ? pathParts[3] : null;
          User user = null;

          if (!isBlank(id)) {
            content = user = User.findUserByKey(id);

            /*-- get the appPageKey based on the action --*/
            if (isBlank(action) && content != null) {
              appPageKey = UrlKeyPageModification.AppPageKeys.User;
            } else if (FOLLOWERS_PATH.equals(action)) {
              appPageKey = UrlKeyPageModification.AppPageKeys.UserFollowers;
            } else if (NEWS_PATH.equals(action)) {
              appPageKey = UrlKeyPageModification.AppPageKeys.UserNews;
            } else if (SCORES_SCHEDULES_PATH.equals(action)) {
              appPageKey = AppPageKeys.UserScoresSchedules;
            } else if (STATS_PATH.equals(action)) {
              appPageKey = AppPageKeys.UserStats;
            } else if (MEDIA_PATH.equals(action)) {
              appPageKey = AppPageKeys.UserMedia;
              UrlKeyPageModification.AppPageKeys videoPageKey = AppPageKeys.UserVideo;
              UrlKeyPageModification.AppPageKeys galleryPageKey = AppPageKeys.UserGallery;

              String assetId = null;
              String galleryId = null;

              if (pathParts.length > 4) {
                galleryId = pathParts[4];
                servletRequest.setAttribute(DynamicMediaGallery.GALLERY_ID_ATTRIBUTE, galleryId);
                appPageKey = galleryPageKey;

                DynamicMediaGallery gallery =
                    Query.findUnique(DynamicMediaGallery.class, "id", galleryId);
                if (!(gallery instanceof DateRangeGallery || gallery instanceof EventGallery)) {
                  page = null;
                  appPageKey = null;
                }

                if (null != gallery) {
                  servletRequest.setAttribute(GALLERY_ATTRIBUTE, gallery);
                }

                if (gallery instanceof VideoGallery) {
                  appPageKey = videoPageKey;
                }

                if (pathParts.length > 5) {
                  assetId = pathParts[5];

                  servletRequest.setAttribute(
                      DynamicMediaGallery.GALLERY_ASSET_ID_ATTRIBUTE, assetId);
                }
              }
            } else if ("following".equals(action)) {
              appPageKey = UrlKeyPageModification.AppPageKeys.UserFollowing;
            } else if ("highlights".equals(action)) {
              appPageKey = UrlKeyPageModification.AppPageKeys.UserHighlights;
            }
          }
        }
      } else if (servletPath.startsWith("/game/")
          || servletPath.startsWith("/meet/")
          || servletPath.startsWith("/event/")) {
        // use regular processing if the request yields a page
        // otherwise, if no page would be returned, use this code
        if (null == page) {
          String[] pathParts = servletPath.split("/");
          String schoolNaturalKey = pathParts.length > 2 ? pathParts[2] : null;
          String teamDiscriminator = pathParts.length > 3 ? pathParts[3] : null;
          String eventKey = pathParts.length > 4 ? pathParts[4] : null;
          String action = pathParts.length > 5 ? pathParts[5] : null;
          Event event = null;

          if (!isBlank(eventKey)) {
            content = event = getDisplayQuery(Event.findEventByKeyQuery(eventKey)).first();

            String teamUrlNaturalKey = schoolNaturalKey + "/" + teamDiscriminator;
            Query<Team> teamQuery =
                Query.from(Team.class).where("naturalKey = ?", teamUrlNaturalKey.toLowerCase());

            getDisplayQuery(teamQuery);
            Team team = teamQuery.first();

            Query<School> schoolQuery =
                Query.from(School.class).where("naturalKey = ?", schoolNaturalKey.toLowerCase());

            getDisplayQuery(schoolQuery);
            School contextSchool = schoolQuery.first();

            httpServletRequest.setAttribute(HssUtils.SCHOOL_CONTEXT_ATTRIBUTE, contextSchool);

            if (content != null) {
              /*-- get the appPageKey based on the action --*/
              if (isBlank(action)) {

                if (event instanceof Game) {
                  appPageKey = UrlKeyPageModification.AppPageKeys.Game;

                } else if (event instanceof Meet) {
                  appPageKey = UrlKeyPageModification.AppPageKeys.Meet;
                }

                if (null != appPageKey) page = appPageKey.getPage();

                if (isBlank(page)) {
                  appPageKey = UrlKeyPageModification.AppPageKeys.Event;
                }
              } else if (MEDIA_PATH.equals(action)) {
                appPageKey = AppPageKeys.EventMedia;
                UrlKeyPageModification.AppPageKeys videoPageKey = AppPageKeys.EventVideo;
                UrlKeyPageModification.AppPageKeys galleryPageKey = AppPageKeys.EventGallery;

                String assetId = null;
                String galleryId = null;
                if (event instanceof Game) {
                  appPageKey = AppPageKeys.GameMedia;
                  galleryPageKey = UrlKeyPageModification.AppPageKeys.GameGallery;
                  videoPageKey = UrlKeyPageModification.AppPageKeys.GameVideo;
                } else if (event instanceof Meet) {
                  appPageKey = AppPageKeys.MeetMedia;
                  galleryPageKey = UrlKeyPageModification.AppPageKeys.MeetGallery;
                  videoPageKey = UrlKeyPageModification.AppPageKeys.MeetVideo;
                }

                if (pathParts.length > 6) {
                  galleryId = pathParts[6];
                  servletRequest.setAttribute(DynamicMediaGallery.GALLERY_ID_ATTRIBUTE, galleryId);
                  appPageKey = galleryPageKey;

                  if (null != appPageKey) {
                    page = appPageKey.getPage();
                  }

                  if (isBlank(page)) {
                    appPageKey = UrlKeyPageModification.AppPageKeys.EventGallery;
                  }

                  DynamicMediaGallery gallery =
                      Query.findUnique(DynamicMediaGallery.class, "id", galleryId);
                  if (!(gallery instanceof DateRangeGallery || gallery instanceof EventGallery)) {
                    page = null;
                    appPageKey = null;
                  }

                  if (null != gallery) {
                    servletRequest.setAttribute(GALLERY_ATTRIBUTE, gallery);
                  }

                  if (gallery instanceof VideoGallery) {
                    appPageKey = videoPageKey;
                  }

                  if (pathParts.length > 7) {
                    assetId = pathParts[7];

                    servletRequest.setAttribute(
                        DynamicMediaGallery.GALLERY_ASSET_ID_ATTRIBUTE, assetId);
                  }
                }
              }
            }
          }
        }
      }

      if (null == page) { // use a handler
        try {
          for (Handler handler : handlers) {
            if (null == handler) continue;

            if (handler.matches(httpServletRequest)) {
              handlerResult = handler.findHandlerResult(httpServletRequest, httpServletResponse);

              if (null == handlerResult) continue;

              if (null == page || null != handlerResult.page) page = handlerResult.page;

              content = handlerResult.content;

              if (null != handlerResult.pageInfo) pageInfo = handlerResult.pageInfo;

              if (handlerResult.stopHandlerChain) {
                doFilterChain = !handlerResult.stopFilterChain;
                break;
              }
            }
          }
        } catch (Exception e) {
          logger.debug("failed in Handler", e);
        }
      }

      if (null != appPageKey && page == null) {
        page = appPageKey.getPage();
      }

      if (null != page) {
        PageFilter.setPage(httpServletRequest, page);
        servletRequest.setAttribute(HssUtils.PAGE_ATTRIBUTE, page);

        if (null != content) {
          servletRequest.setAttribute(
              UrlKeyPageModification.ID_FROM_NATURAL_KEY_ATTR, content.getId().toString());
          PageFilter.setMainObject(httpServletRequest, content);
        }
      }

      if (true) {
        httpServletRequest.setAttribute(
            PAGE_INFO_ATTRIBUTE, pageInfo.updateFromRequest(httpServletRequest));
      }
    }

    if (doFilterChain) {
      filterChain.doFilter(servletRequest, servletResponse);
      return;
    }
  }
          public HandlerResult findHandlerResult(
              HttpServletRequest request, HttpServletResponse response) {
            String servletPath = request.getServletPath();
            String[] pathParts = servletPath.split("/");
            String schoolKey = pathParts.length > 2 ? pathParts[2] : null;
            String eventKey = pathParts.length > 3 ? pathParts[3] : null;
            String sportName = pathParts.length > 4 ? pathParts[4] : null;
            String levelName = pathParts.length > 5 ? pathParts[5] : null;
            String genderName = pathParts.length > 6 ? pathParts[6] : null;
            HandlerResult result = new HandlerResult();
            School school = null;
            if (!isBlank(schoolKey)) {
              school = School.findSchoolByKey(schoolKey.toLowerCase());
            }

            if (isBlank(school)) {
              result.content = null;
              result.page = null;
              result.stopHandlerChain = false;
              result.stopFilterChain = false;

              return result;
            } else if (isBlank(eventKey)) {
              result.content = school;
              result.page = AppPageKeys.SchoolScoresSchedules.getPage();
              result.stopHandlerChain = true;
              result.stopFilterChain = false;

              try {
                Sport sport = Sport.valueOf(sportName.toUpperCase());
                SportLevel level = SportLevel.valueOf(levelName.toUpperCase());
                SportGender gender = SportGender.valueOf(genderName.toUpperCase());

                Query<Team> teamQuery =
                    Query.from(Team.class)
                        .where(
                            "sport = ? && gender = ? && level = ? && school = ?",
                            sport,
                            gender,
                            level,
                            school);
                getDisplayQuery(teamQuery);

                Team team = teamQuery.first();

                if (null != team) {
                  result.content = team;
                  result.page = AppPageKeys.TeamScoresSchedules.getPage();
                }
              } catch (Exception e) {
                logger.debug("failed to get team from request: " + servletPath, e);
              }

              return result;

            } else {

              try {
                Event event = Event.findEventByKey(eventKey);

                if (null != event) {
                  setSchool(request, school);
                  result.content = event;
                  if (event instanceof Meet) {
                    result.page = AppPageKeys.Meet.getPage();
                  } else {
                    result.page = AppPageKeys.Game.getPage();
                  }
                  result.stopHandlerChain = true;
                  result.stopFilterChain = false;
                }

                return result;
              } catch (Exception e) {
                logger.debug("failed to get event from school request: " + servletPath, e);
              }
            }

            return result;
          }