Skip to content

Commit

Permalink
Versão 1.0.0 concluída
Browse files Browse the repository at this point in the history
  • Loading branch information
Clayderson Ferreira committed Mar 31, 2019
1 parent b2462d9 commit 82d1f15
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 201 deletions.
11 changes: 0 additions & 11 deletions msdk.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../../../../Java Librarys/spigot-1.7.2.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MODULE_DIR$/../../../../Java Librarys/spigot-1.7.2.jar!/" />
</SOURCES>
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
Expand Down
169 changes: 169 additions & 0 deletions src/br/com/mineshop/msdk/MSDK.java
Original file line number Diff line number Diff line change
@@ -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)
));
}
}
7 changes: 7 additions & 0 deletions src/br/com/mineshop/msdk/exceptions/MsdkException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package br.com.mineshop.msdk.exceptions;

public class MsdkException extends Exception {
public MsdkException(String message) {
super(message);
}
}
7 changes: 7 additions & 0 deletions src/br/com/mineshop/msdk/exceptions/WebServiceException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package br.com.mineshop.msdk.exceptions;

public class WebServiceException extends Exception {
public WebServiceException(String message) {
super(message);
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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() {
Expand Down
22 changes: 0 additions & 22 deletions src/br/com/mineshop/spigot/msdk/Endpoints/CheckToken.java

This file was deleted.

7 changes: 0 additions & 7 deletions src/br/com/mineshop/spigot/msdk/Exceptions/WSException.java

This file was deleted.

Loading

0 comments on commit 82d1f15

Please sign in to comment.