// This file is provided under the terms of the BrowserHawk4J license
// agreement.  Copyright (c) 1999-2005 cyScape, Inc. All rights reserved.

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

import com.cyscape.browserhawk.*;

/** 
 * This servlet tests if the client browser supports frames, tables, and
 * cookies.  Browsers that don't are redirected to an explanation page.
 */

public class Redir extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    BrowserInfo b = null;
    try {
      b = BrowserHawk.getBrowserInfo(req);
    }
    catch (BrowserHawkException e) {
      // Runtime exception thrown if BrowserHawk is misconfigured
      out.println("<big><i>");
      out.println("No browser information available.");
      out.println("</i></big>");
      out.println("<pre>");
      e.printStackTrace(out);
      out.println("</pre>");
      out.flush();
      return;
    }

    out.println("<html>");
    out.println("<head><title>Redirection Example</title></head>");

    out.println("<h2>Redirection Example</h2>");

    out.println("This example tests if your browser supports frames,");
    out.println("tables, and cookies.  For browsers that don't, we redirect");
    out.println("to an explanation page.");

    out.println("<p>");

    // Redirect if any of these aren't true
    if (! (b.getFrames() && b.getTables() && b.getCookies())) {
      res.sendRedirect("/index.html");
    }

    out.println("<i>");
    out.println("If you see this, it means your browser supports frames,");
    out.println("tables, and cookies!");
    out.println("</i>");

    out.println("</html>");
  }
}
