Skip to content

Commit

Permalink
Merge pull request I-TECH-UW#1258 from konavivekramakrishna/fix#inter…
Browse files Browse the repository at this point in the history
…anlservererror

Add Exception handling for push service
  • Loading branch information
mozzy11 authored Sep 3, 2024
2 parents b8bd969 + 8409746 commit 0ae8e1a
Showing 1 changed file with 31 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,39 @@ public List<Notification> getNotificationsByUserId(HttpServletRequest request) {

@PostMapping("/notification/{userId}")
public ResponseEntity<?> saveNotification(@PathVariable String userId, @RequestBody Notification notification) {
notification.setUser(systemUserService.getUserById(userId));
notification.setCreatedDate(OffsetDateTime.now());
notification.setReadAt(null);
notificationDAO.save(notification);

// Ensure BouncyCastleProvider is added for cryptographic operations
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
Security.addProvider(new BouncyCastleProvider());
try {
// Set user and created date
notification.setUser(systemUserService.getUserById(userId));
notification.setCreatedDate(OffsetDateTime.now());
notification.setReadAt(null);

// Save notification
notificationDAO.save(notification);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Failed to save notification: " + e.getMessage());
}

NotificationSubscriptions ns = notificationSubscriptionDAO
.getNotificationSubscriptionByUserId(Long.valueOf(userId));
try {
// Ensure BouncyCastleProvider is added for cryptographic operations
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
Security.addProvider(new BouncyCastleProvider());
}
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Failed to add BouncyCastleProvider: " + e.getMessage());
}

if (ns == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Subscription not found");
NotificationSubscriptions ns;
try {
// Get notification subscription by userId
ns = notificationSubscriptionDAO.getNotificationSubscriptionByUserId(Long.valueOf(userId));
if (ns == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Subscription not found for userId: " + userId);
}
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Failed to retrieve notification subscription: " + e.getMessage());
}

try {
Expand All @@ -101,6 +119,7 @@ public ResponseEntity<?> saveNotification(@PathVariable String userId, @RequestB
nl.martijndwars.webpush.Notification webPushNotification = new nl.martijndwars.webpush.Notification(
ns.getPfEndpoint(), ns.getPfP256dh(), ns.getPfAuth(), payloadJson);

// Send push notification
HttpResponse response = pushService.send(webPushNotification);

return ResponseEntity.ok()
Expand All @@ -109,7 +128,6 @@ public ResponseEntity<?> saveNotification(@PathVariable String userId, @RequestB
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Failed to send push notification: " + e.getMessage());
}

}

@GetMapping("/notification/pnconfig")
Expand Down

0 comments on commit 0ae8e1a

Please sign in to comment.