将获取验证服务器信息的代码移入AuthlibInjectorServer

This commit is contained in:
yushijinhun
2018-06-17 14:24:17 +08:00
parent 538f501bc0
commit bdee9ea72f
4 changed files with 41 additions and 76 deletions

View File

@@ -17,7 +17,35 @@
*/
package org.jackhuang.hmcl.auth.authlibinjector;
import static org.jackhuang.hmcl.util.Lang.tryCast;
import java.io.IOException;
import java.util.Optional;
import org.jackhuang.hmcl.util.JsonUtils;
import org.jackhuang.hmcl.util.NetworkUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
public class AuthlibInjectorServer {
public static AuthlibInjectorServer fetchServerInfo(String url) throws IOException {
try {
JsonObject response = JsonUtils.fromNonNullJson(NetworkUtils.doGet(NetworkUtils.toURL(url)), JsonObject.class);
String name = extractServerName(response).orElse(url);
return new AuthlibInjectorServer(url, name);
} catch (JsonSyntaxException e) {
throw new IOException("Malformed response", e);
}
}
private static Optional<String> extractServerName(JsonObject response){
return tryCast(response.get("meta"), JsonObject.class)
.flatMap(meta -> tryCast(meta.get("serverName"), JsonPrimitive.class).map(JsonPrimitive::getAsString));
}
private String url;
private String name;
@@ -34,6 +62,11 @@ public class AuthlibInjectorServer {
return name;
}
@Override
public String toString() {
return url + " (" + name + ")";
}
@Override
public int hashCode() {
return url.hashCode();

View File

@@ -1,51 +0,0 @@
/*
* Hello Minecraft! Launcher.
* Copyright (C) 2018 huangyuhui <huanghongxun2008@126.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see {http://www.gnu.org/licenses/}.
*/
package org.jackhuang.hmcl.auth.authlibinjector;
public class AuthlibInjectorServerResponse {
private final Meta meta;
public AuthlibInjectorServerResponse() {
this(new Meta());
}
public AuthlibInjectorServerResponse(Meta meta) {
this.meta = meta;
}
public Meta getMeta() {
return meta;
}
public static class Meta {
private final String serverName;
public Meta() {
this("");
}
public Meta(String serverName) {
this.serverName = serverName;
}
public String getServerName() {
return serverName;
}
}
}