Skip to content

Commit 2fcfbe7

Browse files
author
Marco (Valandur)
committed
Merge branch 'release/v5.4.6'
2 parents 4d12590 + c71e6c3 commit 2fcfbe7

6 files changed

Lines changed: 97 additions & 52 deletions

File tree

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
version=5.4.5
1+
version=5.4.6
22

33
minecraftVersion=1.12.2
4-
spongeVersion=7.1.0
4+
spongeVersion=7.2.0-SNAPSHOT
55

66
jacksonVersion=2.9.4
77
jettyVersion=9.4.8.v20171121

webapi-sponge/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ dependencies {
7272
compile group: "org.glassfish.jersey.inject", name: "jersey-hk2", version: "2.26"
7373
compile group: "org.bstats", name: "bstats-sponge", version: "1.4"
7474

75-
compileOnly group: "org.spongepowered", name: "spongeapi", version: "${project.spongeVersion}-SNAPSHOT"
75+
compileOnly group: "org.spongepowered", name: "spongeapi", version: project.spongeVersion
7676

7777
compile group: 'redis.clients', name: 'jedis', version: '2.9.0'
7878
compile group: 'com.rabbitmq', name: 'amqp-client', version: '5.3.0'

webapi-sponge/src/main/java/valandur/webapi/WebAPI.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@
9191
)
9292
public class WebAPI {
9393

94-
static {
95-
System.out.println("Static initialized");
96-
}
97-
9894
private static WebAPI instance;
9995
public static WebAPI getInstance() {
10096
return WebAPI.instance;

webapi-sponge/src/main/java/valandur/webapi/hook/WebHookSerializer.java

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
66
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
77
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
8-
import org.slf4j.Logger;
98
import valandur.webapi.WebAPI;
109
import valandur.webapi.hook.filter.BaseWebHookFilter;
1110
import valandur.webapi.security.SecurityService;
1211
import valandur.webapi.util.TreeNode;
1312

14-
import javax.ws.rs.HttpMethod;
1513
import java.lang.reflect.Constructor;
1614
import java.lang.reflect.InvocationTargetException;
1715
import java.util.ArrayList;
@@ -22,17 +20,12 @@ public class WebHookSerializer implements TypeSerializer<WebHook> {
2220

2321
@Override
2422
public WebHook deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
25-
Logger logger = WebAPI.getLogger();
26-
2723
String address = value.getNode("address").getString();
2824

2925
if (address == null) {
30-
logger.error("No address specified for web hook!");
31-
return null;
26+
throw new ObjectMappingException("No address specified for web hook!");
3227
}
3328

34-
logger.info(" - " + address);
35-
3629
boolean enabled = value.getNode("enabled").getBoolean();
3730
String method = value.getNode("method").getString();
3831
WebHook.WebHookDataType dataType = value.getNode("dataType").getValue(TypeToken.of(WebHook.WebHookDataType.class));
@@ -51,43 +44,35 @@ public WebHook deserialize(TypeToken<?> type, ConfigurationNode value) throws Ob
5144
}
5245

5346
if (method == null) {
54-
method = HttpMethod.POST;
55-
logger.warn(" Does not specify 'method', defaulting to 'POST'");
47+
throw new ObjectMappingException("Webhook " + address + " is missing property 'method'.");
5648
}
5749

5850
if (dataType == null) {
59-
dataType = WebHook.WebHookDataType.JSON;
60-
logger.warn(" Does not specify 'dataType', defaulting to 'JSON'");
51+
throw new ObjectMappingException("Webhook " + address + " is missing property 'dataType'.");
6152
}
6253

6354
if (value.getNode("permissions").isVirtual()) {
64-
logger.warn(" Does not specify 'permissions', defaulting to '*'");
55+
throw new ObjectMappingException("Webhook " + address + " is missing property 'permissions'.");
6556
} else {
66-
permissions = WebAPI.getSecurityService().permissionTreeFromConfig(value.getNode("permissions"));
57+
permissions = SecurityService.permissionTreeFromConfig(value.getNode("permissions"));
6758
}
6859

6960
WebHook hook = new WebHook(address, enabled, method, dataType, form, headers, details, permissions);
7061

7162
if (filterName != null) {
7263
Optional<Class<? extends BaseWebHookFilter>> opt = WebAPI.getWebHookService().getFilter(filterName);
7364
if (!opt.isPresent()) {
74-
logger.error(" Could not find filter with name '" + filterName + "'");
65+
throw new ObjectMappingException("Could not find filter with name '" + filterName + "'");
7566
} else {
7667
try {
7768
Constructor ctor = opt.get().getConstructor(WebHook.class, ConfigurationNode.class);
7869
hook.setFilter((BaseWebHookFilter) ctor.newInstance(hook, filterConfig));
7970
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
80-
logger.error(" Could not setup filter '" + filterName + "': " + e.getMessage());
71+
throw new ObjectMappingException("Could not setup filter '" + filterName + "': " + e.getMessage());
8172
}
8273
}
8374
}
8475

85-
if (enabled) {
86-
logger.info(" -> Ok");
87-
} else {
88-
logger.info(" -> Disabled");
89-
}
90-
9176
return hook;
9277
}
9378

@@ -130,7 +115,7 @@ public void serialize(TypeToken<?> type, WebHook obj, ConfigurationNode value) t
130115
}
131116
}
132117

133-
WebAPI.getSecurityService().permissionTreeToConfig(value.getNode("permissions"), obj.getPermissions());
118+
SecurityService.permissionTreeToConfig(value.getNode("permissions"), obj.getPermissions());
134119
if (value.getNode("permissions") instanceof CommentedConfigurationNode) {
135120
((CommentedConfigurationNode) value.getNode("permissions")).setComment(
136121
"Permissions node same as the ones in the permissions.conf file,\n" +

webapi-sponge/src/main/java/valandur/webapi/hook/WebHookService.java

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import java.util.concurrent.CompletableFuture;
4848
import java.util.stream.Collectors;
4949

50+
import static valandur.webapi.util.Constants.*;
51+
5052
/**
5153
* The web hook service provides access to the Web-API web hooks.
5254
*/
@@ -94,18 +96,13 @@ public void init() {
9496
" Minecraft/" + mc +
9597
" Java/" + System.getProperty("java.version");
9698

97-
// Load filters
98-
logger.info("Loading filters...");
99-
10099
filters.clear();
101100

102-
// Add some default filters
101+
// Add filters
103102
filters.put(BlockTypeFilter.name, BlockTypeFilter.class);
104103
filters.put(PlayerFilter.name, PlayerFilter.class);
105104
filters.put(ItemTypeFilter.name, ItemTypeFilter.class);
106105

107-
logger.info("Done loading filters");
108-
109106
// Load config
110107
Path configPath = WebAPI.getConfigPath().resolve(configFileName).normalize();
111108
HookConfig config = BaseConfig.load(configPath, new HookConfig());
@@ -115,41 +112,88 @@ public void init() {
115112
customHooks.clear();
116113
commandHooks.clear();
117114

115+
// Calculate max width of any hook text for printing
116+
int maxAddressLength = 0;
117+
for (CommandWebHook cmdHook : config.command.values()) {
118+
maxAddressLength = Math.max(
119+
maxAddressLength,
120+
cmdHook.getHooks().stream().map(h -> h.getAddress().length()).max(Comparator.comparingInt(a -> a)).orElse(0)
121+
);
122+
}
123+
for (List<WebHook> hooks : config.events.asMap().values()) {
124+
maxAddressLength = Math.max(
125+
maxAddressLength,
126+
hooks.stream().map(h -> h.getAddress().length()).max(Comparator.comparingInt(a -> a)).orElse(0)
127+
);
128+
}
129+
for (List<WebHook> hooks : config.custom.values()) {
130+
maxAddressLength = Math.max(
131+
maxAddressLength,
132+
hooks.stream().map(h -> h.getAddress().length()).max(Comparator.comparingInt(a -> a)).orElse(0)
133+
);
134+
}
135+
118136
// Add command hooks
119137
for (Map.Entry<String, CommandWebHook> entry : config.command.entrySet()) {
120-
if (!entry.getValue().isEnabled())
121-
continue;
122-
commandHooks.put(entry.getKey(), entry.getValue());
138+
String cmd = entry.getKey();
139+
CommandWebHook cmdHook = entry.getValue();
140+
141+
String separator = String.join("", Collections.nCopies(maxAddressLength - cmd.length() - 7, " "));
142+
logger.info(" Command: " + cmd + separator + " [" + (cmdHook.isEnabled() ? ANSI_GREEN + "ON" : ANSI_RED + "DISABLED") + ANSI_RESET + "]");
143+
for (WebHook hook : cmdHook.getHooks()) {
144+
separator = String.join("", Collections.nCopies(maxAddressLength - hook.getAddress().length(), " "));
145+
logger.info(" " + hook.getAddress() + separator + " [" + (hook.isEnabled() ? ANSI_GREEN + "ON" : ANSI_RED + "DISABLED") + ANSI_RESET + "]");
146+
}
147+
148+
if (cmdHook.isEnabled()) {
149+
commandHooks.put(cmd, cmdHook);
150+
}
123151
}
124152

125153
// Add event hooks
126154
for (Map.Entry<WebHookType, List<WebHook>> entry : config.events.asMap().entrySet()) {
127-
eventHooks.put(
128-
entry.getKey(),
129-
entry.getValue().stream().filter(WebHook::isEnabled).collect(Collectors.toList())
130-
);
155+
WebHookType type = entry.getKey();
156+
List<WebHook> hooks = entry.getValue();
157+
158+
eventHooks.put(type, hooks.stream().filter(WebHook::isEnabled).collect(Collectors.toList()));
159+
160+
if (hooks.size() == 0) {
161+
continue;
162+
}
163+
164+
logger.info(" Event: " + type);
165+
for (WebHook hook : hooks) {
166+
String separator = String.join("", Collections.nCopies(maxAddressLength - hook.getAddress().length(), " "));
167+
logger.info(" " + hook.getAddress() + separator + " [" + (hook.isEnabled() ? ANSI_GREEN + "ON" : ANSI_RED + "DISABLED") + ANSI_RESET + "]");
168+
}
131169
}
132170

133171
// Add custom event hooks
134172
for (Map.Entry<String, List<WebHook>> entry : config.custom.entrySet()) {
135173
String className = entry.getKey();
174+
List<WebHook> hooks = entry.getValue();
136175

137176
try {
138177
Class c = Class.forName(className);
139-
if (!Event.class.isAssignableFrom(c))
178+
if (!Event.class.isAssignableFrom(c)) {
140179
throw new InvalidClassException("Class " + c.toString() + " must be a subclass of " +
141180
Event.class.toString() + " so that it can be used as a custom web hook");
181+
}
142182
Class<? extends Event> clazz = (Class<? extends Event>) c;
143183

144184
WebHookEventListener listener = new WebHookEventListener(clazz);
145-
List<WebHook> hooks = entry.getValue().stream().filter(WebHook::isEnabled).collect(Collectors.toList());
146-
147185
Sponge.getEventManager().registerListener(WebAPI.getInstance(), clazz, listener);
148-
customHooks.put(clazz, new Tuple<>(hooks, listener));
186+
customHooks.put(clazz, new Tuple<>(hooks.stream().filter(WebHook::isEnabled).collect(Collectors.toList()), listener));
187+
188+
logger.info(" Custom Event: " + c.getName());
189+
for (WebHook hook : hooks) {
190+
String separator = String.join("", Collections.nCopies(maxAddressLength - hook.getAddress().length(), " "));
191+
logger.info(" " + hook.getAddress() + separator + " [" + (hook.isEnabled() ? ANSI_GREEN + "ON" : ANSI_RED + "DISABLED") + ANSI_RESET + "]");
192+
}
149193
} catch (ClassNotFoundException e) {
150-
logger.error("Could not find class for custom web hook: " + className);
194+
logger.error(" Could not find class for custom web hook: " + className);
151195
} catch (InvalidClassException e) {
152-
logger.error(e.getMessage());
196+
logger.error(" " + e.getMessage());
153197
}
154198
}
155199
}
@@ -166,8 +210,18 @@ public Optional<Class<? extends BaseWebHookFilter>> getFilter(String name) {
166210
public void notifyHooks(WebHookType type, Object data) {
167211
Timings.WEBHOOK_NOTIFY.startTimingIfSync();
168212

169-
List<WebHook> notifyHooks = new ArrayList<>(eventHooks.get(type));
170-
notifyHooks.addAll(eventHooks.get(WebHookType.ALL));
213+
List<WebHook> notifyHooks = new ArrayList<>();
214+
215+
List<WebHook> origHooks = eventHooks.get(type);
216+
if (origHooks != null) {
217+
notifyHooks.addAll(origHooks);
218+
}
219+
220+
List<WebHook> allHooks = eventHooks.get(WebHookType.ALL);
221+
if (allHooks != null) {
222+
notifyHooks.addAll(allHooks);
223+
}
224+
171225
for (WebHook hook : notifyHooks) {
172226
notifyHook(hook, type, null, data);
173227
}
@@ -192,7 +246,7 @@ public void notifyHook(CommandWebHook cmdHook, String source, Map<String, Object
192246
public void notifyHooks(Class<? extends Event> clazz, Object data) {
193247
Timings.WEBHOOK_NOTIFY.startTimingIfSync();
194248

195-
List<WebHook> notifyHooks = new ArrayList<>(customHooks.get(clazz).getFirst());
249+
List<WebHook> notifyHooks = customHooks.get(clazz).getFirst();
196250
for (WebHook hook : notifyHooks) {
197251
notifyHook(hook, WebHookType.CUSTOM_EVENT, null, data);
198252
}

webapi-sponge/src/main/java/valandur/webapi/util/Constants.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,14 @@ public class Constants {
99
public static final String UPDATE_URL = "https://api.github.com/repos/Valandur/Web-API/releases/latest";
1010

1111
public static final String BASE_PATH = "/api/v5";
12+
13+
public static final String ANSI_RESET = "\u001B[0m";
14+
public static final String ANSI_BLACK = "\u001B[30m";
15+
public static final String ANSI_RED = "\u001B[31m";
16+
public static final String ANSI_GREEN = "\u001B[32m";
17+
public static final String ANSI_YELLOW = "\u001B[33m";
18+
public static final String ANSI_BLUE = "\u001B[34m";
19+
public static final String ANSI_PURPLE = "\u001B[35m";
20+
public static final String ANSI_CYAN = "\u001B[36m";
21+
public static final String ANSI_WHITE = "\u001B[37m";
1222
}

0 commit comments

Comments
 (0)