User agents are applications, which are used to browse the Web, such as Web browsers, search engine spiders, as well as accessibility tools, such as screen readers and Braille browsers.
When a browser connects to a server, it passes the information regarding the brand and version of the browser, and the operating system in which the browser is running, in the User-Agent field contained in the HTTP Request header. For example, Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; ).
You can use the User Agent service of the Portal Runtime to access the user agent information, as described below.
To use the User Agent Service, add a sharing reference entry to the deployment descriptor (file portalapp.xml) in the <application-config> section.
Syntax
<application-config> <property name="SharingReference" value="com.sap.portal.useragent"/> </application-config>
Obtain a reference to the IUserAgentService interface, as follows:
Syntax
IUserAgentService userAgentService =(IUserAgentService) PortalRuntime.getRuntimeResources().getService("com.sap.portal.useragent.useragent");
Obtain a user agent object from the HttpServletRequest object, as follows:
Syntax
HttpServletRequest sRequest = request.getServletRequest(); String userAgentName = sRequest.getHeader("User-Agent"); //With this information call getUserAgent( to get the user agent object of type IUserAgent: IUserAgent userAgent =userAgentService.getUserAgent(sRequest.getHeader(userAgentName));
Obtain the user agent family information by calling the getUserAgentFamily() method, as follows:
Syntax
IUserAgentFamily userAgentFamily =userAgentService.getUserAgentFamily(); /* From this object you get an enumeration of all user agent sets of type IUserAgentSet that matches the IUserAgent object */ Enumeration enum = userAgentFamily.findUserAgentSets(userAgent);
For more information about the IUserAgentService methods, refer to the Enterprise Portal Javadocs at http://help.sap.com/javadocs.