From c18ea096d1268cba08743e9b63f79a8739d5372b 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 (cherry picked from commit 1ebe68f5b874df9ffac2a633d17cbb36618de44e) --- .../auth/password/AuthCodePassword.java | 6 +-- .../pro/gravit/utils/helper/CommonHelper.java | 12 +++-- .../pro/gravit/utils/helper/QueryHelper.java | 47 +++++++++++++++++++ 3 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 LauncherCore/src/main/java/pro/gravit/utils/helper/QueryHelper.java diff --git a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/password/AuthCodePassword.java b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/password/AuthCodePassword.java index d9595e34..fead6d93 100644 --- a/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/password/AuthCodePassword.java +++ b/LauncherAPI/src/main/java/pro/gravit/launcher/request/auth/password/AuthCodePassword.java @@ -3,10 +3,10 @@ import pro.gravit.launcher.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 ccaeb8ea..dcde9b76 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java @@ -5,10 +5,7 @@ import javax.script.ScriptEngine; import java.lang.reflect.Type; -import java.util.Base64; -import java.util.Collection; -import java.util.LinkedList; -import java.util.Locale; +import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -105,6 +102,13 @@ public static String[] parseCommand(CharSequence line) throws CommandException { return result.toArray(new String[0]); } + public static V multimapFirstOrNullValue(K key, Map> params) { + List list = params.getOrDefault(key, Collections.emptyList()); + if (list.isEmpty()) { + return null; + } + return list.getFirst(); + } public static GsonBuilder newBuilder() { return new GsonBuilder().registerTypeHierarchyAdapter(byte[].class, 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..03ed93c3 --- /dev/null +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/QueryHelper.java @@ -0,0 +1,47 @@ +package pro.gravit.utils.helper; + +import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.*; + +public class QueryHelper { + public static Map> splitUriQuery(URI uri) { + String 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(decode(splitParams[0], StandardCharsets.UTF_8), + k -> new ArrayList<>(1)); + strings.add(decode(splitParams[1], StandardCharsets.UTF_8)); + } + return params; + } + + public static String encodeFormPair(String key, String value) { + return encode(key, StandardCharsets.UTF_8) + "=" + encode(value, StandardCharsets.UTF_8); + } + + private static String encode(String value, Charset charset) { + try { + return URLEncoder.encode(value, charset.name()); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + private static String decode(String value, Charset charset) { + try { + return URLDecoder.decode(value, charset.name()); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } +}