Search in sources :

Example 1 with CasRegisteredServiceExpiredEvent

use of org.apereo.cas.support.events.service.CasRegisteredServiceExpiredEvent in project cas by apereo.

the class RegisteredServicesEventListener method handleRegisteredServiceExpiredEvent.

/**
 * Handle registered service expired event.
 *
 * @param event the event
 */
@EventListener
public void handleRegisteredServiceExpiredEvent(final CasRegisteredServiceExpiredEvent event) {
    final RegisteredService registeredService = event.getRegisteredService();
    final List<RegisteredServiceContact> contacts = registeredService.getContacts();
    final EmailProperties mail = casProperties.getServiceRegistry().getMail();
    final SmsProperties sms = casProperties.getServiceRegistry().getSms();
    final String serviceName = StringUtils.defaultIfBlank(registeredService.getName(), registeredService.getServiceId());
    if (communicationsManager.isMailSenderDefined()) {
        final String message = String.format(mail.getText(), serviceName);
        contacts.stream().filter(c -> StringUtils.isNotBlank(c.getEmail())).forEach(c -> communicationsManager.email(message, mail.getFrom(), mail.getSubject(), c.getEmail(), mail.getCc(), mail.getBcc()));
    }
    if (communicationsManager.isSmsSenderDefined()) {
        final String message = String.format(sms.getText(), serviceName);
        contacts.stream().filter(c -> StringUtils.isNotBlank(c.getPhone())).forEach(c -> communicationsManager.sms(sms.getFrom(), c.getPhone(), message));
    }
    servicesManager.load();
}
Also used : EmailProperties(org.apereo.cas.configuration.model.support.email.EmailProperties) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) CommunicationsManager(org.apereo.cas.util.io.CommunicationsManager) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) CasRegisteredServicesRefreshEvent(org.apereo.cas.support.events.service.CasRegisteredServicesRefreshEvent) SmsProperties(org.apereo.cas.configuration.model.support.sms.SmsProperties) EventListener(org.springframework.context.event.EventListener) EmailProperties(org.apereo.cas.configuration.model.support.email.EmailProperties) AllArgsConstructor(lombok.AllArgsConstructor) CasRegisteredServiceExpiredEvent(org.apereo.cas.support.events.service.CasRegisteredServiceExpiredEvent) StringUtils(org.apache.commons.lang3.StringUtils) SmsProperties(org.apereo.cas.configuration.model.support.sms.SmsProperties) EventListener(org.springframework.context.event.EventListener)

Example 2 with CasRegisteredServiceExpiredEvent

use of org.apereo.cas.support.events.service.CasRegisteredServiceExpiredEvent in project cas by apereo.

the class DefaultRegisteredServicesEventListener method handleRegisteredServiceExpiredEvent.

@Override
public void handleRegisteredServiceExpiredEvent(final CasRegisteredServiceExpiredEvent event) {
    val registeredService = event.getRegisteredService();
    val contacts = registeredService.getContacts();
    val serviceRegistry = casProperties.getServiceRegistry();
    val serviceName = StringUtils.defaultIfBlank(registeredService.getName(), registeredService.getServiceId());
    if (contacts == null || contacts.isEmpty()) {
        LOGGER.debug("No contacts are defined to be notified for policy changes to service [{}]", serviceName);
        return;
    }
    val logMessage = String.format("Sending notification to [{}] as service [{}] is %s from registry", event.isDeleted() ? "deleted" : "expired");
    LOGGER.info(logMessage, contacts, serviceName);
    communicationsManager.validate();
    if (communicationsManager.isMailSenderDefined()) {
        val mail = serviceRegistry.getMail();
        val body = EmailMessageBodyBuilder.builder().properties(mail).parameters(Map.of("service", serviceName)).build().produce();
        contacts.stream().filter(c -> StringUtils.isNotBlank(c.getEmail())).forEach(c -> communicationsManager.email(mail, c.getEmail(), body));
    }
    if (communicationsManager.isSmsSenderDefined()) {
        val sms = serviceRegistry.getSms();
        val message = sms.getFormattedText(serviceName);
        contacts.stream().filter(c -> StringUtils.isNotBlank(c.getPhone())).forEach(c -> communicationsManager.sms(sms.getFrom(), c.getPhone(), message));
    }
}
Also used : lombok.val(lombok.val) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) Slf4j(lombok.extern.slf4j.Slf4j) EnvironmentChangeEvent(org.springframework.cloud.context.environment.EnvironmentChangeEvent) CasRegisteredServicesRefreshEvent(org.apereo.cas.support.events.service.CasRegisteredServicesRefreshEvent) Map(java.util.Map) EmailMessageBodyBuilder(org.apereo.cas.notifications.mail.EmailMessageBodyBuilder) RequiredArgsConstructor(lombok.RequiredArgsConstructor) lombok.val(lombok.val) CasRegisteredServiceExpiredEvent(org.apereo.cas.support.events.service.CasRegisteredServiceExpiredEvent) StringUtils(org.apache.commons.lang3.StringUtils) CommunicationsManager(org.apereo.cas.notifications.CommunicationsManager)

Example 3 with CasRegisteredServiceExpiredEvent

use of org.apereo.cas.support.events.service.CasRegisteredServiceExpiredEvent in project cas by apereo.

the class AbstractServicesManager method processExpiredRegisteredService.

private RegisteredService processExpiredRegisteredService(final RegisteredService registeredService) {
    val policy = registeredService.getExpirationPolicy();
    LOGGER.warn("Registered service [{}] has expired on [{}]", registeredService.getServiceId(), policy.getExpirationDate());
    if (policy.isNotifyWhenExpired()) {
        LOGGER.debug("Contacts for registered service [{}] will be notified of service expiry", registeredService.getServiceId());
        publishEvent(new CasRegisteredServiceExpiredEvent(this, registeredService, false));
    }
    if (policy.isDeleteWhenExpired()) {
        LOGGER.debug("Deleting expired registered service [{}] from registry.", registeredService.getServiceId());
        if (policy.isNotifyWhenDeleted()) {
            LOGGER.debug("Contacts for registered service [{}] will be notified of service expiry and removal", registeredService.getServiceId());
            publishEvent(new CasRegisteredServiceExpiredEvent(this, registeredService, true));
        }
        delete(registeredService);
        return null;
    }
    return registeredService;
}
Also used : lombok.val(lombok.val) CasRegisteredServiceExpiredEvent(org.apereo.cas.support.events.service.CasRegisteredServiceExpiredEvent)

Example 4 with CasRegisteredServiceExpiredEvent

use of org.apereo.cas.support.events.service.CasRegisteredServiceExpiredEvent in project cas by apereo.

the class RegisteredServicesEventListenerTests method verifyServiceExpirationWithRemovalEvent.

@Test
public void verifyServiceExpirationWithRemovalEvent() {
    val registeredService = RegisteredServiceTestUtils.getRegisteredService();
    val contact = new DefaultRegisteredServiceContact();
    contact.setName("Test");
    contact.setEmail("casuser@example.org");
    contact.setPhone("13477465421");
    registeredService.getContacts().add(contact);
    val listener = new DefaultRegisteredServicesEventListener(this.servicesManager, casProperties, communicationsManager);
    listener.handleRefreshEvent(new CasRegisteredServicesRefreshEvent(this));
    listener.handleEnvironmentChangeEvent(new EnvironmentChangeEvent(Set.of()));
    val event = new CasRegisteredServiceExpiredEvent(this, registeredService, true);
    listener.handleRegisteredServiceExpiredEvent(event);
}
Also used : lombok.val(lombok.val) EnvironmentChangeEvent(org.springframework.cloud.context.environment.EnvironmentChangeEvent) CasRegisteredServicesRefreshEvent(org.apereo.cas.support.events.service.CasRegisteredServicesRefreshEvent) CasRegisteredServiceExpiredEvent(org.apereo.cas.support.events.service.CasRegisteredServiceExpiredEvent) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with CasRegisteredServiceExpiredEvent

use of org.apereo.cas.support.events.service.CasRegisteredServiceExpiredEvent in project cas by apereo.

the class RegisteredServicesEventListenerTests method verifyServiceExpirationEventWithContact.

@Test
public void verifyServiceExpirationEventWithContact() {
    val registeredService = RegisteredServiceTestUtils.getRegisteredService();
    val contact = new DefaultRegisteredServiceContact();
    contact.setName("Test");
    contact.setEmail("casuser@example.org");
    contact.setPhone("13477465421");
    registeredService.getContacts().add(contact);
    val listener = new DefaultRegisteredServicesEventListener(this.servicesManager, casProperties, communicationsManager);
    val event = new CasRegisteredServiceExpiredEvent(this, registeredService, false);
    assertDoesNotThrow(new Executable() {

        @Override
        public void execute() throws Throwable {
            listener.handleRegisteredServiceExpiredEvent(event);
        }
    });
}
Also used : lombok.val(lombok.val) CasRegisteredServiceExpiredEvent(org.apereo.cas.support.events.service.CasRegisteredServiceExpiredEvent) Executable(org.junit.jupiter.api.function.Executable) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

CasRegisteredServiceExpiredEvent (org.apereo.cas.support.events.service.CasRegisteredServiceExpiredEvent)6 lombok.val (lombok.val)5 CasRegisteredServicesRefreshEvent (org.apereo.cas.support.events.service.CasRegisteredServicesRefreshEvent)3 Test (org.junit.jupiter.api.Test)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 Slf4j (lombok.extern.slf4j.Slf4j)2 StringUtils (org.apache.commons.lang3.StringUtils)2 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)2 Executable (org.junit.jupiter.api.function.Executable)2 EnvironmentChangeEvent (org.springframework.cloud.context.environment.EnvironmentChangeEvent)2 List (java.util.List)1 Map (java.util.Map)1 AllArgsConstructor (lombok.AllArgsConstructor)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 EmailProperties (org.apereo.cas.configuration.model.support.email.EmailProperties)1 SmsProperties (org.apereo.cas.configuration.model.support.sms.SmsProperties)1 CommunicationsManager (org.apereo.cas.notifications.CommunicationsManager)1 EmailMessageBodyBuilder (org.apereo.cas.notifications.mail.EmailMessageBodyBuilder)1 CommunicationsManager (org.apereo.cas.util.io.CommunicationsManager)1 EventListener (org.springframework.context.event.EventListener)1