From b7b5fbea7182ce70982b48a52932eeb2847ff7c4 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 24 Jan 2025 16:17:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=B8=BF=E8=92=99=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=20UA=20=E8=A7=A3=E6=9E=90=EF=BC=88pr#1301@Gitee?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/hutool/http/useragent/OS.java | 57 ++++++++++--------- .../hutool/http/useragent/Platform.java | 46 +++++++++------ .../http/useragent/UserAgentUtilTest.java | 11 ++++ 3 files changed, 70 insertions(+), 44 deletions(-) diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/useragent/OS.java b/hutool-http/src/main/java/org/dromara/hutool/http/useragent/OS.java index 0d620dc32..ce9f623d6 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/useragent/OS.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/useragent/OS.java @@ -40,32 +40,33 @@ public class OS extends UserAgentInfo { * 支持的引擎类型 */ public static final List oses = ListUtil.view(// - new OS("Windows 10 or Windows Server 2016", "windows nt 10\\.0", "windows nt (10\\.0)"),// - new OS("Windows 8.1 or Windows Server 2012R2", "windows nt 6\\.3", "windows nt (6\\.3)"),// - new OS("Windows 8 or Windows Server 2012", "windows nt 6\\.2", "windows nt (6\\.2)"),// - new OS("Windows Vista", "windows nt 6\\.0", "windows nt (6\\.0)"), // - new OS("Windows 7 or Windows Server 2008R2", "windows nt 6\\.1", "windows nt (6\\.1)"), // - new OS("Windows 2003", "windows nt 5\\.2", "windows nt (5\\.2)"), // - new OS("Windows XP", "windows nt 5\\.1", "windows nt (5\\.1)"), // - new OS("Windows 2000", "windows nt 5\\.0", "windows nt (5\\.0)"), // - new OS("Windows Phone", "windows (ce|phone|mobile)( os)?", "windows (?:ce|phone|mobile) (\\d+([._]\\d+)*)"), // - new OS("Windows", "windows"), // - new OS("OSX", "os x (\\d+)[._](\\d+)", "os x (\\d+([._]\\d+)*)"), // - new OS("Android", "Android", "Android (\\d+([._]\\d+)*)"),// - new OS("Android", "XiaoMi|MI\\s+", "\\(X(\\d+([._]\\d+)*)"),// - new OS("Linux", "linux"), // - new OS("Wii", "wii", "wii libnup/(\\d+([._]\\d+)*)"), // - new OS("PS3", "playstation 3", "playstation 3; (\\d+([._]\\d+)*)"), // - new OS("PSP", "playstation portable", "Portable\\); (\\d+([._]\\d+)*)"), // - new OS("iPad", "\\(iPad.*os (\\d+)[._](\\d+)", "\\(iPad.*os (\\d+([._]\\d+)*)"), // - new OS("iPhone", "\\(iPhone.*os (\\d+)[._](\\d+)", "\\(iPhone.*os (\\d+([._]\\d+)*)"), // - new OS("YPod", "iPod touch[\\s\\;]+iPhone.*os (\\d+)[._](\\d+)", "iPod touch[\\s\\;]+iPhone.*os (\\d+([._]\\d+)*)"), // - new OS("YPad", "iPad[\\s\\;]+iPhone.*os (\\d+)[._](\\d+)", "iPad[\\s\\;]+iPhone.*os (\\d+([._]\\d+)*)"), // - new OS("YPhone", "iPhone[\\s\\;]+iPhone.*os (\\d+)[._](\\d+)", "iPhone[\\s\\;]+iPhone.*os (\\d+([._]\\d+)*)"), // - new OS("Symbian", "symbian(os)?"), // - new OS("Darwin", "Darwin\\/([\\d\\w\\.\\-]+)", "Darwin\\/([\\d\\w\\.\\-]+)"), // - new OS("Adobe Air", "AdobeAir\\/([\\d\\w\\.\\-]+)", "AdobeAir\\/([\\d\\w\\.\\-]+)"), // - new OS("Java", "Java[\\s]+([\\d\\w\\.\\-]+)", "Java[\\s]+([\\d\\w\\.\\-]+)")// + new OS("Windows 10 or Windows Server 2016", "windows nt 10\\.0", "windows nt (10\\.0)"),// + new OS("Windows 8.1 or Windows Server 2012R2", "windows nt 6\\.3", "windows nt (6\\.3)"),// + new OS("Windows 8 or Windows Server 2012", "windows nt 6\\.2", "windows nt (6\\.2)"),// + new OS("Windows Vista", "windows nt 6\\.0", "windows nt (6\\.0)"), // + new OS("Windows 7 or Windows Server 2008R2", "windows nt 6\\.1", "windows nt (6\\.1)"), // + new OS("Windows 2003", "windows nt 5\\.2", "windows nt (5\\.2)"), // + new OS("Windows XP", "windows nt 5\\.1", "windows nt (5\\.1)"), // + new OS("Windows 2000", "windows nt 5\\.0", "windows nt (5\\.0)"), // + new OS("Windows Phone", "windows (ce|phone|mobile)( os)?", "windows (?:ce|phone|mobile) (\\d+([._]\\d+)*)"), // + new OS("Windows", "windows"), // + new OS("OSX", "os x (\\d+)[._](\\d+)", "os x (\\d+([._]\\d+)*)"), // + new OS("Android", "Android", "Android (\\d+([._]\\d+)*)"),// + new OS("Harmony", "OpenHarmony", "OpenHarmony (\\d+([._]\\d+)*)"), // + new OS("Android", "XiaoMi|MI\\s+", "\\(X(\\d+([._]\\d+)*)"),// + new OS("Linux", "linux"), // + new OS("Wii", "wii", "wii libnup/(\\d+([._]\\d+)*)"), // + new OS("PS3", "playstation 3", "playstation 3; (\\d+([._]\\d+)*)"), // + new OS("PSP", "playstation portable", "Portable\\); (\\d+([._]\\d+)*)"), // + new OS("iPad", "\\(iPad.*os (\\d+)[._](\\d+)", "\\(iPad.*os (\\d+([._]\\d+)*)"), // + new OS("iPhone", "\\(iPhone.*os (\\d+)[._](\\d+)", "\\(iPhone.*os (\\d+([._]\\d+)*)"), // + new OS("YPod", "iPod touch[\\s\\;]+iPhone.*os (\\d+)[._](\\d+)", "iPod touch[\\s\\;]+iPhone.*os (\\d+([._]\\d+)*)"), // + new OS("YPad", "iPad[\\s\\;]+iPhone.*os (\\d+)[._](\\d+)", "iPad[\\s\\;]+iPhone.*os (\\d+([._]\\d+)*)"), // + new OS("YPhone", "iPhone[\\s\\;]+iPhone.*os (\\d+)[._](\\d+)", "iPhone[\\s\\;]+iPhone.*os (\\d+([._]\\d+)*)"), // + new OS("Symbian", "symbian(os)?"), // + new OS("Darwin", "Darwin\\/([\\d\\w\\.\\-]+)", "Darwin\\/([\\d\\w\\.\\-]+)"), // + new OS("Adobe Air", "AdobeAir\\/([\\d\\w\\.\\-]+)", "AdobeAir\\/([\\d\\w\\.\\-]+)"), // + new OS("Java", "Java[\\s]+([\\d\\w\\.\\-]+)", "Java[\\s]+([\\d\\w\\.\\-]+)")// ); /** @@ -114,7 +115,7 @@ public class OS extends UserAgentInfo { * @return 版本 */ public String getVersion(final String userAgentString) { - if(isUnknown() || null == this.versionPattern){ + if (isUnknown() || null == this.versionPattern) { // 无版本信息 return null; } @@ -127,7 +128,7 @@ public class OS extends UserAgentInfo { * @return 是否为MacOS * @since 5.8.29 */ - public boolean isMacOS(){ + public boolean isMacOS() { return "OSX".equals(getName()); } } diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/useragent/Platform.java b/hutool-http/src/main/java/org/dromara/hutool/http/useragent/Platform.java index 87f2ea487..7752ac470 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/useragent/Platform.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/useragent/Platform.java @@ -52,6 +52,10 @@ public class Platform extends UserAgentInfo { * android */ public static final Platform ANDROID = new Platform("Android", "android"); + /** + * harmony + */ + public static final Platform HARMONY = new Platform("Harmony", "OpenHarmony"); /** * android */ @@ -66,28 +70,29 @@ public class Platform extends UserAgentInfo { * 支持的移动平台类型 */ public static final List mobilePlatforms = ListUtil.view(// - WINDOWS_PHONE, // - IPAD, // - IPOD, // - IPHONE, // - new Platform("Android", "XiaoMi|MI\\s+"), // - ANDROID, // - GOOGLE_TV, // - new Platform("htcFlyer", "htc_flyer"), // - new Platform("Symbian", "symbian(os)?"), // - new Platform("Blackberry", "blackberry") // + WINDOWS_PHONE, // + IPAD, // + IPOD, // + IPHONE, // + new Platform("Android", "XiaoMi|MI\\s+"), // + ANDROID, // + HARMONY, // + GOOGLE_TV, // + new Platform("htcFlyer", "htc_flyer"), // + new Platform("Symbian", "symbian(os)?"), // + new Platform("Blackberry", "blackberry") // ); /** * 支持的桌面平台类型 */ public static final List desktopPlatforms = ListUtil.view(// - new Platform("Windows", "windows"), // - new Platform("Mac", "(macintosh|darwin)"), // - new Platform("Linux", "linux"), // - new Platform("Wii", "wii"), // - new Platform("Playstation", "playstation"), // - new Platform("Java", "java") // + new Platform("Windows", "windows"), // + new Platform("Mac", "(macintosh|darwin)"), // + new Platform("Linux", "linux"), // + new Platform("Wii", "wii"), // + new Platform("Playstation", "playstation"), // + new Platform("Java", "java") // ); /** @@ -160,4 +165,13 @@ public class Platform extends UserAgentInfo { return this.equals(ANDROID) || this.equals(GOOGLE_TV); } + /** + * 是否为Harmony平台 + * + * @return 是否为Harmony平台 + */ + public boolean isHarmony() { + return this.equals(HARMONY); + } + } diff --git a/hutool-http/src/test/java/org/dromara/hutool/http/useragent/UserAgentUtilTest.java b/hutool-http/src/test/java/org/dromara/hutool/http/useragent/UserAgentUtilTest.java index cbae5d0d5..bffeb5654 100644 --- a/hutool-http/src/test/java/org/dromara/hutool/http/useragent/UserAgentUtilTest.java +++ b/hutool-http/src/test/java/org/dromara/hutool/http/useragent/UserAgentUtilTest.java @@ -505,4 +505,15 @@ public class UserAgentUtilTest { assertEquals("Mac", ua.getPlatform().toString()); assertFalse(ua.isMobile()); } + + @Test + public void parseHarmonyUATest() { + final String uaStr = "Mozilla/5.0 (Phone; OpenHarmony 4.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 ArkWeb/4.1.6.1 Mobile "; + final UserAgent ua = UserAgentUtil.parse(uaStr); + assertEquals("Harmony", ua.getPlatform().toString()); + assertTrue(ua.getPlatform().isHarmony()); + assertEquals("Harmony", ua.getOs().toString()); + assertEquals("4.1", ua.getOsVersion()); + assertTrue(ua.isMobile()); + } }