Example usage for org.w3c.dom Element getAttributeNode

List of usage examples for org.w3c.dom Element getAttributeNode

Introduction

In this page you can find the example usage for org.w3c.dom Element getAttributeNode.

Prototype

public Attr getAttributeNode(String name);

Source Link

Document

Retrieves an attribute node by name.

Usage

From source file:org.owasp.webscarab.plugin.saml.SamlModel.java

private boolean hasDestinationIndicationSaml2AuthnRequest(Element authnRequestElement) {
    if (null != authnRequestElement.getAttributeNode("Destination")) {
        return true;
    }/*from   w w w .jav a2 s.  com*/
    return false;
}

From source file:org.owasp.webscarab.plugin.saml.SamlModel.java

public boolean hasValidityIntervalIndication(ConversationID id) {
    Document document = getSAMLDocument(id);
    if (null == document) {
        return false;
    }//  w w w  . ja  va  2s .c  om

    NodeList saml1AssertionNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion",
            "Assertion");
    if (0 != saml1AssertionNodeList.getLength()) {
        Element assertionElement = (Element) saml1AssertionNodeList.item(0);
        NodeList conditionsNodeList = assertionElement
                .getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion", "Conditions");
        if (0 != conditionsNodeList.getLength()) {
            Element conditionsElement = (Element) conditionsNodeList.item(0);
            if (null != conditionsElement.getAttributeNode("NotBefore")
                    && null != conditionsElement.getAttributeNode("NotOnOrAfter")) {
                return true;
            }
        }
    }

    NodeList saml2AssertionNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion",
            "Assertion");
    if (0 != saml2AssertionNodeList.getLength()) {
        Element assertionElement = (Element) saml2AssertionNodeList.item(0);
        NodeList conditionsNodeList = assertionElement
                .getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "Conditions");
        if (0 != conditionsNodeList.getLength()) {
            Element conditionsElement = (Element) conditionsNodeList.item(0);
            if (null != conditionsElement.getAttributeNode("NotBefore")
                    && null != conditionsElement.getAttributeNode("NotOnOrAfter")) {
                return true;
            }
        }
    }

    NodeList saml2AuthnRequestNodeList = document.getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:protocol",
            "AuthnRequest");
    if (0 != saml2AuthnRequestNodeList.getLength()) {
        Element authnRequestElement = (Element) saml2AuthnRequestNodeList.item(0);
        NodeList conditionsNodeList = authnRequestElement
                .getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "Conditions");
        if (0 != conditionsNodeList.getLength()) {
            Element conditionsElement = (Element) conditionsNodeList.item(0);
            if (null != conditionsElement.getAttributeNode("NotBefore")
                    && null != conditionsElement.getAttributeNode("NotOnOrAfter")) {
                return true;
            }
        }
    }

    return false;
}

From source file:org.soybeanMilk.core.config.parser.ConfigurationParser.java

/**
 * ?//  www  .j a v  a  2 s .co m
 * @param element
 * @param attrName
 * @return
 */
protected String getAttributeValue(Element element, String attrName) {
    Attr attr = element.getAttributeNode(attrName);
    return attr == null ? null : attr.getValue();
}

From source file:test.unit.be.agiv.security.client.TestUtils.java

/**
 * XMLSEC 1.5 requires us to explicitly mark the Id's within a DOM document.
 * //from  w ww . j  av  a2 s . c om
 * @param document
 */
public static void markAllIdAttributesAsId(Document document) {
    Element nsElement = document.createElement("nsElement");
    nsElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:wsu", WSConstants.WS_SECURITY_UTILITY_NAMESPACE);

    NodeList elementsWithIdNodeList;
    try {
        elementsWithIdNodeList = XPathAPI.selectNodeList(document, "//*[@Id or @wsu:Id]", nsElement);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }

    for (int nodeIdx = 0; nodeIdx < elementsWithIdNodeList.getLength(); nodeIdx++) {
        Element elementWithId = (Element) elementsWithIdNodeList.item(nodeIdx);
        LOG.debug("element with Id: " + elementWithId.getLocalName());
        Attr attributeNode = elementWithId.getAttributeNode("Id");
        if (null == attributeNode) {
            attributeNode = elementWithId.getAttributeNodeNS(WSConstants.WS_SECURITY_UTILITY_NAMESPACE, "Id");
        }
        elementWithId.setIdAttributeNode(attributeNode, true);
    }
}

From source file:test.unit.be.fedict.eid.idp.protocol.saml2.SAML2Test.java

@Test
public void testAssertionSigning() throws Exception {

    // Setup/* w w  w  .ja v  a2  s .c  om*/
    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);

    KeyPair rootKeyPair = generateKeyPair();
    X509Certificate rootCertificate = generateSelfSignedCertificate(rootKeyPair, "CN=TestRoot", notBefore,
            notAfter);

    KeyPair endKeyPair = generateKeyPair();
    X509Certificate endCertificate = generateCertificate(endKeyPair.getPublic(), "CN=Test", notBefore, notAfter,
            rootCertificate, rootKeyPair.getPrivate());

    Certificate[] certChain = { endCertificate, rootCertificate };

    KeyStore.PrivateKeyEntry idpIdentity = new KeyStore.PrivateKeyEntry(endKeyPair.getPrivate(), certChain);

    // Operate: sign
    Assertion assertion = Saml2Util.getAssertion("test-issuer", "test-in-response-to", "test-audience",
            "test-recipient", 5, new DateTime(), SamlAuthenticationPolicy.AUTHENTICATION,
            UUID.randomUUID().toString(), new HashMap<String, be.fedict.eid.idp.common.Attribute>(), null,
            null);
    Assertion signedAssertion = (Assertion) Saml2Util.sign(assertion, idpIdentity);

    // Verify
    String result = Saml2Util.domToString(Saml2Util.marshall(signedAssertion), true);
    LOG.debug("DOM signed assertion: " + result);
    String result2 = Saml2Util.domToString(Saml2Util.marshall(assertion), true);
    LOG.debug("signed assertion: " + result2);
    assertEquals(result, result2);

    // Fix for recent Apache Xmlsec libraries.
    Element signedAssertionElement = (Element) signedAssertion.getDOM();
    String assertionId = assertion.getID();
    Element locatedElement = signedAssertionElement.getOwnerDocument().getElementById(assertionId);
    LOG.debug("element located by ID: " + (null != locatedElement));

    Attr attr = signedAssertionElement.getAttributeNode("ID");
    signedAssertionElement.setIdAttributeNode(attr, true);
    signedAssertion.setDOM(signedAssertionElement);

    // Operate: validate
    Saml2Util.validateSignature(signedAssertion.getSignature());
}

From source file:us.derfers.tribex.rapids.Loader.java

/**
 * Starts loading the GUI. Sets Swing look and feel, then loads the GUI using the GUI_Swing object.
 * @param escapedFile The content .rsm file to load UI elements from.
 * @param parent The (optional) parent Object, Eg, a JFrame or JPanel.
 * @param engine The JavaScript engine to pass to GUI_Swing
 *///from   w  w  w  .ja  v  a2 s  . c o m
public void loadAll(String escapedFile) {

    //Attempt to load .rsm file filePath
    try {

        //Parse filePath
        Document doc = Utilities.XMLStringToDocument(escapedFile);

        //Stabilize parsed document
        doc.normalize();

        //Get body element
        NodeList mainNodeList = doc.getElementsByTagName("rsm");

        //Make sure there is only ONE body element
        if (mainNodeList.getLength() == 1) {
            debugMsg("Parsing Main Element", 4);
            //Get rsm Element
            Element mainElement = (Element) mainNodeList.item(0);

            debugMsg("Setting Theme", 4);
            //If the mainElement has the attribute "theme"
            if (mainElement.getAttributeNode("theme") != null) {
                //Get the value of the attribute theme for the body element
                Attr swing_Theme = mainElement.getAttributeNode("theme");

                //See if the rsm file specifies a theme other than camo
                if (swing_Theme != null && !swing_Theme.getNodeValue().equalsIgnoreCase("camo")) {
                    try {
                        //Split the theme into the jarfile and the classname (JARFILE.jar : com.stuff.stuff.theme)
                        String[] splitTheme = swing_Theme.getNodeValue().split(":");

                        System.out.println(splitTheme[0].trim() + "**" + splitTheme[1].trim());
                        //Attempt to dynamically load the specified jarfile
                        Sys.addJarToClasspath(Globals.getCWD(splitTheme[0].trim()));

                        //Attempt to set the look'n'feel to the theme specified by the file
                        UIManager.setLookAndFeel(splitTheme[1].trim());

                        debugMsg("Look and Feel set to '" + swing_Theme.getNodeValue() + "'.", 3);

                    } catch (Exception e) {
                        //If unable to set to .rsm's theme, use the system look'n'feel
                        try {
                            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                        } catch (Exception a) {
                            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                        }
                        Utilities.showError(
                                "Error loading Look and Feel Specified, Look and Feel set to System");
                        e.printStackTrace();

                    }
                } else {
                    //If swing_Theme == camo or is not set, use the system look'n'feel
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (Exception a) {
                        a.printStackTrace();
                        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                    }
                    debugMsg("Look and Feel (Swing) set to System", 3);
                }
            } else {
                //If swing_Theme == camo or is not set, use the system look'n'feel
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception a) {
                    a.printStackTrace();
                    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                }
                debugMsg("Look and Feel (Swing) set to System", 3);

            }

            //Parse styles
            for (int i = 0; i < mainElement.getElementsByTagName("style").getLength(); i++) {
                Element styleElement = (Element) mainElement.getElementsByTagName("style").item(i);
                //Load all styles from the style tags
                if (styleElement.getAttributeNode("href") != null) {
                    loadStyles(null, styleElement.getTextContent());
                } else {
                    loadStyles(styleElement.getTextContent(), null);

                }
            }

            //Parse links
            for (int i = 0; i < mainElement.getElementsByTagName("link").getLength(); i++) {
                Element linkElement = (Element) mainElement.getElementsByTagName("link").item(i);
                parseLinks(linkElement, engine);
            }

            //Parse JavaScript in <script> tags
            Main.loader.loadJS(escapedFile, engine);

            //Parse GUI
            for (int i = 0; i < mainElement.getElementsByTagName("window").getLength(); i++) {
                GUI.loadWindow((Element) mainElement.getElementsByTagName("window").item(i), engine);
            }

        } else { //There was more than one body tag, or 0 body tags

            //Display Error and quit, as we cannot recover from an abnormally formatted file
            Utilities.showError("Error: More or less than one <rsm> tag in '" + escapedFile + "'.\n\n"
                    + "Please add ONE <rsm> tag to '" + escapedFile + "'.");
            System.exit(1);
        }

    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }

}

From source file:us.derfers.tribex.rapids.Loader.java

/**
 * Loads scripts and styles from link tags.
 * @param linkElement The element from which to load the content from.
 * @param engine The ScriptEngine to run links to scripts in.
 * @return//from   w w w  . j ava  2  s.  c  o  m
 */
private boolean parseLinks(Element linkElement, ScriptEngine engine) {

    //Check and see if the <link> tag contains a rel and href attribute
    if (linkElement.getAttribute("href") != null) {
        //If it links to a stylesheet
        if ((linkElement.getAttributeNode("rel") != null
                && linkElement.getAttributeNode("rel").getTextContent().equals("stylesheet"))
                || linkElement.getAttributeNode("href").getTextContent().endsWith(".css")) {

            //Check and see if the href and URL exist.
            if (linkElement.getAttributeNode("href").getNodeValue().contains("://")) {
                try {
                    if (this.loadStyles(
                            IOUtils.toString(new URL(linkElement.getAttributeNode("href").getNodeValue())),
                            null) == false) {
                        Utilities.showError("Error: invalid file in link tag pointing to "
                                + linkElement.getAttributeNode("href").getTextContent());
                        return false;
                    }

                    if (linkElement.getAttributeNode("cache") != null) {
                        try {
                            FileUtils.writeStringToFile(
                                    new File(Globals
                                            .getCWD(linkElement.getAttributeNode("cache").getTextContent())),
                                    IOUtils.toString(
                                            new URL(linkElement.getAttributeNode("href").getNodeValue())));
                        } catch (Exception e2) {
                            Utilities.showError("Error: unable to cache to file "
                                    + linkElement.getAttributeNode("cache").getTextContent());
                        }
                    }

                } catch (Exception e) {
                    Utilities.showError(
                            "Unable to locate " + linkElement.getAttributeNode("href").getNodeValue());
                    //Attempt to load from the fallback file. (If tag and file exist)
                    if (linkElement.getAttributeNode("fallback") != null) {
                        if (this.loadStyles(null, Globals
                                .getCWD(linkElement.getAttributeNode("fallback").getTextContent())) == false) {
                            Utilities.showError("Error: invalid file in fallback tag pointing to "
                                    + linkElement.getAttributeNode("fallback").getTextContent());
                            return false;
                        }
                        ;
                    }
                }
                //Load from file
            } else {
                if (this.loadStyles(null,
                        Globals.getCWD(linkElement.getAttributeNode("href").getTextContent())) == false) {
                    Utilities.showError("Error: invalid file in link tag pointing to "
                            + linkElement.getAttributeNode("href").getTextContent());
                    return false;
                }
                ;
            }

            //If it links to a script
        } else if ((linkElement.getAttributeNode("rel") != null
                && linkElement.getAttributeNode("rel").getTextContent().equals("script"))
                || linkElement.getAttributeNode("href").getTextContent().endsWith(".js")) {

            //Check and see if the file exists
            if (linkElement.getAttributeNode("href").getNodeValue().contains("://")) {
                //Run script in file
                URL url;
                try {
                    url = new URL(linkElement.getAttributeNode("href").getTextContent());

                    URLConnection connection = url.openConnection();
                    connection.setConnectTimeout(10000);
                    connection.setReadTimeout(10000);
                    engine.eval(new InputStreamReader(connection.getInputStream()),
                            "Remote file: " + linkElement.getAttributeNode("href").getTextContent());

                    if (linkElement.getAttributeNode("cache") != null) {
                        try {
                            FileUtils.writeStringToFile(
                                    new File(Globals
                                            .getCWD(linkElement.getAttributeNode("cache").getTextContent())),
                                    IOUtils.toString(
                                            new URL(linkElement.getAttributeNode("href").getNodeValue())));
                        } catch (Exception e2) {
                            Utilities.showError("Error: unable to cache to file "
                                    + linkElement.getAttributeNode("cache").getTextContent());
                        }
                    }

                    return true;
                } catch (Exception e) {
                    //Attempt to load from the fallback file. (If tag and file exist)
                    if (linkElement.getAttributeNode("fallback") != null) {
                        try {
                            engine.eval(
                                    new java.io.FileReader(Globals
                                            .getCWD(linkElement.getAttributeNode("fallback").getTextContent())),
                                    linkElement.getAttributeNode("fallback").getTextContent());
                        } catch (Exception e2) {
                            Utilities.showError("Error: invalid file in fallback tag pointing to "
                                    + linkElement.getAttributeNode("fallback").getTextContent());
                        }
                    } else {
                        Utilities.showError(
                                "Unable to load " + linkElement.getAttributeNode("href").getTextContent()
                                        + ". No fallback found.");
                    }

                }

            } else {
                try {
                    //Run script in file
                    engine.eval(
                            new java.io.FileReader(
                                    Globals.getCWD(linkElement.getAttributeNode("href").getTextContent())),
                            linkElement.getAttributeNode("href").getTextContent());
                    return true;

                } catch (FileNotFoundException e) {
                    Utilities.showError("Error: invalid file in link tag pointing to "
                            + linkElement.getAttributeNode("href").getTextContent());
                    e.printStackTrace();
                    return false;
                } catch (DOMException e) {
                    Utilities.showError("Error: Improperly formatted XML");
                    e.printStackTrace();
                    return false;
                } catch (Exception e) {
                    Utilities.showError("Error: invalid script in file "
                            + linkElement.getAttributeNode("href").getTextContent());
                    e.printStackTrace();
                    return false;
                }
            }
        } else {
            //Attempt to load as a .rsm file

            //If the file is a URL on the internet:
            if (linkElement.getAttributeNode("href").getNodeValue().contains("://")) {
                try {
                    //Load the file from the internet.
                    Main.loader.loadAll(Utilities.EscapeScriptTags(
                            IOUtils.toString(new URL(linkElement.getAttributeNode("href").getNodeValue()))));

                    if (linkElement.getAttributeNode("cache") != null) {
                        try {
                            FileUtils.writeStringToFile(
                                    new File(Globals
                                            .getCWD(linkElement.getAttributeNode("cache").getTextContent())),
                                    IOUtils.toString(
                                            new URL(linkElement.getAttributeNode("href").getNodeValue())));
                        } catch (Exception e2) {
                            Utilities.showError("Error: unable to cache to file "
                                    + linkElement.getAttributeNode("cache").getTextContent());
                        }
                    }
                } catch (Exception e) {
                    if (linkElement.getAttributeNode("fallback") != null) {
                        try {
                            Main.loader.loadAll(Utilities.EscapeScriptTags(FileUtils.readFileToString(new File(
                                    Globals.getCWD(linkElement.getAttributeNode("fallback").getNodeValue())))));
                        } catch (Exception e2) {
                            Utilities.showError("Error: invalid file in fallback tag pointing to "
                                    + linkElement.getAttributeNode("fallback").getTextContent());
                        }
                    }
                }

                //Load the file from the Hard Drive
            } else {
                try {
                    Main.loader.loadAll(Utilities.EscapeScriptTags(FileUtils.readFileToString(
                            new File(Globals.getCWD(linkElement.getAttributeNode("href").getNodeValue())))));
                } catch (DOMException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

    } else {
        System.out.println(linkElement.toString());
        Utilities.showError(
                "Warning: <link> tags must contain a href attribute and a rel attribute. Skipping tag.");
    }
    return false;
}