|
| 1 | +package org.codegame.client; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.net.HttpURLConnection; |
| 6 | +import java.net.URL; |
| 7 | +import java.util.Arrays; |
| 8 | + |
| 9 | +import javax.net.ssl.HttpsURLConnection; |
| 10 | + |
| 11 | +import com.google.gson.FieldNamingPolicy; |
| 12 | +import com.google.gson.Gson; |
| 13 | +import com.google.gson.GsonBuilder; |
| 14 | + |
| 15 | +public class Api { |
| 16 | + private String url; |
| 17 | + private boolean tls; |
| 18 | + private String baseURL; |
| 19 | + |
| 20 | + private static Gson json = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) |
| 21 | + .create(); |
| 22 | + |
| 23 | + public Api(String url) { |
| 24 | + this.url = trimURL(url); |
| 25 | + this.tls = isTLS(this.url); |
| 26 | + this.baseURL = Api.baseURL("http", this.tls, this.url); |
| 27 | + HttpURLConnection.setFollowRedirects(true); |
| 28 | + } |
| 29 | + |
| 30 | + public GameInfo fetchInfo() throws IOException { |
| 31 | + URL obj = new URL(this.baseURL + "/api/info"); |
| 32 | + HttpURLConnection con = (HttpURLConnection) obj.openConnection(); |
| 33 | + con.setRequestProperty("Accept", "application/json"); |
| 34 | + InputStreamReader reader = new InputStreamReader(con.getInputStream()); |
| 35 | + int responseCode = con.getResponseCode(); |
| 36 | + if (responseCode != 200) |
| 37 | + throw new IOException("Failed to fetch game info: unexpected response code: " + responseCode); |
| 38 | + GameInfo info = json.fromJson(reader, GameInfo.class); |
| 39 | + return info; |
| 40 | + } |
| 41 | + |
| 42 | + static String trimURL(String url) { |
| 43 | + if (url.endsWith("/")) |
| 44 | + url = url.substring(0, url.length() - 1); |
| 45 | + String[] parts = url.split("://"); |
| 46 | + if (parts.length < 2) { |
| 47 | + return url; |
| 48 | + } |
| 49 | + return String.join("://", Arrays.copyOfRange(parts, 1, parts.length)); |
| 50 | + } |
| 51 | + |
| 52 | + static String baseURL(String protocol, boolean tls, String trimmedURL) { |
| 53 | + if (tls) |
| 54 | + return protocol + "s://" + trimmedURL; |
| 55 | + return protocol + "://" + trimmedURL; |
| 56 | + } |
| 57 | + |
| 58 | + static boolean isTLS(String trimmedURL) { |
| 59 | + try { |
| 60 | + URL url = new URL(baseURL("http", true, trimmedURL) + "/api/info"); |
| 61 | + HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); |
| 62 | + connection.getInputStream(); |
| 63 | + if (connection.getSSLSession().isEmpty()) |
| 64 | + return false; |
| 65 | + return connection.getSSLSession().get().isValid(); |
| 66 | + } catch (IOException e) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public String getURL() { |
| 72 | + return url; |
| 73 | + } |
| 74 | + |
| 75 | + public String getBaseURL() { |
| 76 | + return baseURL; |
| 77 | + } |
| 78 | + |
| 79 | + public boolean isTLS() { |
| 80 | + return tls; |
| 81 | + } |
| 82 | +} |
0 commit comments