[ANY] Add query helper (#708)

* [ANY] Add query helper

* [ANY] Rename AuthCodePassword field to the uri

---------

Co-authored-by: d3coder <admin@xakeps.dk>

(cherry picked from commit 1ebe68f5b8)
This commit is contained in:
XakepSDK 2024-04-01 22:09:38 +05:00 committed by d3coder
parent de34b62036
commit c18ea096d1
3 changed files with 58 additions and 7 deletions

View file

@ -3,10 +3,10 @@
import pro.gravit.launcher.request.auth.AuthRequest; import pro.gravit.launcher.request.auth.AuthRequest;
public class AuthCodePassword implements AuthRequest.AuthPasswordInterface { public class AuthCodePassword implements AuthRequest.AuthPasswordInterface {
public final String code; public final String uri;
public AuthCodePassword(String code) { public AuthCodePassword(String uri) {
this.code = code; this.uri = uri;
} }
@Override @Override

View file

@ -5,10 +5,7 @@
import javax.script.ScriptEngine; import javax.script.ScriptEngine;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Base64; import java.util.*;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Locale;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -105,6 +102,13 @@ public static String[] parseCommand(CharSequence line) throws CommandException {
return result.toArray(new String[0]); return result.toArray(new String[0]);
} }
public static <K, V> V multimapFirstOrNullValue(K key, Map<K, List<V>> params) {
List<V> list = params.getOrDefault(key, Collections.emptyList());
if (list.isEmpty()) {
return null;
}
return list.getFirst();
}
public static GsonBuilder newBuilder() { public static GsonBuilder newBuilder() {
return new GsonBuilder().registerTypeHierarchyAdapter(byte[].class, return new GsonBuilder().registerTypeHierarchyAdapter(byte[].class,

View file

@ -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<String, List<String>> splitUriQuery(URI uri) {
String query = uri.getRawQuery();
if (query == null) {
return Collections.emptyMap();
}
Map<String, List<String>> params = new HashMap<>();
String[] split = query.split("&");
for (String qParams : split) {
String[] splitParams = qParams.split("=");
List<String> 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);
}
}
}