Commits

Colin Chauvet committed 16ac7c2

Added Javadoc

  • Participants
  • Parent commits ddf5c9e

Comments (0)

Files changed (1)

src/main/java/com/example/plugins/tutorial/confluence/notification/resource/NotificationResource.java

 
 
 /**
- * A resource of message.
+ * A resource of creating and listing notifications
  */
 @Path ("/")
 @Consumes (MediaType.APPLICATION_JSON)
         this.permissionManager = permissionManager;
     }
 
-    @POST
-    public Response createNotification(@FormParam ("title") String title, @FormParam ("message") String message)
-            throws Exception
+@POST
+public Response createNotification(@FormParam ("title") String title, @FormParam ("message") String message)
+        throws Exception
+{
+    if (isAdmin())
     {
-        if (isAdmin())
-        {
-            sendNotificationToAllUsers(title, message);
-            return Response.ok().build();
-        }
-        else
-        {
-            return Response.status(Response.Status.FORBIDDEN).build();
-        }
+        sendNotificationToAllUsers(title, message);
+        return Response.ok().build();
     }
-
-    @GET
-    public Response findAllNotifications() throws Exception
+    else
     {
-        if (isAdmin())
-        {
-            final Iterable<Notification> notifications = notificationService.findAll(AuthenticatedUserThreadLocal.getUser().getName());
-            Iterables.filter(notifications, new Predicate<Notification>()
-            {
-                @Override
-                public boolean apply(@Nullable final Notification notification)
-                {
-                    return PLUGIN_KEY.equals(notification.getApplication());
-                }
-            });
-            return Response.ok(Iterables.limit(notifications, MAX_RESULT)).build();
-        }
-        else
-        {
-            return Response.status(Response.Status.FORBIDDEN).build();
-        }
+        return Response.status(Response.Status.FORBIDDEN).build();
+    }
+}
+
+@GET
+public Response findAllNotifications() throws Exception{
+    if (isAdmin()) {
+        // find all the notifications received by the logged in user
+        final Iterable<Notification> notifications = notificationService.findAll(AuthenticatedUserThreadLocal.getUser().getName());
+        Iterables.filter(notifications, new Predicate<Notification>() {
+            @Override
+            public boolean apply(@Nullable final Notification notification){
+                // we are only interested in the notification send by our plugin
+                return PLUGIN_KEY.equals(notification.getApplication());
+            }
+        });
+        // Let's only display the last MAX_RESULT notifications
+        return Response.ok(Iterables.limit(notifications, MAX_RESULT)).build();
+    }
+    else {
+        return Response.status(Response.Status.FORBIDDEN).build();
     }
+}
 
     private boolean isAdmin()
     {
         return permissionManager.isConfluenceAdministrator(AuthenticatedUserThreadLocal.getUser());
     }
 
-    private void sendNotificationToAllUsers(final String title, final String message)
-            throws ExecutionException, InterruptedException
+/**
+ * Iterate on all users of the "confluence-user" group and send a notification to each of them
+ * @param title the title of the notification to send
+ * @param message the body of the notification to send
+ * @throws ExecutionException
+ * @throws InterruptedException
+ */
+private void sendNotificationToAllUsers(final String title, final String message)
+        throws ExecutionException, InterruptedException
+{
+    Pager<String> memberNames = userAccessor.getMemberNames(new DefaultGroup(UserAccessor.GROUP_CONFLUENCE_USERS));
+    while (memberNames.iterator().hasNext())
     {
-        Pager<String> memberNames = userAccessor.getMemberNames(new DefaultGroup(UserAccessor.GROUP_CONFLUENCE_USERS));
-        while (memberNames.iterator().hasNext())
-        {
-            sendNotification(memberNames.iterator().next(), title, message);
-        }
+        sendNotification(memberNames.iterator().next(), title, message);
     }
+}
 
-    private void sendNotification(final String user, final String title, final String message)
-            throws InterruptedException, ExecutionException
-    {
-        Notification notification = notificationService.createOrUpdate(user, new NotificationBuilder()
-                .application(PLUGIN_KEY) // a unique key that identifies your plugin
-                .title("Message from your beloved administrator")
-                .itemTitle(title)
-                .description(message)
-                .globalId("com.example.plugins.tutorial.confluence.notification") // a key to aggregate notifications
-                .createNotification()).get();
-    }
+/**
+ * Create a single notification and send it to user
+ * @param user the user who will receive the notification
+ * @param title the title of the notification
+ * @param message the body of the notification
+ * @return the created notification
+ * @throws InterruptedException
+ * @throws ExecutionException
+ */
+private Notification sendNotification(final String user, final String title, final String message) throws InterruptedException, ExecutionException{
+    Notification notification = notificationService.createOrUpdate(user, new NotificationBuilder()
+            .application(PLUGIN_KEY) // a unique key that identifies your plugin
+            .title("Message from your beloved administrator")
+            .itemTitle(title)
+            .description(message)
+            .globalId("com.example.plugins.tutorial.confluence.notification") // a key to aggregate notifications
+            .createNotification()).get();
+    return notification;
+}
 
 }