|
16 | 16 | */ |
17 | 17 | package com.devparada.logic; |
18 | 18 |
|
| 19 | +import com.devparada.model.StatusMCModel; |
19 | 20 | import com.google.gson.JsonElement; |
20 | 21 | import com.google.gson.JsonObject; |
21 | 22 | import com.google.gson.JsonParser; |
22 | 23 | import com.google.gson.JsonSyntaxException; |
23 | | -import java.io.BufferedReader; |
24 | 24 | import java.io.IOException; |
25 | | -import java.io.InputStreamReader; |
26 | | -import java.net.HttpURLConnection; |
27 | | -import java.net.URL; |
| 25 | +import java.net.URI; |
| 26 | +import java.net.http.HttpClient; |
| 27 | +import java.net.http.HttpRequest; |
| 28 | +import java.net.http.HttpResponse; |
28 | 29 | import javax.swing.JOptionPane; |
29 | 30 |
|
30 | 31 | /** |
|
33 | 34 | */ |
34 | 35 | public class StatusMCServer { |
35 | 36 |
|
36 | | - public String host; |
37 | | - public int port; |
| 37 | + private final StatusMCModel statusMC; |
| 38 | + private final String ipServer; |
| 39 | + /** |
| 40 | + * The value is OK in HTTPConnection |
| 41 | + */ |
| 42 | + public static final int HTTP_OK = 200; |
38 | 43 |
|
39 | | - public StatusMCServer(String host, int port) { |
40 | | - this.host = host; |
41 | | - this.port = port; |
| 44 | + public StatusMCServer(StatusMCModel statusMC) { |
| 45 | + this.statusMC = statusMC; |
| 46 | + ipServer = this.statusMC.getHost() + ":" + this.statusMC.getPort(); |
42 | 47 | } |
43 | 48 |
|
44 | | - public JsonObject fetchData(String ipServer) { |
| 49 | + private JsonObject fetchData() { |
45 | 50 | if (!ipServer.isEmpty()) { |
46 | | - String urlJSON = "https://api.mcstatus.io/v2/status/java/" + ipServer; // URL of the remote JSON |
| 51 | + String url = "https://api.mcstatus.io/v2/status/java/" + ipServer; // URL of the remote JSON |
47 | 52 | try { |
48 | | - URL url = new URL(urlJSON); |
49 | | - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
50 | | - conn.setRequestMethod("GET"); |
51 | | - conn.setRequestProperty("Accept", "application/json"); |
| 53 | + HttpClient client = HttpClient.newHttpClient(); |
52 | 54 |
|
53 | | - if (conn.getResponseCode() == 200) { |
54 | | - BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); |
55 | | - StringBuilder sb = new StringBuilder(); |
56 | | - String output; |
57 | | - while ((output = br.readLine()) != null) { |
58 | | - sb.append(output); |
59 | | - } |
| 55 | + HttpRequest request = HttpRequest.newBuilder() |
| 56 | + .uri(URI.create(url)) |
| 57 | + .header("Accept", "application/json") |
| 58 | + .build(); |
60 | 59 |
|
61 | | - conn.disconnect(); |
| 60 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
62 | 61 |
|
63 | | - JsonObject jsonReceived = JsonParser.parseString(sb.toString()).getAsJsonObject(); |
| 62 | + if (response.statusCode() == HTTP_OK) { |
| 63 | + // Parse the JSON response body |
| 64 | + JsonObject jsonReceived = JsonParser.parseString(response.body()).getAsJsonObject(); |
64 | 65 | return jsonReceived; |
| 66 | + } else { |
| 67 | + JOptionPane.showMessageDialog(null, "Not data", "An error occurred", JOptionPane.ERROR_MESSAGE); |
65 | 68 | } |
66 | | - } catch (JsonSyntaxException | IOException e) { |
| 69 | + } catch (JsonSyntaxException | IOException | InterruptedException e) { |
67 | 70 | JOptionPane.showMessageDialog(null, e, "An error occurred", JOptionPane.WARNING_MESSAGE); |
68 | 71 | } |
69 | 72 | } |
70 | 73 | return null; |
71 | 74 | } |
72 | 75 |
|
73 | | - public JsonObject obtainDataJSON(String ipServer) { |
74 | | - return fetchData(ipServer); |
75 | | - } |
76 | | - |
77 | | - public String obtainData(String ipServer) { |
78 | | - return showData(fetchData(ipServer)); |
79 | | - } |
80 | | - |
81 | | - public String showData(JsonObject jsonReceived) { |
82 | | - String textResult = ""; |
83 | | - |
84 | | - if (jsonReceived != null) { |
85 | | - for (String key : jsonReceived.keySet()) { |
86 | | - JsonElement value = jsonReceived.get(key); |
87 | | - |
88 | | - if ("online".equals(key)) { |
89 | | - String statusServerOnline = "offline"; |
90 | | - |
91 | | - if ("true".equals(value.getAsString())) { |
92 | | - statusServerOnline = "online"; |
93 | | - } |
94 | | - textResult = "The server is " + statusServerOnline + "\n"; |
95 | | - } |
96 | | - |
97 | | - if ("version".equals(key)) { |
98 | | - String nameClean = value.getAsJsonObject().get("name_clean").getAsString(); |
99 | | - textResult += "Server version " + nameClean + "\n"; |
100 | | - } |
101 | | - |
102 | | - if ("players".equals(key)) { |
103 | | - String playersNumber = value.getAsJsonObject().get("online").getAsString(); |
104 | | - textResult += "Players playing " + playersNumber + "\n"; |
105 | | - } |
106 | | - |
107 | | - } |
108 | | - } |
109 | | - return textResult; |
110 | | - } |
111 | | - |
112 | 76 | public String showDataSection(String ipServer, String section) { |
113 | | - JsonObject jsonReceived = obtainDataJSON(ipServer); |
| 77 | + JsonObject jsonReceived = fetchData(); |
114 | 78 | String textResult = "N/A"; |
115 | 79 |
|
116 | 80 | if (jsonReceived != null) { |
117 | 81 | for (String key : jsonReceived.keySet()) { |
118 | | - JsonElement value = jsonReceived.get(key); |
119 | | - |
120 | | - switch (section) { |
121 | | - case "online" -> { |
122 | | - String statusServerOnline; |
123 | | - if (section.equals(key)) { |
| 82 | + if (section.equals(key)) { |
| 83 | + JsonElement value = jsonReceived.get(key); |
| 84 | + switch (section) { |
| 85 | + case "online" -> { |
| 86 | + String statusServerOnline; |
124 | 87 | statusServerOnline = "Offline"; |
125 | 88 | if ("true".equals(value.getAsString())) { |
126 | 89 | statusServerOnline = "Online"; |
127 | 90 | } |
128 | 91 | textResult = statusServerOnline; |
129 | 92 | } |
130 | | - } |
131 | | - case "version" -> { |
132 | | - if (section.equals(key)) { |
| 93 | + case "version" -> { |
133 | 94 | String nameClean = value.getAsJsonObject().get("name_clean").getAsString(); |
134 | | - textResult = nameClean; |
| 95 | + String versionName = nameClean; |
| 96 | + |
| 97 | + if (nameClean.isEmpty()) { |
| 98 | + versionName = "Unknown"; |
| 99 | + } |
| 100 | + textResult = "Version: " + versionName; |
135 | 101 | } |
136 | | - } |
137 | | - case "players" -> { |
138 | | - if (section.equals(key)) { |
| 102 | + case "players" -> { |
139 | 103 | String playersNumber = value.getAsJsonObject().get("online").getAsString(); |
140 | 104 | String maxPlayers = value.getAsJsonObject().get("max").getAsString(); |
141 | 105 | textResult = playersNumber + "/" + maxPlayers + " players"; |
142 | 106 | } |
143 | | - } |
144 | | - case "icon" -> { |
145 | | - if (section.equals(key)) { |
| 107 | + case "icon" -> { |
146 | 108 | String icon = value.getAsString(); |
147 | 109 |
|
148 | 110 | if (icon.contains(",")) { |
|
0 commit comments