Example usage for javax.xml.soap SOAPElement setAttributeNS

List of usage examples for javax.xml.soap SOAPElement setAttributeNS

Introduction

In this page you can find the example usage for javax.xml.soap SOAPElement setAttributeNS.

Prototype

public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

/**
 * Test the behavior of {@link SOAPElement#getAttributeValue(QName)} for an attribute with
 * namespace. In particular, check that the prefix is not considered when matching attribute
 * names./*from   ww w.  j  ava2s.c  o m*/
 * 
 * @throws Exception
 */
@Validated
@Test
public void testGetAttributeValueByQNameWithNamespace() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement("urn:test", "test", "p");
    element.setAttributeNS("urn:test", "p:attr", "value");
    assertEquals("value", element.getAttributeValue(new QName("urn:test", "attr", "")));
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

/**
 * Test that {@link SOAPElement#getAllAttributes()} returns the correct {@link Name} for an
 * attribute with a namespace.//from  w  w  w  .j a v  a2  s.  co  m
 */
@Validated
@Test
public void testGetAllAttributesWithNamespace() {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    element.setAttributeNS("urn:ns", "p:test", "value");
    Iterator<?> it = element.getAllAttributes();
    assertTrue(it.hasNext());
    Name name = (Name) it.next();
    assertEquals("urn:ns", name.getURI());
    assertEquals("p", name.getPrefix());
    assertEquals("test", name.getLocalName());
    assertEquals("p:test", name.getQualifiedName());
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

/**
 * Test that {@link SOAPElement#getAllAttributes()} returns the correct {@link Name} for an
 * attribute without namespace.//from ww  w. jav a  2 s .  c  o m
 */
@Validated
@Test
public void testGetAllAttributesWithoutNamespace() {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    element.setAttributeNS(null, "test", "value");
    Iterator<?> it = element.getAllAttributes();
    assertTrue(it.hasNext());
    Name name = (Name) it.next();
    assertTrue(StringUtils.isEmpty(name.getURI()));
    assertTrue(StringUtils.isEmpty(name.getPrefix()));
    assertEquals("test", name.getLocalName());
    assertEquals("test", name.getQualifiedName());
}

From source file:com.googlecode.ddom.frontend.saaj.SOAPElementTest.java

@Validated
@Test//from   w  w w .  ja v  a 2 s .  c o  m
public void testRemoveAttributeByName() throws Exception {
    SOAPEnvelope envelope = saajUtil.createSOAP11Envelope();
    SOAPBody body = envelope.addBody();
    SOAPElement element = body.addChildElement(new QName("urn:test", "test"));
    element.setAttributeNS("urn:ns1", "ns1:attr1", "value");
    element.setAttributeNS("urn:ns1", "ns1:attr2", "value");
    assertTrue(element.removeAttribute(envelope.createName("attr2", "p", "urn:ns1")));
    NamedNodeMap attributes = element.getAttributes();
    assertNull(attributes.getNamedItemNS("urn:ns1", "attr2"));
}

From source file:edu.unc.lib.dl.util.TripleStoreQueryServiceMulgaraImpl.java

private String sendTQL(String query) {
    log.debug(query);/*from  w  ww  .  ja v a  2s.c o m*/
    String result = null;
    SOAPMessage reply = null;
    SOAPConnection connection = null;
    try {
        // Next, create the actual message
        MessageFactory messageFactory = MessageFactory.newInstance();
        SOAPMessage message = messageFactory.createMessage();
        message.getMimeHeaders().setHeader("SOAPAction", "itqlbean:executeQueryToString");
        SOAPBody soapBody = message.getSOAPPart().getEnvelope().getBody();
        soapBody.addNamespaceDeclaration("xsd", JDOMNamespaceUtil.XSD_NS.getURI());
        soapBody.addNamespaceDeclaration("xsi", JDOMNamespaceUtil.XSI_NS.getURI());
        soapBody.addNamespaceDeclaration("itqlbean", this.getItqlEndpointURL());
        SOAPElement eqts = soapBody.addChildElement("executeQueryToString", "itqlbean");
        SOAPElement queryStr = eqts.addChildElement("queryString", "itqlbean");
        queryStr.setAttributeNS(JDOMNamespaceUtil.XSI_NS.getURI(), "xsi:type", "xsd:string");
        CDATASection queryCDATA = message.getSOAPPart().createCDATASection(query);
        queryStr.appendChild(queryCDATA);
        message.saveChanges();

        // First create the connection
        SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
        connection = soapConnFactory.createConnection();
        reply = connection.call(message, this.getItqlEndpointURL());

        if (reply.getSOAPBody().hasFault()) {
            reportSOAPFault(reply);
            if (log.isDebugEnabled()) {
                // log the full soap body response
                DOMBuilder builder = new DOMBuilder();
                org.jdom2.Document jdomDoc = builder.build(reply.getSOAPBody().getOwnerDocument());
                log.debug(new XMLOutputter().outputString(jdomDoc));
            }
        } else {
            NodeList nl = reply.getSOAPPart().getEnvelope().getBody().getElementsByTagNameNS("*",
                    "executeQueryToStringReturn");
            if (nl.getLength() > 0) {
                result = nl.item(0).getFirstChild().getNodeValue();
            }
            log.debug(result);
        }
    } catch (SOAPException e) {
        log.error("Failed to prepare or send iTQL via SOAP", e);
        throw new RuntimeException("Cannot query triple store at " + this.getItqlEndpointURL(), e);
    } finally {
        try {
            connection.close();
        } catch (SOAPException e) {
            log.error("Failed to close SOAP connection", e);
            throw new RuntimeException(
                    "Failed to close SOAP connection for triple store at " + this.getItqlEndpointURL(), e);
        }
    }
    return result;
}

From source file:org.jbpm.bpel.integration.soap.SoapUtil.java

public static void copyAttributes(SOAPElement target, Element source) {
    // easy way out: no attributes
    if (!source.hasAttributes())
        return;//from w w  w .  j  av a2  s  . co  m
    // traverse attributes
    NamedNodeMap attributes = source.getAttributes();
    for (int i = 0, n = attributes.getLength(); i < n; i++) {
        Node attribute = attributes.item(i);
        String namespaceURI = attribute.getNamespaceURI();
        // isn't the attribute a namespace declaration?
        if (BpelConstants.NS_XMLNS.equals(namespaceURI))
            continue;

        String name = attribute.getNodeName();
        String value = attribute.getNodeValue();
        if (namespaceURI == null) {
            /*
             * use the DOM level 1 method as some SAAJ implementations complain when presented a null
             * namespace URI
             */
            target.setAttribute(name, value);
        } else
            target.setAttributeNS(namespaceURI, name, value);

        if (traceEnabled)
            log.trace("set attribute: " + name);
    }
}