From 82d1f15a9fa3d02b545bd21c06fbb586a211aa13 Mon Sep 17 00:00:00 2001 From: Clayderson Ferreira Date: Sun, 31 Mar 2019 10:35:29 -0300 Subject: [PATCH] =?UTF-8?q?Vers=C3=A3o=201.0.0=20conclu=C3=ADda?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- msdk.iml | 11 -- src/br/com/mineshop/msdk/MSDK.java | 169 ++++++++++++++++++ .../msdk/exceptions/MsdkException.java | 7 + .../msdk/exceptions/WebServiceException.java | 7 + .../mineshop/msdk/webservice/endpoints/.empty | 0 .../webservice/endpoints/v1}/Queue.java | 21 ++- .../spigot/msdk/Endpoints/CheckToken.java | 22 --- .../spigot/msdk/Exceptions/WSException.java | 7 - src/br/com/mineshop/spigot/msdk/MSDK.java | 148 --------------- src/plugin.yml | 7 - 10 files changed, 198 insertions(+), 201 deletions(-) create mode 100644 src/br/com/mineshop/msdk/MSDK.java create mode 100644 src/br/com/mineshop/msdk/exceptions/MsdkException.java create mode 100644 src/br/com/mineshop/msdk/exceptions/WebServiceException.java create mode 100644 src/br/com/mineshop/msdk/webservice/endpoints/.empty rename src/br/com/mineshop/{spigot/msdk/Endpoints => msdk/webservice/endpoints/v1}/Queue.java (62%) delete mode 100644 src/br/com/mineshop/spigot/msdk/Endpoints/CheckToken.java delete mode 100644 src/br/com/mineshop/spigot/msdk/Exceptions/WSException.java delete mode 100644 src/br/com/mineshop/spigot/msdk/MSDK.java delete mode 100644 src/plugin.yml diff --git a/msdk.iml b/msdk.iml index a65392b..fca66ae 100644 --- a/msdk.iml +++ b/msdk.iml @@ -7,17 +7,6 @@ - - - - - - - - - - - diff --git a/src/br/com/mineshop/msdk/MSDK.java b/src/br/com/mineshop/msdk/MSDK.java new file mode 100644 index 0000000..c228ee9 --- /dev/null +++ b/src/br/com/mineshop/msdk/MSDK.java @@ -0,0 +1,169 @@ +package br.com.mineshop.msdk; + +import br.com.mineshop.msdk.webservice.endpoints.v1.Queue; +import br.com.mineshop.msdk.exceptions.MsdkException; +import br.com.mineshop.msdk.exceptions.WebServiceException; +import com.google.gson.Gson; + +import javax.net.ssl.HttpsURLConnection; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.URL; +import java.util.logging.Level; +import java.util.logging.Logger; + +public class MSDK { + public final String SDK_VERSION = "1.0.0"; + public final String API_ADDR = "https://api.mineshop.com.br/plugins"; + + private int connectTimeout = 1500; + private int readTimeout = 3000; + private String authorization; + + public void setConnectTimeout(int ms) { + this.connectTimeout = ms; + } + + public void setReadTimeout(int ms) { + this.readTimeout = ms; + } + + public void setCredentials(String authorization) { + this.authorization = authorization; + } + + public Queue[] getQueue() throws WebServiceException, MsdkException { + return this.getQueue(""); + } + + public Queue[] getQueue(String nickname) throws WebServiceException, MsdkException { + String response = this.get(String.format("/v1/queue/%s", nickname)); + return new Gson().fromJson(response, Queue[].class); + } + + public void hasBeenDelivered(String nickname, String queueItemUuid) throws MsdkException, WebServiceException { + this.update(String.format("/v1/queue/%s/%s", nickname, queueItemUuid)); + } + + private String get(String endpoint) throws WebServiceException, MsdkException { + HttpsURLConnection c = null; + int statusCode = 0; + + try { + URL u = new URL(this.API_ADDR + endpoint); + + c = (HttpsURLConnection) u.openConnection(); + c.setRequestMethod("GET"); + c.setRequestProperty("Authorization", this.authorization); + c.setRequestProperty("Content-Type", "application/json"); + c.setUseCaches(false); + c.setAllowUserInteraction(false); + c.setConnectTimeout(this.connectTimeout); + c.setReadTimeout(this.readTimeout); + c.connect(); + + statusCode = c.getResponseCode(); + + if (statusCode == 200 || statusCode == 201 || statusCode == 204) { + String line; + BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); + StringBuilder sb = new StringBuilder(); + + while ((line = br.readLine()) != null) { + sb.append(line).append("\n"); + } + + br.close(); + + return sb.toString(); + } + } catch (IOException ex) { + Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); + } finally { + if (c != null) { + try { + c.disconnect(); + } catch (Exception ex) { + Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); + } + } + } + + this.exceptionsByStatusCode(statusCode); + return null; + } + + private void update(String endpoint) throws WebServiceException, MsdkException { + HttpsURLConnection c = null; + int statusCode = 0; + + try { + URL u = new URL(this.API_ADDR + endpoint); + + c = (HttpsURLConnection) u.openConnection(); + c.setRequestMethod("PUT"); + c.setRequestProperty("Authorization", this.authorization); + c.setRequestProperty("Content-Type", "application/json"); + c.setDoOutput(true); + c.setUseCaches(false); + c.setAllowUserInteraction(false); + c.setConnectTimeout(this.connectTimeout); + c.setReadTimeout(this.readTimeout); + c.connect(); + + OutputStreamWriter osw = new OutputStreamWriter(c.getOutputStream()); + + osw.write("{}"); + osw.flush(); + osw.close(); + + statusCode = c.getResponseCode(); + + if (statusCode == 200 || statusCode == 201 || statusCode == 204) { + return; + } + } catch (IOException ex) { + Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); + } finally { + if (c != null) { + try { + c.disconnect(); + } catch (Exception ex) { + Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); + } + } + } + + this.exceptionsByStatusCode(statusCode); + } + + private void exceptionsByStatusCode(int statusCode) throws MsdkException, WebServiceException { + if (statusCode == 0) { + throw new MsdkException(String.format( + "[%s] Servidor sem conexão com a internet", + Integer.toString(statusCode) + )); + } + + if (statusCode == 401) { + throw new MsdkException(String.format( + "[%s] Conexão não autorizada! Por favor, verifique as credenciais do seu servidor...", + Integer.toString(statusCode) + )); + } + + if (statusCode >= 500) { + throw new WebServiceException(String.format( + "[%s] Os servidores WebService do MS2 comportaram-se de maneira inesperada", + Integer.toString(statusCode) + )); + } + + throw new MsdkException(String.format( + "[%s] Provável falha causada por entrada de dados incompatíveis com o endpoint requisitado", + Integer.toString(statusCode) + )); + } +} diff --git a/src/br/com/mineshop/msdk/exceptions/MsdkException.java b/src/br/com/mineshop/msdk/exceptions/MsdkException.java new file mode 100644 index 0000000..46e41b9 --- /dev/null +++ b/src/br/com/mineshop/msdk/exceptions/MsdkException.java @@ -0,0 +1,7 @@ +package br.com.mineshop.msdk.exceptions; + +public class MsdkException extends Exception { + public MsdkException(String message) { + super(message); + } +} diff --git a/src/br/com/mineshop/msdk/exceptions/WebServiceException.java b/src/br/com/mineshop/msdk/exceptions/WebServiceException.java new file mode 100644 index 0000000..3fda311 --- /dev/null +++ b/src/br/com/mineshop/msdk/exceptions/WebServiceException.java @@ -0,0 +1,7 @@ +package br.com.mineshop.msdk.exceptions; + +public class WebServiceException extends Exception { + public WebServiceException(String message) { + super(message); + } +} diff --git a/src/br/com/mineshop/msdk/webservice/endpoints/.empty b/src/br/com/mineshop/msdk/webservice/endpoints/.empty new file mode 100644 index 0000000..e69de29 diff --git a/src/br/com/mineshop/spigot/msdk/Endpoints/Queue.java b/src/br/com/mineshop/msdk/webservice/endpoints/v1/Queue.java similarity index 62% rename from src/br/com/mineshop/spigot/msdk/Endpoints/Queue.java rename to src/br/com/mineshop/msdk/webservice/endpoints/v1/Queue.java index 0686d41..16d0699 100644 --- a/src/br/com/mineshop/spigot/msdk/Endpoints/Queue.java +++ b/src/br/com/mineshop/msdk/webservice/endpoints/v1/Queue.java @@ -1,9 +1,10 @@ -package br.com.mineshop.spigot.msdk.Endpoints; +package br.com.mineshop.msdk.webservice.endpoints.v1; public class Queue { private String uuid; private String nickname; - private String commands; + private String command; + private int slotsNeeded; private String type; private String status; @@ -23,12 +24,20 @@ public void setNickname(String nickname) { this.nickname = nickname; } - public String[] getCommands() { - return this.commands.split("\n"); + public String getCommand() { + return this.command; } - public void setCommands(String commands) { - this.commands = commands; + public void setCommand(String command) { + this.command = command; + } + + public int getSlotsNeeded() { + return this.slotsNeeded; + } + + public void setSlotsNeeded(int slotsNeeded) { + this.slotsNeeded = slotsNeeded; } public String getType() { diff --git a/src/br/com/mineshop/spigot/msdk/Endpoints/CheckToken.java b/src/br/com/mineshop/spigot/msdk/Endpoints/CheckToken.java deleted file mode 100644 index 923971e..0000000 --- a/src/br/com/mineshop/spigot/msdk/Endpoints/CheckToken.java +++ /dev/null @@ -1,22 +0,0 @@ -package br.com.mineshop.spigot.msdk.Endpoints; - -public class CheckToken { - private String name; - private String token; - - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - - public String getToken() { - return this.token; - } - - public void setToken(String token) { - this.token = token; - } -} diff --git a/src/br/com/mineshop/spigot/msdk/Exceptions/WSException.java b/src/br/com/mineshop/spigot/msdk/Exceptions/WSException.java deleted file mode 100644 index cae6338..0000000 --- a/src/br/com/mineshop/spigot/msdk/Exceptions/WSException.java +++ /dev/null @@ -1,7 +0,0 @@ -package br.com.mineshop.spigot.msdk.Exceptions; - -public class WSException extends Exception { - public WSException(String message) { - super(message); - } -} diff --git a/src/br/com/mineshop/spigot/msdk/MSDK.java b/src/br/com/mineshop/spigot/msdk/MSDK.java deleted file mode 100644 index 03d1360..0000000 --- a/src/br/com/mineshop/spigot/msdk/MSDK.java +++ /dev/null @@ -1,148 +0,0 @@ -package br.com.mineshop.spigot.msdk; - -import br.com.mineshop.spigot.msdk.Endpoints.CheckToken; -import br.com.mineshop.spigot.msdk.Endpoints.Queue; -import br.com.mineshop.spigot.msdk.Exceptions.WSException; -import com.google.gson.Gson; -import org.bukkit.plugin.java.JavaPlugin; - -import javax.net.ssl.HttpsURLConnection; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.net.URL; -import java.util.logging.Level; -import java.util.logging.Logger; - -public final class MSDK extends JavaPlugin { - private final String api = "https://api.mineshop.com.br/plugin"; - private String servername; - private String authorization; - - public void setAuthorization(String token) throws WSException { - String response = this.get(String.format("/v1/check-token/%s", token.trim().toLowerCase())); - CheckToken checkToken = new Gson().fromJson(response, CheckToken.class); - if (!checkToken.getName().isEmpty() && !checkToken.getToken().isEmpty()) { - this.servername = checkToken.getName(); - this.authorization = checkToken.getToken(); - } - } - - public String getServername() { - return this.servername; - } - - public Queue[] getQueue() throws WSException { - return this.getQueue(""); - } - - public Queue[] getQueue(String nickname) throws WSException { - nickname = nickname.trim(); - if (!nickname.isEmpty()) { - nickname = String.format("/%s", nickname); - } - - String response = this.get(String.format("/v1/queue%s", nickname)); - return new Gson().fromJson(response, Queue[].class); - } - - public void delivered(Queue queue) throws WSException { - this.delivered(queue.getUuid()); - } - - public void delivered(String uuid) throws WSException { - this.update(String.format("/v1/queue/delivered/%s", uuid)); - } - - private String get(String endpoint) throws WSException { - HttpsURLConnection c = null; - int statusCode = 0; - - try { - URL u = new URL((this.api + endpoint).toLowerCase()); - - c = (HttpsURLConnection) u.openConnection(); - c.setRequestMethod("GET"); - c.setRequestProperty("Authorization", this.authorization); - c.setRequestProperty("Content-Type", "application/json"); - c.setUseCaches(false); - c.setAllowUserInteraction(false); - c.setConnectTimeout(1000); - c.setReadTimeout(1000); - c.connect(); - - statusCode = c.getResponseCode(); - - if (statusCode >= 200 && statusCode <= 299) { - String line; - BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); - StringBuilder sb = new StringBuilder(); - - while ((line = br.readLine()) != null) { - sb.append(line).append("\n"); - } - - br.close(); - - return sb.toString(); - } - } catch (IOException ex) { - Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); - } finally { - if (c != null) { - try { - c.disconnect(); - } catch (Exception ex) { - Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); - } - } - } - - throw new WSException(String.format("[%s] %s Falha ao tentar comunicar-se com a web api", this.getDescription().getName(), Integer.toString(statusCode))); - } - - private void update(String endpoint) throws WSException { - HttpsURLConnection c = null; - int statusCode = 0; - - try { - URL u = new URL((this.api + endpoint).toLowerCase()); - - c = (HttpsURLConnection) u.openConnection(); - c.setRequestMethod("PUT"); - c.setRequestProperty("Authorization", this.authorization); - c.setRequestProperty("Content-Type", "application/json"); - c.setDoOutput(true); - c.setUseCaches(false); - c.setAllowUserInteraction(false); - c.setConnectTimeout(1000); - c.setReadTimeout(1000); - c.connect(); - - OutputStreamWriter osw = new OutputStreamWriter(c.getOutputStream()); - - osw.write("{}"); - osw.flush(); - osw.close(); - - statusCode = c.getResponseCode(); - - if (statusCode >= 200 && statusCode <= 299) { - return; - } - } catch (IOException ex) { - Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); - } finally { - if (c != null) { - try { - c.disconnect(); - } catch (Exception ex) { - Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); - } - } - } - - throw new WSException(String.format("[%s] %s Falha ao tentar comunicar-se com a web api", this.getDescription().getName(), Integer.toString(statusCode))); - } -} diff --git a/src/plugin.yml b/src/plugin.yml deleted file mode 100644 index 8945cef..0000000 --- a/src/plugin.yml +++ /dev/null @@ -1,7 +0,0 @@ -name: msdk -description: sdk de auxílio ao desenvolvimento de plugins para lojas criadas em mineshop.com.br -version: 1.0.0 -main: br.com.mineshop.spigot.msdk.MSDK -load: POSTWORLD -author: Sapiente -website: https://github.com/clayderson/msdk-spigot \ No newline at end of file