Accessing user agent

Information about development with Vaadin UI.

Accessing user agent

Postby Development@SIB » Wed Jan 14, 2015 7:13 pm

Sometimes it's useful to know the browser name which runs the vaadin application. It could be useful for showing custom controls or creating responsive layouts.

We have different options to get the name of the browser (= user agent). The first one is the recommended option. Simply use your launcher to get the information:

Syntax: [ Download ] [ Hide ]
getLauncher().getParameter(VaadinUI.PROP_USERAGENT);
or
Syntax: [ Download ] [ Hide ]
getLauncher().getParameter("Web.useragent");


If you need more details about the browser, e.g. if it runs on a mobile device or on a table, simply call

Syntax: [ Download ] [ Hide ]
((VaadinUI)getApplication().getLauncher().getResource()).getAgentInfo

The object will be an instance of com.handinteractive.mobile.UAgentInfo. The class was copied from MobileESP and integrated in our UI because we tried to reduce dependencies and it was only one single file.

The class has methods like detectIpad, detectIphone, detectSmartphone but also detectSonyPlayStation or detectXbox.

If you want to know if the application runs on a tabled or smartphone, simply check

Syntax: [ Download ] [ Hide ]
getLauncher().getParameter(VaadinUI.PROP_PHONE);
or
Syntax: [ Download ] [ Hide ]
getLauncher().getParameter("Web.mobile.phone");
or
Syntax: [ Download ] [ Hide ]
getLauncher().getParameter(VaadinUI.PROP_TABLET);
or
Syntax: [ Download ] [ Hide ]
getLauncher().getParameter("Web.mobile.tablet");


If you need access to the original HttpRequest, simply use HttpContext to get access:

Syntax: [ Download ] [ Hide ]
HttpContext hctxt = HttpContext.getCurrentInstance();

if (hctxt != null)
{
    Object request = hctxt.getRequest();

    if (request instanceof HttpServletRequest)
    {
        ((HttpServletRequest)request).getHeader("User-Agent");
    }
}

Be careful, it's also possible that the original request was a PortletRequest.
User avatar
Development@SIB
 
Posts: 311
Joined: Mon Sep 28, 2009 1:54 pm

Return to Documentation