mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-22 16:41:46 +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;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.eclipsesource.json.Json;
|
||||
|
@ -14,6 +9,7 @@
|
|||
import com.eclipsesource.json.JsonValue;
|
||||
|
||||
import ru.gravit.launchserver.auth.provider.AuthProviderResult;
|
||||
import ru.gravit.utils.HTTPRequest;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
import ru.gravit.utils.helper.VerifyHelper;
|
||||
import ru.gravit.launcher.serialize.config.entry.BlockConfigEntry;
|
||||
|
@ -22,7 +18,6 @@
|
|||
@SuppressWarnings("unused")
|
||||
public class JsonAuthHandler extends AuthHandler {
|
||||
|
||||
private static final int TIMEOUT = 10;
|
||||
private final URL url;
|
||||
private final URL urlCheckServer;
|
||||
private final URL urlJoinServer;
|
||||
|
@ -33,6 +28,7 @@ public class JsonAuthHandler extends AuthHandler {
|
|||
private final String accessTokenKeyName;
|
||||
private final String uuidKeyName;
|
||||
private final String responseErrorKeyName;
|
||||
private final String responseOKKeyName;
|
||||
|
||||
protected JsonAuthHandler(BlockConfigEntry block) {
|
||||
super(block);
|
||||
|
@ -51,6 +47,8 @@ protected JsonAuthHandler(BlockConfigEntry block) {
|
|||
VerifyHelper.NOT_EMPTY, "AccessToken key name can't be empty");
|
||||
responseErrorKeyName = VerifyHelper.verify(block.getEntryValue("responseErrorKeyName", StringConfigEntry.class),
|
||||
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);
|
||||
urlCheckServer = IOHelper.convertToURL(configUrlCheckServer);
|
||||
urlJoinServer = IOHelper.convertToURL(configUrlJoinServer);
|
||||
|
@ -61,7 +59,7 @@ protected JsonAuthHandler(BlockConfigEntry block) {
|
|||
@Override
|
||||
public UUID auth(AuthProviderResult authResult) throws IOException {
|
||||
JsonObject request = Json.object().add(userKeyName, authResult.username).add(accessTokenKeyName, authResult.accessToken);
|
||||
JsonObject result = jsonRequest(request, url);
|
||||
JsonObject result = jsonRequestChecked(request, url);
|
||||
String value;
|
||||
if ((value = result.getString(uuidKeyName, null)) != null)
|
||||
return UUID.fromString(value);
|
||||
|
@ -71,7 +69,7 @@ public UUID auth(AuthProviderResult authResult) throws IOException {
|
|||
@Override
|
||||
public UUID checkServer(String username, String serverID) throws IOException {
|
||||
JsonObject request = Json.object().add(userKeyName, username).add(serverIDKeyName, serverID);
|
||||
JsonObject result = jsonRequest(request, urlCheckServer);
|
||||
JsonObject result = jsonRequestChecked(request, urlCheckServer);
|
||||
String value;
|
||||
if ((value = result.getString(uuidKeyName, null)) != null)
|
||||
return UUID.fromString(value);
|
||||
|
@ -85,14 +83,14 @@ public void close() {
|
|||
@Override
|
||||
public boolean joinServer(String username, String accessToken, String serverID) throws IOException {
|
||||
JsonObject request = Json.object().add(userKeyName, username).add(serverIDKeyName, serverID).add(accessTokenKeyName, accessToken);
|
||||
jsonRequest(request, urlJoinServer);
|
||||
return request.getString(responseErrorKeyName,null).equals("OK");
|
||||
HTTPRequest.jsonRequest(request, urlJoinServer);
|
||||
return request.getString(responseOKKeyName,null).equals("OK");
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID usernameToUUID(String username) throws IOException {
|
||||
JsonObject request = Json.object().add(userKeyName, username);
|
||||
JsonObject result = jsonRequest(request, urlUsernameToUUID);
|
||||
JsonObject result = jsonRequestChecked(request, urlUsernameToUUID);
|
||||
String value;
|
||||
if ((value = result.getString(uuidKeyName, null)) != null)
|
||||
return UUID.fromString(value);
|
||||
|
@ -102,44 +100,23 @@ public UUID usernameToUUID(String username) throws IOException {
|
|||
@Override
|
||||
public String uuidToUsername(UUID uuid) throws IOException {
|
||||
JsonObject request = Json.object().add(uuidKeyName, uuid.toString());
|
||||
JsonObject result = jsonRequest(request, urlUUIDToUsername);
|
||||
JsonObject result = jsonRequestChecked(request, urlUUIDToUsername);
|
||||
String value;
|
||||
if ((value = result.getString(userKeyName, null)) != null)
|
||||
return value;
|
||||
throw new IOException("Service error");
|
||||
}
|
||||
|
||||
public JsonObject 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);
|
||||
public JsonObject jsonRequestChecked(JsonObject object,URL url) throws IOException {
|
||||
JsonValue result = HTTPRequest.jsonRequest(object,url);
|
||||
if (!result.isObject())
|
||||
authError("Authentication server response is malformed");
|
||||
|
||||
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())
|
||||
authError("Authentication server response is malformed");
|
||||
|
||||
JsonObject response = content.asObject();
|
||||
JsonObject response = result.asObject();
|
||||
String value;
|
||||
|
||||
if ((value = response.getString(responseErrorKeyName, null)) != null)
|
||||
authError(value);
|
||||
authError(value);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
import com.eclipsesource.json.JsonObject;
|
||||
import com.eclipsesource.json.JsonValue;
|
||||
|
||||
import ru.gravit.utils.HTTPRequest;
|
||||
import ru.gravit.utils.helper.IOHelper;
|
||||
import ru.gravit.utils.helper.SecurityHelper;
|
||||
import ru.gravit.utils.helper.VerifyHelper;
|
||||
|
@ -48,28 +49,7 @@ public final class JsonAuthProvider extends AuthProvider {
|
|||
@Override
|
||||
public AuthProviderResult auth(String login, String password, String ip) throws IOException {
|
||||
JsonObject request = Json.object().add(userKeyName, login).add(passKeyName, password).add(ipKeyName, ip);
|
||||
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);
|
||||
JsonValue content = HTTPRequest.jsonRequest(request,url);
|
||||
if (!content.isObject())
|
||||
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.HashedEntry;
|
||||
import ru.gravit.launcher.hasher.HashedFile;
|
||||
import ru.gravit.utils.HTTPRequest;
|
||||
import ru.gravit.utils.helper.CommonHelper;
|
||||
import ru.gravit.utils.helper.EnvHelper;
|
||||
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