added browser information (port of browser information functionality from
Venice-Dynamo, includes browscap.ini)
This commit is contained in:
		
							parent
							
								
									1351306780
								
							
						
					
					
						commit
						b4c7aa4c8b
					
				@ -115,6 +115,7 @@
 | 
			
		||||
    </javac>
 | 
			
		||||
    <copy todir="buildwork">
 | 
			
		||||
      <fileset dir="src" includes="**/*.properties"/>
 | 
			
		||||
      <fileset dir="src" includes="**/*.ini"/>
 | 
			
		||||
    </copy>
 | 
			
		||||
  </target>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,28 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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@ricochet.com>,
 | 
			
		||||
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 | 
			
		||||
 * Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 | 
			
		||||
 * 
 | 
			
		||||
 * Contributor(s): 
 | 
			
		||||
 */
 | 
			
		||||
package com.silverwrist.venice.ui.helpers;
 | 
			
		||||
 | 
			
		||||
public interface BrowserInformation
 | 
			
		||||
{
 | 
			
		||||
  public String getAgent();
 | 
			
		||||
 | 
			
		||||
  public String getAttribute(String name);
 | 
			
		||||
 | 
			
		||||
  public boolean hasCapability(String name);
 | 
			
		||||
 | 
			
		||||
} // end interface BrowserInformation
 | 
			
		||||
@ -415,6 +415,7 @@ public abstract class BaseServlet extends HttpServlet
 | 
			
		||||
	} // end if
 | 
			
		||||
 | 
			
		||||
	ScriptManagerContainer.initialize(rootconf,ctxt);  // make sure container initialized
 | 
			
		||||
	BrowserDatabase.get(ctxt);  // initialize browser database
 | 
			
		||||
 | 
			
		||||
      } // end synchronized block
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										254
									
								
								src/com/silverwrist/venice/ui/servlet/BrowserDatabase.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										254
									
								
								src/com/silverwrist/venice/ui/servlet/BrowserDatabase.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,254 @@
 | 
			
		||||
/*
 | 
			
		||||
 * 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@ricochet.com>,
 | 
			
		||||
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 | 
			
		||||
 * Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 | 
			
		||||
 * 
 | 
			
		||||
 * Contributor(s): 
 | 
			
		||||
 */
 | 
			
		||||
package com.silverwrist.venice.ui.servlet;
 | 
			
		||||
 | 
			
		||||
import java.io.*;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import javax.servlet.*;
 | 
			
		||||
import org.apache.log4j.*;
 | 
			
		||||
import org.apache.regexp.*;
 | 
			
		||||
import com.silverwrist.util.*;
 | 
			
		||||
 | 
			
		||||
class BrowserDatabase
 | 
			
		||||
{
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * Static data members
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  private static Logger logger = Logger.getLogger(BrowserDatabase.class);
 | 
			
		||||
 | 
			
		||||
  private static final String SERVLET_ATTR = "com.silverwrist.venice.ui.BrowserDatabase";
 | 
			
		||||
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * Attributes
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  private boolean m_broken = false;                    // true if we had an error in loading
 | 
			
		||||
  private ArrayList m_browser_list = new ArrayList();  // the actual list
 | 
			
		||||
  private HashMap m_name_map = new HashMap();          // map names to elements
 | 
			
		||||
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * Constructor
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  private BrowserDatabase()
 | 
			
		||||
  {
 | 
			
		||||
    InputStream stm = null;
 | 
			
		||||
    BufferedReader rdr = null;
 | 
			
		||||
    try
 | 
			
		||||
    { // load the contents of the browscap.ini file
 | 
			
		||||
      stm = this.getClass().getResourceAsStream("browscap.ini");
 | 
			
		||||
      rdr = new BufferedReader(new InputStreamReader(stm));
 | 
			
		||||
      String line;
 | 
			
		||||
      BrowserEntry current = null;
 | 
			
		||||
      while ((line = rdr.readLine())!=null)
 | 
			
		||||
      { // read in lines from the resource
 | 
			
		||||
	line = line.trim();
 | 
			
		||||
	if ((line.length()==0) || line.startsWith(";"))
 | 
			
		||||
	  continue;  // blank line or comment
 | 
			
		||||
 | 
			
		||||
	if (line.startsWith("[") && line.endsWith("]"))
 | 
			
		||||
	{ // start of new browser entry
 | 
			
		||||
	  current = new BrowserEntry(line.substring(1,line.length() - 1));
 | 
			
		||||
	  m_name_map.put(current.getName(),current);
 | 
			
		||||
	  m_browser_list.add(current);
 | 
			
		||||
	  
 | 
			
		||||
	} // end if
 | 
			
		||||
 | 
			
		||||
	if (current!=null)
 | 
			
		||||
	{ // break the line at the equals sign
 | 
			
		||||
	  int p = line.indexOf('=');
 | 
			
		||||
	  if (p!=-1)
 | 
			
		||||
	  { // separate the name and value, and stow them
 | 
			
		||||
	    String name = line.substring(0,p).trim();
 | 
			
		||||
	    String value = line.substring(p + 1).trim();
 | 
			
		||||
	    if (value.startsWith("#"))
 | 
			
		||||
	      value = value.substring(1);
 | 
			
		||||
	    current.addValue(name,value);
 | 
			
		||||
 | 
			
		||||
	  } // end if
 | 
			
		||||
 | 
			
		||||
	} // end if
 | 
			
		||||
 | 
			
		||||
      } // end while
 | 
			
		||||
 | 
			
		||||
    } // end try
 | 
			
		||||
    catch (IOException e)
 | 
			
		||||
    { // WTF?  this is bad!
 | 
			
		||||
      logger.fatal("Error loading BROWSCAP.INI database",e);
 | 
			
		||||
      m_broken = true;
 | 
			
		||||
 | 
			
		||||
    } // end catch
 | 
			
		||||
    catch (RESyntaxException e)
 | 
			
		||||
    { // WTF?  this is bad!
 | 
			
		||||
      logger.fatal("Error loading BROWSCAP.INI database",e);
 | 
			
		||||
      m_broken = true;
 | 
			
		||||
 | 
			
		||||
    } // end catch
 | 
			
		||||
    finally
 | 
			
		||||
    { // shut down the stream
 | 
			
		||||
      IOUtil.shutdown(rdr);
 | 
			
		||||
      IOUtil.shutdown(stm);
 | 
			
		||||
 | 
			
		||||
    } // end finally
 | 
			
		||||
 | 
			
		||||
  } // end constructor
 | 
			
		||||
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * External operations
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  BrowserEntry lookup(String agent)
 | 
			
		||||
  {
 | 
			
		||||
    if (m_broken)
 | 
			
		||||
      return null;  // database wasn't loaded
 | 
			
		||||
 | 
			
		||||
    for (Iterator it=m_browser_list.iterator(); it.hasNext(); )
 | 
			
		||||
    { // look for a matching agent
 | 
			
		||||
      BrowserEntry be = (BrowserEntry)(it.next());
 | 
			
		||||
      if (be.matches(agent))
 | 
			
		||||
	return be;
 | 
			
		||||
 | 
			
		||||
    } // end for
 | 
			
		||||
 | 
			
		||||
    return null;
 | 
			
		||||
 | 
			
		||||
  } // end lookup
 | 
			
		||||
 | 
			
		||||
  BrowserEntry getByName(String name)
 | 
			
		||||
  {
 | 
			
		||||
    return (BrowserEntry)(m_name_map.get(name));
 | 
			
		||||
 | 
			
		||||
  } // end getByName
 | 
			
		||||
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * External static operations
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  static synchronized BrowserDatabase get(ServletContext ctxt)
 | 
			
		||||
  {
 | 
			
		||||
    BrowserDatabase rc = (BrowserDatabase)(ctxt.getAttribute(SERVLET_ATTR));
 | 
			
		||||
    if (rc==null)
 | 
			
		||||
    { // create database and add it to attributes
 | 
			
		||||
      rc = new BrowserDatabase();
 | 
			
		||||
      ctxt.setAttribute(SERVLET_ATTR,rc);
 | 
			
		||||
 | 
			
		||||
    } // end if
 | 
			
		||||
 | 
			
		||||
    return rc;
 | 
			
		||||
 | 
			
		||||
  } // end get
 | 
			
		||||
 | 
			
		||||
} // end class BrowserDatabase
 | 
			
		||||
 | 
			
		||||
class BrowserEntry
 | 
			
		||||
{
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * Static data members
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  private static final String SPECIAL_CHARS = "*.?[](){}+|^$\\";
 | 
			
		||||
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * Attributes
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  private String m_id;                            // ID of this entry
 | 
			
		||||
  private REProgram m_pattern;                    // pattern the entry matches
 | 
			
		||||
  private HashMap m_values = new HashMap();       // values table for this entry
 | 
			
		||||
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * Constructor
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  BrowserEntry(String header) throws RESyntaxException
 | 
			
		||||
  {
 | 
			
		||||
    m_id = header.intern();
 | 
			
		||||
    RECompiler compiler = new RECompiler();
 | 
			
		||||
    m_pattern = compiler.compile(preprocess(header));
 | 
			
		||||
    
 | 
			
		||||
  } // end constructor
 | 
			
		||||
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * Internal operations
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  private static final String preprocess(String header)
 | 
			
		||||
  {
 | 
			
		||||
    StringBuffer buf = new StringBuffer();
 | 
			
		||||
    char[] src = header.toCharArray();
 | 
			
		||||
    for (int i=0; i<src.length; i++)
 | 
			
		||||
    { // turn DOS-style glob characters into a proper regular expression
 | 
			
		||||
      if (src[i]=='*')
 | 
			
		||||
	buf.append(".*");
 | 
			
		||||
      else if (src[i]=='?')
 | 
			
		||||
	buf.append('.');
 | 
			
		||||
      else
 | 
			
		||||
      { // append the standard character
 | 
			
		||||
	if (SPECIAL_CHARS.indexOf(src[i])>=0)
 | 
			
		||||
	  buf.append('\\');
 | 
			
		||||
	buf.append(src[i]);
 | 
			
		||||
 | 
			
		||||
      } // end else
 | 
			
		||||
 | 
			
		||||
    } // end for
 | 
			
		||||
 | 
			
		||||
    return buf.toString();
 | 
			
		||||
 | 
			
		||||
  } // end preprocess
 | 
			
		||||
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * External operations
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  String getName()
 | 
			
		||||
  {
 | 
			
		||||
    return m_id;
 | 
			
		||||
 | 
			
		||||
  } // end getName
 | 
			
		||||
 | 
			
		||||
  void addValue(String name, String value)
 | 
			
		||||
  {
 | 
			
		||||
    m_values.put(name,value);
 | 
			
		||||
 | 
			
		||||
  } // end addValue
 | 
			
		||||
 | 
			
		||||
  boolean matches(String agent)
 | 
			
		||||
  {
 | 
			
		||||
    RE r = new RE(m_pattern);
 | 
			
		||||
    return r.match(agent);
 | 
			
		||||
 | 
			
		||||
  } // end matches
 | 
			
		||||
 | 
			
		||||
  String getValue(String name)
 | 
			
		||||
  {
 | 
			
		||||
    return (String)(m_values.get(name));
 | 
			
		||||
 | 
			
		||||
  } // end getValue
 | 
			
		||||
 | 
			
		||||
} // end class BrowserEntry
 | 
			
		||||
@ -78,6 +78,8 @@ public class RequestImpl implements RequestInput
 | 
			
		||||
	return RequestImpl.this.queryService(klass);
 | 
			
		||||
      if (klass==ResourceLoader.class)
 | 
			
		||||
	return RequestImpl.this.queryService(klass);
 | 
			
		||||
      if (klass==BrowserInformation.class)
 | 
			
		||||
	return RequestImpl.this.queryService(klass);
 | 
			
		||||
      throw new NoSuchServiceException("RequestOutput",klass);
 | 
			
		||||
 | 
			
		||||
    } // end queryService
 | 
			
		||||
@ -557,6 +559,7 @@ public class RequestImpl implements RequestInput
 | 
			
		||||
  private HTMLRenderingImpl html_rendering = null;       // HTML rendering interface
 | 
			
		||||
  private ScriptSupportImpl script_support = null;       // script support interface
 | 
			
		||||
  private ResourceLoaderImpl m_resource_loader = null;   // resource loader interface
 | 
			
		||||
  private BrowserInformationImpl m_browser_info = null;  // browser information interface
 | 
			
		||||
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * Constructor
 | 
			
		||||
@ -914,6 +917,14 @@ public class RequestImpl implements RequestInput
 | 
			
		||||
 | 
			
		||||
    } // end if
 | 
			
		||||
 | 
			
		||||
    if (klass==BrowserInformation.class)
 | 
			
		||||
    { // create browser information obejct and return it
 | 
			
		||||
      if (m_browser_info==null)
 | 
			
		||||
	m_browser_info = new BrowserInformationImpl(request,ctxt);
 | 
			
		||||
      return m_browser_info;
 | 
			
		||||
 | 
			
		||||
    } // end if
 | 
			
		||||
 | 
			
		||||
    throw new NoSuchServiceException("RequestInput",klass);
 | 
			
		||||
 | 
			
		||||
  } // end queryService
 | 
			
		||||
@ -2426,3 +2437,105 @@ class ResourceLoaderImpl implements ResourceLoader
 | 
			
		||||
  } // end getMenuTemplate
 | 
			
		||||
 | 
			
		||||
} // end class ResourceLoader
 | 
			
		||||
 | 
			
		||||
class BrowserInformationImpl implements BrowserInformation
 | 
			
		||||
{
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * Static data members
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  private static final String AGENT_ATTR = "com.silverwrist.venice.BrowserInformation.agent";
 | 
			
		||||
  private static final String ENTRY_ATTR = "com.silverwrist.venice.BrowserInformation.entries";
 | 
			
		||||
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * Attributes
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  private String m_agent;
 | 
			
		||||
  private List m_entries;
 | 
			
		||||
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * Constructor
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  BrowserInformationImpl(HttpServletRequest req, ServletContext ctxt)
 | 
			
		||||
  {
 | 
			
		||||
    HttpSession sess = req.getSession(true);
 | 
			
		||||
    m_agent = (String)(sess.getAttribute(AGENT_ATTR));
 | 
			
		||||
    m_entries = (List)(sess.getAttribute(ENTRY_ATTR));
 | 
			
		||||
    if ((m_agent==null) || (m_entries==null))
 | 
			
		||||
    { // get the agent header
 | 
			
		||||
      m_agent = req.getHeader("User-Agent");
 | 
			
		||||
      if (m_agent==null)
 | 
			
		||||
	m_agent = req.getHeader("HTTP_USER_AGENT");
 | 
			
		||||
      if (m_agent==null)
 | 
			
		||||
	m_agent = "";
 | 
			
		||||
 | 
			
		||||
      // get the browser database
 | 
			
		||||
      BrowserDatabase bdata = BrowserDatabase.get(ctxt);
 | 
			
		||||
      BrowserEntry ntry = bdata.lookup(m_agent);
 | 
			
		||||
      if (ntry!=null)
 | 
			
		||||
      { // get the list of entries
 | 
			
		||||
	ArrayList tmp = new ArrayList();
 | 
			
		||||
	tmp.add(ntry);
 | 
			
		||||
	String s = ntry.getName();
 | 
			
		||||
	while (!(s.equals("*")))
 | 
			
		||||
	{ // follow "parent" links
 | 
			
		||||
	  s = ntry.getValue("parent");
 | 
			
		||||
	  if (s==null)
 | 
			
		||||
	    s = "*";
 | 
			
		||||
	  ntry = bdata.getByName(s);
 | 
			
		||||
	  tmp.add(ntry);
 | 
			
		||||
 | 
			
		||||
	} // end while
 | 
			
		||||
 | 
			
		||||
	tmp.trimToSize();
 | 
			
		||||
	m_entries = Collections.unmodifiableList(tmp);
 | 
			
		||||
 | 
			
		||||
      } // end if
 | 
			
		||||
      else
 | 
			
		||||
	m_entries = Collections.EMPTY_LIST;
 | 
			
		||||
 | 
			
		||||
      sess.setAttribute(AGENT_ATTR,m_agent);
 | 
			
		||||
      sess.setAttribute(ENTRY_ATTR,m_entries);
 | 
			
		||||
 | 
			
		||||
    } // end if
 | 
			
		||||
 | 
			
		||||
  } // end constructor
 | 
			
		||||
 | 
			
		||||
  /*--------------------------------------------------------------------------------
 | 
			
		||||
   * Implementations from interface BrowserInformation
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  public String getAgent()
 | 
			
		||||
  {
 | 
			
		||||
    return m_agent;
 | 
			
		||||
 | 
			
		||||
  } // end getAgent
 | 
			
		||||
 | 
			
		||||
  public String getAttribute(String name)
 | 
			
		||||
  {
 | 
			
		||||
    for (Iterator it=m_entries.iterator(); it.hasNext(); )
 | 
			
		||||
    { // get first BrowserEntry with the attribute in question
 | 
			
		||||
      BrowserEntry ntry = (BrowserEntry)(it.next());
 | 
			
		||||
      String rc = ntry.getValue(name);
 | 
			
		||||
      if (rc!=null)
 | 
			
		||||
	return rc;
 | 
			
		||||
 | 
			
		||||
    } // end for
 | 
			
		||||
 | 
			
		||||
    return null;
 | 
			
		||||
 | 
			
		||||
  } // end getAttribute
 | 
			
		||||
 | 
			
		||||
  public boolean hasCapability(String name)
 | 
			
		||||
  {
 | 
			
		||||
    return StringUtil.isBooleanTrue(getAttribute(name));
 | 
			
		||||
 | 
			
		||||
  } // end hasCapability
 | 
			
		||||
 | 
			
		||||
} // end class BrowserInformationImpl
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										13164
									
								
								src/com/silverwrist/venice/ui/servlet/browscap.ini
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13164
									
								
								src/com/silverwrist/venice/ui/servlet/browscap.ini
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user