Search in sources :

Example 6 with NotificationTO

use of org.apache.syncope.common.lib.to.NotificationTO in project syncope by apache.

the class NotificationTaskITCase method issueSYNCOPE446.

@Test
public void issueSYNCOPE446() throws Exception {
    // 1. Create notification
    ImplementationTO recipientsProvider = new ImplementationTO();
    recipientsProvider.setKey(TestNotificationRecipientsProvider.class.getSimpleName());
    recipientsProvider.setEngine(ImplementationEngine.JAVA);
    recipientsProvider.setType(ImplementationType.RECIPIENTS_PROVIDER);
    recipientsProvider.setBody(TestNotificationRecipientsProvider.class.getName());
    Response response = implementationService.create(recipientsProvider);
    recipientsProvider = implementationService.read(recipientsProvider.getType(), response.getHeaderString(RESTHeaders.RESOURCE_KEY));
    assertNotNull(recipientsProvider);
    NotificationTO notification = new NotificationTO();
    notification.setTraceLevel(TraceLevel.ALL);
    notification.getEvents().add("[LOGIC]:[GroupLogic]:[]:[create]:[SUCCESS]");
    String groupName = "group" + getUUIDString();
    notification.getAbouts().put(AnyTypeKind.GROUP.name(), SyncopeClient.getGroupSearchConditionBuilder().is("name").equalTo(groupName).query());
    notification.setRecipientsFIQL(SyncopeClient.getUserSearchConditionBuilder().inGroups("f779c0d4-633b-4be5-8f57-32eb478a3ca5").query());
    notification.setSelfAsRecipient(false);
    notification.setRecipientAttrName("email");
    notification.getStaticRecipients().add("notificationtest@syncope.apache.org");
    notification.setRecipientsProvider(recipientsProvider.getKey());
    String sender = "syncopetest-" + getUUIDString() + "@syncope.apache.org";
    notification.setSender(sender);
    String subject = "Test notification " + getUUIDString();
    notification.setSubject(subject);
    notification.setTemplate("optin");
    notification.setActive(true);
    response = notificationService.create(notification);
    notification = getObject(response.getLocation(), NotificationService.class, NotificationTO.class);
    assertNotNull(notification);
    assertEquals(recipientsProvider.getKey(), notification.getRecipientsProvider());
    // 2. create group
    GroupTO groupTO = new GroupTO();
    groupTO.setName(groupName);
    groupTO.setRealm("/even/two");
    groupTO = createGroup(groupTO).getEntity();
    assertNotNull(groupTO);
    // 3. verify
    NotificationTaskTO taskTO = findNotificationTask(notification.getKey(), 50);
    assertNotNull(taskTO);
    assertNotNull(taskTO.getNotification());
    assertTrue(taskTO.getRecipients().containsAll(new TestNotificationRecipientsProvider().provideRecipients(null)));
    NotificationTaskTO foundViaList = taskService.<NotificationTaskTO>search(new TaskQuery.Builder(TaskType.NOTIFICATION).notification(notification.getKey()).build()).getResult().get(0);
    assertEquals(taskTO, foundViaList);
    execNotificationTask(taskService, taskTO.getKey(), 50);
    assertTrue(verifyMail(sender, subject, "notificationtest@syncope.apache.org"));
}
Also used : ImplementationTO(org.apache.syncope.common.lib.to.ImplementationTO) Response(javax.ws.rs.core.Response) NotificationTO(org.apache.syncope.common.lib.to.NotificationTO) TaskQuery(org.apache.syncope.common.rest.api.beans.TaskQuery) NotificationService(org.apache.syncope.common.rest.api.service.NotificationService) TestNotificationRecipientsProvider(org.apache.syncope.fit.core.reference.TestNotificationRecipientsProvider) GroupTO(org.apache.syncope.common.lib.to.GroupTO) NotificationTaskTO(org.apache.syncope.common.lib.to.NotificationTaskTO) Test(org.junit.jupiter.api.Test)

Example 7 with NotificationTO

use of org.apache.syncope.common.lib.to.NotificationTO in project syncope by apache.

the class NotificationDataBinderImpl method getNotificationTO.

@Override
public NotificationTO getNotificationTO(final Notification notification) {
    NotificationTO notificationTO = new NotificationTO();
    notificationTO.setKey(notification.getKey());
    notificationTO.setTemplate(notification.getTemplate().getKey());
    BeanUtils.copyProperties(notification, notificationTO, IGNORE_PROPERTIES);
    notification.getAbouts().forEach(about -> {
        notificationTO.getAbouts().put(about.getAnyType().getKey(), about.get());
    });
    if (notification.getRecipientsProvider() != null) {
        notificationTO.setRecipientsProvider(notification.getRecipientsProvider().getKey());
    }
    return notificationTO;
}
Also used : NotificationTO(org.apache.syncope.common.lib.to.NotificationTO)

Example 8 with NotificationTO

use of org.apache.syncope.common.lib.to.NotificationTO in project syncope by apache.

the class NotificationLogic method delete.

@PreAuthorize("hasRole('" + StandardEntitlement.NOTIFICATION_DELETE + "')")
public NotificationTO delete(final String key) {
    Notification notification = notificationDAO.find(key);
    if (notification == null) {
        LOG.error("Could not find notification '" + key + "'");
        throw new NotFoundException(String.valueOf(key));
    }
    NotificationTO deleted = binder.getNotificationTO(notification);
    notificationDAO.delete(key);
    return deleted;
}
Also used : NotificationTO(org.apache.syncope.common.lib.to.NotificationTO) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) Notification(org.apache.syncope.core.persistence.api.entity.Notification) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 9 with NotificationTO

use of org.apache.syncope.common.lib.to.NotificationTO in project syncope by apache.

the class AbstractITCase method createNotificationTask.

protected Pair<String, String> createNotificationTask(final boolean active, final boolean includeAbout, final TraceLevel traceLevel, final String sender, final String subject, final String... staticRecipients) {
    // 1. Create notification
    NotificationTO notification = new NotificationTO();
    notification.setTraceLevel(traceLevel);
    notification.getEvents().add("[LOGIC]:[UserLogic]:[]:[create]:[SUCCESS]");
    if (includeAbout) {
        notification.getAbouts().put(AnyTypeKind.USER.name(), SyncopeClient.getUserSearchConditionBuilder().inGroups("bf825fe1-7320-4a54-bd64-143b5c18ab97").query());
    }
    notification.setRecipientsFIQL(SyncopeClient.getUserSearchConditionBuilder().inGroups("f779c0d4-633b-4be5-8f57-32eb478a3ca5").query());
    notification.setSelfAsRecipient(true);
    notification.setRecipientAttrName("email");
    if (staticRecipients != null) {
        notification.getStaticRecipients().addAll(Arrays.asList(staticRecipients));
    }
    notification.setSender(sender);
    notification.setSubject(subject);
    notification.setTemplate("optin");
    notification.setActive(active);
    Response response = notificationService.create(notification);
    notification = getObject(response.getLocation(), NotificationService.class, NotificationTO.class);
    assertNotNull(notification);
    // 2. create user
    UserTO userTO = UserITCase.getUniqueSampleTO("notificationtest@syncope.apache.org");
    userTO.getMemberships().add(new MembershipTO.Builder().group("bf825fe1-7320-4a54-bd64-143b5c18ab97").build());
    userTO = createUser(userTO).getEntity();
    assertNotNull(userTO);
    return Pair.of(notification.getKey(), userTO.getUsername());
}
Also used : Response(javax.ws.rs.core.Response) NotificationTO(org.apache.syncope.common.lib.to.NotificationTO) UserTO(org.apache.syncope.common.lib.to.UserTO) MembershipTO(org.apache.syncope.common.lib.to.MembershipTO) NotificationService(org.apache.syncope.common.rest.api.service.NotificationService)

Example 10 with NotificationTO

use of org.apache.syncope.common.lib.to.NotificationTO in project syncope by apache.

the class NotificationDataBinderImpl method update.

@Override
public void update(final Notification notification, final NotificationTO notificationTO) {
    BeanUtils.copyProperties(notificationTO, notification, IGNORE_PROPERTIES);
    SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
    MailTemplate template = mailTemplateDAO.find(notificationTO.getTemplate());
    if (template == null) {
        sce.getElements().add("template");
    }
    notification.setTemplate(template);
    if (notification.getEvents().isEmpty()) {
        sce.getElements().add("events");
    }
    if (!notification.getStaticRecipients().isEmpty()) {
        notification.getStaticRecipients().forEach(mail -> {
            Matcher matcher = SyncopeConstants.EMAIL_PATTERN.matcher(mail);
            if (!matcher.matches()) {
                LOG.error("Invalid mail address: {}", mail);
                sce.getElements().add("staticRecipients: " + mail);
            }
        });
    }
    if (!sce.isEmpty()) {
        throw sce;
    }
    // 1. add or update all (valid) abouts from TO
    notificationTO.getAbouts().entrySet().stream().filter(entry -> StringUtils.isNotBlank(entry.getValue())).forEachOrdered((entry) -> {
        AnyType anyType = anyTypeDAO.find(entry.getKey());
        if (anyType == null) {
            LOG.debug("Invalid AnyType {} specified, ignoring...", entry.getKey());
        } else {
            AnyAbout about = notification.getAbout(anyType).orElse(null);
            if (about == null) {
                about = entityFactory.newEntity(AnyAbout.class);
                about.setAnyType(anyType);
                about.setNotification(notification);
                notification.add(about);
            }
            about.set(entry.getValue());
        }
    });
    // 2. remove all abouts not contained in the TO
    notification.getAbouts().removeIf(anyAbout -> !notificationTO.getAbouts().containsKey(anyAbout.getAnyType().getKey()));
    // 3. verify recipientAttrName
    try {
        intAttrNameParser.parse(notification.getRecipientAttrName(), AnyTypeKind.USER);
    } catch (ParseException e) {
        SyncopeClientException invalidRequest = SyncopeClientException.build(ClientExceptionType.InvalidRequest);
        invalidRequest.getElements().add(e.getMessage());
        throw invalidRequest;
    }
    if (notificationTO.getRecipientsProvider() == null) {
        notification.setRecipientsProvider(null);
    } else {
        Implementation recipientsProvider = implementationDAO.find(notificationTO.getRecipientsProvider());
        if (recipientsProvider == null) {
            LOG.debug("Invalid " + Implementation.class.getSimpleName() + " {}, ignoring...", notificationTO.getRecipientsProvider());
        } else {
            notification.setRecipientsProvider(recipientsProvider);
        }
    }
}
Also used : SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) Logger(org.slf4j.Logger) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) LoggerFactory(org.slf4j.LoggerFactory) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation) Autowired(org.springframework.beans.factory.annotation.Autowired) BeanUtils(org.apache.syncope.core.spring.BeanUtils) StringUtils(org.apache.commons.lang3.StringUtils) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) Notification(org.apache.syncope.core.persistence.api.entity.Notification) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) Component(org.springframework.stereotype.Component) Matcher(java.util.regex.Matcher) NotificationTO(org.apache.syncope.common.lib.to.NotificationTO) AnyAbout(org.apache.syncope.core.persistence.api.entity.AnyAbout) MailTemplate(org.apache.syncope.core.persistence.api.entity.MailTemplate) MailTemplateDAO(org.apache.syncope.core.persistence.api.dao.MailTemplateDAO) ClientExceptionType(org.apache.syncope.common.lib.types.ClientExceptionType) IntAttrNameParser(org.apache.syncope.core.provisioning.java.IntAttrNameParser) ParseException(java.text.ParseException) NotificationDataBinder(org.apache.syncope.core.provisioning.api.data.NotificationDataBinder) ImplementationDAO(org.apache.syncope.core.persistence.api.dao.ImplementationDAO) Matcher(java.util.regex.Matcher) AnyAbout(org.apache.syncope.core.persistence.api.entity.AnyAbout) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) MailTemplate(org.apache.syncope.core.persistence.api.entity.MailTemplate) ParseException(java.text.ParseException) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Implementation(org.apache.syncope.core.persistence.api.entity.Implementation)

Aggregations

NotificationTO (org.apache.syncope.common.lib.to.NotificationTO)17 Test (org.junit.jupiter.api.Test)11 Response (javax.ws.rs.core.Response)8 NotificationService (org.apache.syncope.common.rest.api.service.NotificationService)7 SyncopeClientException (org.apache.syncope.common.lib.SyncopeClientException)6 NotificationTaskTO (org.apache.syncope.common.lib.to.NotificationTaskTO)2 Notification (org.apache.syncope.core.persistence.api.entity.Notification)2 URI (java.net.URI)1 ParseException (java.text.ParseException)1 Matcher (java.util.regex.Matcher)1 StringUtils (org.apache.commons.lang3.StringUtils)1 SyncopeConstants (org.apache.syncope.common.lib.SyncopeConstants)1 GroupTO (org.apache.syncope.common.lib.to.GroupTO)1 ImplementationTO (org.apache.syncope.common.lib.to.ImplementationTO)1 MembershipTO (org.apache.syncope.common.lib.to.MembershipTO)1 PushTaskTO (org.apache.syncope.common.lib.to.PushTaskTO)1 UserTO (org.apache.syncope.common.lib.to.UserTO)1 AnyTypeKind (org.apache.syncope.common.lib.types.AnyTypeKind)1 ClientExceptionType (org.apache.syncope.common.lib.types.ClientExceptionType)1 TaskQuery (org.apache.syncope.common.rest.api.beans.TaskQuery)1