content: /*
   * Copyright (C) 2019 Stichting Akvo (Akvo Foundation)
   *
   * This file is part of Akvo FLOW.
   *
   * Akvo FLOW is free software: you can redistribute it and modify it under the terms of
   * the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
   * either version 3 of the License or any later version.
   *
   * Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
   * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   * See the GNU Affero General Public License included below for more details.
   *
   * The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
   */
  
  package org.akvo.flow.rest.security;
  
  import org.springframework.web.filter.GenericFilterBean;
  
  import javax.servlet.Filter;
  import javax.servlet.FilterChain;
  import javax.servlet.FilterConfig;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  import java.util.logging.Logger;
  
  import static org.waterforpeople.mapping.app.web.EnvServlet.INDEX_REDIRECT_DISABLED;
  
  public class RedirectIndexFilter extends GenericFilterBean {
  
      private static final Logger logger = Logger.getLogger(RedirectIndexFilter.class.getName());
  
      @Override
      public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
          HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
          String requestPath = httpRequest.getServletPath();
          logger.fine("Request path is: " + requestPath);
  
          boolean indexRedirectIsActive = System.getProperty(INDEX_REDIRECT_DISABLED) == null;
  
          if (("/".equals(requestPath) || requestPath.startsWith("/index")) && indexRedirectIsActive) {
              HttpServletResponse response = (HttpServletResponse) servletResponse;
              response.sendRedirect("/admin");
          } else {
              filterChain.doFilter(servletRequest, servletResponse);
          }
      }
  }
  