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.TimestampTest.java

/**
 * This is a test for processing an Timestamp where the "Created" element is greater than
 * the expiration time./* w w w .  j  a va2  s . com*/
 */
@org.junit.Test
public void testExpiresBeforeCreated() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.CREATED_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 300000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementCreated);

    Date expiresDate = new Date();
    expiresDate.setTime(expiresDate.getTime() - 300000);

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

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    try {
        verify(doc, WSSConfig.getNewInstance());
        fail("The timestamp validation should have failed");
    } catch (WSSecurityException ex) {
        assertTrue(ex.getErrorCode() == WSSecurityException.MESSAGE_EXPIRED);
    }
}

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where it contains multiple "Created" elements.
 * This Timestamp should be rejected.//from   ww  w .j a  v a 2  s.  c o  m
 */
@org.junit.Test
public void testMultipleCreated() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.CREATED_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 300000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementCreated);
    timestampElement.appendChild(elementCreated.cloneNode(true));

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    try {
        verify(doc, WSSConfig.getNewInstance());
        fail("The timestamp validation should have failed on multiple Created elements");
    } catch (WSSecurityException ex) {
        // expected
    }
}

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where it contains no "Created" element.
 * This Timestamp should be rejected.//w  w w .j  a v  a  2  s .c  om
 */
@org.junit.Test
public void testNoCreated() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.EXPIRES_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 300000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementCreated);

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    WSSConfig wssConfig = WSSConfig.getNewInstance();
    wssConfig.setWsiBSPCompliant(true);
    try {
        verify(doc, wssConfig);
        fail("The timestamp validation should have failed on no Created element");
    } catch (WSSecurityException ex) {
        // expected
    }
}

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where it contains multiple "Expires" elements.
 * This Timestamp should be rejected.//from www  . j a va 2  s  .c  om
 */
@org.junit.Test
public void testMultipleExpires() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.EXPIRES_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 300000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementCreated);
    timestampElement.appendChild(elementCreated.cloneNode(true));

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    try {
        verify(doc, WSSConfig.getNewInstance());
        fail("The timestamp validation should have failed on multiple Expires elements");
    } catch (WSSecurityException ex) {
        // expected
    }
}

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where it contains an "Expires" element before
 * the Created element. This Timestamp should be rejected as per the BSP spec.
 *///from   w  w w.ja  va  2 s . c o  m
@org.junit.Test
public void testExpiresInFrontOfCreated() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.EXPIRES_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 300000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementCreated);

    Element elementExpires = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.CREATED_LN);
    elementExpires.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementExpires);

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    WSSConfig wssConfig = WSSConfig.getNewInstance();
    wssConfig.setWsiBSPCompliant(true);
    try {
        verify(doc, wssConfig);
        fail("The timestamp validation should have failed");
    } catch (WSSecurityException ex) {
        // expected
    }
}

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where it contains a Created element with
 * seconds > 60. This should be rejected as per the BSP spec.
 *///from   w ww  . ja v  a2  s  .  c  o m
@org.junit.Test
public void testCreatedSeconds() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.CREATED_LN);
    elementCreated.appendChild(doc.createTextNode("2011-02-08T13:13:84.535Z"));
    timestampElement.appendChild(elementCreated);

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing - disable the validator to make sure that the Timestamp processor
    // is rejecting the Timestamp
    //
    WSSConfig wssConfig = WSSConfig.getNewInstance();
    wssConfig.setWsiBSPCompliant(true);
    wssConfig.setValidator(WSSecurityEngine.TIMESTAMP, new NoOpValidator());
    try {
        verify(doc, wssConfig);
        fail("The timestamp validation should have failed");
    } catch (WSSecurityException ex) {
        //assertTrue(ex.getMessage().contains("Unparseable date"));
    }
}

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where it contains a Created element with
 * a ValueType. This should be rejected as per the BSP spec.
 *//*w  w w . j  a v a 2s . com*/
@org.junit.Test
public void testCreatedValueType() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.CREATED_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 300000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    elementCreated.setAttributeNS(null, "ValueType", WSConstants.WSS_SAML_KI_VALUE_TYPE);
    timestampElement.appendChild(elementCreated);

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    WSSConfig wssConfig = WSSConfig.getNewInstance();
    wssConfig.setWsiBSPCompliant(true);
    wssConfig.setValidator(WSSecurityEngine.TIMESTAMP, new NoOpValidator());
    try {
        verify(doc, wssConfig);
        fail("The timestamp validation should have failed");
    } catch (WSSecurityException ex) {
        //
    }

    // Now it should pass...
    wssConfig.setWsiBSPCompliant(false);
    verify(doc, wssConfig);
}

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where it contains a CustomElement. This should
 * be rejected as per the BSP spec./*from  w  ww .  j  a  va 2 s. co  m*/
 */
@org.junit.Test
public void testCustomElement() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.EXPIRES_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 300000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementCreated);

    Element elementCustom = doc.createElementNS(WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + "Custom");
    timestampElement.appendChild(elementCustom);

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    WSSConfig wssConfig = WSSConfig.getNewInstance();
    wssConfig.setWsiBSPCompliant(true);
    try {
        verify(doc, wssConfig);
        fail("The timestamp validation should have failed");
    } catch (WSSecurityException ex) {
        //
    }

    // Now it should pass...
    wssConfig.setWsiBSPCompliant(false);
    verify(doc, wssConfig);
}

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

/**
 * Constructor./*from   w  w w.ja v  a  2 s  .c o m*/
 * 
 * @param doc 
 */
public BinarySecurity(Document doc) {
    this.element = doc.createElementNS(WSConstants.WSSE_NS, "wsse:BinarySecurityToken");
    WSSecurityUtil.setNamespace(this.element, WSConstants.WSSE_NS, WSConstants.WSSE_PREFIX);
    setEncodingType(BASE64_ENCODING);
    this.element.appendChild(doc.createTextNode(""));
}

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

/**
 * This will create an empty DerivedKeyToken
 *
 * @param doc The DOM document/*ww  w .jav  a2s . c o  m*/
 */
public DerivedKeyToken(int version, Document doc) throws ConversationException {
    log.debug("DerivedKeyToken: created");

    this.ns = ConversationConstants.getWSCNs(version);
    this.element = doc.createElementNS(ns, "wsc:" + ConversationConstants.DERIVED_KEY_TOKEN_LN);
    WSSecurityUtil.setNamespace(this.element, ns, ConversationConstants.WSC_PREFIX);
}