Intercept Jira issues macro in Confluence to use another different user

We have a confluence instance that uses the Jira Issues macro.
Users that access Confluence won’t have access to the Jira project, so I wanted to intercept the call to Jira issues and use a default user.

My servlet filter is as follows:

<servlet-filter name="Portal Jira Issues Filter" key="jiraIssuesFilter"
                      class="com.xxx.filter.JiraIssuesFilter" location="before-decoration">
    <url-pattern>/rest/jiraanywhere/1.0/jira/clientIds*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</servlet-filter>

Which triggers nicely on the load of the page.
I have the following code in the doFilter method:

package com.xxx.filter;

import com.atlassian.applinks.api.ApplicationLink;
import com.atlassian.applinks.api.ApplicationLinkRequest;
import com.atlassian.applinks.api.ApplicationLinkRequestFactory;
import com.atlassian.applinks.api.ApplicationLinkService;
import com.atlassian.applinks.api.application.jira.JiraApplicationType;

import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
import com.atlassian.confluence.user.ConfluenceUser;
import com.atlassian.confluence.user.ConfluenceUserManager;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import com.atlassian.sal.api.net.Request;

import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


@Scanned
@Named("jiraIssuesFilter")
public class JiraIssuesFilter implements Filter {

    @ComponentImport
    private final com.atlassian.sal.api.user.UserManager salUserManager;

    @ComponentImport
    private final ApplicationLinkService applicationLinkService;

    @ComponentImport
    private final ConfluenceUserManager userManager;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Inject
    public JiraIssuesFilter(final com.atlassian.sal.api.user.UserManager salUserManager,
                            final ApplicationLinkService applicationLinkService,
                            final ConfluenceUserManager userManager) {
        this.salUserManager = salUserManager;
        this.applicationLinkService = applicationLinkService;
        this.userManager = userManager;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        String url = httpRequest.getRequestURI();


        ApplicationLink jiraLink = applicationLinkService.getPrimaryApplicationLink(JiraApplicationType.class);

        try {
            ConfluenceUser user = (ConfluenceUser) this.userManager.getUser("myportal");
            AuthenticatedUserThreadLocal.set(user);
            ApplicationLinkRequestFactory requestFactory = jiraLink.createAuthenticatedRequestFactory();
            ApplicationLinkRequest request = requestFactory.createRequest(Request.MethodType.valueOf(httpRequest.getMethod()), url);
            request.execute();

        } catch (Exception e) {
            e.printStackTrace();
        }


        filterChain.doFilter((ServletRequest) servletRequest , (ServletResponse) servletResponse);
    }

    @Override
    public void destroy() {}
}

When I try to install the plugin, it hangs and I can’t work out why. There are no errors in the logs.

My dependencies are:

 <dependency>
            <groupId>com.atlassian.confluence</groupId>
            <artifactId>confluence</artifactId>
            <version>${confluence.version}</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.atlassian.plugin</groupId>
            <artifactId>atlassian-spring-scanner-annotation</artifactId>
            <version>${atlassian.spring.scanner.version}</version>
            <scope>compile</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.atlassian.applinks/applinks-api -->
        <dependency>
            <groupId>com.atlassian.applinks</groupId>
            <artifactId>applinks-api</artifactId>
            <version>7.1.3</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.atlassian.plugin</groupId>
            <artifactId>atlassian-spring-scanner-runtime</artifactId>
            <version>${atlassian.spring.scanner.version}</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.3</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.atlassian.sal</groupId>
            <artifactId>sal-api</artifactId>
            <version>${atlassian.sal-api}</version>
            <scope>provided</scope>
        </dependency>

Any help to identify what I’m missing would be fantastic.

marina.

Hard to figure out without any log messages.

One thing that popped out from your setup. You are importing two different UserManager components. I have seen an issue like this before and I think the solution was to give them different “keys”

@ComponentImport("confluenceUserManager")
private final ConfluenceUserManager userManager;

@ComponentImport("salUserManager")
 private final com.atlassian.sal.api.user.UserManager salUserManager;