Search in sources :

Example 21 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project OpenAM by OpenRock.

the class SmsServerPropertiesResource method getOptions.

private Map<String, Set<String>> getOptions(Document propertySheet, String tabName) {
    Map<String, Set<String>> radioOptions = new HashMap<>();
    try {
        XPath xPath = XPathFactory.newInstance().newXPath();
        List<String> attributeNamesForTab = getDefaultValueNames(tabName);
        for (String defaultValueName : attributeNamesForTab) {
            String convertedName = getConvertedName(defaultValueName);
            String expression = "//propertysheet/section/property/cc[@name='" + convertedName + "']/option/@value";
            NodeList optionsList = (NodeList) xPath.compile(expression).evaluate(propertySheet, XPathConstants.NODESET);
            Set<String> options = new HashSet<>();
            for (int i = 0; i < optionsList.getLength(); i++) {
                options.add(optionsList.item(i).getNodeValue());
            }
            if (!options.isEmpty()) {
                radioOptions.put(defaultValueName, options);
            }
        }
    } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException e) {
        logger.error("Error reading property sheet", e);
    }
    return radioOptions;
}
Also used : XPath(javax.xml.xpath.XPath) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) HashSet(java.util.HashSet)

Example 22 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException 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 23 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project OpenAM by OpenRock.

the class SmsServerPropertiesResource method getOptionalAttributes.

private Set<String> getOptionalAttributes(Document propertySheet, String tabName) {
    XPath xPath = XPathFactory.newInstance().newXPath();
    Set<String> optionalValues = new HashSet<>();
    try {
        String expression = "//propertysheet/section/property[@required='false']/cc/@name";
        NodeList optionalValuesList = (NodeList) xPath.compile(expression).evaluate(propertySheet, XPathConstants.NODESET);
        for (int i = 0; i < optionalValuesList.getLength(); i++) {
            optionalValues.add(optionalValuesList.item(i).getNodeValue());
        }
    } catch (XPathExpressionException e) {
        logger.error("Error reading property sheet for tab " + tabName, e);
    }
    return optionalValues;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) HashSet(java.util.HashSet)

Example 24 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project OpenAM by OpenRock.

the class SchemaResourceNamesStep method initialize.

@Override
public void initialize() throws UpgradeException {
    if (VersionUtils.isCurrentVersionLessThan(AM_13, true)) {
        Map<String, Document> serviceXmlContent = UpgradeServiceUtils.getServiceDefinitions(getAdminToken());
        serviceModifications = new HashMap<>();
        for (Map.Entry<String, Document> service : serviceXmlContent.entrySet()) {
            try {
                DEBUG.message("Finding resource names in {}", service.getKey());
                ServiceModifier modifier = new ServiceModifier();
                NodeList nodes = (NodeList) xpath.evaluate("//*[@" + RESOURCE_NAME + "]", service.getValue().getDocumentElement(), XPathConstants.NODESET);
                for (int i = 0; i < nodes.getLength(); i++) {
                    Element element = (Element) nodes.item(i);
                    ElementModifier target = buildPath(element, modifier);
                    target.resourceNameModifier = new ResourceNameModifier(element.getAttribute(RESOURCE_NAME));
                }
                if (!modifier.modifiers.isEmpty()) {
                    serviceModifications.put(service.getKey(), modifier);
                }
            } catch (XPathExpressionException e) {
                throw new UpgradeException(e);
            }
        }
    }
}
Also used : XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) UpgradeException(org.forgerock.openam.upgrade.UpgradeException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 25 with XPathExpressionException

use of javax.xml.xpath.XPathExpressionException in project Tundra by Permafrost.

the class xpath method exists.

// ---( server methods )---
public static final void exists(IData pipeline) throws ServiceException {
    // --- <<IS-START(exists)>> ---
    // @subtype unknown
    // @sigtype java 3.5
    // [i] object:0:optional $content
    // [i] field:0:optional $encoding
    // [i] field:0:required $expression
    // [i] record:0:optional $namespace
    // [i] - field:0:optional default
    // [o] field:0:required $exists?
    IDataCursor cursor = pipeline.getCursor();
    try {
        Object content = IDataHelper.get(cursor, "$content");
        Charset charset = IDataHelper.get(cursor, "$encoding", Charset.class);
        String expression = IDataHelper.get(cursor, "$expression", String.class);
        NamespaceContext namespace = IDataHelper.get(cursor, "$namespace", IDataNamespaceContext.class);
        XPathExpression compiledExpression = XPathHelper.compile(expression, namespace);
        Node node = null;
        if (content instanceof Node) {
            node = (Node) content;
        } else if (content instanceof InputSource) {
            node = DocumentHelper.parse((InputSource) content, namespace);
        } else if (content != null) {
            node = DocumentHelper.parse(InputStreamHelper.normalize(content, charset), charset, true, namespace);
        }
        IDataHelper.put(cursor, "$exists?", XPathHelper.exists(node, compiledExpression), String.class);
    } catch (XPathExpressionException ex) {
        ExceptionHelper.raise(ex);
    } finally {
        cursor.destroy();
    }
// --- <<IS-END>> ---
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) IDataNamespaceContext(permafrost.tundra.xml.namespace.IDataNamespaceContext) NamespaceContext(javax.xml.namespace.NamespaceContext) XPathExpressionException(javax.xml.xpath.XPathExpressionException) Node(org.w3c.dom.Node) Charset(java.nio.charset.Charset)

Aggregations

XPathExpressionException (javax.xml.xpath.XPathExpressionException)139 NodeList (org.w3c.dom.NodeList)65 XPath (javax.xml.xpath.XPath)64 Document (org.w3c.dom.Document)46 Node (org.w3c.dom.Node)46 IOException (java.io.IOException)42 XPathExpression (javax.xml.xpath.XPathExpression)38 SAXException (org.xml.sax.SAXException)27 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)25 XPathFactory (javax.xml.xpath.XPathFactory)23 ArrayList (java.util.ArrayList)22 HashMap (java.util.HashMap)18 Test (org.junit.Test)17 InputSource (org.xml.sax.InputSource)17 Element (org.w3c.dom.Element)16 Response (com.jayway.restassured.response.Response)12 ValidatableResponse (com.jayway.restassured.response.ValidatableResponse)12 DocumentBuilder (javax.xml.parsers.DocumentBuilder)12 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)12 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)11