@RequestParam(value = "async", required = false, defaultValue = "false") Boolean async,
@RequestParam(value = "jsonp", required = false, defaultValue = "false") Boolean jsonp,
HttpServletRequest request,
HttpSession session) throws ServletException, InterruptedException, SVGConverterException, NoSuchElementException, PoolException, TimeoutException, IOException, ZeroRequestParameterException {
MimeType mime = getMime(type);
String randomFilename = null;
String jsonpCallback = "";
boolean isAndroid = request.getHeader("user-agent") != null && request.getHeader("user-agent").contains("Android");
if ("GET".equalsIgnoreCase(request.getMethod())) {
// Handle redirect downloads for Android devices, these come in without request parameters
String tempFile = (String) session.getAttribute("tempFile");
session.removeAttribute("tempFile");
if (tempFile != null && !tempFile.isEmpty()) {
logger.debug("filename stored in session, read and stream from filesystem");
String basename = FilenameUtils.getBaseName(tempFile);
String extension = FilenameUtils.getExtension(tempFile);
return getFile(basename, extension);
}
}
// check for visitors who don't know this domain is really only for the exporting service ;)
if (request.getParameterMap().isEmpty()) {
throw new ZeroRequestParameterException();
}
/* Most JSONP implementations use the 'callback' request parameter and this overwrites
* the original callback parameter for chart creation with Highcharts. If JSONP is
* used we recommend using the requestparameter callbackHC as the callback for Highcharts.
* store the callback method name and reset the callback parameter,
* otherwise it will be used when creation charts
*/
if (jsonp) {
async = true;
jsonpCallback = callback;
callback = null;
if (callbackHC != null) {
callback = callbackHC;
}
}
if (isAndroid || MimeType.PDF.equals(mime) || async) {
randomFilename = createRandomFileName(mime.name().toLowerCase());
}
/* If randomFilename is not null, then we want to save the filename in session, in case of GET is used later on*/
if (isAndroid) {
logger.debug("storing randomfile in session: " + FilenameUtils.getName(randomFilename));
session.setAttribute("tempFile", FilenameUtils.getName(randomFilename));
}
String output = convert(svg, mime, width, scale, options, constructor, callback, globalOptions, randomFilename);
ByteArrayOutputStream stream;
HttpHeaders headers = new HttpHeaders();
if (async) {
String link = TempDir.getDownloadLink(randomFilename);
stream = new ByteArrayOutputStream();
if (jsonp) {
StringBuilder sb = new StringBuilder(jsonpCallback);
sb.append("('");
sb.append(link);
sb.append("')");
stream.write(sb.toString().getBytes("utf-8"));
headers.add("Content-Type", "text/javascript; charset=utf-8");
} else {
stream.write(link.getBytes("utf-8"));
headers.add("Content-Type", "text/html; charset=UTF-8");
}
} else {
headers.add("Content-Type", mime.getType() + "; charset=utf-8");
if (randomFilename != null && randomFilename.equals(output)) {
stream = writeFileToStream(randomFilename);
} else {
boolean base64 = !mime.getExtension().equals("svg");
stream = outputToStream(output, base64);
}
filename = getFilename(filename);
headers.add("Content-Disposition",
"attachment; filename=" + filename.replace(" ", "_") + "." + mime.name().toLowerCase());
}
headers.setContentLength(stream.size());
return new HttpEntity<byte[]>(stream.toByteArray(), headers);