diff --git a/GapsWeb/src/main/java/com/jasonhhouse/gaps/controller/NotificationController.java b/GapsWeb/src/main/java/com/jasonhhouse/gaps/controller/NotificationController.java index ed10c9b..32d88f5 100644 --- a/GapsWeb/src/main/java/com/jasonhhouse/gaps/controller/NotificationController.java +++ b/GapsWeb/src/main/java/com/jasonhhouse/gaps/controller/NotificationController.java @@ -41,9 +41,15 @@ public class NotificationController { LOGGER.info("putTestAll()"); try { - notificationService.test(); - LOGGER.info("Notification Test All Succeeded"); - return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_SUCCEEDED); + boolean result = notificationService.test(); + + if (result) { + LOGGER.info("Notification Test All Succeeded"); + return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_SUCCEEDED); + } else { + LOGGER.error("Notification Test All Failed"); + return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_FAILED); + } } catch (Exception e) { LOGGER.error("Notification Test All Failed", e); return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_FAILED.setExtras(e.getMessage())); @@ -57,9 +63,14 @@ public class NotificationController { LOGGER.info("putTest( {} )", id); try { - notificationService.test(id); - LOGGER.info("Notification Test with ID {} Succeeded", id); - return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_SUCCEEDED); + boolean result = notificationService.test(id); + if (result) { + LOGGER.info("Notification Test with ID {} Succeeded", id); + return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_SUCCEEDED); + } else { + LOGGER.error("Notification Test Failed with ID {}", id); + return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_FAILED); + } } catch (Exception e) { LOGGER.error(String.format("Notification Test Failed with ID %s", id), e); return ResponseEntity.ok().body(Payload.NOTIFICATION_TEST_FAILED.setExtras(e.getMessage())); diff --git a/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/EmailNotificationAgent.java b/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/EmailNotificationAgent.java index 0df3942..36d4d6d 100644 --- a/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/EmailNotificationAgent.java +++ b/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/EmailNotificationAgent.java @@ -2,7 +2,6 @@ package com.jasonhhouse.gaps.notifications; import com.jasonhhouse.gaps.NotificationType; import com.jasonhhouse.gaps.service.IoService; -import java.util.List; import java.util.Properties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,7 +46,7 @@ public class EmailNotificationAgent implements NotificationAgent { } @Override - public void sendMessage(NotificationType notificationType, String level, String title, String message) { + public boolean sendMessage(NotificationType notificationType, String level, String title, String message) { try { SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); simpleMailMessage.setFrom(mailFrom); @@ -55,8 +54,10 @@ public class EmailNotificationAgent implements NotificationAgent { simpleMailMessage.setSubject(title); simpleMailMessage.setText(message); mailSender.send(simpleMailMessage); + return true; } catch (MailException e) { LOGGER.error("Error with Sending Email Notification.", e); + return false; } } diff --git a/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/GotifyNotificationAgent.java b/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/GotifyNotificationAgent.java index 4f07e15..640a4db 100644 --- a/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/GotifyNotificationAgent.java +++ b/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/GotifyNotificationAgent.java @@ -74,7 +74,7 @@ public class GotifyNotificationAgent implements NotificationAgent { } @Override - public void sendMessage(NotificationType notificationType, String level, String title, String message) { + public boolean sendMessage(NotificationType notificationType, String level, String title, String message) { LOGGER.info("sendMessage( {}, {}, {} )", level, title, message); HttpUrl url = HttpUrl.parse(String.format("%s/message?token=%s", address, token)); @@ -86,7 +86,7 @@ public class GotifyNotificationAgent implements NotificationAgent { gotifyMessage = objectMapper.writeValueAsString(gotify); } catch (JsonProcessingException e) { LOGGER.error("Failed to turn Gotify message into JSON", e); - return; + return false; } LOGGER.info("Gotify {}", gotifyMessage); @@ -100,16 +100,18 @@ public class GotifyNotificationAgent implements NotificationAgent { try (Response response = client.newCall(request).execute()) { if (response.isSuccessful()) { LOGGER.info("Gotify message sent via {}", url); + return true; } else { LOGGER.error("Error with Gotify Url: {} Body returned {}", url, response.body().toString()); + return false; } } catch (IOException e) { LOGGER.error(String.format("Error with Gotify Url: %s", url), e); + return false; } } - public static final class Gotify { private final String title; private final String message; diff --git a/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/NotificationAgent.java b/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/NotificationAgent.java index 73d59e6..1027bf5 100644 --- a/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/NotificationAgent.java +++ b/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/NotificationAgent.java @@ -22,5 +22,5 @@ public interface NotificationAgent { boolean isEnabled(); - void sendMessage(NotificationType notificationType, String level, String title, String message); + boolean sendMessage(NotificationType notificationType, String level, String title, String message); } diff --git a/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/PushBulletNotificationAgent.java b/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/PushBulletNotificationAgent.java index 29576a1..9afd5a4 100644 --- a/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/PushBulletNotificationAgent.java +++ b/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/PushBulletNotificationAgent.java @@ -64,7 +64,7 @@ public class PushBulletNotificationAgent implements NotificationAgent { } @Override - public void sendMessage(NotificationType notificationType, String level, String title, String message) { + public boolean sendMessage(NotificationType notificationType, String level, String title, String message) { LOGGER.info("sendMessage( {}, {}, {} )", level, title, message); HttpUrl url = new HttpUrl.Builder() @@ -89,7 +89,7 @@ public class PushBulletNotificationAgent implements NotificationAgent { pushBulletMessage = objectMapper.writeValueAsString(pushBullet); } catch (JsonProcessingException e) { LOGGER.error("Failed to turn PushBullet message into JSON", e); - return; + return false; } LOGGER.info("pushBulletMessage {}", pushBulletMessage); @@ -104,12 +104,15 @@ public class PushBulletNotificationAgent implements NotificationAgent { try (Response response = client.newCall(request).execute()) { if (response.isSuccessful()) { LOGGER.info("PushBullet message sent via {}", url); + return true; } else { LOGGER.error("Error with PushBullet Url: {} Body returned {}", url, response.body().toString()); + return false; } } catch (IOException e) { LOGGER.error(String.format("Error with PushBullet Url: %s", url), e); + return false; } } diff --git a/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/SlackNotificationAgent.java b/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/SlackNotificationAgent.java index 405e7a9..3976ca0 100644 --- a/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/SlackNotificationAgent.java +++ b/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/SlackNotificationAgent.java @@ -52,7 +52,7 @@ public class SlackNotificationAgent implements NotificationAgent { } @Override - public void sendMessage(NotificationType notificationType, String level, String title, String message) { + public boolean sendMessage(NotificationType notificationType, String level, String title, String message) { LOGGER.info("sendMessage( {}, {}, {} )", level, title, message); HttpUrl url = HttpUrl.get(webHookUrl); @@ -68,7 +68,7 @@ public class SlackNotificationAgent implements NotificationAgent { slackMessage = objectMapper.writeValueAsString(slack); } catch (JsonProcessingException e) { LOGGER.error("Failed to turn Slack message into JSON", e); - return; + return false; } LOGGER.info("slackMessage {}", slackMessage); @@ -83,12 +83,15 @@ public class SlackNotificationAgent implements NotificationAgent { try (Response response = client.newCall(request).execute()) { if (response.isSuccessful()) { LOGGER.info("Slack message sent via {}", url); + return true; } else { LOGGER.error("Error with Slack Url: {} Body returned {}", url, response.body().toString()); + return false; } } catch (IOException e) { LOGGER.error(String.format("Error with Slack Url: %s", url), e); + return false; } } diff --git a/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/TelegramNotificationAgent.java b/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/TelegramNotificationAgent.java index c3a793a..d7b9130 100644 --- a/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/TelegramNotificationAgent.java +++ b/GapsWeb/src/main/java/com/jasonhhouse/gaps/notifications/TelegramNotificationAgent.java @@ -70,7 +70,7 @@ public class TelegramNotificationAgent implements NotificationAgent { } @Override - public void sendMessage(NotificationType notificationType, String level, String title, String message) { + public boolean sendMessage(NotificationType notificationType, String level, String title, String message) { LOGGER.info("sendMessage( {}, {}, {} )", level, title, message); HttpUrl url = new HttpUrl.Builder() @@ -92,12 +92,15 @@ public class TelegramNotificationAgent implements NotificationAgent { try (Response response = client.newCall(request).execute()) { if (response.isSuccessful()) { LOGGER.info("Telegram message sent via {}", url); + return true; } else { LOGGER.error("Error with Telegram Url: {} Body returned {}", url, response.body().toString()); + return false; } } catch (IOException e) { LOGGER.error(String.format("Error with Telegram Url: %s", url), e); + return false; } } diff --git a/GapsWeb/src/main/java/com/jasonhhouse/gaps/service/Notification.java b/GapsWeb/src/main/java/com/jasonhhouse/gaps/service/Notification.java index 1e22d78..35a4253 100644 --- a/GapsWeb/src/main/java/com/jasonhhouse/gaps/service/Notification.java +++ b/GapsWeb/src/main/java/com/jasonhhouse/gaps/service/Notification.java @@ -23,7 +23,7 @@ public interface Notification { void recommendedMoviesSearchFinished(PlexServer plexServer, PlexLibrary plexLibrary); - void test(); + boolean test(); - void test(int id) throws IllegalArgumentException, IllegalAccessException; + boolean test(int id) throws IllegalArgumentException, IllegalAccessException; } diff --git a/GapsWeb/src/main/java/com/jasonhhouse/gaps/service/NotificationService.java b/GapsWeb/src/main/java/com/jasonhhouse/gaps/service/NotificationService.java index cb2e05e..aa02179 100644 --- a/GapsWeb/src/main/java/com/jasonhhouse/gaps/service/NotificationService.java +++ b/GapsWeb/src/main/java/com/jasonhhouse/gaps/service/NotificationService.java @@ -38,7 +38,7 @@ public class NotificationService implements Notification { notificationAgents .stream() .filter(NotificationAgent::isEnabled) - .forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.TEST_PLEX_SERVER,"INFO", "Gaps Search", String.format("Connection to Plex Server %s Successful", plexServer.getFriendlyName()))); + .forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.TEST_PLEX_SERVER, "INFO", "Gaps Search", String.format("Connection to Plex Server %s Successful", plexServer.getFriendlyName()))); } @Override @@ -46,7 +46,7 @@ public class NotificationService implements Notification { notificationAgents .stream() .filter(NotificationAgent::isEnabled) - .forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.SCAN_PLEX_SERVER,"INFO", "Gaps Search", String.format("Scanning Plex Server %s in %s Library Failed. %s", plexServer.getFriendlyName(), plexLibrary.getTitle(), error))); + .forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.SCAN_PLEX_SERVER, "INFO", "Gaps Search", String.format("Scanning Plex Server %s in %s Library Failed. %s", plexServer.getFriendlyName(), plexLibrary.getTitle(), error))); } @Override @@ -54,7 +54,7 @@ public class NotificationService implements Notification { notificationAgents .stream() .filter(NotificationAgent::isEnabled) - .forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.SCAN_PLEX_SERVER,"INFO", "Gaps Search", String.format("Scanning Plex Server %s in %s Library Successful", plexServer.getFriendlyName(), plexLibrary.getTitle()))); + .forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.SCAN_PLEX_SERVER, "INFO", "Gaps Search", String.format("Scanning Plex Server %s in %s Library Successful", plexServer.getFriendlyName(), plexLibrary.getTitle()))); } @Override @@ -86,7 +86,7 @@ public class NotificationService implements Notification { notificationAgents .stream() .filter(NotificationAgent::isEnabled) - .forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.RECOMMENDED_MOVIES,"INFO", "Gaps Search", String.format("Scanning Plex Server %s on Library %s Failed %s", plexServer.getFriendlyName(), plexLibrary.getTitle(), error))); + .forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.RECOMMENDED_MOVIES, "INFO", "Gaps Search", String.format("Scanning Plex Server %s on Library %s Failed %s", plexServer.getFriendlyName(), plexLibrary.getTitle(), error))); } @Override @@ -94,23 +94,28 @@ public class NotificationService implements Notification { notificationAgents .stream() .filter(NotificationAgent::isEnabled) - .forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.RECOMMENDED_MOVIES,"INFO", "Gaps Search", String.format("Scanning Plex Server %s on Library %s Successfully Finished", plexServer.getFriendlyName(), plexLibrary.getTitle()))); + .forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.RECOMMENDED_MOVIES, "INFO", "Gaps Search", String.format("Scanning Plex Server %s on Library %s Successfully Finished", plexServer.getFriendlyName(), plexLibrary.getTitle()))); } @Override - public void test() { - notificationAgents - .stream() - .filter(NotificationAgent::isEnabled) - .forEach(notificationAgent -> notificationAgent.sendMessage(NotificationType.TEST, "DEBUG", "Gaps Test", "Test Successful")); + public boolean test() { + boolean passedTest = true; + + for (NotificationAgent notificationAgent : notificationAgents) { + boolean result = notificationAgent.sendMessage(NotificationType.TEST, "DEBUG", "Gaps Test", "Test Successful"); + if (!result) { + passedTest = false; + } + } + + return passedTest; } @Override - public void test(int id) throws IllegalArgumentException { + public boolean test(int id) throws IllegalArgumentException { for (NotificationAgent notificationAgent : notificationAgents) { if (notificationAgent.getId() == id) { - notificationAgent.sendMessage(NotificationType.TEST,"DEBUG", "Gaps Test", "Test Successful"); - return; + return notificationAgent.sendMessage(NotificationType.TEST, "DEBUG", "Gaps Test", "Test Successful"); } } throw new IllegalArgumentException("Invalid Id for Notification Agent");