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

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 Community Champion Jun 07, 2017

Hi,

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

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

Aleksandr Zuevich Community Champion Jun 07, 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();

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 Community Champion Jun 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() {

}
}

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 Community Champion Jun 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>

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Tuesday in Uncategorized

Friday fun: how many celebrates Midsummer holiday or is this a Swedish tradition only?

Any other country that celebrates Midsummer holiday (this friday 22 June)?  

32 views 3 1
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you