Search in sources :

Example 11 with NotSupportedException

use of org.forgerock.json.resource.NotSupportedException in project OpenAM by OpenRock.

the class IdentityResourceV1 method createRegistrationEmail.

/**
     * This method will create a confirmation email that contains a {@link org.forgerock.openam.cts.api.tokens.Token},
     * confirmationId and email that was provided in the request.
     * @param context Current Server Context
     * @param request Request from client to retrieve id
     */
private Promise<ActionResponse, ResourceException> createRegistrationEmail(final Context context, final ActionRequest request, final String realm, final RestSecurity restSecurity) {
    JsonValue result = new JsonValue(new LinkedHashMap<String, Object>(1));
    final JsonValue jVal = request.getContent();
    String emailAddress = null;
    String confirmationLink;
    String tokenID;
    try {
        if (restSecurity == null) {
            if (debug.warningEnabled()) {
                debug.warning("IdentityResource.createRegistrationEmail(): Rest Security not created. " + "restSecurity={}", restSecurity);
            }
            throw new NotFoundException("Rest Security Service not created");
        }
        if (!restSecurity.isSelfRegistration()) {
            if (debug.warningEnabled()) {
                debug.warning("IdentityResource.createRegistrationEmail(): Self-Registration set to : {}", restSecurity.isSelfRegistration());
            }
            throw new NotSupportedException("Self Registration is not enabled.");
        }
        // Get full deployment URL
        HttpContext header = context.asContext(HttpContext.class);
        StringBuilder deploymentURL = RestUtils.getFullDeploymentURI(header.getPath());
        // Get the email address provided from registration page
        emailAddress = jVal.get(EMAIL).asString();
        if (StringUtils.isBlank(emailAddress)) {
            throw new BadRequestException("Email not provided");
        }
        String subject = jVal.get("subject").asString();
        String message = jVal.get("message").asString();
        // Retrieve email registration token life time
        Long tokenLifeTime = restSecurity.getSelfRegTLT();
        // Create CTS Token
        org.forgerock.openam.cts.api.tokens.Token ctsToken = generateToken(emailAddress, "anonymous", tokenLifeTime, realm);
        // Store token in datastore
        CTSHolder.getCTS().createAsync(ctsToken);
        tokenID = ctsToken.getTokenId();
        // Create confirmationId
        String confirmationId = Hash.hash(tokenID + emailAddress + SystemProperties.get(AM_ENCRYPTION_PWD));
        // Build Confirmation URL
        String confURL = restSecurity.getSelfRegistrationConfirmationUrl();
        StringBuilder confURLBuilder = new StringBuilder(100);
        if (StringUtils.isBlank(confURL)) {
            confURLBuilder.append(deploymentURL.append("/json/confirmation/register").toString());
        } else {
            confURLBuilder.append(confURL);
        }
        confirmationLink = confURLBuilder.append("?confirmationId=").append(requestParamEncode(confirmationId)).append("&email=").append(requestParamEncode(emailAddress)).append("&tokenId=").append(requestParamEncode(tokenID)).append("&realm=").append(realm).toString();
        // Send Registration
        sendNotification(emailAddress, subject, message, realm, confirmationLink);
        if (debug.messageEnabled()) {
            debug.message("IdentityResource.createRegistrationEmail() :: Sent notification to={} with subject={}. " + "In realm={} for token ID={}", emailAddress, subject, realm, tokenID);
        }
        return newResultPromise(newActionResponse(result));
    } catch (BadRequestException | NotFoundException be) {
        debug.warning("IdentityResource.createRegistrationEmail: Cannot send email to {}", emailAddress, be);
        return be.asPromise();
    } catch (NotSupportedException nse) {
        debug.error("IdentityResource.createRegistrationEmail: Operation not enabled", nse);
        return nse.asPromise();
    } catch (Exception e) {
        debug.error("IdentityResource.createRegistrationEmail: Cannot send email to {}", emailAddress, e);
        return new NotFoundException("Email not sent").asPromise();
    }
}
Also used : IdentityRestUtils.identityDetailsToJsonValue(org.forgerock.openam.core.rest.IdentityRestUtils.identityDetailsToJsonValue) JsonValue(org.forgerock.json.JsonValue) HttpContext(org.forgerock.json.resource.http.HttpContext) NotFoundException(org.forgerock.json.resource.NotFoundException) ServiceNotFoundException(com.sun.identity.sm.ServiceNotFoundException) 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) BadRequestException(org.forgerock.json.resource.BadRequestException) NotSupportedException(org.forgerock.json.resource.NotSupportedException)

Example 12 with NotSupportedException

use of org.forgerock.json.resource.NotSupportedException in project OpenAM by OpenRock.

the class CommonTasksResource method queryCollection.

@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler handler) {
    if (!"true".equals(request.getQueryFilter().toString())) {
        return new NotSupportedException("Query not supported: " + request.getQueryFilter()).asPromise();
    }
    //TODO pass in locale
    Locale locale = Locale.ROOT;
    JsonValue configuration = configurationManager.getCommonTasksConfiguration(getResourceBundle(locale));
    for (String key : configuration.keys()) {
        JsonValue resource = configuration.get(key);
        resource.add(ResourceResponse.FIELD_CONTENT_ID, key);
        handler.handleResource(newResourceResponse(key, String.valueOf(resource.getObject().hashCode()), resource));
    }
    return newResultPromise(newQueryResponse());
}
Also used : Locale(java.util.Locale) JsonValue(org.forgerock.json.JsonValue) NotSupportedException(org.forgerock.json.resource.NotSupportedException)

Example 13 with NotSupportedException

use of org.forgerock.json.resource.NotSupportedException in project OpenAM by OpenRock.

the class AuthenticationModuleTypeHandler method handleQuery.

/**
     * Returns the list of configured authentication module instances for the current realm.
     *
     * {@inheritDoc}
     */
@Override
public Promise<QueryResponse, ResourceException> handleQuery(Context context, QueryRequest request, QueryResourceHandler handler) {
    if (!"true".equals(request.getQueryFilter().toString())) {
        return new NotSupportedException("Query not supported: " + request.getQueryFilter()).asPromise();
    }
    if (request.getPagedResultsCookie() != null || request.getPagedResultsOffset() > 0 || request.getPageSize() > 0) {
        return new NotSupportedException("Query paging not currently supported").asPromise();
    }
    try {
        SSOToken ssoToken = context.asContext(SSOTokenContext.class).getCallerSSOToken();
        String realm = context.asContext(RealmContext.class).getResolvedRealm();
        AMAuthenticationManager mgr = new AMAuthenticationManager(ssoToken, realm);
        Set<String> authenticationServiceNames = AMAuthenticationManager.getAuthenticationServiceNames();
        for (String serviceName : authenticationServiceNames) {
            ServiceSchemaManager schemaManager = new ServiceSchemaManager(serviceName, adminToken);
            String resourceId = schemaManager.getResourceName();
            String typeI18N = getI18NValue(schemaManager, resourceId, debug);
            JsonValue result = json(object(field(ResourceResponse.FIELD_CONTENT_ID, resourceId), field("name", typeI18N)));
            handler.handleResource(newResourceResponse(resourceId, String.valueOf(result.hashCode()), result));
        }
        return newResultPromise(newQueryResponse());
    } catch (AMConfigurationException e) {
        debug.warning("::AuthenticationModuleCollectionHandler:: AMConfigurationException on create", e);
        return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
    } catch (SSOException e) {
        debug.warning("::AuthenticationModuleCollectionHandler:: SSOException on create", e);
        return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
    } catch (SMSException e) {
        debug.warning("::AuthenticationModuleCollectionHandler:: SMSException on create", e);
        return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
    }
}
Also used : SSOToken(com.iplanet.sso.SSOToken) RealmContext(org.forgerock.openam.rest.RealmContext) SMSException(com.sun.identity.sm.SMSException) JsonValue(org.forgerock.json.JsonValue) SSOException(com.iplanet.sso.SSOException) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) AMConfigurationException(com.sun.identity.authentication.config.AMConfigurationException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) NotSupportedException(org.forgerock.json.resource.NotSupportedException) ServiceSchemaManager(com.sun.identity.sm.ServiceSchemaManager) AMAuthenticationManager(com.sun.identity.authentication.config.AMAuthenticationManager)

Example 14 with NotSupportedException

use of org.forgerock.json.resource.NotSupportedException in project OpenAM by OpenRock.

the class AuthenticationModuleCollectionHandler method handleQuery.

/**
     * Returns the list of configured authentication module instances for the current realm.
     *
     * {@inheritDoc}
     */
@Override
public Promise<QueryResponse, ResourceException> handleQuery(Context context, QueryRequest request, QueryResourceHandler handler) {
    String searchForId;
    try {
        searchForId = request.getQueryFilter().accept(new AuthenticationModuleQueryFilterVisitor(), null);
    } catch (UnsupportedOperationException e) {
        return new NotSupportedException("Query not supported: " + request.getQueryFilter()).asPromise();
    }
    if (request.getPagedResultsCookie() != null || request.getPagedResultsOffset() > 0 || request.getPageSize() > 0) {
        return new NotSupportedException("Query paging not currently supported").asPromise();
    }
    try {
        SSOToken ssoToken = context.asContext(SSOTokenContext.class).getCallerSSOToken();
        String realm = context.asContext(RealmContext.class).getResolvedRealm();
        AMAuthenticationManager mgr = new AMAuthenticationManager(ssoToken, realm);
        Set<AMAuthenticationInstance> moduleInstances = mgr.getAuthenticationInstances();
        List<ResourceResponse> resourceResponses = new ArrayList<>();
        for (AMAuthenticationInstance instance : moduleInstances) {
            String name = instance.getName();
            if (searchForId == null || searchForId.equalsIgnoreCase(name)) {
                try {
                    ServiceSchemaManager schemaManager = getSchemaManager(instance.getType());
                    String type = schemaManager.getResourceName();
                    String typeDescription = getI18NValue(schemaManager, instance.getType(), debug);
                    JsonValue result = json(object(field(ResourceResponse.FIELD_CONTENT_ID, name), field("typeDescription", typeDescription), field("type", type)));
                    resourceResponses.add(newResourceResponse(name, String.valueOf(result.hashCode()), result));
                } catch (AMConfigurationException ex) {
                    debug.error("AuthenticationModuleCollectionHandler.handleQuery(): Invalid auth module " + "instance configuration: {}", name);
                    if (debug.messageEnabled()) {
                        debug.message("AuthenticationModuleCollectionHandler.handleQuery(): Configuration exception: {}", name, ex);
                    }
                }
            }
        }
        return QueryResponsePresentation.perform(handler, request, resourceResponses);
    } catch (AMConfigurationException e) {
        debug.warning("::AuthenticationModuleCollectionHandler:: AMConfigurationException on create", e);
        return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
    } catch (SSOException e) {
        debug.warning("::AuthenticationModuleCollectionHandler:: SSOException on create", e);
        return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
    } catch (SMSException e) {
        debug.warning("::AuthenticationModuleCollectionHandler:: SMSException on create", e);
        return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
    }
}
Also used : SSOToken(com.iplanet.sso.SSOToken) RealmContext(org.forgerock.openam.rest.RealmContext) SMSException(com.sun.identity.sm.SMSException) ArrayList(java.util.ArrayList) JsonValue(org.forgerock.json.JsonValue) SSOException(com.iplanet.sso.SSOException) SSOTokenContext(org.forgerock.openam.rest.resource.SSOTokenContext) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) AMConfigurationException(com.sun.identity.authentication.config.AMConfigurationException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) AMAuthenticationInstance(com.sun.identity.authentication.config.AMAuthenticationInstance) NotSupportedException(org.forgerock.json.resource.NotSupportedException) ServiceSchemaManager(com.sun.identity.sm.ServiceSchemaManager) AMAuthenticationManager(com.sun.identity.authentication.config.AMAuthenticationManager)

Example 15 with NotSupportedException

use of org.forgerock.json.resource.NotSupportedException in project OpenAM by OpenRock.

the class SessionResource method internalHandleAction.

/**
     * Handle the action specified by the user (i.e. one of those in the validActions set).
     * @param tokenId The id of the token to concentrate on.
     * @param request The ActionRequest, giving us all our parameters.
     */
private Promise<ActionResponse, ResourceException> internalHandleAction(String tokenId, Context context, ActionRequest request) {
    final String action = request.getAction();
    final ActionHandler actionHandler = actionHandlers.get(action);
    if (actionHandler != null) {
        return actionHandler.handle(tokenId, context, request);
    } else {
        String message = String.format("Action %s not implemented for this resource", action);
        NotSupportedException e = new NotSupportedException(message);
        if (LOGGER.messageEnabled()) {
            LOGGER.message("SessionResource.actionInstance :: " + message, e);
        }
        return e.asPromise();
    }
}
Also used : NotSupportedException(org.forgerock.json.resource.NotSupportedException)

Aggregations

NotSupportedException (org.forgerock.json.resource.NotSupportedException)21 JsonValue (org.forgerock.json.JsonValue)11 SSOException (com.iplanet.sso.SSOException)8 SMSException (com.sun.identity.sm.SMSException)8 InternalServerErrorException (org.forgerock.json.resource.InternalServerErrorException)8 ResourceException (org.forgerock.json.resource.ResourceException)7 BadRequestException (org.forgerock.json.resource.BadRequestException)5 RealmContext (org.forgerock.openam.rest.RealmContext)4 IdRepoException (com.sun.identity.idm.IdRepoException)3 ArrayList (java.util.ArrayList)3 ForbiddenException (org.forgerock.json.resource.ForbiddenException)3 SSOToken (com.iplanet.sso.SSOToken)2 AMAuthenticationManager (com.sun.identity.authentication.config.AMAuthenticationManager)2 AMConfigurationException (com.sun.identity.authentication.config.AMConfigurationException)2 ServiceConfig (com.sun.identity.sm.ServiceConfig)2 ServiceConfigManager (com.sun.identity.sm.ServiceConfigManager)2 ServiceNotFoundException (com.sun.identity.sm.ServiceNotFoundException)2 ServiceSchemaManager (com.sun.identity.sm.ServiceSchemaManager)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MessagingException (javax.mail.MessagingException)2