How Servlet Filter Plugin Module communicate with JIRA software(Not Cloud)

HongChao Song June 7, 2017

Hi,

   I am confused about communcation mechanism between JIAR software(Not Cloud) and  Plugin Module.

 For example:

   I intend to utilize Servlet Filter Plugin to monitor user behaviors by checking some urls. During that process, I need to some information, such as projects, roles and so on. I  could achive that by calling rest api, but it means http been used  between Plugin and JIAR software, is it correct?

    Somebody could please give me more information?

 Thanks in advance.

 

1 answer

1 vote
Aleksandr Zuevich
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 7, 2017

Hi,

you can inject the dependencies or use static ComponentAccessor methods to get ProjectManager, UserManager and so on.

HongChao Song June 7, 2017

Could you please give me more info about  communcation mechanism between JIAR software(Not Cloud) and  Plugin Module?

Aleksandr Zuevich
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 7, 2017

You register your servlet-filter in atlassian-plugin.xml, then implements javax.servlet.Filter interface. So now you have access to servletRequest (HttpServletRequest), servletResponse and filterChain objects. E.g. you can also get current user by 

ApplicationUser loggedUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
HongChao Song June 18, 2017

It is very crucial clue, thank you very much.

 

Could you please give me more inforamtion, such as sample code?

thanks for your support.

Aleksandr Zuevich
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 19, 2017

Please take a look:

public class SprintServletFilter implements Filter {

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

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
RequestWrapper requestWrapper = new RequestWrapper(httpServletRequest);
ApplicationUser loggedUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
filterChain.doFilter(requestWrapper, servletResponse);
String requestBody = new String(requestWrapper.getRawData());
JsonObject jsonObject = new JsonParser().parse(requestBody).getAsJsonObject();
JsonElement jsonElement = jsonObject.get("sprintId");
if (!jsonElement.isJsonNull()) {
Long sprintId = jsonElement.getAsLong();
}
}

@Override
public void destroy() {

}
}
HongChao Song June 20, 2017

thanks.

 ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()  the result is null, and I tried isLoggedInUser() return false , getUser() return null

I don't know how to deal it, :(.

Aleksandr Zuevich
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 21, 2017

What do you have inside atlassian-plugin.xml?

Example:

<servlet-filter name="Sprint Servlet" key="sprintServlet"
class="com.xxx.plugins.jira.filter.SprintServletFilter"
location="before-decoration" weight="200">
<description>Tackle sprint events</description>
<url-pattern>/rest/greenhopper/1.0/sprint/*</url-pattern>
</servlet-filter>
HongChao Song June 21, 2017

thanks , I have fixed this issue by changing  location="after-encoding" to  location="before-decoration" .

Maybe the user info not been injected on the top of chain, I guess

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events