use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class IdentityResourceV1 method anonymousUpdate.
/**
* Perform an anonymous update of a user's password using the provided token.
*
* The token must match a token placed in the CTS in order for the request
* to proceed.
*
* @param context Non null
* @param request Non null
* @param realm Non null
*/
private Promise<ActionResponse, ResourceException> anonymousUpdate(final Context context, final ActionRequest request, final String realm) {
final String tokenID;
String confirmationId;
String username;
String nwpassword;
final JsonValue jVal = request.getContent();
try {
tokenID = jVal.get(TOKEN_ID).asString();
jVal.remove(TOKEN_ID);
confirmationId = jVal.get(CONFIRMATION_ID).asString();
jVal.remove(CONFIRMATION_ID);
username = jVal.get(USERNAME).asString();
nwpassword = jVal.get("userpassword").asString();
if (username == null || username.isEmpty()) {
throw new BadRequestException("username not provided");
}
if (nwpassword == null || username.isEmpty()) {
throw new BadRequestException("new password not provided");
}
validateToken(tokenID, realm, username, confirmationId);
// update Identity
SSOToken admin = RestUtils.getToken();
// Update instance with new password value
return updateInstance(admin, jVal, realm).thenAsync(new AsyncFunction<ActionResponse, ActionResponse, ResourceException>() {
@Override
public Promise<ActionResponse, ResourceException> apply(ActionResponse response) {
// Only remove the token if the update was successful, errors will be set in the handler.
try {
// Even though the generated token will eventually timeout, delete it after a successful read
// so that the reset password request cannot be made again using the same token.
CTSHolder.getCTS().deleteAsync(tokenID);
} catch (DeleteFailedException e) {
// reading and deleting, the token has expired.
if (debug.messageEnabled()) {
debug.message("Deleting token " + tokenID + " after a successful " + "read failed due to " + e.getMessage(), e);
}
} catch (CoreTokenException cte) {
// For any unexpected CTS error
debug.error("Error performing anonymousUpdate", cte);
return new InternalServerErrorException(cte.getMessage(), cte).asPromise();
}
return newResultPromise(response);
}
});
} catch (BadRequestException bre) {
// For any malformed request.
debug.warning("Bad request received for anonymousUpdate " + bre.getMessage());
return bre.asPromise();
} catch (ResourceException re) {
debug.warning("Error performing anonymousUpdate", re);
return re.asPromise();
} catch (CoreTokenException cte) {
// For any unexpected CTS error
debug.error("Error performing anonymousUpdate", cte);
return new InternalServerErrorException(cte).asPromise();
}
}
use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class SmsRealmProvider method handleQuery.
@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();
}
final String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
try {
final SessionID sessionID = new SessionID(getUserSsoToken(context).getTokenID().toString());
final String realmPath = coreWrapper.convertOrgNameToRealmName(sessionCache.getSession(sessionID).getClientDomain());
final OrganizationConfigManager ocm = new OrganizationConfigManager(getUserSsoToken(context), realmPath);
//Return realm query is being performed on
handler.handleResource(getResource(getJsonValue(realmPath)));
for (final Object subRealmRelativePath : ocm.getSubOrganizationNames("*", true)) {
String realmName;
if (realmPath.endsWith("/")) {
realmName = realmPath + subRealmRelativePath;
} else {
realmName = realmPath + "/" + subRealmRelativePath;
}
handler.handleResource(getResource(getJsonValue(realmName)));
}
debug.message("RealmResource :: QUERY : performed by {}", principalName);
return newResultPromise(newQueryResponse());
} catch (SSOException ex) {
debug.error("RealmResource :: QUERY by " + principalName + " failed : " + ex);
return new ForbiddenException().asPromise();
} catch (SessionException ex) {
debug.error("RealmResource :: QUERY by " + principalName + " failed : " + ex);
return new InternalServerErrorException().asPromise();
} catch (SMSException ex) {
debug.error("RealmResource :: QUERY by " + principalName + " failed :" + ex);
switch(ex.getExceptionCode()) {
case STATUS_NO_PERMISSION:
// This exception will be thrown if permission to read realms from SMS has not been delegated
return new ForbiddenException().asPromise();
default:
return new InternalServerErrorException().asPromise();
}
}
}
use of org.forgerock.json.resource.InternalServerErrorException in project OpenAM by OpenRock.
the class SmsSingletonProvider method handleUpdate.
/**
* Updates config for the singleton instance referenced, and returns the JsonValue representation.
* {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> handleUpdate(Context serverContext, UpdateRequest updateRequest) {
String resourceId = resourceId();
if (dynamicSchema != null) {
try {
updateDynamicAttributes(serverContext, updateRequest.getContent());
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on create", e);
return new InternalServerErrorException("Unable to update SMS config: " + e.getMessage()).asPromise();
} catch (SSOException e) {
debug.warning("::SmsCollectionProvider:: SSOException on create", e);
return new InternalServerErrorException("Unable to update SMS config: " + e.getMessage()).asPromise();
} catch (IdRepoException e) {
debug.warning("::SmsCollectionProvider:: IdRepoException on create", e);
return new InternalServerErrorException("Unable to update SMS config: " + e.getMessage()).asPromise();
} catch (ResourceException e) {
return e.asPromise();
}
}
try {
ServiceConfig config = getServiceConfigNode(serverContext, resourceId);
String realm = realmFor(serverContext);
saveConfigAttributes(config, convertFromJson(updateRequest.getContent(), realm));
JsonValue result = withExtraAttributes(realm, convertToJson(realm, config));
return newResultPromise(newResourceResponse(resourceId, String.valueOf(result.hashCode()), result));
} catch (SMSException e) {
debug.warning("::SmsCollectionProvider:: SMSException on create", e);
return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
} catch (SSOException e) {
debug.warning("::SmsCollectionProvider:: SSOException on create", e);
return new InternalServerErrorException("Unable to create SMS config: " + e.getMessage()).asPromise();
} catch (ResourceException e) {
return e.asPromise();
}
}
use of org.forgerock.json.resource.InternalServerErrorException 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.InternalServerErrorException in project OpenAM by OpenRock.
the class SitesResourceProvider method deleteInstance.
@Override
public Promise<ResourceResponse, ResourceException> deleteInstance(Context context, String id, DeleteRequest request) {
ResourceResponse site;
SSOToken token;
try {
token = getSsoToken(context);
site = getSite(token, id);
} catch (SMSException | SSOException | ConfigurationException e) {
debug.error("Could not read site {}", id, e);
return new InternalServerErrorException("Could not read site").asPromise();
} catch (NotFoundException e) {
return e.asPromise();
}
try {
if (!site.getRevision().equals(request.getRevision())) {
return new PreconditionFailedException("Revision did not match").asPromise();
} else if (!SiteConfiguration.listServers(token, id).isEmpty()) {
return new PreconditionFailedException("Site still has servers attached to it").asPromise();
} else if (!SiteConfiguration.deleteSite(token, id)) {
return new InternalServerErrorException("Could not delete site: " + id).asPromise();
} else {
return newResultPromise(site);
}
} catch (SSOException | SMSException | ConfigurationException e) {
debug.error("Could not delete site {}", id, e);
return new InternalServerErrorException("Could not delete site").asPromise();
}
}
Aggregations