HttpServletResponse response, HttpServletRequest request)
throws ServletException, IOException, InterruptedException, SVGConverterException, NoSuchElementException, PoolException, TimeoutException {
long start1 = System.currentTimeMillis();
MimeType mime = getMime(type);
filename = getFilename(filename);
Float parsedWidth = widthToFloat(width);
Float parsedScale = scaleToFloat(scale);
options = sanitize(options);
String input;
boolean convertSvg = false;
if (options != null) {
// create a svg file out of the options
input = options;
callback = sanitize(callback);
} else {
// assume SVG conversion
if (svg == null) {
throw new ServletException(
"The manadatory svg POST parameter is undefined.");
} else {
svg = sanitize(svg);
if (svg == null) {
throw new ServletException(
"The manadatory svg POST parameter is undefined.");
}
convertSvg = true;
input = svg;
}
}
ByteArrayOutputStream stream = null;
if (convertSvg && mime.equals(MimeType.SVG)) {
// send this to the client, without converting.
stream = new ByteArrayOutputStream();
// add XML Doctype for svg
input = SVG_DOCTYPE + input;
stream.write(input.getBytes());
} else {
//stream = SVGCreator.getInstance().convert(input, mime, constructor, callback, parsedWidth, parsedScale);
stream = converter.convert(input, mime, constructor, callback, parsedWidth, parsedScale);
}
if (stream == null) {
throw new ServletException("Error while converting");
}
logger.debug(request.getHeader("referer") + " Total time: " + (System.currentTimeMillis() - start1));
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentLength(stream.size());
response.setStatus(HttpStatus.OK.value());
response.setHeader("Content-disposition", "attachment; filename=\""
+ filename + "." + mime.name().toLowerCase() + "\"");
IOUtils.write(stream.toByteArray(), response.getOutputStream());
response.flushBuffer();
}