3 Replies Latest reply on Mar 30, 2010 7:47 AM by bsgcic

    Determine users browser (firefox, ie, etc) and assign to variable

    bsgcic Newbie

      I need the equivalent of accessing navigator.appName (i.e., what browser the user is using) in javascript but within a conversation scoped stateful Seam component.


      I have tried ways including the following statements:


      private Parameters userParameters = (Parameters) Parameters.instance();
           
      @In(create=true, value="org.jboss.seam.web.parameters")
      private org.jboss.seam.web.Parameters webParameters;
      
      this.userRequestParameters = this.redirectPageInformation.getParameters();
      this.userRequestParameters = this.userParameters.getRequestParameters();
      this.userRequestParameters = this.webParameters.getRequestParameters();
      



      But have not been able to get this to successfully work yet.


      I would greatly appreciate it if someone would post example code for this.


      Thank you,


      Jeff

        • 1. Re: Determine users browser (firefox, ie, etc) and assign to variable
          Damian Harvey Apprentice

          You may find this useful:



          String userAgent = FacesContext.getCurrentInstance().getExternalContext().getRequestHeaderMap().get("user-agent");




          Cheers,


          D.

          • 3. Re: Determine users browser (firefox, ie, etc) and assign to variable
            bsgcic Newbie

            I finally got this working and thus I thought I would post the code. This solution is based on Damian's comment above and on the work by Faisal Khan in his posting at link.


            //$Id: DetectBrowser.java 2010-03-30 12:52:00 $
            
            package com.mysite.util;
            
            import java.io.Serializable;
            
            import javax.faces.context.FacesContext;
            import javax.servlet.http.HttpServletRequest;
            
            import org.jboss.seam.ScopeType;
            import org.jboss.seam.annotations.Create;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.annotations.Scope;
            import org.jboss.seam.annotations.Startup;
            import org.jboss.seam.annotations.web.RequestParameter;
            
            @Name("detectBrowser")
            @Scope(ScopeType.SESSION)
            @Startup
            public class DetectBrowser implements Serializable //Definition Begin
            {
            
                 /**
                  * 
                  */
                 private static final long serialVersionUID = -108298423454361891327L;
                 
                 public DetectBrowser() {};
                 
                 
                 private HttpServletRequest request = null;
                 
                 //Note 20100330: I believe that I can also use the @RequestParameter annotation as well.
                 //@RequestParameter("userAgent")
                 //private String userAgent = null;
                 //private String userAgent;
                 private String userAgent = FacesContext.getCurrentInstance().getExternalContext().getRequestHeaderMap().get("user-agent");
                 
                 private boolean isNetEnabled = false;
                 
                 private boolean isIE = false;
                 
                 private boolean isFirefox = false;
                 
                 private boolean isNS6 = false;
                 
                 private boolean isNS4 = false;
                 
                 private String cssPostfix = null;
                 
                 private final String cssPostfix_IE = "";
                 
                 private final String cssPostfix_Firefox = "_FF";
                 
                 
                 @Create
                 public void create()
                 {
                      this.setUserAgentAttributes();
                      
                 } //public void create()
                 
                 
                 public void setRequest(HttpServletRequest request)
                 {
                      this.request = request;
                      
                      String userAgent = this.request.getHeader("User-Agent");
                      
                      this.setUserAgent(userAgent);
                      
                 } //public void setRequest(HttpServletRequest req)
                 
                 
                 public void setUserAgent(String userAgent)
                 {
                      this.userAgent = userAgent;
                      
                 } //public void setUserAgent(String userAgent)
                 
                 
                 public void setUserAgentAttributes()
                 {
                      String userAgentCheck = this.userAgent.toLowerCase();
                      
                      if (userAgentCheck.indexOf("msie") != -1)
                      {
                           this.isIE = true;
                           
                           this.cssPostfix = this.cssPostfix_IE;
                           
                      } //if (userAgentCheck.indexOf("msie") != -1)
                      
                      else if(userAgentCheck.indexOf("firefox") != -1)
                      {
                           this.isFirefox = true;
                           
                           this.cssPostfix = this.cssPostfix_Firefox;
                           
                      } //else if(userAgentCheck.indexOf("netscape6") != -1)
                      
                      else if(userAgentCheck.indexOf("netscape6") != -1)
                      {
                           this.isNS6 = true;
                           
                           this.cssPostfix = this.cssPostfix_Firefox;
                           
                      } //else if(userAgentCheck.indexOf("netscape6") != -1)
                      
                      else if(userAgentCheck.indexOf("mozilla") != -1)
                      {
                           this.isNS4 = true;
                           
                           this.cssPostfix = this.cssPostfix_Firefox;
                           
                      } //else if(userAgentCheck.indexOf("mozilla") != -1)
                      
                      else //Not (if (userAgentCheck.indexOf("msie") != -1))
                      {
                           this.cssPostfix = this.cssPostfix_Firefox;
                           
                      } //else //Not (if (userAgentCheck.indexOf("msie") != -1))
                      
                      
                      if(userAgentCheck.indexOf(".net clr") != -1)
                           this.isNetEnabled = true;
                      
                 } //public void setUserAgentAttributes()
                 
                 
                 public String getUseragent()
                 {
                      return userAgent;
                 }
                 
                 public boolean isNetEnabled()
                 {
                      return isNetEnabled;
                 }
                 
                 public boolean isIE()
                 {
                      return isIE;
                 }
                 
                 public boolean isFirefox()
                 {
                      return isFirefox;
                 }
                 
                 public boolean isNS6()
                 {
                      return isNS6;
                 }
                 
                 public boolean isNS4()
                 {
                      return isNS4;
                 }
            
                 public String getCssPostfix()
                 {
                      return cssPostfix;
                 }
                 
                 public void setCssPostfix(String cssPostfix)
                 {
                      this.cssPostfix = cssPostfix;
                 }     
                 
            } //public class DetectBrowser implements Serializable //Definition Begin
            



            And then in the xhtml code:


            <div class="beginningOfClassName#{detectBrowser.cssPostfix}">
            




            Jeff