Author: nbubna
Date: Tue Feb  3 19:08:45 2009
New Revision: 740376

URL: http://svn.apache.org/viewvc?rev=740376&view=rev
Log:
allow userAgent to be set independent of request and add a nicer toString()

Modified:
    
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/BrowserTool.java

Modified: 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/BrowserTool.java
URL: 
http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/BrowserTool.java?rev=740376&r1=740375&r2=740376&view=diff
==============================================================================
--- 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/BrowserTool.java
 (original)
+++ 
velocity/tools/trunk/src/main/java/org/apache/velocity/tools/view/BrowserTool.java
 Tue Feb  3 19:08:45 2009
@@ -74,24 +74,46 @@
     private int geckoMinorVersion = -1;
 
     /**
-     * Initializes this tool by setting the current {...@link 
HttpServletRequest}.
-     * This is required for this tool to operate and will throw a
-     * NullPointerException if this is not set or is set to {...@code null}.
+     * Retrieves the User-Agent header from the request (if any).
+     * @see #setUserAgent
      */
     public void setRequest(HttpServletRequest request)
     {
-        if (request == null)
+        if (request != null)
         {
-            throw new NullPointerException("request should not be null");
+            setUserAgent(request.getHeader("User-Agent"));
         }
-        userAgent = request.getHeader("User-Agent");
-        if (userAgent == null) {
+        else
+        {
+            setUserAgent(null);
+        }
+    }
+
+    /**
+     * Sets the User-Agent string to be parsed for info.  If null, the string
+     * will be empty and everything will return false or null.  Otherwise,
+     * it will set the whole string to lower case before storing to simplify
+     * parsing.
+     */
+    public void setUserAgent(String ua)
+    {
+        if (ua == null)
+        {
             userAgent = "";
-        } else {
-            userAgent = userAgent.toLowerCase();
+        }
+        else
+        {
+            userAgent = ua.toLowerCase();
         }
     }
 
+    @Override
+    public String toString()
+    {
+        return this.getClass().getSimpleName()+"[ua="+userAgent+"]";
+    }
+
+
     /* Generic getter for unknown tests
      */
     public boolean get(String key)


Reply via email to