[grid] Refectoring rejectEvent and guarding UUID parsing
Fixes
@@ -209,7 +209,7 @@ public void close() {
}
private class PollingRunnable implements Runnable {
- private Secret secret;
+ private final Secret secret;
public PollingRunnable(Secret secret) {
this.secret = secret;
@@ -234,11 +234,25 @@ public void run() {
try {
eventSecret = JSON.toType(receivedEventSecret, Secret.class);
} catch (JsonException e) {
- rejectEvent(eventName, receivedEventSecret);
+ rejectEvent(
+ eventName,
+ receivedEventSecret,
+ "Could not parse event secret, rejecting event. " + e.getMessage());
+ return;
+ }
+
+ UUID id;
+ String eventId = new String(socket.recv(), UTF_8);
+ try {
+ id = UUID.fromString(eventId);
+ } catch (IllegalArgumentException e) {
+ rejectEvent(
+ eventName,
+ receivedEventSecret,
+ "Could not parse event id, rejecting event. " + e.getMessage());
return;
}
- UUID id = UUID.fromString(new String(socket.recv(), UTF_8));
String data = new String(socket.recv(), UTF_8);
// Don't bother doing more work if we've seen this message.
@@ -252,7 +266,7 @@ public void run() {
recentMessages.add(id);
if (!Secret.matches(secret, eventSecret)) {
- rejectEvent(eventName, data);
+ rejectEvent(eventName, data, "Rejecting message without a valid secret");
return;
}
@@ -260,9 +274,7 @@ public void run() {
}
}
} catch (Exception e) {
- if (e.getCause() instanceof AssertionError) {
- // Do nothing.
- } else {
+ if (!(e.getCause() instanceof AssertionError)) {
LOG.log(Level.WARNING, e,
() -> "Caught exception while polling for event bus messages: " +
e.getMessage());
@@ -271,12 +283,10 @@ public void run() {
}
}
- private void rejectEvent(EventName eventName, String data) {
+ private void rejectEvent(EventName eventName, String data, String message) {
Event rejectedEvent = new Event(REJECTED_EVENT,
new ZeroMqEventBus.RejectedEvent(eventName, data));
- LOG.log(Level.SEVERE,
- "Received message without a valid secret. Rejecting. {0} -> {1}",
- new Object[]{rejectedEvent, data}); // String formatting only applied if needed
+ LOG.log(Level.SEVERE, "{0}. {1}", new Object[]{message, rejectedEvent});
notifyListeners(REJECTED_EVENT, rejectedEvent);
}
