@Override
protected void giveService(HttpServletRequest req, HttpServletResponse res,
User user, LoginAnswer loginAnswer, FetchData fetch, String langId)
throws ServletException, IOException {
FileHolder fileHolder;
String schema = "";
SAXReader saxReader = new SAXReader();
// We are validating a variables xml file
fileHolder = (FileHolder) fetch
.getObject("IMPORTVARIABLES_XMLTOIMPORT");
schema = fetch.getValueAsString("IMPORTVARIABLES_XSLSTYLESHEET");
if (fileHolder != null && fileHolder.getFileContent() != null) {
ByteArrayInputStream is = null;
InputStream schemaIs = null;
URL schemaUrl = null;
try {
is = new ByteArrayInputStream(fileHolder.getFileContent());
Document doc = saxReader.read(is);
ClassLoader loader = Thread.currentThread()
.getContextClassLoader();
// Special hack to handle the case where we generate a PDF. todo: Maybe add an output parameter instead?
if (schema != null && schema.equals("OpenQReg40ToFOP.xsl")) {
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
Fop fop = null;
try {
res.setContentType("application/pdf");
fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, res.getOutputStream());
// Setup JAXP using identity transformer
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(loader.getResourceAsStream(schema)));
// Set the value of a <param> in the stylesheet
transformer.setParameter("versionParam", "2.0");
transformer.setParameter("language", fetch.getValueAsString("IMPORTVARIABLES_LANGUAGE"));
transformer.setParameter("controldate", fetch.getValueAsString("IMPORTVARIABLES_CONTROLDATE"));
transformer.setParameter("showallignoredate", fetch.getValueAsString("IMPORTVARIABLES_SHOWALLIGNOREDATE"));
transformer.setParameter("showextravariables", fetch.getValueAsString("IMPORTVARIABLES_SHOWEXTRAVARIABLES"));
transformer.setParameter("extravariableprefix", fetch.getValueAsString("IMPORTVARIABLES_EXTRAVARIABLESPREFIX"));
DocumentSource source = new DocumentSource(doc);
// Resulting SAX events (the generated FO) must be piped through to FOP
Result result = null;
result = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(source, result);
} catch (FOPException e) {
throw new RuntimeException(e);
}
} else {
schemaIs = loader.getResourceAsStream(schema);
if (schemaIs == null) {
schemaUrl = new URL(schema);
schemaIs = schemaUrl.openStream();
}
doc = styleDocument(doc, schemaIs);
res.setContentType("application/xml");
// res.setHeader(CONTENT_DISPOSTION, "attachment; filename=" + "OpenQReg41_" + fileHolder.getFileName());
res.setHeader(CONTENT_DISPOSTION, "attachment; filename=" + fileHolder.getFileName());
//Write the xml/html document nicely
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(res.getOutputStream(), format);
writer.write(doc);
writer.close();
}
//Make sure the OutputStream buffer of the Response is flushed.
res.flushBuffer();
} catch (MalformedURLException murle) {
log.log(Level.WARN, "URL is malformed: " + schema, murle);
} catch (IOException ioe) {
log.log(Level.WARN, "Unable to read schema: " + schema, ioe);
} catch (DocumentException doce) {
log.log(Level.WARN, "Unable to read schema: " + schema, doce);
} catch (TransformerConfigurationException tce) {
log.log(Level.ERROR, "Unable to configure transformer", tce);
} catch (TransformerException te) {
log.log(Level.ERROR, "Unable to transform document", te);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException ioe) {
log.log(Level.ERROR,
"Error on closing input stream (imposible)",
ioe);
}
}
}
fetch.addValue(fileHolder.getCompletePath(), "filepath");
}
}