mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
HTTPRequest умеет отправлять json
This commit is contained in:
parent
31597cc81c
commit
85559c0ea3
5 changed files with 83 additions and 90 deletions
|
@ -1,12 +1,7 @@
|
||||||
package ru.gravit.launchserver.auth.handler;
|
package ru.gravit.launchserver.auth.handler;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import com.eclipsesource.json.Json;
|
import com.eclipsesource.json.Json;
|
||||||
|
@ -14,6 +9,7 @@
|
||||||
import com.eclipsesource.json.JsonValue;
|
import com.eclipsesource.json.JsonValue;
|
||||||
|
|
||||||
import ru.gravit.launchserver.auth.provider.AuthProviderResult;
|
import ru.gravit.launchserver.auth.provider.AuthProviderResult;
|
||||||
|
import ru.gravit.utils.HTTPRequest;
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
import ru.gravit.utils.helper.IOHelper;
|
||||||
import ru.gravit.utils.helper.VerifyHelper;
|
import ru.gravit.utils.helper.VerifyHelper;
|
||||||
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
|
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
|
||||||
|
@ -22,7 +18,6 @@
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public class JsonAuthHandler extends AuthHandler {
|
public class JsonAuthHandler extends AuthHandler {
|
||||||
|
|
||||||
private static final int TIMEOUT = 10;
|
|
||||||
private final URL url;
|
private final URL url;
|
||||||
private final URL urlCheckServer;
|
private final URL urlCheckServer;
|
||||||
private final URL urlJoinServer;
|
private final URL urlJoinServer;
|
||||||
|
@ -33,6 +28,7 @@ public class JsonAuthHandler extends AuthHandler {
|
||||||
private final String accessTokenKeyName;
|
private final String accessTokenKeyName;
|
||||||
private final String uuidKeyName;
|
private final String uuidKeyName;
|
||||||
private final String responseErrorKeyName;
|
private final String responseErrorKeyName;
|
||||||
|
private final String responseOKKeyName;
|
||||||
|
|
||||||
protected JsonAuthHandler(BlockConfigEntry block) {
|
protected JsonAuthHandler(BlockConfigEntry block) {
|
||||||
super(block);
|
super(block);
|
||||||
|
@ -51,6 +47,8 @@ protected JsonAuthHandler(BlockConfigEntry block) {
|
||||||
VerifyHelper.NOT_EMPTY, "AccessToken key name can't be empty");
|
VerifyHelper.NOT_EMPTY, "AccessToken key name can't be empty");
|
||||||
responseErrorKeyName = VerifyHelper.verify(block.getEntryValue("responseErrorKeyName", StringConfigEntry.class),
|
responseErrorKeyName = VerifyHelper.verify(block.getEntryValue("responseErrorKeyName", StringConfigEntry.class),
|
||||||
VerifyHelper.NOT_EMPTY, "Response error key can't be empty");
|
VerifyHelper.NOT_EMPTY, "Response error key can't be empty");
|
||||||
|
responseOKKeyName = VerifyHelper.verify(block.getEntryValue("responseOKKeyName", StringConfigEntry.class),
|
||||||
|
VerifyHelper.NOT_EMPTY, "Response okay key can't be empty");
|
||||||
url = IOHelper.convertToURL(configUrl);
|
url = IOHelper.convertToURL(configUrl);
|
||||||
urlCheckServer = IOHelper.convertToURL(configUrlCheckServer);
|
urlCheckServer = IOHelper.convertToURL(configUrlCheckServer);
|
||||||
urlJoinServer = IOHelper.convertToURL(configUrlJoinServer);
|
urlJoinServer = IOHelper.convertToURL(configUrlJoinServer);
|
||||||
|
@ -61,7 +59,7 @@ protected JsonAuthHandler(BlockConfigEntry block) {
|
||||||
@Override
|
@Override
|
||||||
public UUID auth(AuthProviderResult authResult) throws IOException {
|
public UUID auth(AuthProviderResult authResult) throws IOException {
|
||||||
JsonObject request = Json.object().add(userKeyName, authResult.username).add(accessTokenKeyName, authResult.accessToken);
|
JsonObject request = Json.object().add(userKeyName, authResult.username).add(accessTokenKeyName, authResult.accessToken);
|
||||||
JsonObject result = jsonRequest(request, url);
|
JsonObject result = jsonRequestChecked(request, url);
|
||||||
String value;
|
String value;
|
||||||
if ((value = result.getString(uuidKeyName, null)) != null)
|
if ((value = result.getString(uuidKeyName, null)) != null)
|
||||||
return UUID.fromString(value);
|
return UUID.fromString(value);
|
||||||
|
@ -71,7 +69,7 @@ public UUID auth(AuthProviderResult authResult) throws IOException {
|
||||||
@Override
|
@Override
|
||||||
public UUID checkServer(String username, String serverID) throws IOException {
|
public UUID checkServer(String username, String serverID) throws IOException {
|
||||||
JsonObject request = Json.object().add(userKeyName, username).add(serverIDKeyName, serverID);
|
JsonObject request = Json.object().add(userKeyName, username).add(serverIDKeyName, serverID);
|
||||||
JsonObject result = jsonRequest(request, urlCheckServer);
|
JsonObject result = jsonRequestChecked(request, urlCheckServer);
|
||||||
String value;
|
String value;
|
||||||
if ((value = result.getString(uuidKeyName, null)) != null)
|
if ((value = result.getString(uuidKeyName, null)) != null)
|
||||||
return UUID.fromString(value);
|
return UUID.fromString(value);
|
||||||
|
@ -85,14 +83,14 @@ public void close() {
|
||||||
@Override
|
@Override
|
||||||
public boolean joinServer(String username, String accessToken, String serverID) throws IOException {
|
public boolean joinServer(String username, String accessToken, String serverID) throws IOException {
|
||||||
JsonObject request = Json.object().add(userKeyName, username).add(serverIDKeyName, serverID).add(accessTokenKeyName, accessToken);
|
JsonObject request = Json.object().add(userKeyName, username).add(serverIDKeyName, serverID).add(accessTokenKeyName, accessToken);
|
||||||
jsonRequest(request, urlJoinServer);
|
HTTPRequest.jsonRequest(request, urlJoinServer);
|
||||||
return request.getString(responseErrorKeyName,null).equals("OK");
|
return request.getString(responseOKKeyName,null).equals("OK");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UUID usernameToUUID(String username) throws IOException {
|
public UUID usernameToUUID(String username) throws IOException {
|
||||||
JsonObject request = Json.object().add(userKeyName, username);
|
JsonObject request = Json.object().add(userKeyName, username);
|
||||||
JsonObject result = jsonRequest(request, urlUsernameToUUID);
|
JsonObject result = jsonRequestChecked(request, urlUsernameToUUID);
|
||||||
String value;
|
String value;
|
||||||
if ((value = result.getString(uuidKeyName, null)) != null)
|
if ((value = result.getString(uuidKeyName, null)) != null)
|
||||||
return UUID.fromString(value);
|
return UUID.fromString(value);
|
||||||
|
@ -102,44 +100,23 @@ public UUID usernameToUUID(String username) throws IOException {
|
||||||
@Override
|
@Override
|
||||||
public String uuidToUsername(UUID uuid) throws IOException {
|
public String uuidToUsername(UUID uuid) throws IOException {
|
||||||
JsonObject request = Json.object().add(uuidKeyName, uuid.toString());
|
JsonObject request = Json.object().add(uuidKeyName, uuid.toString());
|
||||||
JsonObject result = jsonRequest(request, urlUUIDToUsername);
|
JsonObject result = jsonRequestChecked(request, urlUUIDToUsername);
|
||||||
String value;
|
String value;
|
||||||
if ((value = result.getString(userKeyName, null)) != null)
|
if ((value = result.getString(userKeyName, null)) != null)
|
||||||
return value;
|
return value;
|
||||||
throw new IOException("Service error");
|
throw new IOException("Service error");
|
||||||
}
|
}
|
||||||
|
|
||||||
public JsonObject jsonRequest(JsonObject request, URL url) throws IOException {
|
public JsonObject jsonRequestChecked(JsonObject object,URL url) throws IOException {
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
JsonValue result = HTTPRequest.jsonRequest(object,url);
|
||||||
connection.setDoInput(true);
|
if (!result.isObject())
|
||||||
connection.setDoOutput(true);
|
authError("Authentication server response is malformed");
|
||||||
connection.setRequestMethod("POST");
|
|
||||||
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
|
||||||
connection.setRequestProperty("Accept", "application/json");
|
|
||||||
if (TIMEOUT > 0)
|
|
||||||
connection.setConnectTimeout(TIMEOUT);
|
|
||||||
|
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), Charset.forName("UTF-8"));
|
JsonObject response = result.asObject();
|
||||||
writer.write(request.toString());
|
|
||||||
writer.flush();
|
|
||||||
writer.close();
|
|
||||||
|
|
||||||
InputStreamReader reader;
|
|
||||||
int statusCode = connection.getResponseCode();
|
|
||||||
|
|
||||||
if (200 <= statusCode && statusCode < 300)
|
|
||||||
reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
|
|
||||||
else
|
|
||||||
reader = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
|
|
||||||
JsonValue content = Json.parse(reader);
|
|
||||||
if (!content.isObject())
|
|
||||||
authError("Authentication server response is malformed");
|
|
||||||
|
|
||||||
JsonObject response = content.asObject();
|
|
||||||
String value;
|
String value;
|
||||||
|
|
||||||
if ((value = response.getString(responseErrorKeyName, null)) != null)
|
if ((value = response.getString(responseErrorKeyName, null)) != null)
|
||||||
authError(value);
|
authError(value);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
import com.eclipsesource.json.JsonObject;
|
import com.eclipsesource.json.JsonObject;
|
||||||
import com.eclipsesource.json.JsonValue;
|
import com.eclipsesource.json.JsonValue;
|
||||||
|
|
||||||
|
import ru.gravit.utils.HTTPRequest;
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
import ru.gravit.utils.helper.IOHelper;
|
||||||
import ru.gravit.utils.helper.SecurityHelper;
|
import ru.gravit.utils.helper.SecurityHelper;
|
||||||
import ru.gravit.utils.helper.VerifyHelper;
|
import ru.gravit.utils.helper.VerifyHelper;
|
||||||
|
@ -48,28 +49,7 @@ public final class JsonAuthProvider extends AuthProvider {
|
||||||
@Override
|
@Override
|
||||||
public AuthProviderResult auth(String login, String password, String ip) throws IOException {
|
public AuthProviderResult auth(String login, String password, String ip) throws IOException {
|
||||||
JsonObject request = Json.object().add(userKeyName, login).add(passKeyName, password).add(ipKeyName, ip);
|
JsonObject request = Json.object().add(userKeyName, login).add(passKeyName, password).add(ipKeyName, ip);
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
JsonValue content = HTTPRequest.jsonRequest(request,url);
|
||||||
connection.setDoInput(true);
|
|
||||||
connection.setDoOutput(true);
|
|
||||||
connection.setRequestMethod("POST");
|
|
||||||
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
|
||||||
connection.setRequestProperty("Accept", "application/json");
|
|
||||||
if (TIMEOUT > 0)
|
|
||||||
connection.setConnectTimeout(TIMEOUT);
|
|
||||||
|
|
||||||
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), Charset.forName("UTF-8"));
|
|
||||||
writer.write(request.toString());
|
|
||||||
writer.flush();
|
|
||||||
writer.close();
|
|
||||||
|
|
||||||
InputStreamReader reader;
|
|
||||||
int statusCode = connection.getResponseCode();
|
|
||||||
|
|
||||||
if (200 <= statusCode && statusCode < 300)
|
|
||||||
reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
|
|
||||||
else
|
|
||||||
reader = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
|
|
||||||
JsonValue content = Json.parse(reader);
|
|
||||||
if (!content.isObject())
|
if (!content.isObject())
|
||||||
return authError("Authentication server response is malformed");
|
return authError("Authentication server response is malformed");
|
||||||
|
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
package ru.gravit.launcher;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
|
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
|
||||||
|
|
||||||
public class HTTPRequest {
|
|
||||||
public static int sendCrashreport(String strurl, byte[] data) throws IOException {
|
|
||||||
URL url = new URL(strurl);
|
|
||||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
||||||
connection.setRequestMethod("POST");
|
|
||||||
connection.setRequestProperty("Content-Type",
|
|
||||||
"application/x-www-form-urlencoded");
|
|
||||||
connection.setRequestProperty("Content-Length",
|
|
||||||
Integer.toString(data.length));
|
|
||||||
connection.setRequestProperty("Content-Language", "en-US");
|
|
||||||
OutputStream outputStream = connection.getOutputStream();
|
|
||||||
outputStream.write(data);
|
|
||||||
outputStream.close();
|
|
||||||
return connection.getResponseCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int sendCrashreport(String strurl, String data) throws IOException {
|
|
||||||
return sendCrashreport(strurl, data.getBytes(IOHelper.UNICODE_CHARSET));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -26,6 +26,7 @@
|
||||||
import ru.gravit.launcher.hasher.HashedDir;
|
import ru.gravit.launcher.hasher.HashedDir;
|
||||||
import ru.gravit.launcher.hasher.HashedEntry;
|
import ru.gravit.launcher.hasher.HashedEntry;
|
||||||
import ru.gravit.launcher.hasher.HashedFile;
|
import ru.gravit.launcher.hasher.HashedFile;
|
||||||
|
import ru.gravit.utils.HTTPRequest;
|
||||||
import ru.gravit.utils.helper.CommonHelper;
|
import ru.gravit.utils.helper.CommonHelper;
|
||||||
import ru.gravit.utils.helper.EnvHelper;
|
import ru.gravit.utils.helper.EnvHelper;
|
||||||
import ru.gravit.utils.helper.IOHelper;
|
import ru.gravit.utils.helper.IOHelper;
|
||||||
|
|
64
libLauncher/src/main/java/ru/gravit/utils/HTTPRequest.java
Normal file
64
libLauncher/src/main/java/ru/gravit/utils/HTTPRequest.java
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
package ru.gravit.utils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import com.eclipsesource.json.Json;
|
||||||
|
import com.eclipsesource.json.JsonObject;
|
||||||
|
import com.eclipsesource.json.JsonValue;
|
||||||
|
import ru.gravit.utils.helper.IOHelper;
|
||||||
|
|
||||||
|
public class HTTPRequest {
|
||||||
|
private static final int TIMEOUT = 10;
|
||||||
|
|
||||||
|
public static int sendCrashreport(String strurl, byte[] data) throws IOException {
|
||||||
|
URL url = new URL(strurl);
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.setRequestProperty("Content-Type",
|
||||||
|
"application/x-www-form-urlencoded");
|
||||||
|
connection.setRequestProperty("Content-Length",
|
||||||
|
Integer.toString(data.length));
|
||||||
|
connection.setRequestProperty("Content-Language", "en-US");
|
||||||
|
OutputStream outputStream = connection.getOutputStream();
|
||||||
|
outputStream.write(data);
|
||||||
|
outputStream.close();
|
||||||
|
return connection.getResponseCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int sendCrashreport(String strurl, String data) throws IOException {
|
||||||
|
return sendCrashreport(strurl, data.getBytes(IOHelper.UNICODE_CHARSET));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static JsonValue jsonRequest(JsonObject request, URL url) throws IOException {
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setDoInput(true);
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
||||||
|
connection.setRequestProperty("Accept", "application/json");
|
||||||
|
if (TIMEOUT > 0)
|
||||||
|
connection.setConnectTimeout(TIMEOUT);
|
||||||
|
|
||||||
|
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), Charset.forName("UTF-8"));
|
||||||
|
writer.write(request.toString());
|
||||||
|
writer.flush();
|
||||||
|
writer.close();
|
||||||
|
|
||||||
|
InputStreamReader reader;
|
||||||
|
int statusCode = connection.getResponseCode();
|
||||||
|
|
||||||
|
if (200 <= statusCode && statusCode < 300)
|
||||||
|
reader = new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8);
|
||||||
|
else
|
||||||
|
reader = new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8);
|
||||||
|
JsonValue content = Json.parse(reader);
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue