How do I get this spring scanner to work? :(

Hi everyone,

So I am working on the writing an admin space screen tutorial and I’m having trouble with the servlet part. I’ve updated my pom.xml so that I can use the spring scanner 2.1.8. However, in the tutorial they use the outdated tags. I am trying to replace them with the @ComponentImport however it is not working.

package com.atlassian.examples.spring;

import com.atlassian.confluence.spaces.Space;
import com.atlassian.confluence.spaces.SpaceManager;
import com.atlassian.confluence.user.ConfluenceUser;
import com.atlassian.confluence.user.UserAccessor;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.sal.api.auth.LoginUriProvider;
import com.atlassian.sal.api.user.UserKey;
import com.atlassian.sal.api.user.UserManager;
import com.atlassian.templaterenderer.TemplateRenderer;
import com.google.common.collect.ImmutableMap;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;

import static java.util.Objects.requireNonNull;


@Named
public class ViewLinkServlet extends HttpServlet {
    private static final String SPACE_ID_KEY = "spaceId";
    @ComponentImport
    private final UserManager userManager;
    @ComponentImport
    private final UserAccessor userAccessor;
    @ComponentImport
    private final LoginUriProvider loginUriProvider;
    @ComponentImport
    private final SpaceManager spaceManager;
    @ComponentImport
    private final TemplateRenderer templateRenderer;

    @Inject
    public ViewLinkServlet( UserManager userManager, UserAccessor userAccessor, LoginUriProvider loginUriProvider,
                            SpaceManager spaceManager, TemplateRenderer templateRenderer) {
        this.userManager = requireNonNull(userManager);
        this.userAccessor = requireNonNull(userAccessor);
        this.loginUriProvider = requireNonNull(loginUriProvider);
        this.spaceManager = requireNonNull(spaceManager);
        this.templateRenderer = requireNonNull(templateRenderer);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Find current user
        UserKey userKey = userManager.getRemoteUserKey(request);
        ConfluenceUser user = userAccessor.getExistingUserByKey(userKey);
        if (userKey == null || user == null) {
            redirectToLogin(request, response);
            return;
        }

        // Find space in the request
        Space space = null;
        String spaceIdString = request.getParameter(SPACE_ID_KEY);
        if (spaceIdString != null) {
            try {
                long spaceId = Long.parseLong(spaceIdString);
                space = spaceManager.getSpace(spaceId);
            } catch (NumberFormatException nfe) {
                // log a warning if needed
            }
        }

        if (space == null) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        response.setContentType("text/html;charset=utf-8");
        templateRenderer.render("/templates/view-link-action.vm",
                ImmutableMap.of("spaceId", space.getId()),
                response.getWriter());

    }

    private void redirectToLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.sendRedirect(loginUriProvider.getLoginUri(getUri(request)).toASCIIString());
    }

    private URI getUri(HttpServletRequest request) {
        StringBuffer builder = request.getRequestURL();
        if (request.getQueryString() != null) {
            builder.append("?");
            builder.append(request.getQueryString());
        }
        return URI.create(builder.toString());
    }
}

What am I doing wrong?

Just starting to find my way around working with the spring scanner atm too, so I might not be of much help here, but did you read through the readme here: https://bitbucket.org/atlassian/atlassian-spring-scanner/src/master/ ?

In particular, did you add the spring.xml here src/main/resources/META-INF/spring/? Your post suggests you only adjusted the pom.xml, while the documentation for the spring scanner suggests this is only part of the necessary work to get the scanner setup correctly for a project.

1 Like

you need to use @Component instead of @Named , you can poke around some of the configurations in here https://bitbucket.org/developerexperience/confluence-devhelp/src/main/

Your code is perfectly fine. As Stefan indicated you also have to adjust the plugin-context. Our looks as follows: https://bitbucket.org/apurde/simple-cite/src/master/src/main/resources/META-INF/spring/plugin-context.xml

Thanks!! This was very helpful!