/
WebGetCommand.java
180 lines (156 loc) · 6.32 KB
/
WebGetCommand.java
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package net.aufdemrand.denizencore.scripts.commands.core;
import net.aufdemrand.denizencore.DenizenCore;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Duration;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import net.aufdemrand.denizencore.scripts.commands.Holdable;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import net.aufdemrand.denizencore.utilities.scheduling.Schedulable;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class WebGetCommand extends AbstractCommand implements Holdable {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("url")) {
scriptEntry.addObject("url", new Element(arg.raw_value));
}
else if (!scriptEntry.hasObject("post")
&& arg.matchesOnePrefix("post")) {
scriptEntry.addObject("post", arg.asElement());
}
else if (!scriptEntry.hasObject("timeout")
&& arg.matchesPrefix("timeout", "t")
&& arg.matchesArgumentType(Duration.class)) {
scriptEntry.addObject("timeout", arg.asType(Duration.class));
}
else if (!scriptEntry.hasObject("headers")
&& arg.matchesPrefix("headers")) {
scriptEntry.addObject("headers", arg.asType(dList.class));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("url")) {
throw new InvalidArgumentsException("Must have a valid URL!");
}
Element url = scriptEntry.getElement("url");
if (!url.asString().startsWith("http://") && !url.asString().startsWith("https://")) {
throw new InvalidArgumentsException("Must have a valid (HTTP/HTTPS) URL! Attempted: " + url.asString());
}
scriptEntry.defaultObject("timeout", new Duration(10));
}
@Override
public void execute(final ScriptEntry scriptEntry) {
if (!DenizenCore.getImplementation().allowedToWebget()) {
dB.echoError(scriptEntry.getResidingQueue(), "WebGet disabled by config!");
return;
}
final Element url = scriptEntry.getElement("url");
final Element postData = scriptEntry.getElement("post");
final Duration timeout = scriptEntry.getdObject("timeout");
final dList headers = scriptEntry.getdObject("headers");
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(),
url.debug()
+ (postData != null ? postData.debug() : "")
+ (timeout != null ? timeout.debug() : "")
+ (headers != null ? headers.debug() : ""));
}
Thread thr = new Thread(new Runnable() {
@Override
public void run() {
webGet(scriptEntry, postData, url, timeout, headers);
}
});
thr.start();
}
public void webGet(final ScriptEntry scriptEntry, final Element postData, Element urlp, Duration timeout, dList headers) {
BufferedReader in = null;
try {
URL url = new URL(urlp.asString().replace(" ", "%20"));
final HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
if (postData != null) {
uc.setRequestMethod("POST");
}
if (headers != null) {
for (String str : headers) {
int ind = str.indexOf('/');
if (ind > 0) {
uc.setRequestProperty(str.substring(0, ind), str.substring(ind + 1));
}
}
}
uc.setConnectTimeout((int) timeout.getMillis());
uc.connect();
if (postData != null) {
uc.getOutputStream().write(postData.asString().getBytes("UTF-8"));
}
in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
final StringBuilder sb = new StringBuilder();
// Probably a better way to do this bit.
while (true) {
try {
String temp = in.readLine();
if (temp == null) {
break;
}
sb.append(temp).append("\n");
}
catch (Exception ex) {
break;
}
}
in.close();
DenizenCore.schedule(new Schedulable() {
@Override
public boolean tick(float seconds) {
try {
scriptEntry.addObject("failed", new Element(uc.getResponseCode() == 200 ? "false" : "true"));
}
catch (Exception e) {
dB.echoError(e);
}
scriptEntry.addObject("result", new Element(sb.toString()));
scriptEntry.setFinished(true);
return false;
}
});
}
catch (Exception e) {
dB.echoError(e);
try {
DenizenCore.schedule(new Schedulable() {
@Override
public boolean tick(float seconds) {
scriptEntry.addObject("failed", new Element("true"));
scriptEntry.setFinished(true);
return false;
}
});
}
catch (Exception e2) {
dB.echoError(e2);
}
}
finally {
try {
if (in != null) {
in.close();
}
}
catch (Exception e) {
dB.echoError(e);
}
}
}
}