HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

IE compatibility mode detection

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
modedetectioncompatibility

Problem

This method is written for much larger Web application, but I just extracted it and added class and main method around it so that it can be debugged locally.

Browser detection is done in another method, then this one is called to precisely determine IE version, i.e. if it is in compatibility mode or not. All other checks are done previously.

String headerUserAgent = "headerUserAgent mozilla/5.0 (windows nt 6.1; wow64; trident/7.0; rv:11.0) like gecko"; is "REAL" IE 11 so the output will be 11.0

String headerUserAgent = "mozilla/4.0 (compatible; msie 7.0; windows nt 6.1; wow64; trident/7.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; media center pc 6.0; .net4.0c; .net4.0e; infopath.3; gwx:reserved)"; is one of compatibility modes so output will be as expected: 00

Code works according to my tests.

```
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UnsupportedBrowserMode
{
public static void main(String[] args)
{
String headerUserAgent = "mozilla/4.0 (compatible; msie 7.0; windows nt 6.1; wow64; trident/7.0; slcc2; .net clr 2.0.50727; .net clr 3.5.30729; .net clr 3.0.30729; media center pc 6.0; .net4.0c; .net4.0e; infopath.3; gwx:reserved)";
//String headerUserAgent = "headerUserAgent mozilla/5.0 (windows nt 6.1; wow64; trident/7.0; rv:11.0) like gecko";
String version = getInternerExplorerVersion(headerUserAgent);
System.out.println(version);
}

public static String getInternerExplorerVersion(String headerUserAgent)
{
String version = null;
String TrueVersion = null;
String ActingVersion = null;

if (headerUserAgent != null)
{
headerUserAgent = headerUserAgent.toLowerCase();

// Try to find the Trident version number
Matcher trident = Pattern.compile("trident\\/(\\d+)").matcher(headerUserAgent);

if (trident.find() && trident.groupCount() > 0)
{

Solution

Returning Early

You can remove one level of nesting, and make your code generally easier to understand by returning early:

if (headerUserAgent == null) {
    return null;
}


Now it's also more obvious what happens if the agent is null. But is returning null really what you want? It will lead to a lot of if (agent == null) in the calling code. I would suggest throwing an exception instead. Java also has a nice utility method for this:

Objects.requireNonNull(headerUserAgent, "User Agent is Null");


(This method should be used if the agent being null is not expected. If you do expect it, because it's part of your program logic, this might not be the best approach)

Bugs

Your code doesn't work for quite a lot of input:

  • empty string, `, ua, etc as input results in a NullPointerException.



  • Some Firefox user agents also result in NullPointerException, eg Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0.



  • You can see a list of some possible IE user agents here. I tried some of them, and a lot result in exceptions, such as Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US)), which should be IE 9, Mozilla/5.0 (MSIE 7.0; Macintosh; U; SunOS; X11; gu; SV1; InfoPath.2; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648) which should be IE 7, or Mozilla/1.22 (compatible; MSIE 10.0; Windows 3.1) which should be IE 10 (or 00 in your case).



Misc

  • Your placement of the { is not really following Java conventions.



  • Some of your variable names could be more descriptive, eg x and y.



  • Variables should start with a lower-case character.



  • You have an unnecessary ;`.



  • A JavaDoc style comment describing the exact return values might be a good idea, especially since the method name is a bit confusing (it doesn't always return the version, but 00 for compatibility mode).

Code Snippets

if (headerUserAgent == null) {
    return null;
}
Objects.requireNonNull(headerUserAgent, "User Agent is Null");

Context

StackExchange Code Review Q#99092, answer score: 4

Revisions (0)

No revisions yet.