<%@ page import = "com.cyscape.browserhawk.*" %>
<%@ page import = "java.io.*" %>
<%@ page import = "java.util.*" %>

<%--

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

  One of the most challenging aspects of deploying Java applets is
  guaranteeing that your visitor's browser has Java installed, enabled,
  and of a particular version and vendor that are known to work with
  your applet. BrowserHawk makes this process easy by enabling you to
  detect, before sending your applet to the client, what the client's
  capabilities and settings are. 

  Unfortunately Java detection isn't as easy as a simple on/off switch
  because of the various settings and variables involved. This sample
  therefore is designed to walk you through the process and explain how
  to make the most of BrowserHawk when detecting your visitor's Java
  capabilities within their browser.

  This sample tests the various properties involved in sniffing Java,
  and based on the returned values, reports the client's Java
  capabilities. You should feel free to use this sample as the basis for
  how you sniff Java in your own application.

--%>

<%
  // First we make sure the system executing this example has an
  // Enterprise Edition license as required for this test.
  // Note: This is NOT needed in your pages, only for this sample.
  if (BrowserHawk.getVersion().indexOf("Ent") == -1) {
    out.println("Enterprise License Required.  ");
    out.println("You are using " + BrowserHawk.getVersion() + ".  ");
    out.println("See <a href='http://www.cyscape.com/order/'>http://www.cyscape.com/order/</a> for upgrade information.");
    return;
  }
  
  ExtendedOptions options = new ExtendedOptions();
  options.addProperties("JavaScriptEnabled, JavaEnabled, Plugin_JavaVer, JavaVersion, JavaVendor, MSJVMBuild");
  options.setPageMessage("Please wait while we check your browser...");
  options.setPageTitle("Please wait...");

  BrowserInfo info = BrowserHawk.getBrowserInfo(request);
  ExtendedBrowserInfo einfo = null;
  try {
    einfo = BrowserHawk.getExtendedBrowserInfo(request, response, options);
  }
  catch (Exception e) {
    PrintWriter pw = new PrintWriter(out, true);
    pw.println("<pre>");
    e.printStackTrace(pw);
    pw.println("</pre>");
    return;
  }
  // A null return value indicates JavaScript has been sent and 
  // we should immediately return
  if (einfo == null) return;
%>

<html>
<head><title>BrowserHawk Java Test Page (JSP)</title></head>

<body bgcolor=white>
<font face="helvetica">

<h2>BrowserHawk Java Test Page (JSP)</h2>

Java test results as detected by 
<a href="http://www.cyscape.com">BrowserHawk</a>:<br>

<%--
 The returned value is the UNSUPPORTED constant if the browser isn't
 Netscape, Mozilla, Firefox, Chrome, IE, Opera, Safari, or AOL Explorer.
--%>

<% if (einfo == ExtendedBrowserInfo.UNSUPPORTED) { %>
  <big>No extended information is available.  Only Netscape,
  Mozilla, Firefox, Chrome, IE, Opera, Safari, and AOL Explorer clients are
  supported.</big>

<% } else if (einfo.getJavaScriptEnabled() == false) { %>
  <big>No Java detection information is available.  This client has
  JavaScript disabled.</big>

<% } else if (!isSupportedBrowser(info)) { %>
  <big>No Java detection is available.<br>For Java detection your browser has
  to be IE Windows 3+ (except exactly 4.00), Mozilla, Firefox, Chrome, Safari 1.2+, 
  Netscape 4.0+, Opera 7+, or AOL Explorer.</big>

<% } else { %>

<br>

<table>
<tr><th align="left">Property</th><th>Value</th></tr>
<tr><td width="150">JavaEnabled</font></td>
    <td><%= einfo.getJavaEnabled() %></td></tr>
<tr><td width="150">Plugin_JavaVer</font></td>
    <td><%= einfo.getPluginJavaVer() %></td></tr>
<tr><td width="150">JavaVersion</font></td>
    <td><%= einfo.getJavaVersion() %></td></tr>
<tr><td width="150">JavaVendor</font></td>
    <td><%= einfo.getJavaVendor() %></td></tr>
<tr><td width="150">MSJVMBuild</font></td>
    <td><%= einfo.getMSJVMBuild() %></td></tr>
</table>

<br>
<b>Conclusion:</b>
<p>

  <%
    String javaVendor = einfo.getJavaVendor();
    String javaVersion = einfo.getJavaVersion();
    String pluginJavaVer = einfo.getPluginJavaVer();
    String msjvmBuild = einfo.getMSJVMBuild();
    if ("".equals(pluginJavaVer)) pluginJavaVer = null;
    if ("".equals(msjvmBuild)) msjvmBuild = null;

		String developerWarning =  // added to some error message reports
      "<br><br>" +
      "(Note for the developer: It's also possible that this condition is " +
      "occurring because the JVMDetector.class file is not being found. " +
      "This .class file is distributed with the BrowserHawk files and " +
      "should be placed into the same directory where your web script " +
      "is running. Or, you can place it into another web directory " +
      "and use BrowserHawk's ExtendedOptions.setJVMCodeBase() to tell " +
      "BrowserHawk what the path to the web directory should be for applet " +
      "downloads. The web server's access logs can reveal if the class " +
      "file is being found and downloaded or not.)";


    // Java disabled and non-empty version is a strong indication Java's disabled
    if (!einfo.getJavaEnabled() && pluginJavaVer != null) {
  %>
      JavaEnabled returned false while Plugin_JavaVer detection returned a legitimate
      value.<br>This indicates you have the Java Plugin <%= pluginJavaVer %> installed
      but it's currently disabled.

  <% } else if (!einfo.getJavaEnabled() && msjvmBuild != null) { %>
      JavaEnabled returned false while MSJVMBuild detection returned a legitimate
      value.<br>This indicates you have the the MSJVM Build <%= msjvmBuild %> installed
      but it's currently disabled.

  <% } else if ("-3".equals(javaVersion)) { %>
      JavaVersion returned -3.<br>This indicates you don't have the Java Plugin
      installed and configured to run applets.

  <% } else if (!einfo.getJavaEnabled() /* implicitly pluginJavaVer and msjvmBuild are null */) { %>
      JavaEnabled returned false and Plugin_JavaVer/MSJVMBuild detection returned no legitimate
      value.<br>This indicates you probably have no Java installed.

  <% } else if ("-1".equals(javaVersion)) { %>
      JavaEnabled returned true while JavaVersion returned -1.<br>This indicates you
      most likely have a very old JVM which can't be accessed from JavaScript.
      <%= developerWarning %>

  <% } else if ("-2".equals(javaVersion) && pluginJavaVer != null) { %>
      JavaEnabled returned true, JavaVersion returned -2, and Plugin_JavaVer
      detection returned a legitimate value.<br>This indicates you have the Java Plugin
      <%= pluginJavaVer %> installed but scripting of applets is explicitly disabled
      so it's not possible to confirm the behavior of an applet.
      <%= developerWarning %>

  <% } else if ("-2".equals(javaVersion) && msjvmBuild != null) { %>
      JavaEnabled returned true, JavaVersion returned -2, and MSJVMBuild
      detection returned a legitimate value.<br>This indicates you have the MSJVM Build
      <%= msjvmBuild %> installed but scripting of applets is explicitly disabled
      so it's not possible to confirm the behavior of an applet.
      <%= developerWarning %>

  <% } else if ("-2".equals(javaVersion) /* implicitly pluginJavaVer and msjvmBuild are null */) { %>
      JavaEnabled returned true, JavaVersion returned -2, and Plugin_JavaVer/MSJVMBuild
      detection returned no legitimate value.<br>This indicates you don't have any
      Java installed.
      <%= developerWarning %>

   <% } else if (javaVersion != null && !javaVersion.equals("")) /* javaVendor exists if javaVersion exists */ { %>
      JavaVersion and JavaVendor indicate you have Java <%= javaVersion %> from <%= javaVendor %>
      ready to run applets under <%= info.getBrowser() %> <%= info.getVersion() %>
      on <%= info.getPlatform() %>.
      <%
        try {
          // If the first 3 chars don't match between javaVersion and pluginJavaVer,
          // add a note about what plugin might be available.
          String java3 = javaVersion.substring(0, 3);
          String plugin3 = pluginJavaVer.substring(0, 3);
          if (!java3.equals(plugin3)) {
            out.println("<br>You also have the Java Plugin version " +
                        pluginJavaVer + " at your disposal.");
          }
        }
        catch (Exception e) { /* One of the substrings failed. */ }
      %>
  <% } %>

<% } %>

<P>
<b><a href="java.jsp">Retry this test</a></b><P>
<b><a href="index.html">Return to list of other JSP samples</a></b><P>
<b><a href="http://www.cyscape.com/order/">BrowserHawk Pricing and Ordering</a></b><P>
<b><a href="http://www.cyscape.com/">cyScape home page</a></b><P>
</font>
</body>
</html>

<%!
  private static boolean isSupportedBrowser(BrowserInfo info) {
    String browser = info.getBrowser();
    double version = info.getVersion();
    String platform = info.getPlatform();

    if (browser.equals("Mozilla")) {
      return true;
    }
    if (browser.equals("Firefox")) {
      return true;
    }
    if (browser.equals("Chrome")) {
      return true;
    }
    if (browser.equals("Safari") && version >= 1.2) {
      return true;
    }
    if (browser.equals("Netscape") && version >= 4) {
      return true;
    }
    if (browser.equals("Opera") && version >= 7) {
      return true;
    }
    if (browser.equals("IE") && platform.indexOf("Win") >= 0
            && version >= 3 && version != 4.0) {
      return true;
    }
    return false;
  }
%>
