How to export JIRA data and redirect to download url?

Alex M March 12, 2014

Hi guys,

I'm trying to export some data from JIRA into a file and after that I want to redirect the user to the file's download URL.

So what I need to achieve is:

  • Create and save a file in JIRA's home directory - no ideia how to do that. Is there any available in the Java API to do that?
  • Redirect the user to the just created file. In order to have the browser pop up that "Save file" dialog. Is there a way to make the file available as a resource right after it was generated?

Have anyone already done something similar? Or what should I be looking for to accomplish this?

Thanks!

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Boris Georgiev _Appfire_
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 12, 2014

Do you really need to export it into a file ? If not you can directly pass the file contents to the user.

Here's a sample code for what you've described (should work with a few modifications):

JiraHome jiraHome = ComponentAccessor.getComponent(JiraHome.class);
		File file = new File(jiraHome.getHome(), "myfile.txt");
		// Write the contents to the file
.......
		// Read the file and write it to the response
		HttpServletResponse response = getHttpResponse();
		// set to binary type for example
		response.setContentType("application/octet-stream");
		response.setContentLength((int) file.length());

		// forces download
		String headerKey = "Content-Disposition";
		String headerValue = String.format("attachment; filename=\"%s\"", file.getName());
		response.setHeader(headerKey, headerValue);

		FileInputStream fileInputStream = new FileInputStream(file);
		ServletOutputStream outputStream = response.getOutputStream();
		try {
			IOUtils.copy(fileInputStream, outputStream);
		} finally {
			fileInputStream.close();
			outputStream.close();
		}

You can use this code from a Servlet or Webwork action.

If you really do not need to save the file to JIRA_HOME you can directly write the generated contents to the output stream.

Alex M March 13, 2014

Hi Boris,

Thanks for your quick reply! The code does what I intended. :)

However, I have a small issue. I'm using it from a Webwork action and I get the following exception: "java.lang.IllegalStateException: getOutputStream() has already been called for this response". I believe that this is happening because I'm returning a SUCCESS (and by doing so redirecting the browser to my success view) string after executing the code that you've sent me.

Do you know what should be the work arround for this?

Boris Georgiev _Appfire_
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 13, 2014

It seems I was wrong when I said that you can use the code from webwork action.

Just create a servlet for downloading the file.

Boris Georgiev _Appfire_
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 13, 2014

Or you might also try to get the response by calling ServletActionContext.getResponse(), this seems to be the right way of getting the output stream.

Also then you may return NONE instead SUCCESS.

Alex M March 13, 2014

That is the way to go!

Thank you very much for your help Boris.

Cheers!

TAGS
AUG Leaders

Atlassian Community Events