- Fix java.lang.NullPointerException

at com.wisemapping.util.Browser.isInUserAgentString(Browser.java:267)
      	at com.wisemapping.util.Browser.checkUserAgent(Browser.java:2 issue.
main
Paulo Gustavo Veiga 2012-09-19 19:24:30 -03:00
parent f8cb849597
commit 6555203a94
1 changed files with 154 additions and 149 deletions

View File

@ -57,27 +57,26 @@ package com.wisemapping.util;
* <a href="http://blogs.msdn.com/iemobile/archive/2006/08/03/Detecting_IE_Mobile.aspx">Detecting Internet Explorer Mobile's User-Agent on the server</a> * <a href="http://blogs.msdn.com/iemobile/archive/2006/08/03/Detecting_IE_Mobile.aspx">Detecting Internet Explorer Mobile's User-Agent on the server</a>
*/ */
import org.jetbrains.annotations.Nullable;
/** /**
* @author harald * @author harald
*
*/ */
public class UserAgent public class UserAgent {
{
private OperatingSystem operatingSystem = OperatingSystem.UNKNOWN; private OperatingSystem operatingSystem = OperatingSystem.UNKNOWN;
private Browser browser = Browser.UNKNOWN; private Browser browser = Browser.UNKNOWN;
private int id; private int id;
private String userAgentString; private String userAgentString;
public UserAgent(OperatingSystem operatingSystem, Browser browser) public UserAgent(OperatingSystem operatingSystem, Browser browser) {
{
this.operatingSystem = operatingSystem; this.operatingSystem = operatingSystem;
this.browser = browser; this.browser = browser;
this.id = (( operatingSystem.getId() << 16) + browser.getId()); this.id = ((operatingSystem.getId() << 16) + browser.getId());
} }
public UserAgent(String userAgentString) public UserAgent(@Nullable String userAgentString) {
{ if (userAgentString != null) {
Browser browser = Browser.parseUserAgentString(userAgentString); Browser browser = Browser.parseUserAgentString(userAgentString);
OperatingSystem operatingSystem = OperatingSystem.UNKNOWN; OperatingSystem operatingSystem = OperatingSystem.UNKNOWN;
@ -88,8 +87,13 @@ public class UserAgent
this.operatingSystem = operatingSystem; this.operatingSystem = operatingSystem;
this.browser = browser; this.browser = browser;
this.id = (( operatingSystem.getId() << 16) + browser.getId()); this.id = ((operatingSystem.getId() << 16) + browser.getId());
this.userAgentString = userAgentString; this.userAgentString = userAgentString;
} else {
this.operatingSystem = OperatingSystem.UNKNOWN;
this.browser = Browser.UNKNOWN;
}
} }
@ -106,6 +110,7 @@ public class UserAgent
* Detects the detailed version information of the browser. Depends on the userAgent to be available. * Detects the detailed version information of the browser. Depends on the userAgent to be available.
* Use it only after using UserAgent(String) or UserAgent.parseUserAgent(String). * Use it only after using UserAgent(String) or UserAgent.parseUserAgent(String).
* Returns null if it can not detect the version information. * Returns null if it can not detect the version information.
*
* @return Version * @return Version
*/ */
public Version getBrowserVersion() { public Version getBrowserVersion() {
@ -128,6 +133,7 @@ public class UserAgent
/** /**
* Returns an unique integer value of the operating system & browser combination * Returns an unique integer value of the operating system & browser combination
*
* @return the id * @return the id
*/ */
public int getId() { public int getId() {
@ -143,33 +149,32 @@ public class UserAgent
/** /**
* Returns UserAgent based on specified unique id * Returns UserAgent based on specified unique id
*
* @param id * @param id
* @return * @return
*/ */
public static UserAgent valueOf(int id) public static UserAgent valueOf(int id) {
{
OperatingSystem operatingSystem = OperatingSystem.valueOf((short) (id >> 16)); OperatingSystem operatingSystem = OperatingSystem.valueOf((short) (id >> 16));
Browser browser = Browser.valueOf( (short) (id & 0x0FFFF)); Browser browser = Browser.valueOf((short) (id & 0x0FFFF));
return new UserAgent(operatingSystem,browser); return new UserAgent(operatingSystem, browser);
} }
/** /**
* Returns UserAgent based on combined string representation * Returns UserAgent based on combined string representation
*
* @param name * @param name
* @return * @return
*/ */
public static UserAgent valueOf(String name) public static UserAgent valueOf(String name) {
{
if (name == null) if (name == null)
throw new NullPointerException("Name is null"); throw new NullPointerException("Name is null");
String[] elements = name.split("-"); String[] elements = name.split("-");
if (elements.length == 2) if (elements.length == 2) {
{
OperatingSystem operatingSystem = OperatingSystem.valueOf(elements[0]); OperatingSystem operatingSystem = OperatingSystem.valueOf(elements[0]);
Browser browser = Browser.valueOf(elements[1]); Browser browser = Browser.valueOf(elements[1]);
return new UserAgent(operatingSystem,browser); return new UserAgent(operatingSystem, browser);
} }
throw new IllegalArgumentException( throw new IllegalArgumentException(