6.1.2. İstemci API (Client)#
Notification mail uygulamasının rest api uçlarını kullanmayı sağlayan yapıdır.
Bağımlılıklar#
Gradle Bağımlılıkları
compile(
[group: 'tr.com.havelsan.framework.notification.cloud', name: 'hvl-notification-mail-cloud-client']
)
Rest Servis Tanımı#
Feign client uçlarını kullanabilmek için configurasyon bean'ine asağıdaki anotasyonlar eklenmelidir.
Rest Client Kullanımı6.1.2.1 Hvl-mail Servis Başlatılması#
Uyarı
Öncelikli olarak notification-mail-service başlatılmış olmalıdır.
docker bilgisine buradan ulaşabilirsiniz.
6.1.2.2 Mail Service Ayarları#
hvl:
core:
mail:
fake:
enabled: ${FAKE_MAIL_ENABLED:false}
from-address: ${FAKE_MAIL_FROM_ADRESS:from@bulut.ai}
to-address: ${FAKE_MAIL_TO_ADRESS:to@bulut.ai}
management:
health:
mail:
enabled: ${MAIL_HEALTH_CHECK_ENABLED:true}
spring:
mail:
host: <exhange-server-ip>
port: <exchance-server-port>
username: ${MAIL_USERNAME:}
password: ${MAIL_PASSWORD:}
properties:
mail.smtp.auth: false
Not
Mail ayarlarına buradan ulaşabilirsiniz.
6.1.2.3 Mail Şablonu Oluşturma#
Mail gönderimi için bir şablon kodu belirlenmelidir. Aşağıdaki örnekte 'SAMPLE_TEMPLATE_CODE' kodu ile bir mail şablonu(Türkçe ve İngilizce) olarak oluşturulmaktadır.
final HvlNotificationTemplateRestService templateRestService = configurableApplicationContext.getBean(HvlNotificationTemplateRestService.class);
final HvlNotificationTemplateModelBuilder templateModelBuilder = HvlNotificationTemplateModelBuilder.create()
.withCode("SAMPLE_TEMPLATE_CODE")
.withName("Sample template name")
.withType(HvlNotificationType.MAIL);
final HvlNotificationTemplateContentModel templateContentModelTr = HvlNotificationTemplateContentModelBuilder
.create()
.withSender("sender@bulut.ai")
.withSubject("Örnek Mail Başlığı")
.withContentType(HvlNotificationTemplateContentType.STRING)
.withContent("Merhaba ${nameSurname}")
.build();
final HvlNotificationTemplateContentModel templateContentModelEn = HvlNotificationTemplateContentModelBuilder
.create()
.withSender("sender@bulut.ai")
.withSubject("Sample Subject")
.withContentType(HvlNotificationTemplateContentType.STRING)
.withContent("Hello ${nameSurname}")
.build();
final Map<String, HvlNotificationTemplateContentModel> contentMap = new HashMap<>();
contentMap.put(Locale.of("tr").toString(), templateContentModelTr);
contentMap.put(Locale.of("en").toString(), templateContentModelEn);
templateModelBuilder.contentMap(contentMap);
final HvlNotificationTemplateModel templateModel = templateModelBuilder.build();
templateRestService.save(templateModel);
Mail Konu Alanının Dinamik Yönetimi
Mail şablonunda subject alanı .withSubject("Örnek Mail Başlığı:${veriableField}") şeklinde bir parametre ile kayıt edilirse mail isteği oluşturulurken .withAttribute("veriableField","Dinamik Konu") şeklinde mail isteğine attribute geçirilerek mail konu alanı dinamik yönetilebilir. (Bkz.Toplantı isteği oluşturma kısmında bu örneği bulabilirsiniz.)
Not
Mail şablonu oluşturma işlemi ile ilgili kaynak kodlarının tamamına buradan ulaşabilirsiniz.
6.1.2.4 Rest İle Mail Gönderimi#
Uyarı
"SAMPLE_TEMPLATE_CODE" bu kod ile uygun bir template oluşturulduğu varsayılmaktadır.(Bkz.Mail Şablonu Oluşturma)
HvlNotificationMailRestService notificationService = applicationContext.getBean(HvlNotificationMailRestService.class);
HvlNotificationMailRequestModel mailRequest = HvlNotificationMailRequestModelBuilder.create()
.withCode("SAMPLE_TEMPLATE_CODE") // "SAMPLE_TEMPLATE_CODE" bu kod ile uygun bir template oluşturulduğu varsayılmaktadır.
.withLocale(Locale.of("tr"))
.withToAddressList(List.of("kisi1@example.com", "kisi2@example.com"))
.withAttribute("nameSurname", "Ahmet Yılmaz")
.build();
notificationService.notify(mailRequest);
Not
Rest ile mail gönderimi kaynak kodlarının tamamına buradan ulaşabilirsiniz.
6.1.2.5 Mail İçeriğine Liste Ekleme#
Aşağıdaki örnekte, listeyi e-posta içeriğinde tablo formatında sunma işlemi gerçekleştirilmiştir.
final String collectionHtmlContent = "<html>\n" +
" <body>\n" +
" <p>Sayın ${nameSurname}</p>\n" +
" <p>Son kullanma tarihi yaklaşan ve(ya) tarihi geçmiş seri numaralı malzemelerin listesi aşağıdaki gibidir:</p>\n" +
" <table border=\"1\" cellpadding=\"5\" cellspacing=\"0\" style=\"border-collapse: collapse; width: 100%;\">\n" +
" <thead>\n" +
" <tr>\n" +
" <th colspan=\"4\" style=\"text-align: center;\">Son Kullanma Tarihi Yaklaşan Malzemeler</th>\n" +
" </tr>\n" +
" <tr>\n" +
" <th>Lokasyon</th>\n" +
" <th>Malzeme</th>\n" +
" <th>Seri Numarası</th>\n" +
" <th>Son Kullanım Tarihi</th>\n" +
" </tr>\n" +
" </thead>\n" +
" <tbody>\n" +
" $V{items}\n" +
" <tr>\n" +
" <td>${location}</td>\n" +
" <td>${material}</td>\n" +
" <td>${serialNumber}</td>\n" +
" <td>${expirationDate}</td>\n" +
" </tr>\n" +
" $V{end}\n" +
" </tbody>\n" +
" </table>\n" +
" </body>\n" +
"</html>";
final String COLLECTION_TEMPLATE_CODE = "COLLECTION_TEMPLATE_CODE";
final HvlNotificationTemplateRestService templateRestService = configurableApplicationContext.getBean(HvlNotificationTemplateRestService.class);
final HvlNotificationTemplateModelBuilder templateModelBuilder = HvlNotificationTemplateModelBuilder.create()
.withCode(COLLECTION_TEMPLATE_CODE)
.withName("Son Kullanma Tarihi Yaklaşan Malzemeler Şablonu")
.withType(HvlNotificationType.MAIL);
final HvlNotificationTemplateContentModel templateContentModelTr = HvlNotificationTemplateContentModelBuilder
.create()
.withSender("sender@bulut.ai")
.withSubject("Son Kullanma Tarihi Yaklaşan Malzemeler")
.withContentType(HvlNotificationTemplateContentType.STRING)
.withContent(collectionHtmlContent)
.build();
final Map<String, HvlNotificationTemplateContentModel> contentMap = new HashMap<>();
contentMap.put(Locale.of("tr").toString(), templateContentModelTr);
templateModelBuilder.withContentMap(contentMap);
final HvlNotificationTemplateModel templateModel = templateModelBuilder.build();
templateRestService.save(templateModel);
final HvlNotificationMailRestService notificationRestService = configurableApplicationContext.getBean(HvlNotificationMailRestService.class);
record Item(String location, String material, String serialNumber, String expirationDate) { }
List<Item> items = List.of(
new Item("Ankara", "Kalem", "S123", "2023-01-01"),
new Item("Çubuk", "Defter", "312321", "2023-01-01")
);
final HvlNotificationMailRequestModelWithContent mailNotificationRequestModel = HvlNotificationMailWithContentRequestModelBuilder.create()
.withCode(COLLECTION_TEMPLATE_CODE)
.withLocale(Locale.of("tr"))
.withToAddressList(List.of("to1@javelsan.com.tr", "to2@javelsan.com.tr"))
.withCcAddressList(List.of("cc1@bulut.ai", "cc2@bulut.ai"))
.withBccAddressList(List.of("bcc@bulut.ai"))
.withAttribute(TEMPLATE_NAMESURMANE_ATTRIBUTE, "Ahmet Yılmaz ")
.withAttribute("items", items)
.build();
notificationRestService.notify(mailNotificationRequestModel);
6.1.2.6 Mail İçeriğine Ek Ekleme#
final HvlNotificationMailRestService notificationRestService = configurableApplicationContext.getBean(
HvlNotificationMailRestService.class
);
final HvlNotificationMailRequestModelBuilder mailRequestModelBuilder = HvlNotificationMailRequestModelBuilder.create()
.withCode(SAMPLE_TEMPLATE_CODE)// "SAMPLE_TEMPLATE_CODE" bu kod ile uygun bir template oluşturulduğu varsayılmaktadır
.withLocale(DEFAULT_LOCALE)
.withToAddressList(List.of("to1@javelsan.com.tr", "to2@javelsan.com.tr"))
.withAttribute(TEMPLATE_NAMESURMANE_ATTRIBUTE, "Johhn Doe");
final List<HvlMailAttachment> attachmentList = new ArrayList<>();
try {
final String attachmentFileName = "attachment.pdf";
final HvlMailAttachmentBuilder attachmentBuilder = HvlMailAttachmentBuilder.create()
.withFileName(attachmentFileName);
ClassLoader classLoader = HvlMailNotificationServerApplication.class.getClassLoader();
File file = new File(classLoader.getResource(attachmentFileName).getFile());
FileInputStream fileInputStream = new FileInputStream(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
attachmentBuilder.withContent(byteArrayOutputStream.toByteArray());
attachmentList.add(attachmentBuilder.build());
fileInputStream.close();
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
mailRequestModelBuilder.withAttachmentList(attachmentList);
notificationRestService.notify(mailRequestModelBuilder.build());
6.1.2.7 Toplantı İsteği Oluşturma#
Uyarı
"MEETING_REQUEST_TEMPLATE_CODE" bu kod ile uygun bir template oluşturulduğu varsayılmaktadır. (Bkz.Mail Şablonu Oluşturma)
//toplantı isteği mail şablonu oluşturma
final String MEETING_REQUEST_TEMPLATE_CODE = "MEETING_REQUEST_TEMPLATE_CODE";
final HvlNotificationTemplateRestService templateRestService = configurableApplicationContext.getBean(HvlNotificationTemplateRestService.class);
final HvlNotificationTemplateModelBuilder templateModelBuilder = HvlNotificationTemplateModelBuilder.create()
.withCode(MEETING_REQUEST_TEMPLATE_CODE)
.withName("Meeting Request Template")
.withType(HvlNotificationType.MAIL);
final HvlNotificationTemplateContentModel templateContentModelTr = HvlNotificationTemplateContentModelBuilder
.create()
.withSender("sender@bulut.ai")
.withSubject("Meeting Subject: ${meetingName}")
.withContentType(HvlNotificationTemplateContentType.STRING)
.withContent(" ")
.build();
final Map<String, HvlNotificationTemplateContentModel> contentMap = new HashMap<>();
contentMap.put(Locale.of("tr").toString(), templateContentModelTr);
templateModelBuilder.withContentMap(contentMap);
final HvlNotificationTemplateModel templateModel = templateModelBuilder.build();
templateRestService.save(templateModel);
//toplantı isteği maili oluşturma
final HvlNotificationMailRestService notificationRestService = configurableApplicationContext.getBean(vlNotificationMailRestService.class);
final HvlNotificationCalendarEventMailRequestModelBuilder calendarEventBuilder = HvlNotificationCalendarEventMailRequestModelBuilder
.create()
.withCode(MEETING_REQUEST_TEMPLATE_CODE)
.withLocale(Locale.of("tr"))
.withAttribute("meetingName","Interview Meeting")
.withToAddressList(List.of("to1@bulut.ai", "to2@bulut.ai"));
final String uid = UUID.randomUUID().toString();//toplantı id bilgisi
final List<String> attendee = List.of("to1@bulut.ai", "to2@bulut.ai");//katılımcılar
final ZonedDateTime startDate = ZonedDateTime.now().plusMinutes(60);//başlangıç tarihi
final ZonedDateTime endDate = ZonedDateTime.now().plusMinutes(120);//bitiş tarihi
final String location = "HAVELSAN Technology Campus T100";//konum
final String description = "Meeting Request Description";//açıklama
final String organizer = "szcamurcu@havelsan.com";//toplantıyı düzenleyen
final String summary = "Meeting Request Sample";//bilgilendirme mesajı
final HvlCalendarEventStatus status = HvlCalendarEventStatus.NEEDS_ACTION;
final HvlCalendarEventModel calendar = HvlCalendarEventModelBuilder.create(
uid,
startDate,
endDate,
summary,
organizer,
attendee)
.withUid(uid)
.withAttendee(attendee)
.withStartDate(startDate)
.withEndDate(endDate)
.withLocation(location)
.withDescription(description)
.withOrganizer(organizer)
.withSummary(summary)
.withStatus(status)
.build();
calendarEventBuilder.addCalendar(calendar);
final HvlCalendarEventNotificationRequestModel calendarEventNotificationRequestModel = calendarEventBuilder.build();
notificationRestService.calendarEventNotify(calendarEventNotificationRequestModel);
Not
Toplantı isteği gönderimi kaynak kodlarının tamamına buradan ulaşabilirsiniz.
6.1.2.8 Kafka İle Mail Gönderimi#
Kafka ile mail gönderimi için Kafka'nın çalıştırılması gerekmektedir.
Kafka docker bilgisine buradan ve cluster docker bilgisine buradan ulaşabilirsiniz.
Bilgilendirme
Kafka ile ilgili bilgilendirme dokümanına buradan ulaşabilirsiniz.
Bilgilendirme
EKSEN framework kafka altyapısı ile ilgili bilgilere buradan ulaşabilirsiniz.
Uyarı
MAIL_EVENT_TOPIC mail gönderimi için kullanılacak kafka topic adıdır. (Bkz.Hvl mail service başlatılması) Mail docker instance'ına environment değişken olarak verilen MAIL_EVENT_TOPIC ile eşleşmelidir.
İlgili docker compose dosyasına buradan ulaşabilirsiniz.
Gradle Bağımlılıkları
api(
[group: 'tr.com.havelsan.framework.mq', name: 'hvl-kafka'],
[group: 'tr.com.havelsan.framework.notification', name: 'hvl-notification-mail-common']
)
Uyarı
"SAMPLE_TEMPLATE_CODE" bu kod ile uygun bir template oluşturulduğu varsayılmaktadır.(Bkz.Mail Şablonu Oluşturma)
final HvlEventNotifier eventNotifier = configurableApplicationContext.getBean(HvlEventNotifier.class);
final String MAIL_EVENT_TOPIC="javalt-oauth-mail-notification";
final HvlNotificationMailRequestModel mailRequest = HvlNotificationMailRequestModelBuilder.create()
.withCode("SAMPLE_TEMPLATE_CODE")// "SAMPLE_TEMPLATE_CODE" bu kod ile uygun bir template oluşturulduğu varsayılmaktadır
.withLocale(Locale.of("tr"))
.withToAddressList(List.of("kisi1@example.com", "kisi2@example.com"))
.withAttribute("nameSurname", "Ahmet Yılmaz")
.build();
eventNotifier.notifySync(MAIL_EVENT_TOPIC, mailNotificationRequestModel);
}
Not
Kafka ile mail gönderimi kaynak kodlarına buradan ulaşabilirsiniz.
6.1.2.9 Geliştirme Ortamında Oluşturulan Mail İçeriklerinin Görüntülenmesi#
Kafka docker bilgisine buradan ulaşabilirsiniz.
Mail service ayarlarında mail host:localhost,port:1025 yapılmalıdır. (Bkz.Mail Service Ayarları)
Browser 'da http://localhost:8025 adresine girerek oluşturulan maillerin içerikleri görüntülebilir.
Service metodlarının detaylarına swagger dokümanından ulaşabilirsiniz.