mirror of
https://bitbucket.org/wisemapping/wisemapping-open-source.git
synced 2024-11-25 23:44:54 +01:00
- Fix java.lang.NullPointerException
at com.wisemapping.util.Browser.isInUserAgentString(Browser.java:267) at com.wisemapping.util.Browser.checkUserAgent(Browser.java:2 issue.
This commit is contained in:
parent
f8cb849597
commit
6555203a94
@ -57,164 +57,169 @@ 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 Browser browser = Browser.UNKNOWN;
|
|
||||||
private int id;
|
|
||||||
private String userAgentString;
|
|
||||||
|
|
||||||
public UserAgent(OperatingSystem operatingSystem, Browser browser)
|
|
||||||
{
|
|
||||||
this.operatingSystem = operatingSystem;
|
|
||||||
this.browser = browser;
|
|
||||||
this.id = (( operatingSystem.getId() << 16) + browser.getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserAgent(String userAgentString)
|
|
||||||
{
|
|
||||||
Browser browser = Browser.parseUserAgentString(userAgentString);
|
|
||||||
|
|
||||||
OperatingSystem operatingSystem = OperatingSystem.UNKNOWN;
|
|
||||||
|
|
||||||
// BOTs don't have an interesting OS for us
|
|
||||||
if (browser != Browser.BOT)
|
|
||||||
operatingSystem = OperatingSystem.parseUserAgentString(userAgentString);
|
|
||||||
|
|
||||||
this.operatingSystem = operatingSystem;
|
|
||||||
this.browser = browser;
|
|
||||||
this.id = (( operatingSystem.getId() << 16) + browser.getId());
|
|
||||||
this.userAgentString = userAgentString;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
private OperatingSystem operatingSystem = OperatingSystem.UNKNOWN;
|
||||||
/**
|
private Browser browser = Browser.UNKNOWN;
|
||||||
* @param userAgentString
|
private int id;
|
||||||
* @return UserAgent
|
private String userAgentString;
|
||||||
*/
|
|
||||||
public static UserAgent parseUserAgentString(String userAgentString) {
|
|
||||||
return new UserAgent(userAgentString);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
public UserAgent(OperatingSystem operatingSystem, Browser browser) {
|
||||||
* Detects the detailed version information of the browser. Depends on the userAgent to be available.
|
this.operatingSystem = operatingSystem;
|
||||||
* Use it only after using UserAgent(String) or UserAgent.parseUserAgent(String).
|
this.browser = browser;
|
||||||
* Returns null if it can not detect the version information.
|
this.id = ((operatingSystem.getId() << 16) + browser.getId());
|
||||||
* @return Version
|
}
|
||||||
*/
|
|
||||||
public Version getBrowserVersion() {
|
|
||||||
return this.browser.getVersion(this.userAgentString);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the system
|
|
||||||
*/
|
|
||||||
public OperatingSystem getOperatingSystem() {
|
|
||||||
return operatingSystem;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
public UserAgent(@Nullable String userAgentString) {
|
||||||
* @return the browser
|
if (userAgentString != null) {
|
||||||
*/
|
Browser browser = Browser.parseUserAgentString(userAgentString);
|
||||||
public Browser getBrowser() {
|
|
||||||
return browser;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
OperatingSystem operatingSystem = OperatingSystem.UNKNOWN;
|
||||||
* Returns an unique integer value of the operating system & browser combination
|
|
||||||
* @return the id
|
|
||||||
*/
|
|
||||||
public int getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// BOTs don't have an interesting OS for us
|
||||||
* Combined string representation of both enums
|
if (browser != Browser.BOT)
|
||||||
*/
|
operatingSystem = OperatingSystem.parseUserAgentString(userAgentString);
|
||||||
public String toString() {
|
|
||||||
return this.operatingSystem.toString() + "-" + this.browser.toString();
|
this.operatingSystem = operatingSystem;
|
||||||
}
|
this.browser = browser;
|
||||||
|
this.id = ((operatingSystem.getId() << 16) + browser.getId());
|
||||||
/**
|
this.userAgentString = userAgentString;
|
||||||
* Returns UserAgent based on specified unique id
|
|
||||||
* @param id
|
} else {
|
||||||
* @return
|
this.operatingSystem = OperatingSystem.UNKNOWN;
|
||||||
*/
|
this.browser = Browser.UNKNOWN;
|
||||||
public static UserAgent valueOf(int id)
|
}
|
||||||
{
|
}
|
||||||
OperatingSystem operatingSystem = OperatingSystem.valueOf((short) (id >> 16));
|
|
||||||
Browser browser = Browser.valueOf( (short) (id & 0x0FFFF));
|
|
||||||
return new UserAgent(operatingSystem,browser);
|
/**
|
||||||
}
|
* @param userAgentString
|
||||||
|
* @return UserAgent
|
||||||
/**
|
*/
|
||||||
* Returns UserAgent based on combined string representation
|
public static UserAgent parseUserAgentString(String userAgentString) {
|
||||||
* @param name
|
return new UserAgent(userAgentString);
|
||||||
* @return
|
}
|
||||||
*/
|
|
||||||
public static UserAgent valueOf(String name)
|
|
||||||
{
|
/**
|
||||||
if (name == null)
|
* 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).
|
||||||
|
* Returns null if it can not detect the version information.
|
||||||
|
*
|
||||||
|
* @return Version
|
||||||
|
*/
|
||||||
|
public Version getBrowserVersion() {
|
||||||
|
return this.browser.getVersion(this.userAgentString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the system
|
||||||
|
*/
|
||||||
|
public OperatingSystem getOperatingSystem() {
|
||||||
|
return operatingSystem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return the browser
|
||||||
|
*/
|
||||||
|
public Browser getBrowser() {
|
||||||
|
return browser;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an unique integer value of the operating system & browser combination
|
||||||
|
*
|
||||||
|
* @return the id
|
||||||
|
*/
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Combined string representation of both enums
|
||||||
|
*/
|
||||||
|
public String toString() {
|
||||||
|
return this.operatingSystem.toString() + "-" + this.browser.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns UserAgent based on specified unique id
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static UserAgent valueOf(int id) {
|
||||||
|
OperatingSystem operatingSystem = OperatingSystem.valueOf((short) (id >> 16));
|
||||||
|
Browser browser = Browser.valueOf((short) (id & 0x0FFFF));
|
||||||
|
return new UserAgent(operatingSystem, browser);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns UserAgent based on combined string representation
|
||||||
|
*
|
||||||
|
* @param name
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static UserAgent valueOf(String name) {
|
||||||
|
if (name == null)
|
||||||
throw new NullPointerException("Name is null");
|
throw new NullPointerException("Name is null");
|
||||||
|
|
||||||
String[] elements = name.split("-");
|
|
||||||
|
|
||||||
if (elements.length == 2)
|
|
||||||
{
|
|
||||||
OperatingSystem operatingSystem = OperatingSystem.valueOf(elements[0]);
|
|
||||||
Browser browser = Browser.valueOf(elements[1]);
|
|
||||||
return new UserAgent(operatingSystem,browser);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new IllegalArgumentException(
|
|
||||||
"Invalid string for userAgent " + name);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* (non-Javadoc)
|
String[] elements = name.split("-");
|
||||||
* @see java.lang.Object#hashCode()
|
|
||||||
*/
|
if (elements.length == 2) {
|
||||||
@Override
|
OperatingSystem operatingSystem = OperatingSystem.valueOf(elements[0]);
|
||||||
public int hashCode() {
|
Browser browser = Browser.valueOf(elements[1]);
|
||||||
final int prime = 31;
|
return new UserAgent(operatingSystem, browser);
|
||||||
int result = 1;
|
}
|
||||||
result = prime * result + ((browser == null) ? 0 : browser.hashCode());
|
|
||||||
result = prime * result + id;
|
throw new IllegalArgumentException(
|
||||||
result = prime * result
|
"Invalid string for userAgent " + name);
|
||||||
+ ((operatingSystem == null) ? 0 : operatingSystem.hashCode());
|
}
|
||||||
return result;
|
|
||||||
}
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Object#hashCode()
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + ((browser == null) ? 0 : browser.hashCode());
|
||||||
|
result = prime * result + id;
|
||||||
|
result = prime * result
|
||||||
|
+ ((operatingSystem == null) ? 0 : operatingSystem.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see java.lang.Object#equals(java.lang.Object)
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
final UserAgent other = (UserAgent) obj;
|
||||||
|
if (browser == null) {
|
||||||
|
if (other.browser != null)
|
||||||
|
return false;
|
||||||
|
} else if (!browser.equals(other.browser))
|
||||||
|
return false;
|
||||||
|
if (id != other.id)
|
||||||
|
return false;
|
||||||
|
if (operatingSystem == null) {
|
||||||
|
if (other.operatingSystem != null)
|
||||||
|
return false;
|
||||||
|
} else if (!operatingSystem.equals(other.operatingSystem))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
|
||||||
* @see java.lang.Object#equals(java.lang.Object)
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (this == obj)
|
|
||||||
return true;
|
|
||||||
if (obj == null)
|
|
||||||
return false;
|
|
||||||
if (getClass() != obj.getClass())
|
|
||||||
return false;
|
|
||||||
final UserAgent other = (UserAgent) obj;
|
|
||||||
if (browser == null) {
|
|
||||||
if (other.browser != null)
|
|
||||||
return false;
|
|
||||||
} else if (!browser.equals(other.browser))
|
|
||||||
return false;
|
|
||||||
if (id != other.id)
|
|
||||||
return false;
|
|
||||||
if (operatingSystem == null) {
|
|
||||||
if (other.operatingSystem != null)
|
|
||||||
return false;
|
|
||||||
} else if (!operatingSystem.equals(other.operatingSystem))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user