Here's one I wrote for my current project. Feel free to suggest
improvements.
Robert
/**
* Class that represents what we know about the client machine.
*/
public class Client {
public static final String WINDOWS = "windows";
public static final String MAC = "mac";
public static final String IE = "msie";
public static final String FIREFOX = "firefox";
public static final String MOZILLA = "mozilla"; // NOT Firefox
public static final String SAFARI = "safari";
public static final String OPERA = "opera";
public static final String UNKNOWN = "unknown";
private String ua;
private String os;
private String browser;
public Client(IRequestCycle cycle) {
this(cycle.getRequestContext().getRequest());
}
public Client(HttpServletRequest request) {
ua = request.getHeader(USER_AGENT);
if(ua != null) {
ua = ua.toLowerCase();
} else {
os = UNKNOWN;
browser = UNKNOWN;
}
}
/**
* Returns a String representing the client OS. Returns
Client.WINDOWS, Client.MAC or
* Client.UNKNOWN.
* @return String
*/
public String getOs() {
if(os == null) {
if(ua.indexOf(MAC) > -1) os = MAC;
else if(ua.indexOf(WINDOWS) > -1) os = WINDOWS;
else os = UNKNOWN;
}
return os;
}
/**
* Returns a String representing the client browser. Returns
Client.IE, Client.FIREFOX,
* Client.MOZILLA, Client.SAFARI, Client.OPERA or Client.UNKNOWN.
* @return String
*/
public String getBrowser() {
if(browser == null) {
/*
* Opera's user agent string contains "msie," so check for
it before checking for
* IE. Same for checking for Firefox before Mozilla.
*/
if(ua.indexOf(SAFARI) > -1) browser = SAFARI;
else if(ua.indexOf(OPERA) > -1) browser = OPERA;
else if(ua.indexOf(IE) > -1) browser = IE;
else if(ua.indexOf(FIREFOX) > -1) browser = FIREFOX;
else if(ua.indexOf(MOZILLA) > -1) browser = MOZILLA;
else browser = UNKNOWN;
}
return browser;
}
/**
* Returns true if the client OS matches the given one; false
otherwise.
* @param String osCheck
* @return boolean
*/
public boolean checkOs(String osCheck) {
return getOs().equals(osCheck);
}
/**
* Returns true if the client browser matches the given one; false
otherwise.
* @param String browserCheck
* @return boolean
*/
public boolean checkBrowser(String browserCheck) {
return getBrowser().equals(browserCheck);
}
/**
* Returns true if the client OS and browser match the given ones;
false otherwise.
* @param String osCheck
* @param String browserCheck
* @return boolean
*/
public boolean check(String osCheck, String browserCheck) {
return checkOs(osCheck) && checkBrowser(browserCheck);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]