Search in sources :

Example 1 with Reportlet

use of org.apache.syncope.core.persistence.api.dao.Reportlet in project syncope by apache.

the class DefaultReportJobDelegate method execute.

@Transactional
@Override
public void execute(final String reportKey) throws JobExecutionException {
    Report report = reportDAO.find(reportKey);
    if (report == null) {
        throw new JobExecutionException("Report " + reportKey + " not found");
    }
    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 {
        handler = TRANSFORMER_FACTORY.newTransformerHandler();
        Transformer serializer = handler.getTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
        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);
    status.set("Starting");
    // 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);
        status.set("Generating report header");
        // iterate over reportlet instances defined for this report
        for (int i = 0; i < report.getReportlets().size() && !interrupt; i++) {
            Optional<Reportlet> reportlet = ImplementationManager.buildReportlet(report.getReportlets().get(i));
            if (reportlet.isPresent()) {
                try {
                    status.set("Invoking reportlet " + report.getReportlets().get(i).getKey());
                    reportlet.get().extract(handler, status);
                } 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");
                }
            }
        }
        if (interrupt) {
            LOG.debug("Report job {} interrupted", reportKey);
            interrupted = true;
        }
        // report footer
        status.set("Generating 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 {
        status.set("Completed");
        try {
            zos.closeEntry();
            zos.close();
            baos.close();
        } 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);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Report(org.apache.syncope.core.persistence.api.entity.Report) Reportlet(org.apache.syncope.core.persistence.api.dao.Reportlet) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ReportExec(org.apache.syncope.core.persistence.api.entity.ReportExec) Date(java.util.Date) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) JobExecutionException(org.quartz.JobExecutionException) JobExecutionException(org.quartz.JobExecutionException) AttributesImpl(org.xml.sax.helpers.AttributesImpl) ZipOutputStream(java.util.zip.ZipOutputStream) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with Reportlet

use of org.apache.syncope.core.persistence.api.dao.Reportlet in project syncope by apache.

the class ImplementationManager method buildReportlet.

public static Optional<Reportlet> buildReportlet(final Implementation impl) throws InstantiationException, IllegalAccessException {
    switch(impl.getEngine()) {
        case GROOVY:
            return Optional.of(ImplementationManager.<Reportlet>buildGroovy(impl));
        case JAVA:
        default:
            Reportlet reportlet = null;
            ReportletConf reportletConf = POJOHelper.deserialize(impl.getBody(), ReportletConf.class);
            Class<? extends Reportlet> reportletClass = ApplicationContextProvider.getApplicationContext().getBean(ImplementationLookup.class).getReportletClass(reportletConf.getClass());
            if (reportletClass == null) {
                LOG.warn("Could not find matching reportlet for {}", reportletConf.getClass());
            } else {
                // fetch (or create) 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);
                }
                reportlet.setConf(reportletConf);
            }
            return Optional.ofNullable(reportlet);
    }
}
Also used : Reportlet(org.apache.syncope.core.persistence.api.dao.Reportlet) ReportletConf(org.apache.syncope.common.lib.report.ReportletConf) ImplementationLookup(org.apache.syncope.core.persistence.api.ImplementationLookup)

Example 3 with Reportlet

use of org.apache.syncope.core.persistence.api.dao.Reportlet in project syncope by apache.

the class ClassPathScanImplementationLookup method load.

@Override
@SuppressWarnings("unchecked")
public void load() {
    classNames = new EnumMap<>(ImplementationType.class);
    for (ImplementationType type : ImplementationType.values()) {
        classNames.put(type, new HashSet<>());
    }
    jwtSSOProviderClasses = new HashSet<>();
    reportletClasses = new HashMap<>();
    accountRuleClasses = new HashMap<>();
    passwordRuleClasses = new HashMap<>();
    correlationRuleClasses = new HashMap<>();
    auditAppenderClasses = new HashSet<>();
    ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
    scanner.addIncludeFilter(new AssignableTypeFilter(JWTSSOProvider.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(Reportlet.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(AccountRule.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(PasswordRule.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(PullCorrelationRule.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(ItemTransformer.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(SchedTaskJobDelegate.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(ReconFilterBuilder.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(LogicActions.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(PropagationActions.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(PullActions.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(PushActions.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(Validator.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(RecipientsProvider.class));
    scanner.addIncludeFilter(new AssignableTypeFilter(AuditAppender.class));
    scanner.findCandidateComponents(getBasePackage()).forEach(bd -> {
        try {
            Class<?> clazz = ClassUtils.resolveClassName(bd.getBeanClassName(), ClassUtils.getDefaultClassLoader());
            boolean isAbstractClazz = Modifier.isAbstract(clazz.getModifiers());
            if (JWTSSOProvider.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                classNames.get(ImplementationType.JWT_SSO_PROVIDER).add(clazz.getName());
                jwtSSOProviderClasses.add(clazz);
            }
            if (Reportlet.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                ReportletConfClass annotation = clazz.getAnnotation(ReportletConfClass.class);
                if (annotation == null) {
                    LOG.warn("Found Reportlet {} without declared configuration", clazz.getName());
                } else {
                    classNames.get(ImplementationType.REPORTLET).add(clazz.getName());
                    reportletClasses.put(annotation.value(), (Class<? extends Reportlet>) clazz);
                }
            }
            if (AccountRule.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                AccountRuleConfClass annotation = clazz.getAnnotation(AccountRuleConfClass.class);
                if (annotation == null) {
                    LOG.warn("Found account policy rule {} without declared configuration", clazz.getName());
                } else {
                    classNames.get(ImplementationType.ACCOUNT_RULE).add(clazz.getName());
                    accountRuleClasses.put(annotation.value(), (Class<? extends AccountRule>) clazz);
                }
            }
            if (PasswordRule.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                PasswordRuleConfClass annotation = clazz.getAnnotation(PasswordRuleConfClass.class);
                if (annotation == null) {
                    LOG.warn("Found password policy rule {} without declared configuration", clazz.getName());
                } else {
                    classNames.get(ImplementationType.PASSWORD_RULE).add(clazz.getName());
                    passwordRuleClasses.put(annotation.value(), (Class<? extends PasswordRule>) clazz);
                }
            }
            if (PullCorrelationRule.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                PullCorrelationRuleConfClass annotation = clazz.getAnnotation(PullCorrelationRuleConfClass.class);
                if (annotation == null) {
                    LOG.warn("Found pull correlation rule {} without declared configuration", clazz.getName());
                } else {
                    classNames.get(ImplementationType.ACCOUNT_RULE).add(clazz.getName());
                    correlationRuleClasses.put(annotation.value(), (Class<? extends PullCorrelationRule>) clazz);
                }
            }
            if (ItemTransformer.class.isAssignableFrom(clazz) && !isAbstractClazz && !clazz.equals(JEXLItemTransformerImpl.class)) {
                classNames.get(ImplementationType.ITEM_TRANSFORMER).add(clazz.getName());
            }
            if (SchedTaskJobDelegate.class.isAssignableFrom(clazz) && !isAbstractClazz && !PullJobDelegate.class.isAssignableFrom(clazz) && !PushJobDelegate.class.isAssignableFrom(clazz) && !GroupMemberProvisionTaskJobDelegate.class.isAssignableFrom(clazz)) {
                classNames.get(ImplementationType.TASKJOB_DELEGATE).add(bd.getBeanClassName());
            }
            if (ReconFilterBuilder.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                classNames.get(ImplementationType.RECON_FILTER_BUILDER).add(bd.getBeanClassName());
            }
            if (LogicActions.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                classNames.get(ImplementationType.LOGIC_ACTIONS).add(bd.getBeanClassName());
            }
            if (PropagationActions.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                classNames.get(ImplementationType.PROPAGATION_ACTIONS).add(bd.getBeanClassName());
            }
            if (PullActions.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                classNames.get(ImplementationType.PULL_ACTIONS).add(bd.getBeanClassName());
            }
            if (PushActions.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                classNames.get(ImplementationType.PUSH_ACTIONS).add(bd.getBeanClassName());
            }
            if (Validator.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                classNames.get(ImplementationType.VALIDATOR).add(bd.getBeanClassName());
            }
            if (RecipientsProvider.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                classNames.get(ImplementationType.RECIPIENTS_PROVIDER).add(bd.getBeanClassName());
            }
            if (AuditAppender.class.isAssignableFrom(clazz) && !isAbstractClazz) {
                classNames.get(ImplementationType.AUDIT_APPENDER).add(clazz.getName());
                auditAppenderClasses.add(clazz);
            }
        } catch (Throwable t) {
            LOG.warn("Could not inspect class {}", bd.getBeanClassName(), t);
        }
    });
    classNames = Collections.unmodifiableMap(classNames);
    LOG.debug("Implementation classes found: {}", classNames);
    jwtSSOProviderClasses = Collections.unmodifiableSet(jwtSSOProviderClasses);
    reportletClasses = Collections.unmodifiableMap(reportletClasses);
    accountRuleClasses = Collections.unmodifiableMap(accountRuleClasses);
    passwordRuleClasses = Collections.unmodifiableMap(passwordRuleClasses);
    correlationRuleClasses = Collections.unmodifiableMap(correlationRuleClasses);
    auditAppenderClasses = Collections.unmodifiableSet(auditAppenderClasses);
}
Also used : PasswordRule(org.apache.syncope.core.persistence.api.dao.PasswordRule) PropagationActions(org.apache.syncope.core.provisioning.api.propagation.PropagationActions) PushJobDelegate(org.apache.syncope.core.provisioning.java.pushpull.PushJobDelegate) ItemTransformer(org.apache.syncope.core.provisioning.api.data.ItemTransformer) PullActions(org.apache.syncope.core.provisioning.api.pushpull.PullActions) AccountRuleConfClass(org.apache.syncope.core.persistence.api.dao.AccountRuleConfClass) LogicActions(org.apache.syncope.core.provisioning.api.LogicActions) AuditAppender(org.apache.syncope.core.logic.audit.AuditAppender) PullCorrelationRule(org.apache.syncope.core.persistence.api.dao.PullCorrelationRule) PushActions(org.apache.syncope.core.provisioning.api.pushpull.PushActions) PasswordRuleConfClass(org.apache.syncope.core.persistence.api.dao.PasswordRuleConfClass) SchedTaskJobDelegate(org.apache.syncope.core.provisioning.api.job.SchedTaskJobDelegate) ReportletConfClass(org.apache.syncope.core.persistence.api.dao.ReportletConfClass) AccountRule(org.apache.syncope.core.persistence.api.dao.AccountRule) RecipientsProvider(org.apache.syncope.core.provisioning.api.notification.RecipientsProvider) Reportlet(org.apache.syncope.core.persistence.api.dao.Reportlet) ImplementationType(org.apache.syncope.common.lib.types.ImplementationType) PullCorrelationRuleConfClass(org.apache.syncope.core.persistence.api.dao.PullCorrelationRuleConfClass) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) JWTSSOProvider(org.apache.syncope.core.spring.security.JWTSSOProvider) ReconFilterBuilder(org.apache.syncope.core.provisioning.api.pushpull.ReconFilterBuilder) Validator(org.apache.syncope.core.persistence.api.attrvalue.validation.Validator) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter)

Aggregations

Reportlet (org.apache.syncope.core.persistence.api.dao.Reportlet)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 Date (java.util.Date)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 Transformer (javax.xml.transform.Transformer)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerHandler (javax.xml.transform.sax.TransformerHandler)1 StreamResult (javax.xml.transform.stream.StreamResult)1 ReportletConf (org.apache.syncope.common.lib.report.ReportletConf)1 ImplementationType (org.apache.syncope.common.lib.types.ImplementationType)1 AuditAppender (org.apache.syncope.core.logic.audit.AuditAppender)1 ImplementationLookup (org.apache.syncope.core.persistence.api.ImplementationLookup)1 Validator (org.apache.syncope.core.persistence.api.attrvalue.validation.Validator)1 AccountRule (org.apache.syncope.core.persistence.api.dao.AccountRule)1 AccountRuleConfClass (org.apache.syncope.core.persistence.api.dao.AccountRuleConfClass)1 PasswordRule (org.apache.syncope.core.persistence.api.dao.PasswordRule)1 PasswordRuleConfClass (org.apache.syncope.core.persistence.api.dao.PasswordRuleConfClass)1 PullCorrelationRule (org.apache.syncope.core.persistence.api.dao.PullCorrelationRule)1