From 1ebe68f5b874df9ffac2a633d17cbb36618de44e Mon Sep 17 00:00:00 2001 From: XakepSDK Date: Mon, 1 Apr 2024 22:09:38 +0500 Subject: [PATCH] [ANY] Add query helper (#708) * [ANY] Add query helper * [ANY] Rename AuthCodePassword field to the uri --------- Co-authored-by: d3coder --- .../auth/password/AuthCodePassword.java | 6 ++-- .../pro/gravit/utils/helper/CommonHelper.java | 8 +++++ .../pro/gravit/utils/helper/QueryHelper.java | 29 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 LauncherCore/src/main/java/pro/gravit/utils/helper/QueryHelper.java diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/base/request/auth/password/AuthCodePassword.java b/LauncherAPI/src/main/java/pro/gravit/launcher/base/request/auth/password/AuthCodePassword.java index 5b3c6e03..4d123b53 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/base/request/auth/password/AuthCodePassword.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/base/request/auth/password/AuthCodePassword.java @@ -3,10 +3,10 @@ import pro.gravit.launcher.base.request.auth.AuthRequest; public class AuthCodePassword implements AuthRequest.AuthPasswordInterface { - public final String code; + public final String uri; - public AuthCodePassword(String code) { - this.code = code; + public AuthCodePassword(String uri) { + this.uri = uri; } @Override diff --git a/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java b/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java index a286c248..f11f3567 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java @@ -258,6 +258,14 @@ public static ArgsParseResult parseJavaArgs(List args) { return new ArgsParseResult(conf, classpath, jvmArgs, mainClass, mainModule, jarFile, args); } + public static V multimapFirstOrNullValue(K key, Map> params) { + List list = params.getOrDefault(key, Collections.emptyList()); + if (list.isEmpty()) { + return null; + } + return list.getFirst(); + } + public record ArgsParseResult(LaunchOptions.ModuleConf conf, List classpath, List jvmArgs, String mainClass, String mainModule, String jarFile, List args) { } diff --git a/LauncherCore/src/main/java/pro/gravit/utils/helper/QueryHelper.java b/LauncherCore/src/main/java/pro/gravit/utils/helper/QueryHelper.java new file mode 100644 index 00000000..e34b06d4 --- /dev/null +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/QueryHelper.java @@ -0,0 +1,29 @@ +package pro.gravit.utils.helper; + +import java.net.URI; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.*; + +public class QueryHelper { + public static Map> splitUriQuery(URI uri) { + var query = uri.getRawQuery(); + if (query == null) { + return Collections.emptyMap(); + } + Map> params = new HashMap<>(); + String[] split = query.split("&"); + for (String qParams : split) { + String[] splitParams = qParams.split("="); + List strings = params.computeIfAbsent(URLDecoder.decode(splitParams[0], StandardCharsets.UTF_8), + k -> new ArrayList<>(1)); + strings.add(URLDecoder.decode(splitParams[1], StandardCharsets.UTF_8)); + } + return params; + } + + public static String encodeFormPair(String key, String value) { + return URLEncoder.encode(key, StandardCharsets.UTF_8) + "=" + URLEncoder.encode(value, StandardCharsets.UTF_8); + } +}