How to trigger java code with Web Item?

Tanner Reese June 26, 2018

How do you trigger a java class to execute when a web item is clicked? I would also like to be able to open a modal dialog window with the executed java.

I have seen situations in which people create dialog windows with JS, but is it possible to have the JS code propagate an event back to the server side (java code) with AJAX (or anything else)?

2 answers

1 accepted

0 votes
Answer accepted
Matias Stachowski
Contributor
July 26, 2018

How do you trigger a java class to execute when a web item is clicked?

To simply activate some java code directly from a web-item, you can either bind it to a webwork action or a servlet.

 

Here is a servlet example:

<web-item name="Button" i18n-name-key="button.name" key="button-key"
section="your.section.in.jira" weight="1000">
<description key="desckey">This button activates some java</description>
<label key="button.label"/>
<link linkId="button-item">/jira/plugins/servlet/myservlet?issueKey=$issue</link>
<context-provider class="io.aety.jira.webwork.RequestContextProvider"/>
</web-item>

<servlet
name="My Java Servlet" i18n-name-key="servlet.name" key="servlet-key"
class="io.aety.servlet.MyJavaClass">
<description key="servlet.description">This servlet can do java stuff</description>
<url-pattern>/myservlet</url-pattern>
</servlet>

 

Then add your java class, which you should put in (this example) at io.aety.servlet.MyJavaClass, here this class simply adds a vote to the issue.

 

@Scanned
public class MyJavaClass extends HttpServlet{
private static final Logger log = LoggerFactory.getLogger(PublicCustomerVote.class);

@JiraImport
private IssueService issueService;
@JiraImport
private ProjectService projectService;
@JiraImport
private SearchService searchService;
@JiraImport
private JiraAuthenticationContext authenticationContext;
@JiraImport
private ConstantsManager constantsManager;


public MyJavaClass(IssueService issueService, ProjectService projectService,
SearchService searchService,
JiraAuthenticationContext authenticationContext,
ConstantsManager constantsManager) {
this.issueService = issueService;
this.projectService = projectService;
this.searchService = searchService;
this.authenticationContext = authenticationContext;
this.constantsManager = constantsManager;
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
//Get issue
IssueManager issueManager = ComponentAccessor.getIssueManager();
String issueKey = req.getParameter("issueKey");
Issue issue = issueManager.getIssueByCurrentKey(issueKey);


ComponentAccessor.getVoteManager().addVote(authenticationContext.getLoggedInUser(),currentIssue);

resp.setContentType("text/html");
resp.getWriter().write("<html><body>You ("+authenticationContext.getLoggedInUser().getUsername()+")" +
"voted for issue "+issueKey+"!</body></html>");
}
}

 

In your context provider (in this example located at io.aety.jira.webwork.RequestContextProvider) for the web item, you can find out what context variables you have available, in this example, we fed the parameter $issue from the web item to the servlet, but you can find out which parameters that you have available at your web item by printing them out, and you can even add your own to the map if you need to.

 

public class RequestContextProvider implements ContextProvider {

@Override
public void init(Map<String, String> map) throws PluginParseException {

}

@Override
public Map<String, Object> getContextMap(Map<String, Object> map) {
if (map != null) {
for (Map.Entry<String,Object> entry : map.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue().toString());
}
} else {
//no context map found
}
return map; //dont add anything to the map, but you can add parameters if you want
}
}

 

0 votes
Alexey Matveev
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 26, 2018

Hello,

You can call a Rest Endpoint in your JS code. For example, like this:

function getCurrentUserName()
{
var user;
     AJS.$.ajax({
        url: "/rest/gadget/1.0/currentUser",
        type: 'get',
        dataType: 'json',
        async: false,
        success: function(data) {
            user = data.username;
        } 
     });
     return user;
}

the Rest Endpoint is a Java module.

Suggest an answer

Log in or Sign up to answer