private void mailReport(DeliveryOptions deliveryOptions, ByteArrayOutputStream outputStream) { try { JavaMailer jm = new JavaMailer(); jm.setTo(deliveryOptions.getMailTo()); jm.setSubject(deliveryOptions.getInstanceId()); jm.setMessageText("Here is your report from the OpenNMS report service."); jm.setInputStream(new ByteArrayInputStream(outputStream.toByteArray())); switch (deliveryOptions.getFormat()) { case HTML: jm.setInputStreamName(deliveryOptions.getInstanceId() + ".htm"); jm.setInputStreamContentType("text/html"); break; case PDF: jm.setInputStreamName(deliveryOptions.getInstanceId() + ".pdf"); jm.setInputStreamContentType("application/pdf"); break; case SVG: jm.setInputStreamName(deliveryOptions.getInstanceId() + ".pdf"); jm.setInputStreamContentType("application/pdf"); break; default: jm.setInputStreamName(deliveryOptions.getInstanceId() + ".htm"); jm.setInputStreamContentType("text/html"); } jm.mailSend(); } catch (JavaMailerException e) { log.error("Caught JavaMailer exception sending report", e); } }
/** * validateDeliveryOptions * * @param deliveryOptions a {@link org.opennms.reporting.core.DeliveryOptions} object. * @param context a {@link org.springframework.binding.validation.ValidationContext} object. */ public void validateDeliveryOptions(DeliveryOptions deliveryOptions, ValidationContext context) { MessageContext messages = context.getMessageContext(); if (!((deliveryOptions.getSendMail() | deliveryOptions.getPersist()))) { messages.addMessage( new MessageBuilder() .error() .source("sendMail") .defaultText("one of send mail or persist should be selected") .build()); messages.addMessage( new MessageBuilder() .error() .source("persist") .defaultText("one of send mail or persist should be selected") .build()); } else { if (deliveryOptions.getSendMail() && (deliveryOptions.getMailTo() == "")) { messages.addMessage( new MessageBuilder() .error() .source("mailTo") .defaultText("cannot have empty mail recipient") .build()); } } if (deliveryOptions.getInstanceId().length() == 0) { messages.addMessage( new MessageBuilder() .error() .source("instanceId") .defaultText("cannot have an empty Id for the report instance") .build()); } else if (m_reportSchedulerService.exists(deliveryOptions.getInstanceId())) { messages.addMessage( new MessageBuilder() .error() .source("instanceId") .defaultText("report instanceId is already in use") .build()); } }
/** {@inheritDoc} */ @Override public void run( ReportParameters parameters, ReportMode mode, DeliveryOptions deliveryOptions, String reportId) { if (!deliveryOptions.getPersist()) { ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedOutputStream bout = new BufferedOutputStream(out); try { getReportService(reportId) .runAndRender( parameters.getReportParms(mode), reportId, deliveryOptions.getFormat(), bout); } catch (ReportException reportException) { log.error("failed to run or render report: " + reportId, reportException); } mailReport(deliveryOptions, out); } else { String outputPath; try { outputPath = getReportService(reportId).run(parameters.getReportParms(mode), reportId); ReportCatalogEntry catalogEntry = new ReportCatalogEntry(); catalogEntry.setReportId(reportId); catalogEntry.setTitle(deliveryOptions.getInstanceId()); catalogEntry.setLocation(outputPath); catalogEntry.setDate(new Date()); m_reportStoreService.save(catalogEntry); if (deliveryOptions.getMailTo().length() != 0) { ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedOutputStream bout = new BufferedOutputStream(out); getReportService(reportId) .render(reportId, outputPath, deliveryOptions.getFormat(), bout); mailReport(deliveryOptions, out); } } catch (ReportException reportException) { log.error("failed to run or render report: " + reportId, reportException); } } }