1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
package de.torui.coflsky.network;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import de.torui.coflsky.CoflSky;
import de.torui.coflsky.minecraft_integration.CoflSessionManager;
import de.torui.coflsky.minecraft_integration.PlayerDataProvider;
public class QueryServerCommands {
private static Gson gson = new GsonBuilder().create();
public static String QueryCommands() {
String queryResult = GetRequest(CoflSky.CommandUri);
if(queryResult != null) {
CommandInfo[] commands = gson.fromJson(queryResult, CommandInfo[].class);
System.out.println(">>> "+Arrays.toString(commands));
StringBuilder sb = new StringBuilder();
if(commands.length>0) {
for(CommandInfo cm : commands) {
sb.append(cm + "\n");
}
}
return sb.toString().trim();
}
return "§4ERROR: Could not connect to command server!";
}
private static class CommandInfo {
public String subCommand;
public String description;
public CommandInfo() {}
public CommandInfo(String subCommand, String description) {
super();
this.subCommand = subCommand;
this.description = description;
}
@Override
public String toString() {
return subCommand + ": " + description;
}
}
private static String GetRequest(String uri) {
try {
System.out.println("Get request");
URL url = new URL(uri);
HttpURLConnection con;
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
//con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("User-Agent", "CoflMod");
//con.setDoInput(true);
con.setDoInput(true);
// ...
/*OutputStream os = con.getOutputStream();
byte[] bytes = ("[\"" + getUsername() + "\"]").getBytes("UTF-8");
os.write(bytes);
os.close();
*/
System.out.println("InputStream");
InputStream in = new BufferedInputStream(con.getInputStream());
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int length; (length = in.read(buffer)) != -1; ) {
result.write(buffer, 0, length);
}
// StandardCharsets.UTF_8.name() > JDK 7
String resString = result.toString("UTF-8");
System.out.println("Result= " + resString);
return resString;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String PostRequest(String uri, String data) {
try {
String username = PlayerDataProvider.getUsername();
URL url = new URL(uri);
HttpURLConnection con;
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("User-Agent", "CoflMod");
con.setRequestProperty("conId", CoflSessionManager.GetCoflSession(username).SessionUUID);
con.setRequestProperty("uuid",username);
con.setDoInput(true);
con.setDoOutput(true);
// ...
OutputStream os = con.getOutputStream();
byte[] bytes = data.getBytes("UTF-8");
os.write(bytes);
os.close();
InputStream in = new BufferedInputStream(con.getInputStream());
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int length; (length = in.read(buffer)) != -1; ) {
result.write(buffer, 0, length);
}
// StandardCharsets.UTF_8.name() > JDK 7
String resString = result.toString("UTF-8");
return resString;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
|