Call AJAX/REST from JIRA plugin v1

Radek Kantor
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.
January 24, 2012

Hi,

how can I call ajax from JIRA plugin v1? Exist some workaround or it is impossible get data from server side without page refresh? Rest plugin module is not supported in v1, so I can not use code like this:

AJS.$.ajax({
  url: "/rest/myplugin/1.0/listData",
  type: "GET",
  dataType: "json",
  error: function() { },
  success: function(data, textStatus, XMLHttpRequest) {
	process(data);
	}
});

Combination javascript, ajax, rest works great in plugins v2, but impossible in v1. One solution is read all necessary data on page load (action execution) and store it in some javascript objects. Than dynamicly change page based on user actions or maybe I can used dwr.

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
JamieA
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.
January 24, 2012

Yeah dwr works fine though you need to ship it with your plugin.

A rest module is not possible in plugins1, but there is nothing to stop you using a servlet in plugins1 that you can send and receive JSON from. I do that in one of my plugins, works fine.

The important bits are:

        resp.setContentType("application/json")

        PrintWriter out = resp.getWriter()
        out.write(new JSONObject(["output":output, "errors":errors]).toString())

Radek Kantor
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.
January 24, 2012

Thanks Jamie, I forgot on servlet solution, will try to call servlet from javascript and process the output.

Kinto Soft
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.
February 28, 2013

eah dwr works fine though you need to ship it with your plugin.

Did you get it? It requires a dwr.xml configuration file be present in the WEB-INF directory. As it belongs to JIRA and plug-ins cannot access to it, how do you configure DWR in order to load the dwr.xml file??

Many thanks,

Pablo

Kinto Soft
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.
March 1, 2013

Hi Jamie,

Regardless the initialization issue (DWR is quite flexible and allows be initialized by using some alternative ways supported by JIRA plug-ins), the main issue would be that DWR seems to create javascript files on the fly (for instance, if you publish a Person.java Object at the server side, a Person.js is required at the client side). As those javascript files are created at runtime, they cannot be bundled within the plug-in jar, so I don't figure out how to integrate DWR with JIRA through a plug-in :(

Any ideas?

Pablo.

JamieA
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.
March 2, 2013

Hrm... haven't used dwr for a few years. I thought you just included them then the DWR servlet made them available. I had a look at my source repo but the earliest version I checked in only seems to have a few remnants of DWR.

Sure you actually need DWR?

Kinto Soft
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.
March 2, 2013

I would say yes. I know Ajax/jQuery plus REST is a good solution for many cases, mainly for getting data related to JIRA, but I'm planning to develop a plugin creating its own Java data structures and DWR would make them inmendiatelly accesible from the client and the overall soultion would portable to other platforms without changes (or few changes at least).

As I mentioned, the DWR servlet can be loaded and initilized without much problems from a JIRA plug-in, however the dynamically created javascript files (one per each Java class accessed) are the problem. It looks like DWR has been designed to run inside a Java App Server (Tomcat,...) hence, it expects get access to the WEB-INF/ directory structure to publish the javascripts, but it is not possible from a plug-in :(.

Or maybe there is a magic parameter to tell DMR "sotore the javascript files there" (best cached in memory), but I didn't find it. :((

Kinto Soft
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.
March 2, 2013

Hmm, I googled it again found the overridePath parameter. It could help to point DWR to locate files in alternative url paths. I'll investigate it and come back with the results :)

JamieA
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.
March 2, 2013

I copied my implementation from an old version of either confluence or a confluence plugin. This contains a ConfigurableDWRServlet... might help: https://svn.atlassian.com/fisheye/browse/public/contrib/repository/atlassian-plugin-repository/trunk/jira/src/main/java/com/atlassian/plugin/repository/dwr/ConfigurableDWRServlet.java?r=17649

Kinto Soft
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.
March 3, 2013

Thanks for the code!. It is a very old version of DWR, anyway trying to debug the same code in the newer version I found the problem. Now, it's working fine!!.

The code that you provided is not necessary because DWR can be intialized from a plug-in without problems: in my case, I used annotations rather than a dwr.xml file to avoid problems related to locate the that file.

Regarding the location of the javascripts files it was my fault. I put some static required javascripts (engine.js) and son on in the plug-in (as downloadable resources) and they must be downloaded by using the DWR servlet. Regarding the dynamic interfaces (.js files) they can be downloaded by using the DWR servlet too without problems. I forget to add an "/*" in my atlassian-pligin.xml servlet descriptor to redirect any call through the DWRServlet.

So, your initial words

Yeah dwr works fine though you need to ship it with your plugin.

were right! ;)

Thanks,

Pablo.

0 votes
Radek Kantor
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.
March 1, 2013

Hi,

I implement solution via servlet, example code for JIRA 4.4 below.

In atlassian-plugin.xml define servlet:

<servlet key="aafv-servlet-fv" name="AAFV Servlet Field Value" i18n-name-key="aafv.servlet.fv.name" 
	class="com.mycompany.jiraplugin.aafv.web.FieldValueServlet">
	<description key="aafv.servlet.fv.desc">Returns field value content.</description>
    <url-pattern>/aafv/fieldvalue</url-pattern>
</servlet>

Servlet execude code, where FieldValueServlet extends HttpServlet:

@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/plain");
		resp.setCharacterEncoding("UTF-8");
		
		String fieldId = req.getParameter("fieldId");
		String fieldValue = req.getParameter("fieldValue");
		Long projectId = null;
		try {
			projectId = Long.parseLong(req.getParameter("projectId"));
		}
		catch (NumberFormatException nfe) {
		}
		
		String issueType = req.getParameter("issueType");
		String emptyOption = req.getParameter("emptyOption");
		
		resp.getWriter().write(getFieldValueContent(fieldId, fieldValue, projectId, issueType, emptyOption));
	}

Call from javascript, where selvlet returns string html code content which is assigned as innerHTML for specific element.

var base_url;
setBaseVariables();
			
function updateFieldValueFilter(fieldValue, emptyOption) {
	var projectId = AJS.$("#projectIdFilter").val();
	var issueType = AJS.$("#issueTypeFilter").val();
	var fieldId = AJS.$("#fieldIdFilter").val();
	AJS.$.get(servletFieldValueAddress(fieldId, fieldValue, projectId, issueType, emptyOption), function(data) {
		document.getElementById("fieldValueCell").innerHTML = data;
    });
}
			
function setBaseVariables() {
	var element = AJS.$("a#admin_summary"), contextUrl = element.context.URL;
	base_url = contextUrl.substring(0, contextUrl.indexOf(/secure/));
}
	
function servletFieldValueAddress(fieldId, fieldValue, projectId, issueType, emptyOption) {
	if (!base_url) {
		return null;
	}
	return base_url + "/plugins/servlet/aafv/fieldvalue?fieldId=" + fieldId + "&fieldValue=" + fieldValue + "&projectId=" + projectId + "&issueType=" + issueType + "&emptyOption=" + emptyOption;
}
TAGS
AUG Leaders

Atlassian Community Events