re-write with okhttp
This commit is contained in:
parent
45161953db
commit
57b0e73768
@ -67,5 +67,13 @@
|
|||||||
<version>6.4.2</version>
|
<version>6.4.2</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- OkHttp -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.squareup.okhttp3</groupId>
|
||||||
|
<artifactId>okhttp</artifactId>
|
||||||
|
<version>4.11.0</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
@ -8,16 +8,14 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
import lombok.ToString;
|
import lombok.ToString;
|
||||||
|
import okhttp3.*;
|
||||||
import oshi.SystemInfo;
|
import oshi.SystemInfo;
|
||||||
import oshi.hardware.CentralProcessor;
|
import oshi.hardware.CentralProcessor;
|
||||||
import oshi.hardware.ComputerSystem;
|
import oshi.hardware.ComputerSystem;
|
||||||
import oshi.hardware.HardwareAbstractionLayer;
|
import oshi.hardware.HardwareAbstractionLayer;
|
||||||
import oshi.software.os.OperatingSystem;
|
import oshi.software.os.OperatingSystem;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.IOException;
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -30,7 +28,6 @@ import java.util.Map;
|
|||||||
*
|
*
|
||||||
* @author Braydon
|
* @author Braydon
|
||||||
* @see <a href="https://git.rainnny.club/Rainnny/LicenseServer">License Server</a>
|
* @see <a href="https://git.rainnny.club/Rainnny/LicenseServer">License Server</a>
|
||||||
* TODO: Convert to okhttp?
|
|
||||||
*/
|
*/
|
||||||
public final class LicenseExample {
|
public final class LicenseExample {
|
||||||
/**
|
/**
|
||||||
@ -56,88 +53,67 @@ public final class LicenseExample {
|
|||||||
*/
|
*/
|
||||||
@NonNull
|
@NonNull
|
||||||
public static LicenseResponse check(@NonNull String key, @NonNull String product) {
|
public static LicenseResponse check(@NonNull String key, @NonNull String product) {
|
||||||
String hardwareId = getHardwareId(); // Get the machine's hardware id
|
String hardwareId = getHardwareId(); // Get the hardware id of the machine
|
||||||
|
|
||||||
// Build the body
|
// Build the json body
|
||||||
Map<String, Object> body = new HashMap<>();
|
Map<String, Object> body = new HashMap<>();
|
||||||
body.put("key", key);
|
body.put("key", key);
|
||||||
body.put("product", product);
|
body.put("product", product);
|
||||||
body.put("hwid", hardwareId);
|
body.put("hwid", hardwareId);
|
||||||
String bodyJson = GSON.toJson(body); // The json body
|
String bodyJson = GSON.toJson(body); // The json body
|
||||||
|
|
||||||
HttpURLConnection connection = null;
|
OkHttpClient client = new OkHttpClient(); // Create a new http client
|
||||||
int responseCode = -1; // The response code
|
MediaType mediaType = MediaType.parse("application/json"); // Ensure the media type is json
|
||||||
try {
|
RequestBody requestBody = RequestBody.create(bodyJson, mediaType); // Build the request body
|
||||||
// Try and send the request to the server
|
Request request = new Request.Builder()
|
||||||
connection = (HttpURLConnection) new URL(CHECK_ENDPOINT).openConnection();
|
.url(CHECK_ENDPOINT)
|
||||||
connection.setRequestMethod("POST"); // Sending a POST request
|
.post(requestBody)
|
||||||
connection.setRequestProperty("Content-Type", "application/json"); // We want JSON as the response
|
.build(); // Build the POST request
|
||||||
connection.setDoOutput(true); // We want to send a body
|
|
||||||
|
Response response = null; // The response of the request
|
||||||
|
int responseCode = -1; // The response code of the request
|
||||||
|
try { // Attempt to execute the request
|
||||||
|
response = client.newCall(request).execute();
|
||||||
|
responseCode = response.code();
|
||||||
|
|
||||||
// Write the body to the connection
|
// If the response is successful, we can parse the response
|
||||||
try (OutputStream outputStream = connection.getOutputStream()) {
|
if (response.isSuccessful()) {
|
||||||
byte[] input = bodyJson.getBytes(StandardCharsets.UTF_8);
|
ResponseBody responseBody = response.body();
|
||||||
outputStream.write(input, 0, input.length);
|
assert responseBody != null; // We don't want the response body being null
|
||||||
}
|
|
||||||
responseCode = connection.getResponseCode(); // Get the response code
|
JsonObject json = GSON.fromJson(responseBody.string(), JsonObject.class); // Parse the json
|
||||||
|
JsonElement description = json.get("description");
|
||||||
// If the response code is OK, we can read the response
|
JsonElement ownerSnowflake = json.get("ownerSnowflake");
|
||||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
JsonElement ownerName = json.get("ownerName");
|
||||||
try (InputStream inputStream = connection.getInputStream();
|
JsonElement duration = json.get("duration");
|
||||||
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
|
|
||||||
BufferedReader reader = new BufferedReader(inputStreamReader)
|
// Return the license response
|
||||||
) {
|
return new LicenseResponse(200, null,
|
||||||
// Read the response
|
description.isJsonNull() ? null : description.getAsString(),
|
||||||
StringBuilder response = new StringBuilder();
|
ownerSnowflake.isJsonNull() ? -1 : ownerSnowflake.getAsLong(),
|
||||||
String line;
|
ownerName.isJsonNull() ? null : ownerName.getAsString(),
|
||||||
while ((line = reader.readLine()) != null) {
|
duration.isJsonNull() ? -1 : duration.getAsLong()
|
||||||
response.append(line);
|
);
|
||||||
}
|
} else {
|
||||||
// Parse the response as JSON
|
ResponseBody errorBody = response.body(); // Get the error body
|
||||||
JsonObject json = GSON.fromJson(response.toString(), JsonObject.class);
|
if (errorBody != null) { // If we have an error body, we can parse it
|
||||||
JsonElement description = json.get("description");
|
String errorResponse = errorBody.string();
|
||||||
JsonElement ownerSnowflake = json.get("ownerSnowflake");
|
JsonObject jsonError = GSON.fromJson(errorResponse, JsonObject.class);
|
||||||
JsonElement ownerName = json.get("ownerName");
|
JsonElement errorMessage = jsonError.get("error");
|
||||||
JsonElement duration = json.get("duration");
|
if (!errorMessage.isJsonNull()) { // We have an error message, return it
|
||||||
return new LicenseResponse(200, null,
|
return new LicenseResponse(responseCode, errorMessage.getAsString());
|
||||||
description.isJsonNull() ? null : description.getAsString(),
|
|
||||||
ownerSnowflake.isJsonNull() ? -1 : ownerSnowflake.getAsLong(),
|
|
||||||
ownerName.isJsonNull() ? null : ownerName.getAsString(),
|
|
||||||
duration.isJsonNull() ? -1 : duration.getAsLong()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else { // Otherwise, the request failed
|
|
||||||
// Check if the response has an error message in JSON format
|
|
||||||
try (InputStream errorStream = connection.getErrorStream()) {
|
|
||||||
if (errorStream != null) { // Read the error response
|
|
||||||
try (InputStreamReader errorStreamReader = new InputStreamReader(errorStream);
|
|
||||||
BufferedReader errorReader = new BufferedReader(errorStreamReader)
|
|
||||||
) {
|
|
||||||
|
|
||||||
StringBuilder errorResponse = new StringBuilder();
|
|
||||||
String errorLine;
|
|
||||||
while ((errorLine = errorReader.readLine()) != null) {
|
|
||||||
errorResponse.append(errorLine);
|
|
||||||
}
|
|
||||||
// Parse the error response as JSON
|
|
||||||
JsonObject jsonError = GSON.fromJson(errorResponse.toString(), JsonObject.class);
|
|
||||||
JsonElement errorMessage = jsonError.get("error");
|
|
||||||
if (!errorMessage.isJsonNull()) { // If the error message isn't null, we can return it
|
|
||||||
return new LicenseResponse(responseCode, errorMessage.getAsString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
// Close the connection if it's open
|
// Close the response if it's open
|
||||||
if (connection != null) {
|
if (response != null) {
|
||||||
connection.disconnect();
|
response.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Didn't find an error message, return an unknown error
|
// Return an unknown error
|
||||||
return new LicenseResponse(responseCode, "An unknown error occurred");
|
return new LicenseResponse(responseCode, "An unknown error occurred");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user