How to component import with spring scanner?

Diana Bianco July 2, 2020

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

0 votes
Ibrahim Ozturk
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 15, 2020

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events