Search in sources :

Example 21 with SMSException

use of com.sun.identity.sm.SMSException 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 22 with SMSException

use of com.sun.identity.sm.SMSException 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 23 with SMSException

use of com.sun.identity.sm.SMSException in project OpenAM by OpenRock.

the class RealmResource method updateInstance.

/**
     * {@inheritDoc}
     */
@Override
public Promise<ResourceResponse, ResourceException> updateInstance(Context context, String resourceId, UpdateRequest request) {
    RealmContext realmContext = context.asContext(RealmContext.class);
    String realmPath = realmContext.getResolvedRealm();
    final JsonValue realmDetails = request.getContent();
    ResourceResponse resource;
    String realm;
    OrganizationConfigManager ocm;
    OrganizationConfigManager realmCreatedOcm;
    String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
    try {
        hasPermission(context);
        realm = checkForTopLevelRealm(resourceId);
        if (realm != null && !realm.startsWith("/")) {
            realm = "/" + realm;
        }
        if (!realmPath.equalsIgnoreCase("/")) {
            realm = realmPath + realm;
        }
        // Update a realm - if it's not found, error out.
        ocm = new OrganizationConfigManager(getSSOToken(), realm);
        List newServiceNames;
        // update ID_REPO attributes
        updateConfiguredServices(ocm, createServicesMap(realmDetails));
        newServiceNames = realmDetails.get(SERVICE_NAMES).asList();
        if (newServiceNames == null || newServiceNames.isEmpty()) {
            debug.error("RealmResource.updateInstance() : No Services defined.");
        } else {
            //assign services to realm
            assignServices(ocm, newServiceNames);
        }
        // READ THE REALM
        realmCreatedOcm = new OrganizationConfigManager(getSSOToken(), realm);
        debug.message("RealmResource.updateInstance :: UPDATE of realm " + realm + " performed by " + principalName);
        // create a resource for handler to return
        resource = newResourceResponse(realm, String.valueOf(System.currentTimeMillis()), createJsonMessage("realmUpdated", realmCreatedOcm.getOrganizationName()));
        return newResultPromise(resource);
    } catch (SMSException e) {
        try {
            configureErrorMessage(e);
            return new NotFoundException(e.getMessage(), e).asPromise();
        } catch (ForbiddenException fe) {
            // User does not have authorization
            debug.error("RealmResource.updateInstance() : Cannot UPDATE " + resourceId, fe);
            return fe.asPromise();
        } catch (PermanentException pe) {
            debug.error("RealmResource.updateInstance() : Cannot UPDATE " + resourceId, pe);
            // Cannot recover from this exception
            return pe.asPromise();
        } catch (ConflictException ce) {
            debug.error("RealmResource.updateInstance() : Cannot UPDATE " + resourceId, ce);
            return ce.asPromise();
        } catch (BadRequestException be) {
            debug.error("RealmResource.updateInstance() : Cannot UPDATE " + resourceId, be);
            return be.asPromise();
        } catch (Exception ex) {
            debug.error("RealmResource.updateInstance() : Cannot UPDATE " + resourceId, ex);
            return new NotFoundException("Cannot update realm.", ex).asPromise();
        }
    } catch (SSOException sso) {
        debug.error("RealmResource.updateInstance() : Cannot UPDATE " + resourceId, sso);
        return new PermanentException(401, "Access Denied", null).asPromise();
    } catch (ForbiddenException fe) {
        debug.error("RealmResource.updateInstance() : Cannot UPDATE " + resourceId, fe);
        return fe.asPromise();
    } catch (PermanentException pe) {
        debug.error("RealmResource.Instance() : Cannot UPDATE " + resourceId, pe);
        // Cannot recover from this exception
        return pe.asPromise();
    } catch (Exception ex) {
        debug.error("RealmResource.updateInstance() : Cannot UPDATE " + resourceId, ex);
        return new NotFoundException("Cannot update realm.", ex).asPromise();
    }
}
Also used : ForbiddenException(org.forgerock.json.resource.ForbiddenException) RealmContext(org.forgerock.openam.rest.RealmContext) SMSException(com.sun.identity.sm.SMSException) ConflictException(org.forgerock.json.resource.ConflictException) JsonValue(org.forgerock.json.JsonValue) NotFoundException(org.forgerock.json.resource.NotFoundException) SSOException(com.iplanet.sso.SSOException) NotFoundException(org.forgerock.json.resource.NotFoundException) BadRequestException(org.forgerock.json.resource.BadRequestException) ConflictException(org.forgerock.json.resource.ConflictException) PermanentException(org.forgerock.json.resource.PermanentException) SMSException(com.sun.identity.sm.SMSException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ForbiddenException(org.forgerock.json.resource.ForbiddenException) JsonValueException(org.forgerock.json.JsonValueException) ResourceException(org.forgerock.json.resource.ResourceException) SSOException(com.iplanet.sso.SSOException) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) OrganizationConfigManager(com.sun.identity.sm.OrganizationConfigManager) PermanentException(org.forgerock.json.resource.PermanentException) BadRequestException(org.forgerock.json.resource.BadRequestException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 24 with SMSException

use of com.sun.identity.sm.SMSException in project OpenAM by OpenRock.

the class RealmResource method readInstance.

/**
     * {@inheritDoc}
     */
@Override
public Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId, ReadRequest request) {
    RealmContext realmContext = context.asContext(RealmContext.class);
    String realmPath = realmContext.getResolvedRealm();
    ResourceResponse resource;
    JsonValue jval;
    String holdResourceId = checkForTopLevelRealm(resourceId);
    try {
        hasPermission(context);
        if (holdResourceId != null && !holdResourceId.startsWith("/")) {
            holdResourceId = "/" + holdResourceId;
        }
        if (!realmPath.equalsIgnoreCase("/")) {
            holdResourceId = realmPath + holdResourceId;
        }
        OrganizationConfigManager ocm = new OrganizationConfigManager(getSSOToken(), holdResourceId);
        // get associated services for this realm , include mandatory service names.
        Set serviceNames = ocm.getAssignedServices();
        jval = createJsonMessage(SERVICE_NAMES, serviceNames);
        String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
        resource = newResourceResponse(resourceId, String.valueOf(System.currentTimeMillis()), jval);
        if (debug.messageEnabled()) {
            debug.message("RealmResource.readInstance :: READ : Successfully read realm, " + resourceId + " performed by " + principalName);
        }
        return newResultPromise(resource);
    } catch (SSOException sso) {
        debug.error("RealmResource.updateInstance() : Cannot READ " + resourceId, sso);
        return new PermanentException(401, "Access Denied", null).asPromise();
    } catch (ForbiddenException fe) {
        debug.error("RealmResource.readInstance() : Cannot READ " + resourceId + ":" + fe);
        return fe.asPromise();
    } catch (SMSException smse) {
        debug.error("RealmResource.readInstance() : Cannot READ " + resourceId, smse);
        try {
            configureErrorMessage(smse);
            return new BadRequestException(smse.getMessage(), smse).asPromise();
        } catch (NotFoundException nf) {
            debug.error("RealmResource.readInstance() : Cannot READ " + resourceId, nf);
            return nf.asPromise();
        } catch (ForbiddenException fe) {
            // User does not have authorization
            debug.error("RealmResource.readInstance() : Cannot READ " + resourceId, fe);
            return fe.asPromise();
        } catch (PermanentException pe) {
            debug.error("RealmResource.readInstance() : Cannot READ " + resourceId, pe);
            // Cannot recover from this exception
            return pe.asPromise();
        } catch (ConflictException ce) {
            debug.error("RealmResource.readInstance() : Cannot READ " + resourceId, ce);
            return ce.asPromise();
        } catch (BadRequestException be) {
            debug.error("RealmResource.readInstance() : Cannot READ " + resourceId, be);
            return be.asPromise();
        } catch (Exception e) {
            debug.error("RealmResource.readInstance() : Cannot READ " + resourceId, e);
            return new BadRequestException(e.getMessage(), e).asPromise();
        }
    } catch (Exception e) {
        return new BadRequestException(e.getMessage(), e).asPromise();
    }
}
Also used : ForbiddenException(org.forgerock.json.resource.ForbiddenException) HashSet(java.util.HashSet) Set(java.util.Set) RealmContext(org.forgerock.openam.rest.RealmContext) SMSException(com.sun.identity.sm.SMSException) ConflictException(org.forgerock.json.resource.ConflictException) JsonValue(org.forgerock.json.JsonValue) NotFoundException(org.forgerock.json.resource.NotFoundException) SSOException(com.iplanet.sso.SSOException) NotFoundException(org.forgerock.json.resource.NotFoundException) BadRequestException(org.forgerock.json.resource.BadRequestException) ConflictException(org.forgerock.json.resource.ConflictException) PermanentException(org.forgerock.json.resource.PermanentException) SMSException(com.sun.identity.sm.SMSException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ForbiddenException(org.forgerock.json.resource.ForbiddenException) JsonValueException(org.forgerock.json.JsonValueException) ResourceException(org.forgerock.json.resource.ResourceException) SSOException(com.iplanet.sso.SSOException) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) OrganizationConfigManager(com.sun.identity.sm.OrganizationConfigManager) PermanentException(org.forgerock.json.resource.PermanentException) BadRequestException(org.forgerock.json.resource.BadRequestException)

Example 25 with SMSException

use of com.sun.identity.sm.SMSException in project OpenAM by OpenRock.

the class AuthIdHelper method getSigningKey.

/**
     * Retrieves the secret key to use to sign and verify the JWT.
     *
     * @param orgName The organisation name for the realm being authenticated against.
     * @return The signing key.
     */
private SecretKey getSigningKey(String orgName) throws RestAuthException {
    SSOToken token = coreServicesWrapper.getAdminToken();
    try {
        ServiceConfigManager scm = coreServicesWrapper.getServiceConfigManager(AUTH_SERVICE_NAME, token);
        ServiceConfig orgConfig = scm.getOrganizationConfig(orgName, null);
        byte[] key = Base64.decode(CollectionHelper.getMapAttr(orgConfig.getAttributes(), SHARED_SECRET_ATTR));
        return new SecretKeySpec(key, "RAW");
    } catch (SMSException | SSOException | NullPointerException e) {
        throw new RestAuthException(ResourceException.INTERNAL_ERROR, e);
    }
}
Also used : RestAuthException(org.forgerock.openam.core.rest.authn.exceptions.RestAuthException) SSOToken(com.iplanet.sso.SSOToken) ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SSOException(com.iplanet.sso.SSOException) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager)

Aggregations

SMSException (com.sun.identity.sm.SMSException)704 SSOException (com.iplanet.sso.SSOException)525 Set (java.util.Set)272 HashSet (java.util.HashSet)200 SSOToken (com.iplanet.sso.SSOToken)185 Map (java.util.Map)166 ServiceConfig (com.sun.identity.sm.ServiceConfig)164 HashMap (java.util.HashMap)158 CLIException (com.sun.identity.cli.CLIException)149 ServiceSchema (com.sun.identity.sm.ServiceSchema)138 Iterator (java.util.Iterator)133 ServiceSchemaManager (com.sun.identity.sm.ServiceSchemaManager)131 ServiceConfigManager (com.sun.identity.sm.ServiceConfigManager)104 IOutput (com.sun.identity.cli.IOutput)96 IdRepoException (com.sun.identity.idm.IdRepoException)86 OrganizationConfigManager (com.sun.identity.sm.OrganizationConfigManager)84 AMConsoleException (com.sun.identity.console.base.model.AMConsoleException)83 AttributeSchema (com.sun.identity.sm.AttributeSchema)66 IOException (java.io.IOException)55 List (java.util.List)51