List of usage examples for javax.xml.transform.sax SAXTransformerFactory setFeature
public abstract void setFeature(String name, boolean value) throws TransformerConfigurationException;
Set a feature for this TransformerFactory and Transformer s or Template s created by this factory.
From source file:ee.ria.xroad.proxy.serverproxy.MetadataServiceHandlerImpl.java
private static SAXTransformerFactory createSaxTransformerFactory() { try {//from w ww .j a v a2 s.co m SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance(); factory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", true); return factory; } catch (TransformerConfigurationException e) { throw new RuntimeException("unable to create SAX transformer factory", e); } }
From source file:org.apache.syncope.core.logic.report.ReportJobDelegate.java
@Transactional public void execute(final String reportKey) throws JobExecutionException { Report report = reportDAO.find(reportKey); if (report == null) { throw new JobExecutionException("Report " + reportKey + " not found"); }//www .j a v a 2 s. c o m if (!report.isActive()) { LOG.info("Report {} not active, aborting...", reportKey); return; } // 1. create execution ReportExec execution = entityFactory.newEntity(ReportExec.class); execution.setStatus(ReportExecStatus.STARTED); execution.setStart(new Date()); execution.setReport(report); execution = reportExecDAO.save(execution); report.add(execution); report = reportDAO.save(report); // 2. define a SAX handler for generating result as XML TransformerHandler handler; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); zos.setLevel(Deflater.BEST_COMPRESSION); try { SAXTransformerFactory tFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); tFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true); handler = tFactory.newTransformerHandler(); Transformer serializer = handler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, SyncopeConstants.DEFAULT_ENCODING); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); // a single ZipEntry in the ZipOutputStream zos.putNextEntry(new ZipEntry(report.getName())); // streaming SAX handler in a compressed byte array stream handler.setResult(new StreamResult(zos)); } catch (Exception e) { throw new JobExecutionException("While configuring for SAX generation", e, true); } execution.setStatus(ReportExecStatus.RUNNING); execution = reportExecDAO.save(execution); // 3. actual report execution StringBuilder reportExecutionMessage = new StringBuilder(); try { // report header handler.startDocument(); AttributesImpl atts = new AttributesImpl(); atts.addAttribute("", "", ReportXMLConst.ATTR_NAME, ReportXMLConst.XSD_STRING, report.getName()); handler.startElement("", "", ReportXMLConst.ELEMENT_REPORT, atts); // iterate over reportlet instances defined for this report for (ReportletConf reportletConf : report.getReportletConfs()) { Class<? extends Reportlet> reportletClass = implementationLookup .getReportletClass(reportletConf.getClass()); if (reportletClass == null) { LOG.warn("Could not find matching reportlet for {}", reportletConf.getClass()); } else { // fetch (or create) reportlet Reportlet reportlet; if (ApplicationContextProvider.getBeanFactory().containsSingleton(reportletClass.getName())) { reportlet = (Reportlet) ApplicationContextProvider.getBeanFactory() .getSingleton(reportletClass.getName()); } else { reportlet = (Reportlet) ApplicationContextProvider.getBeanFactory() .createBean(reportletClass, AbstractBeanDefinition.AUTOWIRE_BY_TYPE, false); ApplicationContextProvider.getBeanFactory().registerSingleton(reportletClass.getName(), reportlet); } // invoke reportlet try { reportlet.extract(reportletConf, handler); } catch (Throwable t) { LOG.error("While executing reportlet {} for report {}", reportlet, reportKey, t); execution.setStatus(ReportExecStatus.FAILURE); Throwable effective = t instanceof ReportException ? t.getCause() : t; reportExecutionMessage.append(ExceptionUtils2.getFullStackTrace(effective)) .append("\n==================\n"); } } } // report footer handler.endElement("", "", ReportXMLConst.ELEMENT_REPORT); handler.endDocument(); if (!ReportExecStatus.FAILURE.name().equals(execution.getStatus())) { execution.setStatus(ReportExecStatus.SUCCESS); } } catch (Exception e) { execution.setStatus(ReportExecStatus.FAILURE); reportExecutionMessage.append(ExceptionUtils2.getFullStackTrace(e)); throw new JobExecutionException(e, true); } finally { try { zos.closeEntry(); IOUtils.closeQuietly(zos); IOUtils.closeQuietly(baos); } catch (IOException e) { LOG.error("While closing StreamResult's backend", e); } execution.setExecResult(baos.toByteArray()); execution.setMessage(reportExecutionMessage.toString()); execution.setEnd(new Date()); reportExecDAO.save(execution); } }
From source file:org.apache.syncope.core.persistence.jpa.content.XMLContentExporter.java
@Override public void export(final String domain, final OutputStream os, final String uwfPrefix, final String gwfPrefix, final String awfPrefix) throws SAXException, TransformerConfigurationException { if (StringUtils.isNotBlank(uwfPrefix)) { TABLE_PREFIXES_TO_BE_EXCLUDED.add(uwfPrefix); }/*from w w w .ja v a 2 s . c o m*/ if (StringUtils.isNotBlank(gwfPrefix)) { TABLE_PREFIXES_TO_BE_EXCLUDED.add(gwfPrefix); } if (StringUtils.isNotBlank(awfPrefix)) { TABLE_PREFIXES_TO_BE_EXCLUDED.add(awfPrefix); } StreamResult streamResult = new StreamResult(os); final SAXTransformerFactory transformerFactory = (SAXTransformerFactory) SAXTransformerFactory .newInstance(); transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true); TransformerHandler handler = transformerFactory.newTransformerHandler(); Transformer serializer = handler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name()); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); handler.setResult(streamResult); handler.startDocument(); handler.startElement("", "", ROOT_ELEMENT, new AttributesImpl()); DataSource dataSource = domainsHolder.getDomains().get(domain); if (dataSource == null) { throw new IllegalArgumentException("Could not find DataSource for domain " + domain); } String dbSchema = ApplicationContextProvider.getBeanFactory().getBean(domain + "DatabaseSchema", String.class); Connection conn = null; ResultSet rs = null; try { conn = DataSourceUtils.getConnection(dataSource); final DatabaseMetaData meta = conn.getMetaData(); rs = meta.getTables(null, StringUtils.isBlank(dbSchema) ? null : dbSchema, null, new String[] { "TABLE" }); final Set<String> tableNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); while (rs.next()) { String tableName = rs.getString("TABLE_NAME"); LOG.debug("Found table {}", tableName); if (isTableAllowed(tableName)) { tableNames.add(tableName); } } LOG.debug("Tables to be exported {}", tableNames); // then sort tables based on foreign keys and dump for (String tableName : sortByForeignKeys(dbSchema, conn, tableNames)) { try { doExportTable(handler, dbSchema, conn, tableName, TABLES_TO_BE_FILTERED.get(tableName.toUpperCase())); } catch (Exception e) { LOG.error("Failure exporting table {}", tableName, e); } } } catch (SQLException e) { LOG.error("While exporting database content", e); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { LOG.error("While closing tables result set", e); } } DataSourceUtils.releaseConnection(conn, dataSource); if (conn != null) { try { if (!conn.isClosed()) { conn.close(); } } catch (SQLException e) { LOG.error("While releasing connection", e); } } } handler.endElement("", "", ROOT_ELEMENT); handler.endDocument(); }