|
| 1 | +package pw.chew.jsonrestapi; |
| 2 | + |
| 3 | +import me.clip.placeholderapi.PlaceholderAPI; |
| 4 | +import org.bukkit.Bukkit; |
| 5 | +import org.bukkit.configuration.file.FileConfiguration; |
| 6 | + |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.io.OutputStreamWriter; |
| 11 | +import java.io.PrintWriter; |
| 12 | +import java.net.ServerSocket; |
| 13 | +import java.net.Socket; |
| 14 | +import java.util.Arrays; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Locale; |
| 17 | +import java.util.UUID; |
| 18 | +import java.util.logging.Logger; |
| 19 | + |
| 20 | +public class RestServer extends Thread { |
| 21 | + |
| 22 | + private static FileConfiguration config; |
| 23 | + private static Logger logger; |
| 24 | + |
| 25 | + public RestServer(FileConfiguration config, Logger logger) { |
| 26 | + RestServer.config = config; |
| 27 | + RestServer.logger = logger; |
| 28 | + } |
| 29 | + |
| 30 | + private int getPort() { |
| 31 | + return config.getInt("port"); |
| 32 | + } |
| 33 | + |
| 34 | + public static void updateConfig(FileConfiguration newConfig) { |
| 35 | + config = newConfig; |
| 36 | + } |
| 37 | + |
| 38 | + @SuppressWarnings("InfiniteLoopStatement") |
| 39 | + @Override |
| 40 | + public void run() { |
| 41 | + try { |
| 42 | + ServerSocket socket = new ServerSocket(getPort()); |
| 43 | + while (true) { |
| 44 | + ServerResponder responder = new ServerResponder(socket.accept()); |
| 45 | + responder.setDaemon(true); |
| 46 | + responder.start(); |
| 47 | + } |
| 48 | + } catch (IOException ex) { |
| 49 | + logger.severe("Failed to initialize server socket."); |
| 50 | + ex.printStackTrace(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static class ServerResponder extends Thread { |
| 55 | + private final Socket inSocket; |
| 56 | + private final List<String> acceptedMethods = Arrays.asList("GET", "POST"); |
| 57 | + |
| 58 | + private ServerResponder(Socket inSocket) { |
| 59 | + this.inSocket = inSocket; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void run() { |
| 64 | + try ( |
| 65 | + InputStreamReader inputStreamReader = new InputStreamReader(inSocket.getInputStream()); |
| 66 | + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); |
| 67 | + OutputStreamWriter outputStreamWriter = new OutputStreamWriter(inSocket.getOutputStream()); |
| 68 | + PrintWriter printWriter = new PrintWriter(outputStreamWriter) |
| 69 | + ) { |
| 70 | + String request = bufferedReader.readLine(); |
| 71 | + if (request == null) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + while (true) { |
| 76 | + String ignore = bufferedReader.readLine(); |
| 77 | + if (ignore == null || ignore.length() == 0) { |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + int initialSplit = request.indexOf(' '); |
| 83 | + if (initialSplit == -1) { |
| 84 | + respondBadRequest(printWriter); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + String method = request.substring(0, initialSplit); |
| 89 | + |
| 90 | + if (method.isEmpty()) { |
| 91 | + respondBadRequest(printWriter); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + if (!acceptedMethods.contains(method.toUpperCase(Locale.ROOT))) { |
| 96 | + respondNotAllowed(printWriter); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + int routeSplit = request.indexOf(' ', initialSplit + 1); |
| 101 | + if (routeSplit == -1) { |
| 102 | + respondBadRequest(printWriter); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + String route = request.substring(initialSplit + 1, routeSplit); |
| 107 | + int routeQuerySplit = route.indexOf('?'); |
| 108 | + if (routeQuerySplit != -1) { |
| 109 | + route = route.substring(0, routeQuerySplit); |
| 110 | + } |
| 111 | + |
| 112 | + // Remove initial slash |
| 113 | + route = route.replaceFirst("/", ""); |
| 114 | + |
| 115 | + logger.info("Received request with route: " + route); |
| 116 | + |
| 117 | + // Check if route exists |
| 118 | + if (config.contains("routes." + route)) { |
| 119 | + String key = "routes." + route + "."; |
| 120 | + String configMethod = config.getString(key + "method"); |
| 121 | + |
| 122 | + if (!method.equals(configMethod)) { |
| 123 | + respondNotAllowed(printWriter); |
| 124 | + return; |
| 125 | + } |
| 126 | + |
| 127 | + String response = PlaceholderAPI.setPlaceholders(Bukkit.getOfflinePlayer(UUID.randomUUID()), config.getString(key + "placeholder")); |
| 128 | + |
| 129 | + respondOk(printWriter, response); |
| 130 | + } else { |
| 131 | + respondNotFound(printWriter); |
| 132 | + } |
| 133 | + } catch (IOException e) { |
| 134 | + e.printStackTrace(); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Responds to the HTTP request with a not allowed header. |
| 140 | + * |
| 141 | + * @param writer The {@link PrintWriter} to print the response to. |
| 142 | + */ |
| 143 | + private void respondNotAllowed(PrintWriter writer) { |
| 144 | + writer.printf("HTTP/1.2 405 Not Allowed%n%n"); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Responds to the HTTP request with a bad request header. |
| 149 | + * |
| 150 | + * @param writer The {@link PrintWriter} to print the response to. |
| 151 | + */ |
| 152 | + private void respondBadRequest(PrintWriter writer) { |
| 153 | + writer.printf("HTTP/1.2 400 Bad Request%n%n"); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Responds to the HTTP request with a not found header. |
| 158 | + * |
| 159 | + * @param writer The {@link PrintWriter} to print the response to. |
| 160 | + */ |
| 161 | + private void respondNotFound(PrintWriter writer) { |
| 162 | + writer.printf("HTTP/1.2 404 Not Found%n%n"); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Responds to the HTTP request with an OK header. |
| 167 | + * |
| 168 | + * @param writer The {@link PrintWriter} to print the response to. |
| 169 | + */ |
| 170 | + private void respondOk(PrintWriter writer, String response) { |
| 171 | + writer.printf("HTTP/1.2 200 OK%n%n" + response); |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments