use of org.forgerock.json.resource.BadRequestException in project OpenAM by OpenRock.
the class IdentityResourceV1 method createInstance.
/**
* {@inheritDoc}
*/
@Override
public Promise<ResourceResponse, ResourceException> createInstance(final Context context, final CreateRequest request) {
RealmContext realmContext = context.asContext(RealmContext.class);
final String realm = realmContext.getResolvedRealm();
try {
// anyone can create an account add
SSOToken admin = getSSOToken(getCookieFromServerContext(context));
final JsonValue jVal = request.getContent();
String resourceId = request.getNewResourceId();
UserAttributeInfo userAttributeInfo = configHandler.getConfig(realm, UserAttributeInfoBuilder.class);
enforceWhiteList(context, request.getContent(), objectType, userAttributeInfo.getValidCreationAttributes());
IdentityDetails identity = jsonValueToIdentityDetails(objectType, jVal, realm);
// check to see if request has included resource ID
if (resourceId != null) {
if (identity.getName() != null) {
if (!resourceId.equalsIgnoreCase(identity.getName())) {
ResourceException be = new BadRequestException("id in path does not match id in request body");
debug.error("IdentityResource.createInstance() :: Cannot CREATE ", be);
return be.asPromise();
}
}
identity.setName(resourceId);
} else {
resourceId = identity.getName();
}
final String id = resourceId;
return attemptResourceCreation(realm, admin, identity, resourceId).thenAsync(new AsyncFunction<IdentityDetails, ResourceResponse, ResourceException>() {
@Override
public Promise<ResourceResponse, ResourceException> apply(IdentityDetails dtls) {
if (dtls != null) {
String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
debug.message("IdentityResource.createInstance :: CREATE of resourceId={} in realm={} performed by " + "principalName={}", id, realm, principalName);
return newResultPromise(newResourceResponse(id, "0", identityDetailsToJsonValue(dtls)));
} else {
debug.error("IdentityResource.createInstance :: Identity not found ");
return new NotFoundException("Identity not found").asPromise();
}
}
});
} catch (SSOException e) {
debug.error("IdentityResource.createInstance() :: failed.", e);
return new NotFoundException(e.getMessage(), e).asPromise();
} catch (BadRequestException bre) {
return bre.asPromise();
}
}
use of org.forgerock.json.resource.BadRequestException in project OpenAM by OpenRock.
the class IdentityResourceV1 method anonymousCreate.
private Promise<ActionResponse, ResourceException> anonymousCreate(final Context context, final ActionRequest request, final String realm, RestSecurity restSecurity) {
final JsonValue jVal = request.getContent();
String confirmationId;
String email;
try {
if (!restSecurity.isSelfRegistration()) {
throw new BadRequestException("Self-registration disabled");
}
final String tokenID = jVal.get(TOKEN_ID).asString();
jVal.remove(TOKEN_ID);
confirmationId = jVal.get(CONFIRMATION_ID).asString();
jVal.remove(CONFIRMATION_ID);
email = jVal.get(EMAIL).asString();
if (email == null || email.isEmpty()) {
throw new BadRequestException("Email not provided");
}
// Convert to IDRepo Attribute schema
jVal.put("mail", email);
if (confirmationId == null || confirmationId.isEmpty()) {
throw new BadRequestException("confirmationId not provided");
}
if (tokenID == null || tokenID.isEmpty()) {
throw new BadRequestException("tokenId not provided");
}
validateToken(tokenID, realm, email, confirmationId);
// create an Identity
SSOToken admin = RestUtils.getToken();
return createInstance(admin, jVal, realm).thenAsync(new AsyncFunction<ActionResponse, ActionResponse, ResourceException>() {
@Override
public Promise<ActionResponse, ResourceException> apply(ActionResponse response) {
// Only remove the token if the create 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 completed registration 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("IdentityResource.anonymousCreate: Deleting token {} after a" + " successful read failed.", tokenID, e);
}
} catch (CoreTokenException cte) {
// For any unexpected CTS error
debug.error("IdentityResource.anonymousCreate(): CTS Error", cte);
return new InternalServerErrorException(cte.getMessage(), cte).asPromise();
}
return newResultPromise(response);
}
});
} catch (BadRequestException bre) {
debug.warning("IdentityResource.anonymousCreate() :: Invalid Parameter", bre);
return bre.asPromise();
} catch (ResourceException re) {
debug.warning("IdentityResource.anonymousCreate() :: Resource error", re);
return re.asPromise();
} catch (CoreTokenException cte) {
// For any unexpected CTS error
debug.error("IdentityResource.anonymousCreate() :: CTS error", cte);
return new InternalServerErrorException(cte).asPromise();
} catch (ServiceNotFoundException snfe) {
// Failure from RestSecurity
debug.error("IdentityResource.anonymousCreate() :: Internal error", snfe);
return new InternalServerErrorException(snfe).asPromise();
}
}
use of org.forgerock.json.resource.BadRequestException in project OpenAM by OpenRock.
the class SitesResourceProvider method createInstance.
@Override
public Promise<ResourceResponse, ResourceException> createInstance(Context context, CreateRequest request) {
JsonValue content = request.getContent();
String id = request.getNewResourceId();
try {
id = validWriteOperation(content, id);
} catch (BadRequestException e) {
return e.asPromise();
}
String url = content.get(PRIMARY_URL).asString();
try {
SSOToken token = getSsoToken(context);
if (SiteConfiguration.isSiteExist(token, id)) {
return new ConflictException("Site with id already exists: " + id).asPromise();
}
SiteConfiguration.createSite(token, id, url, content.get(SECONDARY_URLS).asSet());
debug.message("Site created: {}", id);
return newResultPromise(getSite(token, id));
} catch (SMSException | SSOException | ConfigurationException e) {
debug.error("Could not create site", e);
return new InternalServerErrorException("Could not create site").asPromise();
} catch (NotFoundException e) {
return new InternalServerErrorException("Could not read site just created").asPromise();
}
}
use of org.forgerock.json.resource.BadRequestException in project OpenAM by OpenRock.
the class SitesResourceProvider method queryCollection.
@Override
public Promise<QueryResponse, ResourceException> queryCollection(Context context, QueryRequest request, QueryResourceHandler handler) {
if (!"true".equals(request.getQueryFilter().toString())) {
return new BadRequestException("Query only supports 'true' filter").asPromise();
}
try {
SSOToken token = getSsoToken(context);
Set<String> siteNames = SiteConfiguration.getSites(token);
for (String siteName : siteNames) {
handler.handleResource(getSite(token, siteName));
}
return newResultPromise(newQueryResponse());
} catch (SSOException | SMSException | ConfigurationException e) {
debug.error("Could not read sites", e);
return new InternalServerErrorException("Could not read sites").asPromise();
} catch (NotFoundException e) {
debug.error("Could not read site", e);
return new InternalServerErrorException("Could not read site we've just got name for").asPromise();
}
}
use of org.forgerock.json.resource.BadRequestException in project OpenAM by OpenRock.
the class CommonTasksResource method readInstance.
@Override
public Promise<ResourceResponse, ResourceException> readInstance(Context context, String resourceId, ReadRequest request) {
//TODO pass in locale
Locale locale = Locale.ROOT;
JsonValue configuration = configurationManager.getCommonTasksConfiguration(getResourceBundle(locale));
if (!configuration.isDefined(resourceId)) {
return new BadRequestException("Invalid common task").asPromise();
}
JsonValue resource = configuration.get(resourceId);
return newResultPromise(newResourceResponse(resourceId, String.valueOf(resource.getObject().hashCode()), resource));
}
Aggregations