Search in sources :

Example 1 with MailServer

use of org.forgerock.openam.services.email.MailServer in project OpenAM by OpenRock.

the class IdentityResourceV2 method sendNotification.

/**
     * Sends email notification to end user
     * @param to Resource receiving notification
     * @param subject Notification subject
     * @param message Notification Message
     * @param confirmationLink Confirmation Link to be sent
     * @throws Exception when message cannot be sent
     */
private void sendNotification(String to, String subject, String message, String realm, String confirmationLink) throws ResourceException {
    try {
        mailmgr = new ServiceConfigManager(RestUtils.getToken(), MailServerImpl.SERVICE_NAME, MailServerImpl.SERVICE_VERSION);
        mailscm = mailmgr.getOrganizationConfig(realm, null);
        mailattrs = mailscm.getAttributes();
    } catch (SMSException smse) {
        if (debug.errorEnabled()) {
            debug.error("{} :: Cannot create service {}", SEND_NOTIF_TAG, MailServerImpl.SERVICE_NAME, smse);
        }
        throw new InternalServerErrorException("Cannot create the service: " + MailServerImpl.SERVICE_NAME, smse);
    } catch (SSOException ssoe) {
        if (debug.errorEnabled()) {
            debug.error("{} :: Invalid SSOToken ", SEND_NOTIF_TAG, ssoe);
        }
        throw new InternalServerErrorException("Cannot create the service: " + MailServerImpl.SERVICE_NAME, ssoe);
    }
    if (mailattrs == null || mailattrs.isEmpty()) {
        if (debug.errorEnabled()) {
            debug.error("{} :: no attrs set {}", SEND_NOTIF_TAG, mailattrs);
        }
        throw new NotFoundException("No service Config Manager found for realm " + realm);
    }
    // Get MailServer Implementation class
    String attr = mailattrs.get(MAIL_IMPL_CLASS).iterator().next();
    MailServer mailServer;
    try {
        mailServer = mailServerLoader.load(attr, realm);
    } catch (IllegalStateException e) {
        debug.error("{} :: Failed to load mail server implementation: {}", SEND_NOTIF_TAG, attr, e);
        throw new InternalServerErrorException("Failed to load mail server implementation: " + attr, e);
    }
    try {
        // Check if subject has not  been included
        if (StringUtils.isBlank(subject)) {
            // Use default email service subject
            subject = mailattrs.get(MAIL_SUBJECT).iterator().next();
        }
    } catch (Exception e) {
        if (debug.warningEnabled()) {
            debug.warning("{} no subject found ", SEND_NOTIF_TAG, e);
        }
        subject = "";
    }
    try {
        // Check if Custom Message has been included
        if (StringUtils.isBlank(message)) {
            // Use default email service message
            message = mailattrs.get(MAIL_MESSAGE).iterator().next();
        }
        message = message + System.getProperty("line.separator") + confirmationLink;
    } catch (Exception e) {
        if (debug.warningEnabled()) {
            debug.warning("{} no message found", SEND_NOTIF_TAG, e);
        }
        message = confirmationLink;
    }
    // Send the emails via the implementation class
    try {
        mailServer.sendEmail(to, subject, message);
    } catch (MessagingException e) {
        if (debug.errorEnabled()) {
            debug.error("{} Failed to send mail", SEND_NOTIF_TAG, e);
        }
        throw new InternalServerErrorException("Failed to send mail", e);
    }
}
Also used : MailServer(org.forgerock.openam.services.email.MailServer) SMSException(com.sun.identity.sm.SMSException) MessagingException(javax.mail.MessagingException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) NotFoundException(org.forgerock.json.resource.NotFoundException) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) SSOException(com.iplanet.sso.SSOException) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager) MessagingException(javax.mail.MessagingException) ConflictException(org.forgerock.json.resource.ConflictException) PermanentException(org.forgerock.json.resource.PermanentException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ForbiddenException(org.forgerock.json.resource.ForbiddenException) DeleteFailedException(org.forgerock.openam.cts.exceptions.DeleteFailedException) SSOException(com.iplanet.sso.SSOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotFoundException(org.forgerock.json.resource.NotFoundException) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) NotSupportedException(org.forgerock.json.resource.NotSupportedException) BadRequestException(org.forgerock.json.resource.BadRequestException) IdRepoException(com.sun.identity.idm.IdRepoException) SMSException(com.sun.identity.sm.SMSException) ResourceException(org.forgerock.json.resource.ResourceException) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException)

Example 2 with MailServer

use of org.forgerock.openam.services.email.MailServer in project OpenAM by OpenRock.

the class IdentityResourceV1 method sendNotification.

/**
     * Sends email notification to end user
     * @param to Resource receiving notification
     * @param subject Notification subject
     * @param message Notification Message
     * @param confirmationLink Confirmation Link to be sent
     * @throws Exception when message cannot be sent
     */
private void sendNotification(String to, String subject, String message, String realm, String confirmationLink) throws ResourceException {
    try {
        mailmgr = new ServiceConfigManager(RestUtils.getToken(), MailServerImpl.SERVICE_NAME, MailServerImpl.SERVICE_VERSION);
        mailscm = mailmgr.getOrganizationConfig(realm, null);
        mailattrs = mailscm.getAttributes();
    } catch (SMSException smse) {
        if (debug.errorEnabled()) {
            debug.error("{} :: Cannot create service {}", SEND_NOTIF_TAG, MailServerImpl.SERVICE_NAME, smse);
        }
        throw new InternalServerErrorException("Cannot create the service: " + MailServerImpl.SERVICE_NAME, smse);
    } catch (SSOException ssoe) {
        if (debug.errorEnabled()) {
            debug.error("{} :: Invalid SSOToken ", SEND_NOTIF_TAG, ssoe);
        }
        throw new InternalServerErrorException("Cannot create the service: " + MailServerImpl.SERVICE_NAME, ssoe);
    }
    if (mailattrs == null || mailattrs.isEmpty()) {
        if (debug.errorEnabled()) {
            debug.error("{} :: no attrs set {}", SEND_NOTIF_TAG, mailattrs);
        }
        throw new NotFoundException("No service Config Manager found for realm " + realm);
    }
    // Get MailServer Implementation class
    String attr = mailattrs.get(MAIL_IMPL_CLASS).iterator().next();
    MailServer mailServer;
    try {
        mailServer = mailServerLoader.load(attr, realm);
    } catch (IllegalStateException e) {
        debug.error("{} :: Failed to load mail server implementation: {}", SEND_NOTIF_TAG, attr, e);
        throw new InternalServerErrorException("Failed to load mail server implementation: " + attr, e);
    }
    try {
        // Check if subject has not  been included
        if (StringUtils.isBlank(subject)) {
            // Use default email service subject
            subject = mailattrs.get(MAIL_SUBJECT).iterator().next();
        }
    } catch (Exception e) {
        if (debug.warningEnabled()) {
            debug.warning("{} no subject found ", SEND_NOTIF_TAG, e);
        }
        subject = "";
    }
    try {
        // Check if Custom Message has been included
        if (StringUtils.isBlank(message)) {
            // Use default email service message
            message = mailattrs.get(MAIL_MESSAGE).iterator().next();
        }
        message = message + System.getProperty("line.separator") + confirmationLink;
    } catch (Exception e) {
        if (debug.warningEnabled()) {
            debug.warning("{} no message found", SEND_NOTIF_TAG, e);
        }
        message = confirmationLink;
    }
    // Send the emails via the implementation class
    try {
        mailServer.sendEmail(to, subject, message);
    } catch (MessagingException e) {
        if (debug.errorEnabled()) {
            debug.error("{} Failed to send mail", SEND_NOTIF_TAG, e);
        }
        throw new InternalServerErrorException("Failed to send mail", e);
    }
}
Also used : MailServer(org.forgerock.openam.services.email.MailServer) SMSException(com.sun.identity.sm.SMSException) MessagingException(javax.mail.MessagingException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) NotFoundException(org.forgerock.json.resource.NotFoundException) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) SSOException(com.iplanet.sso.SSOException) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) DeleteFailedException(org.forgerock.openam.cts.exceptions.DeleteFailedException) SSOException(com.iplanet.sso.SSOException) NotFoundException(org.forgerock.json.resource.NotFoundException) NotSupportedException(org.forgerock.json.resource.NotSupportedException) BadRequestException(org.forgerock.json.resource.BadRequestException) MessagingException(javax.mail.MessagingException) ConflictException(org.forgerock.json.resource.ConflictException) PermanentException(org.forgerock.json.resource.PermanentException) ForbiddenException(org.forgerock.json.resource.ForbiddenException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) IdRepoException(com.sun.identity.idm.IdRepoException) SMSException(com.sun.identity.sm.SMSException) ResourceException(org.forgerock.json.resource.ResourceException) CoreTokenException(org.forgerock.openam.cts.exceptions.CoreTokenException)

Example 3 with MailServer

use of org.forgerock.openam.services.email.MailServer in project OpenAM by OpenRock.

the class MailService method sendEmail.

// Mapping to known type
@SuppressWarnings("unchecked")
private JsonValue sendEmail(String realm, JsonValue jsonValue) throws ResourceException {
    String to = jsonValue.get("to").asString();
    if (isBlank(to)) {
        throw new BadRequestException("to field is missing");
    }
    String mimeType = jsonValue.get("type").asString();
    if (isBlank(mimeType)) {
        throw new BadRequestException("mime type needs to be specified");
    }
    String subject = jsonValue.get("subject").asString();
    String body = jsonValue.get("body").asString();
    Map<String, Set<String>> mailConfigAttributes;
    try {
        ServiceConfigManager configManager = new ServiceConfigManager(RestUtils.getToken(), MailServerImpl.SERVICE_NAME, MailServerImpl.SERVICE_VERSION);
        ServiceConfig mailConfig = configManager.getOrganizationConfig(realm, null);
        mailConfigAttributes = mailConfig.getAttributes();
    } catch (SMSException | SSOException e) {
        throw new InternalServerErrorException("Cannot create the service " + MailServerImpl.SERVICE_NAME, e);
    }
    if (isEmpty(mailConfigAttributes)) {
        throw new InternalServerErrorException("No service mail config found for realm " + realm);
    }
    MailServer mailServer;
    try {
        String attr = mailConfigAttributes.get(MAIL_SERVER_CLASS).iterator().next();
        mailServer = mailServerLoader.load(attr, realm);
    } catch (IllegalStateException e) {
        throw new InternalServerErrorException("Failed to create mail server", e);
    }
    if (isBlank(subject)) {
        subject = mailConfigAttributes.get(MAIL_SUBJECT).iterator().next();
    }
    if (isBlank(body)) {
        body = mailConfigAttributes.get(MAIL_BODY).iterator().next();
    }
    try {
        mailServer.sendEmail(to, subject, body, mimeType);
    } catch (MessagingException e) {
        throw new InternalServerErrorException("Failed to send email", e);
    }
    return json(object(field("success", "true")));
}
Also used : Set(java.util.Set) SMSException(com.sun.identity.sm.SMSException) MessagingException(javax.mail.MessagingException) SSOException(com.iplanet.sso.SSOException) MailServer(org.forgerock.openam.services.email.MailServer) ServiceConfig(com.sun.identity.sm.ServiceConfig) BadRequestException(org.forgerock.json.resource.BadRequestException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager)

Example 4 with MailServer

use of org.forgerock.openam.services.email.MailServer in project OpenAM by OpenRock.

the class MailServerLoaderTest method shouldLoadTestClass.

@Test
public void shouldLoadTestClass() {
    MailServerLoader loader = new MailServerLoader();
    MailServer result = loader.load(TestMailServer.class.getName(), "badger");
    assertThat(result).isInstanceOf(TestMailServer.class);
}
Also used : MailServer(org.forgerock.openam.services.email.MailServer) Test(org.testng.annotations.Test)

Aggregations

MailServer (org.forgerock.openam.services.email.MailServer)4 SSOException (com.iplanet.sso.SSOException)3 SMSException (com.sun.identity.sm.SMSException)3 ServiceConfigManager (com.sun.identity.sm.ServiceConfigManager)3 MessagingException (javax.mail.MessagingException)3 BadRequestException (org.forgerock.json.resource.BadRequestException)3 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)3 IdRepoException (com.sun.identity.idm.IdRepoException)2 ServiceNotFoundException (com.sun.identity.sm.ServiceNotFoundException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ConflictException (org.forgerock.json.resource.ConflictException)2 ForbiddenException (org.forgerock.json.resource.ForbiddenException)2 NotFoundException (org.forgerock.json.resource.NotFoundException)2 NotSupportedException (org.forgerock.json.resource.NotSupportedException)2 PermanentException (org.forgerock.json.resource.PermanentException)2 ResourceException (org.forgerock.json.resource.ResourceException)2 CoreTokenException (org.forgerock.openam.cts.exceptions.CoreTokenException)2 DeleteFailedException (org.forgerock.openam.cts.exceptions.DeleteFailedException)2 ServiceConfig (com.sun.identity.sm.ServiceConfig)1 Set (java.util.Set)1