/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import com.silverwrist.dynamo.WebConstants;
import com.silverwrist.dynamo.iface.*;

class ServletBrowserDataProvider implements BrowserDataProvider, WebConstants
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private HttpServletRequest m_req;
  private BrowserData m_data = null;

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  ServletBrowserDataProvider(HttpServletRequest req)
  {
    m_req = req;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  private final String getUserAgent()
  {
    String rc = m_req.getHeader("User-Agent");
    if (rc==null)
      rc = m_req.getHeader("HTTP_USER_AGENT");
    return rc;

  } // end getUserAgent

  /*--------------------------------------------------------------------------------
   * Implementations from interface BrowserDataProvider
   *--------------------------------------------------------------------------------
   */

  public BrowserData getBrowserData()
  {
    if (m_data==null)
    { // We might have stashed the browser data in the request attributes.
      m_data = (BrowserData)(m_req.getAttribute(REQUEST_BROWSER_DATA_ATTR));
      if (m_data==null)
      { // We might have stashed the BrowserData in the HTTP session.  Try getting it there.
	HttpSession session = m_req.getSession();
	m_data = (BrowserData)(session.getAttribute(SESSION_BROWSER_DATA_PARAM));
	if (m_data==null)
	{ // Not there - we have to recompute it.
	  String agent = getUserAgent();
	  BrowserDatabaseEntry dbentry = BrowserDatabase.get().lookup(agent);
	  if (dbentry==null)
	    m_data = NullBrowserData.get();
	  else
	    m_data = new ServletBrowserData(agent,dbentry);
	  session.setAttribute(SESSION_BROWSER_DATA_PARAM,m_data);

	} // end if

	m_req.setAttribute(REQUEST_BROWSER_DATA_ATTR,m_data);

      } // end if

    } // end if

    return m_data;

  } // end getBrowserData

} // end class ServletBrowserDataProvider
