Search in sources :

Example 26 with JsonPointer

use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.

the class SmsServerPropertiesResource method getDirectorySchema.

private JsonValue getDirectorySchema(Properties titleProperties, Debug logger) {
    try {
        JsonValue directoryConfigSchema = json(object());
        dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        dbFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        dBuilder = dbFactory.newDocumentBuilder();
        Document document = dBuilder.parse(getClass().getResourceAsStream(DIRECTORY_CONFIG_XML));
        XPath xPath = XPathFactory.newInstance().newXPath();
        final String sectionExpression = "//propertysheet/section/@defaultValue";
        String sectionRawValue = (String) xPath.compile(sectionExpression).evaluate(document, XPathConstants.STRING);
        String sectionTitle = titleProperties.getProperty(sectionRawValue);
        final String baseExpression = "//propertysheet/section/property/label/@defaultValue";
        NodeList attributes = (NodeList) xPath.compile(baseExpression).evaluate(document, XPathConstants.NODESET);
        final String path = "/_schema/properties/directoryConfiguration/" + sectionRawValue;
        directoryConfigSchema.putPermissive(new JsonPointer(path + "/title"), sectionTitle);
        for (int i = 0; i < attributes.getLength(); i++) {
            String attributeRawName = attributes.item(i).getNodeValue();
            String attributePath = path + "/" + attributeRawName;
            directoryConfigSchema.putPermissive(new JsonPointer(attributePath + "/title"), titleProperties.getProperty(attributeRawName));
            directoryConfigSchema.putPermissive(new JsonPointer(attributePath + "/propertyOrder"), i);
            directoryConfigSchema.putPermissive(new JsonPointer(attributePath + "/type"), "string");
        }
        final String serverPath = path + "/servers";
        directoryConfigSchema.putPermissive(new JsonPointer(serverPath + "/title"), titleProperties.get("amconfig.serverconfig.xml.server.table.header"));
        directoryConfigSchema.putPermissive(new JsonPointer(serverPath + "/type"), "array");
        directoryConfigSchema.putPermissive(new JsonPointer(serverPath + "/items/type"), "object");
        List<String> columnNames = new ArrayList<>();
        columnNames.add("name");
        columnNames.add("host");
        columnNames.add("port");
        columnNames.add("type");
        for (String columnName : columnNames) {
            final String attributePath = serverPath + "/items/properties/" + SERVER_TABLE_PROPERTY_PREFIX + columnName;
            directoryConfigSchema.putPermissive(new JsonPointer(attributePath + "/title"), titleProperties.getProperty(SERVER_TABLE_PROPERTY_PREFIX + columnName));
            directoryConfigSchema.putPermissive(new JsonPointer(attributePath + "/type"), "string");
            directoryConfigSchema.putPermissive(new JsonPointer(attributePath + "/propertyOrder"), columnNames.indexOf(columnName));
        }
        return directoryConfigSchema;
    } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException e) {
        logger.error("Error creating document builder", e);
    }
    return null;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) JsonValue(org.forgerock.json.JsonValue) ArrayList(java.util.ArrayList) JsonPointer(org.forgerock.json.JsonPointer) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 27 with JsonPointer

use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.

the class SmsServerPropertiesResource method readInstance.

@Override
public Promise<ResourceResponse, ResourceException> readInstance(Context serverContext, ReadRequest readRequest) {
    Map<String, String> uriVariables = getUriTemplateVariables(serverContext);
    final String tabName = getTabName(uriVariables);
    if (tabName == null) {
        return new BadRequestException("Tab name not specified.").asPromise();
    }
    final String serverName = getServerName(uriVariables);
    if (serverName == null) {
        return new BadRequestException("Server name not specified.").asPromise();
    }
    try {
        ServiceConfigManager scm = getServiceConfigManager(serverContext);
        ServiceConfig serverConfigs = getServerConfigs(scm);
        Properties defaultAttributes = getAttributes(serverConfigs.getSubConfig(SERVER_DEFAULT_NAME));
        final ServiceConfig serverConfig = serverConfigs.getSubConfig(serverName);
        if (serverConfig == null) {
            return new BadRequestException("Unknown Server " + serverName).asPromise();
        }
        Properties serverSpecificAttributes = getAttributes(serverConfig);
        Map<String, String> defaultSection = new HashMap<>();
        JsonValue result = json(object(field("default", defaultSection)));
        List<String> attributeNamesForTab;
        if (tabName.equalsIgnoreCase(DIRECTORY_CONFIGURATION_TAB_NAME)) {
            InputStream resourceStream = new StringInputStream(getServerConfigXml(serverConfig));
            Document serverXml = dBuilder.parse(resourceStream);
            XPath xPath = XPathFactory.newInstance().newXPath();
            final String baseExpression = "//iPlanetDataAccessLayer/ServerGroup[@name='sms']/";
            String minConnections = (String) xPath.compile(baseExpression + "@" + DSConfigMgr.MIN_CONN_POOL).evaluate(serverXml, XPathConstants.STRING);
            String maxConnections = (String) xPath.compile(baseExpression + "@" + DSConfigMgr.MAX_CONN_POOL).evaluate(serverXml, XPathConstants.STRING);
            String dirDN = (String) xPath.compile(baseExpression + "User/DirDN").evaluate(serverXml, XPathConstants.STRING);
            String directoryPassword = (String) xPath.compile(baseExpression + "User/DirPassword").evaluate(serverXml, XPathConstants.STRING);
            result.put("minConnections", minConnections);
            result.put("maxConnections", maxConnections);
            result.put("dirDN", dirDN);
            result.put("directoryPassword", directoryPassword);
            NodeList serverNames = (NodeList) xPath.compile(baseExpression + "Server/@name").evaluate(serverXml, XPathConstants.NODESET);
            for (int i = 0; i < serverNames.getLength(); i++) {
                final String directoryServerName = serverNames.item(i).getNodeValue();
                final String serverExpression = baseExpression + "Server[@name='" + directoryServerName + "']";
                String hostExpression = serverExpression + "/@host";
                String portExpression = serverExpression + "/@port";
                String typeExpression = serverExpression + "/@type";
                NodeList serverAttributes = (NodeList) xPath.compile(hostExpression + "|" + portExpression + "|" + typeExpression).evaluate(serverXml, XPathConstants.NODESET);
                for (int a = 0; a < serverAttributes.getLength(); a++) {
                    final Node serverAttribute = serverAttributes.item(a);
                    result.addPermissive(new JsonPointer("servers/" + directoryServerName + "/" + serverAttribute.getNodeName()), serverAttribute.getNodeValue());
                }
            }
        } else {
            if (tabName.equalsIgnoreCase(ADVANCED_TAB_NAME)) {
                attributeNamesForTab = getAdvancedTabAttributeNames(serverConfig);
            } else {
                attributeNamesForTab = getDefaultValueNames(tabName);
            }
            for (String attributeName : attributeNamesForTab) {
                final String defaultAttribute = (String) defaultAttributes.get(attributeName);
                if (defaultAttribute != null) {
                    defaultSection.put(attributeName, (String) defaultAttributes.get(attributeName));
                }
                final String serverSpecificAttribute = (String) serverSpecificAttributes.get(attributeName);
                if (serverSpecificAttribute != null) {
                    result.add(attributeName, serverSpecificAttribute);
                }
            }
        }
        return newResultPromise(newResourceResponse(serverName + "/properties/" + tabName, String.valueOf(result.hashCode()), result));
    } catch (SMSException | SSOException | ParserConfigurationException | SAXException | IOException | XPathExpressionException e) {
        logger.error("Error reading property sheet for tab " + tabName, e);
    }
    return new BadRequestException("Error reading properties file for " + tabName).asPromise();
}
Also used : XPath(javax.xml.xpath.XPath) HashMap(java.util.HashMap) SMSException(com.sun.identity.sm.SMSException) StringInputStream(com.sun.xml.bind.StringInputStream) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) JsonValue(org.forgerock.json.JsonValue) SSOException(com.iplanet.sso.SSOException) JsonPointer(org.forgerock.json.JsonPointer) IOException(java.io.IOException) Properties(java.util.Properties) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) StringInputStream(com.sun.xml.bind.StringInputStream) ServiceConfig(com.sun.identity.sm.ServiceConfig) BadRequestException(org.forgerock.json.resource.BadRequestException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ServiceConfigManager(com.sun.identity.sm.ServiceConfigManager)

Example 28 with JsonPointer

use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.

the class SmsResourceProvider method addType.

private void addType(JsonValue result, String pointer, AttributeSchema attribute, ResourceBundle schemaI18n, ResourceBundle consoleI18n, Context context) {
    String type = null;
    AttributeSchema.Type attributeType = attribute.getType();
    AttributeSchema.Syntax syntax = attribute.getSyntax();
    if (attributeType == AttributeSchema.Type.LIST && (attribute.getUIType() == AttributeSchema.UIType.GLOBALMAPLIST || attribute.getUIType() == AttributeSchema.UIType.MAPLIST)) {
        type = OBJECT_TYPE;
        JsonValue fieldType = json(object());
        if (attribute.hasChoiceValues()) {
            addEnumChoices(fieldType, attribute, schemaI18n, consoleI18n, context);
        } else {
            fieldType.add(TYPE, STRING_TYPE);
        }
        result.addPermissive(new JsonPointer(pointer + "/" + PATTERN_PROPERTIES), object(field(".*", fieldType.getObject())));
    } else if (attributeType == AttributeSchema.Type.LIST) {
        type = ARRAY_TYPE;
        result.addPermissive(new JsonPointer(pointer + "/" + ITEMS), object(field(TYPE, getTypeFromSyntax(attribute.getSyntax()))));
        if (attribute.hasChoiceValues()) {
            addEnumChoices(result.get(new JsonPointer(pointer + "/" + ITEMS)), attribute, schemaI18n, consoleI18n, context);
        }
    } else if (attributeType.equals(AttributeSchema.Type.MULTIPLE_CHOICE)) {
        type = ARRAY_TYPE;
        result.addPermissive(new JsonPointer(pointer + "/" + ITEMS), object(field(TYPE, getTypeFromSyntax(attribute.getSyntax()))));
        addEnumChoices(result.get(new JsonPointer(pointer + "/" + ITEMS)), attribute, schemaI18n, consoleI18n, context);
    } else if (attributeType.equals(AttributeSchema.Type.SINGLE_CHOICE)) {
        addEnumChoices(result.get(new JsonPointer(pointer)), attribute, schemaI18n, consoleI18n, context);
    } else {
        type = getTypeFromSyntax(syntax);
    }
    if (type != null) {
        result.addPermissive(new JsonPointer(pointer + "/" + TYPE), type);
    }
    if (AttributeSchema.Syntax.PASSWORD.equals(syntax)) {
        result.addPermissive(new JsonPointer(pointer + "/" + FORMAT), PASSWORD_TYPE);
    }
}
Also used : AttributeSchema(com.sun.identity.sm.AttributeSchema) JsonValue(org.forgerock.json.JsonValue) Syntax(com.sun.identity.sm.AttributeSchema.Syntax) JsonPointer(org.forgerock.json.JsonPointer)

Example 29 with JsonPointer

use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.

the class SmsResourceProvider method addEnumChoices.

private void addEnumChoices(JsonValue jsonValue, AttributeSchema attribute, ResourceBundle schemaI18n, ResourceBundle consoleI18n, Context context) {
    List<String> values = new ArrayList<String>();
    List<String> descriptions = new ArrayList<String>();
    Map environment = type == SchemaType.GLOBAL ? Collections.emptyMap() : Collections.singletonMap(Constants.ORGANIZATION_NAME, realmFor(context));
    Map<String, String> valuesMap = attribute.getChoiceValuesMap(environment);
    for (Map.Entry<String, String> value : valuesMap.entrySet()) {
        values.add(value.getKey());
        if (AttributeSchema.UIType.SCRIPTSELECT.equals(attribute.getUIType())) {
            if (value.getValue() != null && consoleI18n.containsKey(value.getValue())) {
                descriptions.add(consoleI18n.getString(value.getValue()));
            } else {
                descriptions.add(value.getValue());
            }
        } else if (value.getValue() != null && schemaI18n.containsKey(value.getValue())) {
            descriptions.add(schemaI18n.getString(value.getValue()));
        } else {
            descriptions.add(value.getKey());
        }
    }
    jsonValue.add(ENUM, values);
    jsonValue.putPermissive(new JsonPointer("options/enum_titles"), descriptions);
}
Also used : ArrayList(java.util.ArrayList) JsonPointer(org.forgerock.json.JsonPointer) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with JsonPointer

use of org.forgerock.json.JsonPointer in project OpenAM by OpenRock.

the class IdentityResourceV3 method queryCollection.

/*******************************************************************************************************************
     * {@inheritDoc}
     */
public Promise<QueryResponse, ResourceException> queryCollection(final Context context, final QueryRequest request, final QueryResourceHandler handler) {
    RealmContext realmContext = context.asContext(RealmContext.class);
    final String realm = realmContext.getResolvedRealm();
    try {
        SSOToken admin = getSSOToken(RestUtils.getToken().getTokenID().toString());
        IdentityServicesImpl identityServices = getIdentityServices();
        List<IdentityDetails> userDetails = null;
        // If the user specified _queryFilter, then (convert and) use that, otherwise look for _queryID
        // and if that isn't there either, pretend the user gave a _queryID of "*"
        //
        QueryFilter<JsonPointer> queryFilter = request.getQueryFilter();
        if (queryFilter != null) {
            CrestQuery crestQuery = new CrestQuery(queryFilter);
            userDetails = identityServices.searchIdentityDetails(crestQuery, getIdentityServicesAttributes(realm, objectType), admin);
        } else {
            String queryId = request.getQueryId();
            if (queryId == null || queryId.isEmpty()) {
                queryId = "*";
            }
            CrestQuery crestQuery = new CrestQuery(queryId);
            userDetails = identityServices.searchIdentityDetails(crestQuery, getIdentityServicesAttributes(realm, objectType), admin);
        }
        String principalName = PrincipalRestUtils.getPrincipalNameFromServerContext(context);
        logger.message("IdentityResourceV3.queryCollection :: QUERY performed on realm " + realm + " by " + principalName);
        for (IdentityDetails userDetail : userDetails) {
            ResourceResponse resource;
            resource = newResourceResponse(userDetail.getName(), "0", identityResourceV2.addRoleInformation(context, userDetail.getName(), identityDetailsToJsonValue(userDetail)));
            handler.handleResource(resource);
        }
    } catch (ResourceException resourceException) {
        logger.warning("IdentityResourceV3.queryCollection caught ResourceException", resourceException);
        return resourceException.asPromise();
    } catch (Exception exception) {
        logger.error("IdentityResourceV3.queryCollection caught exception", exception);
        return new InternalServerErrorException(exception.getMessage(), exception).asPromise();
    }
    return newResultPromise(newQueryResponse());
}
Also used : CrestQuery(org.forgerock.openam.utils.CrestQuery) SSOToken(com.iplanet.sso.SSOToken) RealmContext(org.forgerock.openam.rest.RealmContext) JsonPointer(org.forgerock.json.JsonPointer) PermanentException(org.forgerock.json.resource.PermanentException) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ForbiddenException(org.forgerock.json.resource.ForbiddenException) NotFoundException(org.forgerock.json.resource.NotFoundException) BadRequestException(org.forgerock.json.resource.BadRequestException) ResourceException(org.forgerock.json.resource.ResourceException) IdentityServicesImpl(com.sun.identity.idsvcs.opensso.IdentityServicesImpl) Responses.newResourceResponse(org.forgerock.json.resource.Responses.newResourceResponse) ResourceResponse(org.forgerock.json.resource.ResourceResponse) IdentityDetails(com.sun.identity.idsvcs.IdentityDetails) InternalServerErrorException(org.forgerock.json.resource.InternalServerErrorException) ResourceException(org.forgerock.json.resource.ResourceException)

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