Search in sources :

Example 51 with JsonPointer

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
}
Also used : QueryRequest(org.forgerock.json.resource.QueryRequest) JsonPointer(org.forgerock.json.JsonPointer) Subject(javax.security.auth.Subject) Test(org.testng.annotations.Test)

Example 52 with JsonPointer

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();
    }
}
Also used : RealmContext(org.forgerock.openam.rest.RealmContext) HashMap(java.util.HashMap) SMSException(com.sun.identity.sm.SMSException) JsonValue(org.forgerock.json.JsonValue) SSOException(com.iplanet.sso.SSOException) JsonPointer(org.forgerock.json.JsonPointer) OrganizationConfigManager(com.sun.identity.sm.OrganizationConfigManager) PermanentException(org.forgerock.json.resource.PermanentException) BadRequestException(org.forgerock.json.resource.BadRequestException) Map(java.util.Map) HashMap(java.util.HashMap)

Example 53 with JsonPointer

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);
        }
    }
}
Also used : Set(java.util.Set) DecimalFormat(java.text.DecimalFormat) AttributeSchema(com.sun.identity.sm.AttributeSchema) ResourceBundle(java.util.ResourceBundle) JsonPointer(org.forgerock.json.JsonPointer) NumberFormat(java.text.NumberFormat)

Example 54 with JsonPointer

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();
    }
}
Also used : ServiceConfig(com.sun.identity.sm.ServiceConfig) SMSException(com.sun.identity.sm.SMSException) JsonValue(org.forgerock.json.JsonValue) BadRequestException(org.forgerock.json.resource.BadRequestException) SSOException(com.iplanet.sso.SSOException) JsonPointer(org.forgerock.json.JsonPointer) NotSupportedException(org.forgerock.json.resource.NotSupportedException) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager)

Example 55 with JsonPointer

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);
}
Also used : ResourceResponse(org.forgerock.json.resource.ResourceResponse) JsonValue(org.forgerock.json.JsonValue) List(java.util.List) SortKey(org.forgerock.json.resource.SortKey) JsonPointer(org.forgerock.json.JsonPointer) Comparator(java.util.Comparator)

Aggregations

JsonPointer (org.forgerock.json.JsonPointer)64 Test (org.testng.annotations.Test)40 QueryRequest (org.forgerock.json.resource.QueryRequest)34 JsonValue (org.forgerock.json.JsonValue)21 QueryResponse (org.forgerock.json.resource.QueryResponse)19 Context (org.forgerock.services.context.Context)18 RealmContext (org.forgerock.openam.rest.RealmContext)17 Collection (java.util.Collection)15 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)13 SubjectContext (org.forgerock.openam.rest.resource.SubjectContext)13 ClientContext (org.forgerock.services.context.ClientContext)13 Subject (javax.security.auth.Subject)10 ResourceException (org.forgerock.json.resource.ResourceException)10 ResourceResponse (org.forgerock.json.resource.ResourceResponse)10 ArrayList (java.util.ArrayList)9 BadRequestException (org.forgerock.json.resource.BadRequestException)9 SearchFilter (com.sun.identity.entitlement.util.SearchFilter)8 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 List (java.util.List)7