com.shareif.bungeeCordDynamicServerPool.RestServer.java Source code

Java tutorial

Introduction

Here is the source code for com.shareif.bungeeCordDynamicServerPool.RestServer.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shareif.bungeeCordDynamicServerPool;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.shareif.bungeeCordDynamicServerPool.exception.AlreadyRegisteredServerException;
import com.shareif.bungeeCordDynamicServerPool.exception.ServerNotFoundException;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;

/**
 *
 * @author cambierr
 */
@Path("/cluster")
public class RestServer {

    @GET
    @Path("/time")
    @Produces(MediaType.TEXT_PLAIN)
    public String get() {
        return Integer.toString((int) (System.currentTimeMillis() / 1000));
    }

    @GET
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String get(@HeaderParam("signature") String _signature, @HeaderParam("time") int _time)
            throws IOException, NoSuchAlgorithmException {

        if (!WebServer.getInstance().validateAuthToken(_signature, _time, "/cluster/list", "GET")) {
            throw new WebApplicationException(401);
        }

        Map<String, ServerInfo> cluster = ClusterManager.getInstance().getServers();

        JsonArray output = new JsonArray();
        for (ServerInfo server : cluster.values()) {
            JsonObject s = new JsonObject();
            s.addProperty("name", server.getName());
            s.addProperty("motd", server.getMotd());
            s.addProperty("address", server.getAddress().getAddress().toString());
            s.addProperty("port", server.getAddress().getPort());
            s.addProperty("players", server.getPlayers().size());
            output.add(s);
        }

        return output.getAsString();
    }

    @GET
    @Path("/server/{name}")
    @Produces(MediaType.APPLICATION_JSON)
    public String get(@HeaderParam("signature") String _signature, @HeaderParam("time") int _time,
            @PathParam("name") String _name) throws IOException, NoSuchAlgorithmException {

        if (!WebServer.getInstance().validateAuthToken(_signature, _time, "/cluster/name/" + _name, "GET")) {
            throw new WebApplicationException(401);
        }

        Map<String, ServerInfo> cluster = ClusterManager.getInstance().getServers();

        if (!cluster.containsKey(_name)) {
            throw new WebApplicationException(404);
        }

        ServerInfo server = cluster.get(_name);
        JsonObject s = new JsonObject();
        s.addProperty("name", server.getName());
        s.addProperty("motd", server.getMotd());
        s.addProperty("address", server.getAddress().getAddress().toString());
        s.addProperty("port", server.getAddress().getPort());

        JsonObject players = new JsonObject();

        for (ProxiedPlayer player : server.getPlayers()) {
            players.addProperty(player.getUniqueId().toString(), player.getDisplayName());
        }
        s.add("players", players);

        return s.getAsString();
    }

    @POST
    @Path("/server")
    @Consumes(MediaType.APPLICATION_JSON)
    public void post(@HeaderParam("signature") String _signature, @HeaderParam("time") int _time, String _json)
            throws IOException, NoSuchAlgorithmException {

        if (!WebServer.getInstance().validateAuthToken(_signature, _time, "/cluster/server", "POST")) {
            throw new WebApplicationException(401);
        }

        try {
            JsonElement node = new JsonParser().parse(_json);
            JsonObject json = node.getAsJsonObject();

            if (json.get("name") == null || json.get("port") == null || json.get("address") == null
                    || json.get("motd") == null) {
                throw new WebApplicationException(400);
            }

            String name = json.get("name").getAsString();
            String address = json.get("address").getAsString();
            int port = json.get("port").getAsInt();
            String motd = json.get("motd").getAsString();

            try {
                ClusterManager.getInstance().addServer(name, address, port, motd);
            } catch (AlreadyRegisteredServerException ex) {
                throw new WebApplicationException(400);
            }
        } catch (JsonSyntaxException e) {
            throw new WebApplicationException(400);
        }
    }

    @DELETE
    @Path("/server/{name}")

    @Consumes(MediaType.APPLICATION_JSON)
    public void delete(@HeaderParam("signature") String _signature, @HeaderParam("time") int _time,
            @PathParam("name") String _name, String _json) throws IOException, NoSuchAlgorithmException {

        if (!WebServer.getInstance().validateAuthToken(_signature, _time, "/cluster/server/" + _name, "DELETE")) {
            throw new WebApplicationException(401);
        }

        Map<String, ServerInfo> cluster = ClusterManager.getInstance().getServers();

        if (!cluster.containsKey(_name)) {
            throw new WebApplicationException(404);
        }

        String msg = null, alternative = null;

        if (_json != null) {
            try {
                JsonElement node = new JsonParser().parse(_json);
                JsonObject json = node.getAsJsonObject();

                alternative = (json.get("alternative") != null) ? json.get("alternative").getAsString() : null;
                msg = (json.get("msg") != null) ? json.get("msg").getAsString() : null;

            } catch (JsonSyntaxException e) {
                throw new WebApplicationException(400);
            }
        }

        if (!cluster.containsKey(alternative)) {
            throw new WebApplicationException(404);
        }

        try {
            ClusterManager.getInstance().removeServer(_name, msg, alternative);
        } catch (ServerNotFoundException ex) {
            throw new WebApplicationException(404);
        }

    }
}