Search in sources :

Example 1 with NotificationFailedException

use of org.apache.nifi.bootstrap.notification.NotificationFailedException in project nifi by apache.

the class EmailNotificationService method notify.

@Override
public void notify(final NotificationContext context, final NotificationType notificationType, final String subject, final String messageText) throws NotificationFailedException {
    final Properties properties = getMailProperties(context);
    final Session mailSession = createMailSession(properties);
    final Message message = new MimeMessage(mailSession);
    try {
        message.setFrom(InternetAddress.parse(context.getProperty(FROM).evaluateAttributeExpressions().getValue())[0]);
        final InternetAddress[] toAddresses = toInetAddresses(context.getProperty(TO).evaluateAttributeExpressions().getValue());
        message.setRecipients(RecipientType.TO, toAddresses);
        final InternetAddress[] ccAddresses = toInetAddresses(context.getProperty(CC).evaluateAttributeExpressions().getValue());
        message.setRecipients(RecipientType.CC, ccAddresses);
        final InternetAddress[] bccAddresses = toInetAddresses(context.getProperty(BCC).evaluateAttributeExpressions().getValue());
        message.setRecipients(RecipientType.BCC, bccAddresses);
        message.setHeader("X-Mailer", context.getProperty(HEADER_XMAILER).evaluateAttributeExpressions().getValue());
        message.setSubject(subject);
        final String contentType = context.getProperty(CONTENT_TYPE).evaluateAttributeExpressions().getValue();
        message.setContent(messageText, contentType);
        message.setSentDate(new Date());
        Transport.send(message);
    } catch (final ProcessException | MessagingException e) {
        throw new NotificationFailedException("Failed to send E-mail Notification", e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) ProcessException(org.apache.nifi.processor.exception.ProcessException) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) NotificationFailedException(org.apache.nifi.bootstrap.notification.NotificationFailedException) Properties(java.util.Properties) Date(java.util.Date) Session(javax.mail.Session)

Example 2 with NotificationFailedException

use of org.apache.nifi.bootstrap.notification.NotificationFailedException in project nifi by apache.

the class HttpNotificationService method notify.

@Override
public void notify(NotificationContext context, NotificationType notificationType, String subject, String message) throws NotificationFailedException {
    try {
        final RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), message);
        Request.Builder requestBuilder = new Request.Builder().post(requestBody).url(urlReference.get());
        Map<PropertyDescriptor, String> configuredProperties = context.getProperties();
        for (PropertyDescriptor propertyDescriptor : configuredProperties.keySet()) {
            if (propertyDescriptor.isDynamic()) {
                String propertyValue = context.getProperty(propertyDescriptor).evaluateAttributeExpressions().getValue();
                requestBuilder = requestBuilder.addHeader(propertyDescriptor.getDisplayName(), propertyValue);
            }
        }
        final Request request = requestBuilder.addHeader(NOTIFICATION_SUBJECT_KEY, subject).addHeader(NOTIFICATION_TYPE_KEY, notificationType.name()).build();
        final OkHttpClient httpClient = httpClientReference.get();
        final Call call = httpClient.newCall(request);
        try (final Response response = call.execute()) {
            if (!response.isSuccessful()) {
                throw new NotificationFailedException("Failed to send Http Notification. Received an unsuccessful status code response '" + response.code() + "'. The message was '" + response.message() + "'");
            }
        }
    } catch (NotificationFailedException e) {
        throw e;
    } catch (Exception e) {
        throw new NotificationFailedException("Failed to send Http Notification", e);
    }
}
Also used : Response(okhttp3.Response) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) Request(okhttp3.Request) NotificationFailedException(org.apache.nifi.bootstrap.notification.NotificationFailedException) NotificationFailedException(org.apache.nifi.bootstrap.notification.NotificationFailedException) ProcessException(org.apache.nifi.processor.exception.ProcessException) RequestBody(okhttp3.RequestBody)

Aggregations

NotificationFailedException (org.apache.nifi.bootstrap.notification.NotificationFailedException)2 ProcessException (org.apache.nifi.processor.exception.ProcessException)2 Date (java.util.Date)1 Properties (java.util.Properties)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1 Session (javax.mail.Session)1 InternetAddress (javax.mail.internet.InternetAddress)1 MimeMessage (javax.mail.internet.MimeMessage)1 Call (okhttp3.Call)1 OkHttpClient (okhttp3.OkHttpClient)1 Request (okhttp3.Request)1 RequestBody (okhttp3.RequestBody)1 Response (okhttp3.Response)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1