use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class ApplicationsResourceTest method shouldRejectPresenceQueries.
@Test(expectedExceptions = EntitlementException.class, expectedExceptionsMessageRegExp = ".*not supported.*")
public void shouldRejectPresenceQueries() throws Exception {
// Given
QueryRequest request = mockQueryRequest(present(new JsonPointer(STRING_ATTRIBUTE)));
Subject subject = new Subject();
// When
applicationsResource.query(request, subject, "/abc");
// Then - exception
}
use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class SmsRealmProvider method handleCreate.
@Override
public Promise<ResourceResponse, ResourceException> handleCreate(Context serverContext, CreateRequest createRequest) {
final JsonValue jsonContent = createRequest.getContent();
final String realmName = jsonContent.get(REALM_NAME_ATTRIBUTE_NAME).asString();
try {
if (StringUtils.isBlank(realmName)) {
throw new BadRequestException("No realm name provided");
}
if (containsBlacklistedCharacters(realmName)) {
throw new BadRequestException("Realm names cannot contain: " + BLACKLIST_CHARACTERS.toString());
}
RealmContext realmContext = serverContext.asContext(RealmContext.class);
StringBuilder realmPath = new StringBuilder(realmContext.getResolvedRealm());
String location = jsonContent.get(new JsonPointer(PATH_ATTRIBUTE_NAME)).asString();
if (realmPath.length() > 1) {
if (realmPath.charAt(realmPath.length() - 1) != '/' && !location.startsWith("/")) {
realmPath.append('/');
}
realmPath.append(location);
} else {
realmPath = new StringBuilder(location);
}
if (realmPath.charAt(realmPath.length() - 1) != '/') {
realmPath.append('/');
}
realmPath.append(realmName);
String path = realmPath.toString();
String parentRealm = RealmUtils.getParentRealm(path);
String childRealm = RealmUtils.getChildRealm(path);
OrganizationConfigManager realmManager = new OrganizationConfigManager(getUserSsoToken(serverContext), parentRealm);
Map<String, Map<String, Set>> serviceAttributes = new HashMap<>();
serviceAttributes.put(IdConstants.REPO_SERVICE, getAttributeMap(jsonContent));
realmManager.createSubOrganization(childRealm, serviceAttributes);
if (debug.messageEnabled()) {
debug.message("RealmResource.createInstance :: CREATE of realm {} in realm {} performed by {}", childRealm, parentRealm, PrincipalRestUtils.getPrincipalNameFromServerContext(serverContext));
}
JsonValue jsonValue = getJsonValue(path, parentRealm);
return newResultPromise(getResource(jsonValue));
} catch (SMSException e) {
return configureErrorMessage(e).asPromise();
} catch (SSOException sso) {
debug.error("RealmResource.createInstance() : Cannot CREATE " + realmName, sso);
return new PermanentException(401, "Access Denied", null).asPromise();
} catch (BadRequestException fe) {
debug.error("RealmResource.createInstance() : Cannot CREATE " + realmName, fe);
return fe.asPromise();
}
}
use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class SmsResourceProvider method addAttributeSchema.
protected void addAttributeSchema(JsonValue result, String path, ServiceSchema schema, List<String> sections, Map<String, String> attributeSectionMap, ResourceBundle consoleI18n, String serviceType, Context context) {
ResourceBundle schemaI18n = ResourceBundle.getBundle(schema.getI18NFileName(), getLocale(context));
NumberFormat sectionFormat = new DecimalFormat("00");
for (AttributeSchema attribute : (Set<AttributeSchema>) schema.getAttributeSchemas()) {
String i18NKey = attribute.getI18NKey();
if (i18NKey != null && i18NKey.length() > 0) {
String attributePath = attribute.getResourceName();
if (!sections.isEmpty()) {
String section = attributeSectionMap.get(attribute.getName());
String sectionLabel = "section.label." + serviceName + "." + serviceType + "." + section;
attributePath = section + "/" + PROPERTIES + "/" + attributePath;
result.putPermissive(new JsonPointer(path + section + "/" + TYPE), OBJECT_TYPE);
result.putPermissive(new JsonPointer(path + section + "/" + TITLE), getConsoleString(consoleI18n, sectionLabel));
result.putPermissive(new JsonPointer(path + section + "/" + PROPERTY_ORDER), "z" + sectionFormat.format(sections.indexOf(section)));
}
result.addPermissive(new JsonPointer(path + attributePath + "/" + TITLE), schemaI18n.getString(i18NKey));
result.addPermissive(new JsonPointer(path + attributePath + "/" + DESCRIPTION), getSchemaDescription(schemaI18n, i18NKey));
result.addPermissive(new JsonPointer(path + attributePath + "/" + PROPERTY_ORDER), i18NKey);
result.addPermissive(new JsonPointer(path + attributePath + "/" + REQUIRED), !attribute.isOptional());
addType(result, path + attributePath, attribute, schemaI18n, consoleI18n, context);
}
}
}
use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class SmsServerPropertiesResource method actionInstance.
@Override
public Promise<ActionResponse, ResourceException> actionInstance(Context serverContext, ActionRequest actionRequest) {
if (actionRequest.getAction().equals("schema")) {
Map<String, String> uriVariables = getUriTemplateVariables(serverContext);
final String serverName = uriVariables.get("serverName").toLowerCase();
if (serverName == null) {
return new BadRequestException("Server name not specified.").asPromise();
}
try {
ServiceConfigManager scm = getServiceConfigManager(serverContext);
ServiceConfig serverConfigs = getServerConfigs(scm);
if (!serverConfigs.getSubConfigNames().contains(serverName)) {
return new BadRequestException("Unknown server: " + serverName).asPromise();
}
} catch (SSOException | SMSException e) {
logger.error("Error getting server config", e);
}
final String tabName = getTabName(uriVariables);
if (tabName == null) {
return new BadRequestException("Tab name not specified.").asPromise();
}
JsonValue schema;
final JsonPointer tabPointer = new JsonPointer("_schema/properties/" + tabName);
if (DIRECTORY_CONFIGURATION_TAB_NAME.equalsIgnoreCase(tabName)) {
schema = directoryConfigSchema;
} else if (serverName.equals(SERVER_DEFAULT_NAME)) {
schema = defaultSchema.get(tabPointer);
} else {
schema = nonDefaultSchema.get(tabPointer);
}
if (schema == null) {
return new BadRequestException("Unknown tab: " + tabName).asPromise();
}
return newResultPromise(newActionResponse(schema));
} else {
return new NotSupportedException("Action not supported: " + actionRequest.getAction()).asPromise();
}
}
use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.
the class QueryResponsePresentation method sortItems.
private static List<ResourceResponse> sortItems(final QueryRequest request, List<ResourceResponse> items) {
Comparator<ResourceResponse> comparator = new Comparator<ResourceResponse>() {
final List<SortKey> sortKeys = request.getSortKeys();
@Override
public int compare(ResourceResponse o1, ResourceResponse o2) {
int compare = 0;
for (SortKey key : sortKeys) {
Comparable<Object> v1 = getField(o1, key.getField());
Comparable<Object> v2 = getField(o2, key.getField());
compare = compare(v1, v2, key.isAscendingOrder());
if (compare != 0) {
break;
}
}
return compare;
}
private int compare(Comparable<Object> first, Comparable<Object> second, boolean ascending) {
int result;
if (first == null && second == null) {
result = 0;
} else if (first == null) {
result = -1;
} else if (second == null) {
result = 1;
} else {
result = first.compareTo(second);
}
if (!ascending) {
result = -result;
}
return result;
}
/**
* Gets the comparable field value from a resource.
*
* @param resource The resource.
* @param field The field.
* @return The comparable field value or {@code null} if the field is empty or the
* field value is not comparable.
*/
private Comparable<Object> getField(ResourceResponse resource, JsonPointer field) {
JsonValue value = resource.getContent();
if (value.get(field).isNull()) {
return null;
}
Object object = value.get(field).getObject();
if (isComparable(object)) {
return asComparable(object);
} else {
return null;
}
}
private boolean isComparable(Object o) {
return o instanceof Comparable;
}
@SuppressWarnings("unchecked")
private Comparable<Object> asComparable(Object o) {
return (Comparable<Object>) o;
}
};
try {
Collections.sort(items, comparator);
} catch (NullPointerException e) {
// No-op, sorting failed.
}
return Collections.unmodifiableList(items);
}
Aggregations