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

<%--

	This sample is provided under the terms of the BrowserHawk End User License
  Agreement.  Copyright (c) 2003-2011 cyScape, Inc. All rights reserved.

	This example demonstrates how to use BrowserHawk to detect some of the
  more popular "extended" properties.  Extended properties are properties
  that can vary depending on how a user has configured their browser and
  system.

  In addition, this sample demonstrates how to use BrowserHawk's built in
  support for automatic caching of extended property test results.  This
  caching of results makes it very convenient to test a user for a certain
  set of extended properties just once in their session, and to retreive
  the cached results from any pages where it is needed, without writing your
  own special logic to handle such caching.

	The major benefit to the caching is that extended property tests, unlike
  "basic" property tests, require a round trip between the client and server.
  Therefore it is far more efficient to perform the test on the user's
  browser just once and then later retrieve the results from the cache on
  subsequent pages as needed.  BrowserHawk does not cache results of extended
  browser tests by default.  In the details below you will see exactly how to
  use the caching feature.

	In this sample we check for disabled cookies, disabled JavaScript, screen
  width and height, available browser width and height, Flash version, and
  whether the user is on a Broadband connection.  If we already have tested
  this user during this session, the results are retrieved from the cache.
  Otherwise the browser is tested and the results are stored in the cache.

  Feel free to use this sample as the basis for your own scripts that
  perform BrowserHawk testing, as it provides a good framework for doing so.
  You can easily expand or shrink this sample's functionality to meet your
  own needs by changing the parameters passed in to addProperties
  (see below).

	Although we have made this sample very verbose with comments for
  educational purposes, note that it takes only a few lines of code to
  detect the extended properties -  1) the creation of the ExtendedOptions
  class, 2) using ExtendedOptions to set testing preferences and options,
  and 3) calling to getExtendedBrowserInfo() to obtain an
  ExtendedBrowserInfo instance where the test results are obtained.

--%>

<%
  // To avoid an exception, we first check that the system executing this
  // example has a Professional or Enterprise Edition license as required
  // for this test.
  // Note: This is NOT needed in your pages, only for this sample.
  boolean isEnterprise = (BrowserHawk.getVersion().indexOf("Ent") != -1);
  boolean isPro = (BrowserHawk.getVersion().indexOf("Pro") != -1);
  if (!isPro && !isEnterprise) {
    out.println("Professional or 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;
  }

  // First we create the ExtendedOptions object.  This object is used to
  // set various preferences and options, such as which extended properties
  // we want tested and related testing parameters.
  ExtendedOptions options = new ExtendedOptions();

  // Now we tell BrowserHawk which tests we want it to perform on the
  // browser. If there are other settings you want to check as well, you can
  // just add them to this list.   See the ExtendedOptions class in the
  // BrowserHawk documentation for more information.
  options.addProperties("PersistentCookies, SessionCookies, JavaScriptEnabled, Width, Height, WidthAvail, HeightAvail, Plugin_Flash, Broadband");

  // Now we set various preferences and options for how we want
  // BrowserHawk to perform its test.  See the ExtendedOptions class in
  // the documentation for details on options available such as
  // controlling the background color of the screen and page messages i.e.
  // "Checking your browser. Please wait...".
  // TIP: Set the BGCOLOR parameter below to your site's background color.
  // Otherwise you may notice a color flicker during the test.
  options.setPageTitle("Please wait...");
  options.setPageMessage("Checking your system. Please wait...");
  options.setBodyTag("BGCOLOR=#FFFFFF");

  // Now we tell BrowserHawk we want it to cache the results of the test
  // in the user's session.  We do this simply by setting the
  // ExtendedOption CacheToken property.  The token name used can be
  // any valid session variable name.  NOTE: In order for this session
  // caching feature to work, you must have session tracking enabled.
  // By default JSP pages do have session tracking enabled.
  options.setCacheToken("bhawk_results");

	// Above we are using "bhawk_results" as the cache token.  Whenver this
	// page runs again for this same user's session, or whenever any other
  // page using BrowserHawk runs again that also specifies this same cache
  // token, BrowserHawk will automaticall pull the results from the cache,
  // and NOT re-execute the test, even though the code below still calls
  // getExtendedBrowserInfo().   In this case getExtendedBrowserInfo() just
  // retrives the cached ExtendedBrowserInfo object from the user's session,
  // instead of retesting the browser.

  // Now call the getExtendedBrowserInfo method which tells BrowserHawk that
  // we are finished setting our options and that it should perform the
  // browser test now.  If this is the first time this page is executing
  // under this user's session, BrowserHawk will actually perform the test
  // and then cache the results in the user's session object.
  // If this is not the first time this page is executing for the user's
  // session, or if a previous page performed an extended property test
  // and specified the same CacheToken value for the ExtendedOptions class,
  // then Browserhawk will NOT perform the test again. Rather, the call to
  // getExtendedBrowserInfo will simply retrieve the ExtendedBrowserInfo
  // from the session.
  // NOTE: You should not attempt to access the cached extBrowser object
  // from the directly session object, but rather by calling 
  // getExtendedBrowserInfo().  BrowserHawk will return the cached
  // ExtendedBrowserInfo object for you automatically.

  // Note: There may be situations where you want to force BrowserHawk to
  // retest a browser even though the information is already in the cache.
  // For example, let's say you test a browser for a required screen size
  // and the user's screen size is too small.  You then provide the user
  // with information on how to increase their screen size and provide a
  // link for them to retry the test to confirm their new settings are
  // correct so they can proceed with your site.  If you do not force a
  // clear of the cached information, BrowserHawk will continue to return
  // their original screen size test result.  Therefore in such situations,
  // you will want to call the setCacheForceRefresh() method and pass in a
  // value of true.  This tells BrowserHawk to retest the browser even if
  // cached information is already available.  The results from the retest
  // are then returned by BrowserHawk and stored in the cache for reuse.

  ExtendedBrowserInfo extBrowser =
    BrowserHawk.getExtendedBrowserInfo(request, response, options);

  // If the return value is null, we must end the current request here.  The
  // browser will now process the client side tests and call this page back a
  // second time, and this time the return value will not be null.
  if (extBrowser == null) return;
%>

<html>
<head>
	<title>BrowserHawk Sample - Performing Popular BrowserHawk Tests (Java)</title>
</head>
<body><font face="arial">
<blockquote>
<h2>Some Popular BrowserHawk Test Results (Java)</h2>

<font size=3>
This sample demonstrates how to perform some of the more popular browser tests.
<p>

It also demonstrates how BrowserHawk can automatically cache this
information in the user's session.  This way if the BrowserHawk test
results are needed on the same or subsequent page later, BrowerHawk can
just return the information to you from the session, rather than retesting
the browser.  See the source code for this sample for more information.

<p>
BrowserHawk results obtained from cache?

<% if (extBrowser.isFromCache()) { %>
  <B><font color="green">YES, results are from cached BrowserHawk
     information in user's session</font></b>
<% } else { %>
  <B><font color="black">NO, results are NOT from the cache but will be on
     next <a href="?">retry</a></font></b>
<% } %>

<p>

Session cookies enabled? <%= extBrowser.getSessionCookies() %><p>
Persistent cookies enabled? <%= extBrowser.getPersistentCookies() %><p>
JavaScript enabled? <%= extBrowser.getJavaScriptEnabled() %><p>
Screen resolution: <%= extBrowser.getWidth() %> x <%= extBrowser.getHeight() %><p>
Available browser window size: <%= extBrowser.getWidthAvail() %> x <%= extBrowser.getHeightAvail() %><p>
Flash plug-in version installed: <%= extBrowser.getPluginFlash() %><p>
Broadband connection? <%= extBrowser.getBroadband() %><p>

<HR>

<p>
<b><a href="?bhRefresh=1">Retry this test, but clear the cached results to
force a retest of the browser</a></b><P>
<b><a href="popular.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>
</font>
</blockquote>
</body>
</html>
