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();
}
}
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());
}
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();
}
}
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();
}
}
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();
}
}
Aggregations