org.wings.session.Browser Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2000,2005 wingS development team.
*
* This file is part of wingS (http://wingsframework.org).
*
* wingS is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* Please see COPYING for the complete licence.
*/
package org.wings.session;
import java.io.Serializable;
import java.util.Locale;
import net.sf.uadetector.OperatingSystemFamily;
import net.sf.uadetector.ReadableUserAgent;
import net.sf.uadetector.UserAgentFamily;
import net.sf.uadetector.VersionNumber;
import net.sf.uadetector.internal.data.domain.BrowserType;
/**
* Detect the browser from the user-agent string passed in the HTTP header.
*
* @author Andre Lison
*/
public class Browser implements Serializable {
final private ReadableUserAgent ua;
final private int majorVersion;
final private double minorVersion;
final private String release;
final private Locale browserLocale;
/**
* Create a new browser object and start scanning for
* browser, os and client language in given string.
*
* @param agent the "User-Agent" string from the request.
*/
public Browser(String agent,Locale locale) {
this.ua = CachedUserAgentStringParser.getParser().parse(agent);
this.majorVersion = parseInt( this.ua.getVersionNumber().getMajor() );
this.minorVersion = parseInt( this.ua.getVersionNumber().getMinor() );
this.release = parseExtension( this.ua.getVersionNumber().getExtension() );
this.browserLocale = locale;
}
static private int parseInt(final String number) {
try {
return Integer.parseInt( number );
}
catch( NumberFormatException nfe ) {
return 0;
}
}
static private String parseExtension(final String extension) {
if( extension == null || extension.isEmpty())
return null;
return extension;
}
/**
* Get the browser browserName (Mozilla, MSIE, Opera etc.).
*/
public String getBrowserName() {
return this.ua.getName();
}
/**
* Gets the classification of the browser, this can be either GECKO, IE, KONQUEROR, MOZILLA, OPERA or UNKNOWN.
*
* @return A classification of the browser {@link BrowserType}
*/
public UserAgentFamily getBrowserType() {
return this.ua.getFamily(); // browserType;
}
/**
* Get the browser major version.
*
f.e. the major version for Netscape 6.2 is 6.
*
* @return the major version or 0 if not found
*/
public int getMajorVersion() {
return majorVersion;
}
/**
* Get the minor version. This is the number after the
* dot in the version string.
*
f.e. the minor version for Netscape 6.01 is 0.01.
*
* @return the minor version if found, 0 otherwise
*/
public double getMinorVersion() {
return minorVersion;
}
/**
* Get additional information about browser version.
*
f.e. the release for MSIE 6.1b is b.
*
* @return the release or null, if not available.
*/
public String getRelease() {
return release;
}
/**
* Get the operating system string provided by the browser. {@link OSType}
*
* @return the os browserName or null, if not available.
*/
public String getOs() {
return this.ua.getOperatingSystem().getName();
}
/**
* Get the operating system version.
*
* @return the os version or null, if not available.
*/
public VersionNumber getOsVersion() {
return this.ua.getOperatingSystem().getVersionNumber();
}
/**
* Get the operating system type.
*
* @return A valid {@link OSType}
*/
public OperatingSystemFamily getOsType() {
return this.ua.getOperatingSystem().getFamily();
}
/**
* Get the browser/client locale.
*
* @return the found locale or the default server locale
* specified by {@link Locale#getDefault} if not found.
*/
public Locale getClientLocale() {
return browserLocale;
}
/**
* Get a full human readable representation of the browser.
*/
public String toString() {
return getBrowserName() + " v" + (majorVersion + minorVersion) + (release == null ? "" : '-' + release) +
" browsertype:[" + getBrowserType() + "], locale:" + browserLocale + ", ostype:" + getOsType() + ": os:" + getOs() + " osv:" + getOsVersion();
}
}