Example usage for org.w3c.dom Document createElementNS

List of usage examples for org.w3c.dom Document createElementNS

Introduction

In this page you can find the example usage for org.w3c.dom Document createElementNS.

Prototype

public Element createElementNS(String namespaceURI, String qualifiedName) throws DOMException;

Source Link

Document

Creates an element of the given qualified name and namespace URI.

Usage

From source file:org.apache.ws.security.message.token.SecurityTokenReference.java

/**
 * Constructor.//  www  .  ja  v a 2 s.com
 * 
 * @param doc
 *            TODO
 */
public SecurityTokenReference(Document doc) {
    doDebug = log.isDebugEnabled();
    this.element = doc.createElementNS(WSConstants.WSSE_NS, "wsse:SecurityTokenReference");
    WSSecurityUtil.setNamespace(this.element, WSConstants.WSSE_NS, WSConstants.WSSE_PREFIX);
}

From source file:org.apache.ws.security.message.token.SecurityTokenReference.java

private void createKeyIdentifier(Document doc, String uri, Node node, boolean base64) {
    Element keyId = doc.createElementNS(WSConstants.WSSE_NS, "wsse:KeyIdentifier");
    keyId.setAttributeNS(null, "ValueType", uri);
    if (base64) {
        keyId.setAttributeNS(null, "EncodingType", BinarySecurity.BASE64_ENCODING);
    }// w w  w. ja v  a  2  s  .  c  o  m

    keyId.appendChild(node);
    Element elem = getFirstElement();
    if (elem != null) {
        this.element.replaceChild(keyId, elem);
    } else {
        this.element.appendChild(keyId);
    }
}

From source file:org.apache.ws.security.message.token.SecurityTokenReferenceTest.java

/**
 * Test for a SecurityTokenReference having a Key Identifier with a bad EncodingType
 *//*from   w  w w  . j a v a2  s . c  om*/
@org.junit.Test
public void testKeyIdentifierBadEncodingType() throws Exception {
    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);

    // Create the STR
    SecurityTokenReference str = new SecurityTokenReference(doc);
    str.addWSSENamespace();
    Element strElement = str.getElement();

    Element keyId = doc.createElementNS(WSConstants.WSSE_NS, "wsse:KeyIdentifier");
    keyId.setAttributeNS(null, "ValueType", SecurityTokenReference.ENC_KEY_SHA1_URI);
    keyId.setAttributeNS(null, "EncodingType", "http://bad_encoding");
    keyId.appendChild(doc.createTextNode("#123"));
    strElement.appendChild(keyId);

    if (LOG.isDebugEnabled()) {
        LOG.debug(str.toString());
    }

    // Process the STR
    try {
        new SecurityTokenReference(strElement);
        fail("Failure expected on a Key Identifier with a Bad EncodingType");
    } catch (WSSecurityException ex) {
        assertTrue(ex.getMessage().contains("bad EncodingType"));
    }

    new SecurityTokenReference(strElement, false);
}

From source file:org.apache.ws.security.message.token.SecurityTokenReferenceTest.java

/**
 * Test for a SecurityTokenReference having a Key Identifier with no EncodingType
 */// www  .j a  v a 2s.  c o m
@org.junit.Test
public void testKeyIdentifierNoEncodingType() throws Exception {
    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);

    // Create the STR
    SecurityTokenReference str = new SecurityTokenReference(doc);
    str.addWSSENamespace();
    Element strElement = str.getElement();

    Element keyId = doc.createElementNS(WSConstants.WSSE_NS, "wsse:KeyIdentifier");
    keyId.setAttributeNS(null, "ValueType", SecurityTokenReference.ENC_KEY_SHA1_URI);
    keyId.appendChild(doc.createTextNode("#123"));
    strElement.appendChild(keyId);

    if (LOG.isDebugEnabled()) {
        LOG.debug(str.toString());
    }

    // Process the STR
    try {
        new SecurityTokenReference(strElement);
        fail("Failure expected on a Key Identifier with no EncodingType");
    } catch (WSSecurityException ex) {
        assertTrue(ex.getMessage().contains("No EncodingType"));
    }

    new SecurityTokenReference(strElement, false);
}

From source file:org.apache.ws.security.message.token.SecurityTokenReferenceTest.java

/**
 * Test for a SecurityTokenReference having a Key Identifier with no EncodingType, but
 * it should pass as the ValueType is for a SAML Assertion.
 *///from   w  ww .  j  a  va 2 s  .com
@org.junit.Test
public void testKeyIdentifierSAMLNoEncodingType() throws Exception {
    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);

    // Create the STR
    SecurityTokenReference str = new SecurityTokenReference(doc);
    str.addWSSENamespace();
    Element strElement = str.getElement();

    Element keyId = doc.createElementNS(WSConstants.WSSE_NS, "wsse:KeyIdentifier");
    keyId.setAttributeNS(null, "ValueType", WSConstants.WSS_SAML_KI_VALUE_TYPE);
    keyId.appendChild(doc.createTextNode("#123"));
    strElement.appendChild(keyId);

    if (LOG.isDebugEnabled()) {
        LOG.debug(str.toString());
    }

    // Process the STR
    new SecurityTokenReference(strElement);
}

From source file:org.apache.ws.security.message.token.SecurityTokenReferenceTest.java

/**
 * Test for a SecurityTokenReference having an Embedded Child, which in turn has a 
 * SecurityTokenReference child.// ww  w  .ja  v a 2  s. com
 */
@org.junit.Test
public void testEmbeddedSTRChild() throws Exception {
    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);

    // Create the STR
    SecurityTokenReference str = new SecurityTokenReference(doc);
    str.addWSSENamespace();
    Element strElement = str.getElement();

    Element embedded = doc.createElementNS(WSConstants.WSSE_NS, "wsse:Embedded");
    str = new SecurityTokenReference(doc);
    str.addWSSENamespace();
    embedded.appendChild(str.getElement());

    strElement.appendChild(embedded);

    if (LOG.isDebugEnabled()) {
        LOG.debug(DOM2Writer.nodeToString(strElement));
    }

    // Process the STR
    try {
        new SecurityTokenReference(strElement);
        fail("Failure expected on an Embedded Child with a SecurityTokenReference child");
    } catch (WSSecurityException ex) {
        assertTrue(ex.getMessage().contains("embedded Reference is invalid"));
    }

    new SecurityTokenReference(strElement, false);
}

From source file:org.apache.ws.security.message.token.SecurityTokenReferenceTest.java

/**
 * Test for a SecurityTokenReference having an Embedded Child, which has multiple
 * children./*from ww  w  .ja v  a 2 s . co m*/
 */
@org.junit.Test
public void testMultipleEmbeddedChildren() throws Exception {
    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);

    // Create the STR
    SecurityTokenReference str = new SecurityTokenReference(doc);
    str.addWSSENamespace();
    Element strElement = str.getElement();

    Element embedded = doc.createElementNS(WSConstants.WSSE_NS, "wsse:Embedded");
    Element embedded1 = doc.createElementNS(WSConstants.WSSE_NS, "wsse:Reference");
    Element embedded2 = doc.createElementNS(WSConstants.WSSE_NS, "wsse:Reference");
    embedded.appendChild(embedded1);
    embedded.appendChild(embedded2);

    strElement.appendChild(embedded);

    if (LOG.isDebugEnabled()) {
        LOG.debug(DOM2Writer.nodeToString(strElement));
    }

    // Process the STR
    try {
        new SecurityTokenReference(strElement);
        fail("Failure expected on an Embedded Child with multiple children");
    } catch (WSSecurityException ex) {
        assertTrue(ex.getMessage().contains("embedded Reference is invalid"));
    }

    new SecurityTokenReference(strElement, false);
}

From source file:org.apache.ws.security.message.token.Timestamp.java

/**
 * Constructs a <code>Timestamp</code> object according
 * to the defined parameters.//from   ww w.  j a  v a2s .c  o m
 *
 * @param doc the SOAP envelope as <code>Document</code>
 * @param ttl the time to live (validity of the security semantics) in seconds
 */
public Timestamp(boolean milliseconds, Document doc, int ttl) {

    customElements = new ArrayList<Element>();
    element = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = null;
    if (milliseconds) {
        zulu = new XmlSchemaDateFormat();
    } else {
        zulu = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        zulu.setTimeZone(TimeZone.getTimeZone("UTC"));
    }
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.CREATED_LN);
    createdDate = new Date();
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    element.appendChild(elementCreated);
    if (ttl != 0) {
        expiresDate = new Date();
        expiresDate.setTime(createdDate.getTime() + ((long) ttl * 1000L));

        Element elementExpires = doc.createElementNS(WSConstants.WSU_NS,
                WSConstants.WSU_PREFIX + ":" + WSConstants.EXPIRES_LN);
        elementExpires.appendChild(doc.createTextNode(zulu.format(expiresDate)));
        element.appendChild(elementExpires);
    }
}

From source file:org.apache.ws.security.message.token.UsernameToken.java

/**
 * Constructs a <code>UsernameToken</code> object according to the defined
 * parameters.// www . j a va  2s.c o m
 * 
 * @param doc the SOAP envelope as <code>Document</code>
 * @param pwType the required password encoding, either
 *               {@link WSConstants#PASSWORD_DIGEST} or
 *               {@link WSConstants#PASSWORD_TEXT} or 
 *               {@link WSConstants#PW_NONE} <code>null</code> if no
 *               password required
 */
public UsernameToken(boolean milliseconds, Document doc, String pwType) {
    element = doc.createElementNS(WSConstants.WSSE_NS, "wsse:" + WSConstants.USERNAME_TOKEN_LN);
    WSSecurityUtil.setNamespace(element, WSConstants.WSSE_NS, WSConstants.WSSE_PREFIX);

    elementUsername = doc.createElementNS(WSConstants.WSSE_NS, "wsse:" + WSConstants.USERNAME_LN);
    elementUsername.appendChild(doc.createTextNode(""));
    element.appendChild(elementUsername);

    if (pwType != null) {
        elementPassword = doc.createElementNS(WSConstants.WSSE_NS, "wsse:" + WSConstants.PASSWORD_LN);
        elementPassword.appendChild(doc.createTextNode(""));
        element.appendChild(elementPassword);

        hashed = false;
        passwordType = pwType;
        if (passwordType.equals(WSConstants.PASSWORD_DIGEST)) {
            hashed = true;
            addNonce(doc);
            addCreated(milliseconds, doc);
        }
    }
}

From source file:org.apache.ws.security.message.token.UsernameToken.java

/**
 * Creates and adds a Nonce element to this UsernameToken
 *///from w w  w . ja va2s. c  om
public void addNonce(Document doc) {
    if (elementNonce != null) {
        return;
    }
    byte[] nonceValue = new byte[16];
    random.nextBytes(nonceValue);
    elementNonce = doc.createElementNS(WSConstants.WSSE_NS, "wsse:" + WSConstants.NONCE_LN);
    elementNonce.appendChild(doc.createTextNode(Base64.encode(nonceValue)));
    elementNonce.setAttributeNS(null, "EncodingType", BASE64_ENCODING);
    element.appendChild(elementNonce);
}