1   /* RootFilter
2    * 
3    * Created on Oct 25, 2004
4    *
5    * Copyright (C) 2004 Internet Archive.
6    * 
7    * This file is part of the Heritrix web crawler (crawler.archive.org).
8    * 
9    * Heritrix is free software; you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser Public License as published by
11   * the Free Software Foundation; either version 2.1 of the License, or
12   * any later version.
13   * 
14   * Heritrix is distributed in the hope that it will be useful, 
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU Lesser Public License
20   * along with Heritrix; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22   */
23  package org.archive.crawler.admin.ui;
24  
25  import java.io.IOException;
26  
27  import javax.servlet.Filter;
28  import javax.servlet.FilterChain;
29  import javax.servlet.FilterConfig;
30  import javax.servlet.ServletException;
31  import javax.servlet.ServletRequest;
32  import javax.servlet.ServletResponse;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  /***
37   * Filter that redirects accesses to 'index.jsp'.
38   * @author stack
39   * @version $Date: 2005-08-29 21:52:36 +0000 (Mon, 29 Aug 2005) $, $Revision: 3771 $
40   */
41  public class RootFilter implements Filter {
42      private FilterConfig filterConfig = null;
43      
44      public void init(FilterConfig config) {
45          this.filterConfig = config;
46      }
47      
48      public void doFilter(ServletRequest req, ServletResponse res,
49              FilterChain chain)
50      throws IOException, ServletException {
51          if (this.filterConfig == null) {
52              return;
53          }
54          if (req instanceof HttpServletRequest) {
55              HttpServletRequest httpRequest = (HttpServletRequest)req;
56              String path = httpRequest.getRequestURI();
57              if (path == null || path.equals(httpRequest.getContextPath()) ||
58                      (path.equals(httpRequest.getContextPath() + "/"))) {
59                  String tgt = this.filterConfig.
60                      getInitParameter("rootFilter.redirectTo");
61                  ((HttpServletResponse)res).sendRedirect((tgt == null)?
62                      httpRequest.getContextPath() + "/index.jsp":
63                      httpRequest.getContextPath() + tgt);
64                  return;
65              }
66          }
67          chain.doFilter(req, res);
68      }
69  
70      public void destroy() {
71          this.filterConfig = null;
72      }
73  }