Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,284
Community Members
 
Community Events
176
Community Groups

How to component import with spring scanner?

Hi everyone,

So I am now working on the posting notifications in confluence tutorial. Which can be found here https://developer.atlassian.com/server/confluence/posting-notifications-in-confluence/ . I've run into many many issues along the way. Now I am wondering how I would component import with the spring scanner. I've read about the spring scanner stuff but I am still unsure if I am doing it right. You see in the tutorial they say to put this line  in the atlasssian-plugin.xml 

<component-import key="notificationService" interface="com.atlassian.mywork.service.LocalNotificationService"/>

 but when I do that I get an error that states:

atlassian-plugin.xml contains a definition of component-import. This is not allowed when Atlassian-Plugin-Key is set.

 Based off of what I've read I edited my NotificationResource.java to look like this

package com.example.plugins.tutorial.confluence.notification.resource;

import java.util.concurrent.ExecutionException;

import javax.annotation.Nullable;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.springframework.stereotype.Scanned;

import com.atlassian.confluence.security.PermissionManager;
import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import com.atlassian.confluence.user.UserAccessor;
import com.atlassian.mywork.model.Notification;
import com.atlassian.mywork.model.NotificationBuilder;
import com.atlassian.mywork.service.LocalNotificationService;
import com.atlassian.user.impl.DefaultGroup;
import com.atlassian.user.search.page.Pager;

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
/**
* A resource of creating and listing notifications
*/
@Path ("/")
@Consumes (MediaType.APPLICATION_JSON)
@Produces (MediaType.APPLICATION_JSON)
@Scanned
public class NotificationResource {
private final LocalNotificationService notificationService;
private final UserAccessor userAccessor;
private final PermissionManager permissionManager;

public NotificationResource(final LocalNotificationService notificationService, final UserAccessor userAccessor, final PermissionManager permissionManager) {
this.notificationService = notificationService;
this.userAccessor = userAccessor;
this.permissionManager = permissionManager;
}
@POST
public Response createNotification(@FormParam ("title") String title, @FormParam ("message") String message)
throws Exception {
if (isAdmin()) {
sendNotificationToAllUsers(title, message);
return Response.ok().build();
}
else {
return Response.status(Response.Status.FORBIDDEN).build();
}
}
@GET
@Produces (MediaType.APPLICATION_JSON)
public Response findAllNotifications() throws Exception{
if (isAdmin()){
// TODO add code to list past notifications sent by this plugin
return Response.ok().build();
}
else{
return Response.status(Response.Status.FORBIDDEN).build();
}
}
private boolean isAdmin(){
return permissionManager.isConfluenceAdministrator(AuthenticatedUserThreadLocal.getUser());
}

/**
* 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("${project.groupId}.${project.artifactId}") // a unique key that identifies your plugin
.title("Message from your beloved administrator")
.itemTitle(title)
.description(message)
.groupingId("com.example.plugins.tutorial.confluence.notification") // a key to aggregate notifications
.createNotification()).get();
return notification;
}

/**
* Iterate on all users of the "confluence-users" group and send a notification to each of them
* @param title the title of the notification to sen
* @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));
for (String memberName : memberNames) {
sendNotification(memberName, title, message);
}
}


}

 Any help would be greatly appreciated. Thank you for your time. 

1 answer

@Diana Bianco have you solved it? I am in the same situation.

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events