Example usage for javax.xml.soap SOAPElement addAttribute

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

Introduction

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

Prototype

public SOAPElement addAttribute(QName qname, String value) throws SOAPException;

Source Link

Document

Adds an attribute with the specified name and value to this SOAPElement object.

Usage

From source file:eu.planets_project.tb.gui.backing.admin.wsclient.util.WSClient.java

/**
 * Builds a hierarchy of SOAPElements given a complex value JDOM Element
 *
 * @param   envelope   The SOAP Envelope
 * @param   rootElem   The root SOAP Element to build content for
 * @param   jdomElem   A JDOM Element that represents a complex value
 * @param   isRPC      Pass true when building for RPC encoded messages
 *
 * @throws SOAPException/*www  . j  av  a 2  s  . co m*/
 */
protected static void buildSoapElement(SOAPEnvelope envelope, SOAPElement soapElem, Element jdomElem,
        boolean isRPC) throws SOAPException {
    // If the source node has text use its value
    String elemText = jdomElem.getText();

    if (elemText != null) {
        if (isRPC == true) {
            // Set the type attribute for this element
            String type = jdomElem.getAttributeValue("type");

            if (type != null) {
                soapElem.addAttribute(envelope.createName(XSI_NAMESPACE_PREFIX + ":type"),
                        XSD_NAMESPACE_PREFIX + ":" + type);
            }
        }

        // Add the element text value
        soapElem.addTextNode(elemText);
    }

    // If the source node has attributes add the attribute values
    List attrs = jdomElem.getAttributes();

    if (attrs != null) {
        Iterator attrIter = attrs.iterator();

        while (attrIter.hasNext()) {
            // Get the attribute to add
            Attribute attr = (Attribute) attrIter.next();

            // Create a name for the attribute
            Name attrName = envelope.createName(attr.getName(), attr.getNamespacePrefix(),
                    attr.getNamespaceURI());

            // Add the attribute and its value to the element
            soapElem.addAttribute(attrName, attr.getValue());
        }
    }

    // Build children
    List children = jdomElem.getChildren();

    if (children != null) {
        Iterator childIter = children.iterator();

        while (childIter.hasNext()) {
            Element jdomChildElem = (Element) childIter.next();

            // Create a new SOAPElement as a child of the current one
            //SOAPElement soapChildElem = soapElem.addChildElement(jdomChildElem.getName());
            SOAPElement soapChildElem = soapElem.addChildElement(jdomChildElem.getName(),
                    jdomChildElem.getNamespacePrefix(), jdomChildElem.getNamespaceURI());

            // Build it
            buildSoapElement(envelope, soapChildElem, jdomChildElem, isRPC);
        }
    }
}

From source file:net.bpelunit.framework.control.deploy.activebpel.BPRDeployRequestEntity.java

@Override
protected void populateMessage(SOAPMessage message) throws SOAPException, IOException {
    SOAPElement xmlDeployBpr = addRootElement(message, new QName(ACTIVEBPEL_ELEMENT_DEPLOYBPR));

    // Add filename
    SOAPElement xmlBprFilename = xmlDeployBpr.addChildElement(ACTIVEBPEL_ELEMENT_ABPRFILENAME);
    xmlBprFilename.addAttribute(new QName(ActiveBPELRequestEntityBase.NS_XMLSCHEMA_INSTANCE, "type"),
            XSD_STRING);//from   ww w  .  j a  v a2s  .c  o m
    xmlBprFilename.setTextContent(FilenameUtils.getName(file.toString()));

    // Add data
    SOAPElement xmlBase64File = xmlDeployBpr.addChildElement(ACTIVEBPEL_ELEMENT_ABASE64FILE);
    xmlBase64File.addAttribute(new QName(ActiveBPELRequestEntityBase.NS_XMLSCHEMA_INSTANCE, "type"),
            XSD_STRING);

    StringBuilder content = new StringBuilder();
    byte[] arr = FileUtils.readFileToByteArray(file);
    byte[] encoded = Base64.encodeBase64Chunked(arr);
    for (int i = 0; i < encoded.length; i++) {
        content.append((char) encoded[i]);
    }
    xmlBase64File.setTextContent(content.toString());
}

From source file:net.bpelunit.framework.control.deploy.ode.ODERequestEntityFactory.java

private void prepareDeploySOAP(File file) throws IOException, SOAPException {
    MessageFactory mFactory = MessageFactory.newInstance();
    SOAPMessage message = mFactory.createMessage();
    SOAPBody body = message.getSOAPBody();

    SOAPElement xmlDeploy = body.addChildElement(ODE_ELEMENT_DEPLOY);
    SOAPElement xmlZipFilename = xmlDeploy.addChildElement(ODE_ELEMENT_ZIPNAME);
    xmlZipFilename.setTextContent(FilenameUtils.getName(file.toString()).split("\\.")[0]);

    SOAPElement xmlZipContent = xmlDeploy.addChildElement(ODE_ELEMENT_PACKAGE);
    SOAPElement xmlBase64ZipFile = xmlZipContent.addChildElement(ODE_ELEMENT_ZIP, "dep", NS_DEPLOY_SERVICE);

    xmlBase64ZipFile.addAttribute(new QName(NS_XML_MIME, CONTENT_TYPE_STRING), ZIP_CONTENT_TYPE);

    StringBuilder content = new StringBuilder();
    byte[] arr = FileUtils.readFileToByteArray(file);
    byte[] encoded = Base64.encodeBase64Chunked(arr);
    for (int i = 0; i < encoded.length; i++) {
        content.append((char) encoded[i]);
    }/*w  w  w .  j a  v a 2 s.  com*/

    xmlBase64ZipFile.setTextContent(content.toString());

    ByteArrayOutputStream b = new ByteArrayOutputStream();
    message.writeTo(b);
    fContent = b.toString();
}

From source file:com.konakart.bl.modules.ordertotal.thomson.HeaderSecrityHandler.java

public boolean handleMessage(SOAPMessageContext smc) {
    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outboundProperty.booleanValue()) {
        SOAPMessage message = smc.getMessage();

        if (log.isInfoEnabled()) {
            log.info("Adding Credentials : " + getUName() + "/" + getPWord());
        }//from ww  w.  j a v  a 2s .  c  o  m

        try {
            SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
            envelope.setPrefix("soapenv");
            envelope.getBody().setPrefix("soapenv");

            SOAPHeader header = envelope.addHeader();
            SOAPElement security = header.addChildElement("Security", "wsse",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

            SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse");
            usernameToken.addAttribute(new QName("wsu:Id"), "UsernameToken-1");
            usernameToken.setAttribute("wsu:Id", "UsernameToken-1");

            usernameToken.addAttribute(new QName("xmlns:wsu"),
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

            SOAPElement username = usernameToken.addChildElement("Username", "wsse");
            username.addTextNode(getUName());

            SOAPElement password = usernameToken.addChildElement("Password", "wsse");
            password.setAttribute("Type",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
            password.addTextNode(getPWord());

            SOAPElement encodingType = usernameToken.addChildElement("Nonce", "wsse");
            encodingType.setAttribute("EncodingType",
                    "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");
            encodingType.addTextNode("Encoding");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return outboundProperty;
}

From source file:backend.Weather.java

private SOAPMessage createSOAPRequest() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();
    String serverURI = "http://ws.cdyne.com/";
    SOAPEnvelope envelope = soapPart.getEnvelope();

    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement("GetCityWeatherByZIP");
    QName attributeName = new QName("xmlns");
    soapBodyElem.addAttribute(attributeName, "http://ws.cdyne.com/WeatherWS/");
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("ZIP");
    soapBodyElem1.addTextNode("02215");
    soapMessage.saveChanges();/* ww w  .  j  a  v a  2  s .c  o m*/
    return soapMessage;

}

From source file:whitelabel.cloud.wsclient.WebServiceAuthenticator.java

public void authenticateInClear(final SOAPMessage request, final String username, final String password)
        throws WsAuthenticationException {

    if (request == null) {
        LOG.error(" SoapMessage request not defined");
        throw new WsAuthenticationException("SOAP_REQUEST_NOT_DEFINED");
    }/* ww w .  j  a  va2s . co m*/
    if (username == null || password == null || username.trim().length() == 0
            || password.trim().length() == 0) {
        LOG.error("Username: " + username + " password: " + password + " - invalid parameters");
        throw new WsAuthenticationException("INVALID_PARAMETERS");
    }

    String nonceValue = generateNonceBase64(16);
    String createdValue = dfe.print(new Date());
    String userValue = username;
    String pwdValue = password;
    String pwdType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText";

    QName securityQName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security",
            "wsse");
    QName usernameTokenQName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
            "UsernameToken", "wsse");
    QName usernameQName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Username",
            "wsse");
    QName PasswordQName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Password",
            "wsse");
    QName PasswordTypeQName = new QName("Type");
    QName nonceQName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Nonce",
            "wsse");
    QName createdQName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Created",
            "wsu");

    try {
        SOAPElement securitySoap = request.getSOAPHeader().addChildElement(securityQName);
        SOAPElement usernameTokenSoap = securitySoap.addChildElement(usernameTokenQName);
        SOAPElement usernameSoap = usernameTokenSoap.addChildElement(usernameQName);
        usernameSoap.addTextNode(userValue);
        SOAPElement passwordSoap = usernameTokenSoap.addChildElement(PasswordQName);
        passwordSoap.addTextNode(pwdValue);
        passwordSoap.addAttribute(PasswordTypeQName, pwdType);
        SOAPElement nonceSoap = usernameTokenSoap.addChildElement(nonceQName);
        nonceSoap.addTextNode(nonceValue);
        SOAPElement creadedSoap = usernameTokenSoap.addChildElement(createdQName);
        creadedSoap.addTextNode(createdValue);
    } catch (SOAPException se) {
        LOG.error(se);
        throw new WsAuthenticationException("SOAPHEADER_CREATION", se);
    }
}

From source file:whitelabel.cloud.wsclient.WebServiceAuthenticator.java

public void authenticateWithDigest(final SOAPMessage request, final String username, final String password)
        throws WsAuthenticationException {

    if (request == null) {
        LOG.error(" SoapMessage request not defined");
        throw new WsAuthenticationException("SOAP_REQUEST_NOT_DEFINED");
    }/*from  w ww.  j  a  v  a 2 s  .  co  m*/
    if (username == null || password == null || username.trim().length() == 0
            || password.trim().length() == 0) {
        LOG.error("Username: " + username + " password: " + password + " - invalid parameters");
        throw new WsAuthenticationException("INVALID_PARAMETERS");
    }

    String nonceValue = generateNonceBase64(16);
    String createdValue = dfe.print(new Date());
    String userValue = username;
    String pwdValue = crypthPassword(nonceValue, createdValue, password);

    String pwdType = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest";

    QName securityQName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security",
            "wsse");
    QName usernameTokenQName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd",
            "UsernameToken", "wsse");
    QName usernameQName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Username",
            "wsse");
    QName PasswordQName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Password",
            "wsse");
    QName PasswordTypeQName = new QName("Type");
    QName nonceQName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Nonce",
            "wsse");
    QName createdQName = new QName(
            "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd", "Created",
            "wsu");

    SOAPElement securitySoap;
    try {
        securitySoap = request.getSOAPHeader().addChildElement(securityQName);
        SOAPElement usernameTokenSoap = securitySoap.addChildElement(usernameTokenQName);
        SOAPElement usernameSoap = usernameTokenSoap.addChildElement(usernameQName);
        usernameSoap.addTextNode(userValue);
        SOAPElement passwordSoap = usernameTokenSoap.addChildElement(PasswordQName);
        passwordSoap.addTextNode(pwdValue);
        passwordSoap.addAttribute(PasswordTypeQName, pwdType);
        SOAPElement nonceSoap = usernameTokenSoap.addChildElement(nonceQName);
        nonceSoap.addTextNode(nonceValue);
        SOAPElement creadedSoap = usernameTokenSoap.addChildElement(createdQName);
        creadedSoap.addTextNode(createdValue);
    } catch (SOAPException se) {
        LOG.error(se);
        throw new WsAuthenticationException("SOAPHEADER_CREATION", se);
    }
}

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

@Validated
@Test/*w w w.  ja  v a  2  s.  com*/
public void testAddAttributeFromQNameWithNamespace() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    SOAPElement retValue = element.addAttribute(new QName("urn:ns", "attr", "p"), "value");
    assertSame(element, retValue);
    Attr attr = element.getAttributeNodeNS("urn:ns", "attr");
    assertEquals("urn:ns", attr.getNamespaceURI());
    assertEquals("attr", attr.getLocalName());
    assertEquals("p", attr.getPrefix());
    assertEquals("value", attr.getValue());
    Attr nsDecl = element.getAttributeNodeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "p");
    assertNotNull(nsDecl);
    assertEquals("urn:ns", nsDecl.getValue());
}

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

@Validated
@Test//w w w  . j a  v  a2s  .  c  o  m
public void testAddAttributeFromQNameWithoutNamespace() throws Exception {
    SOAPElement element = saajUtil.createSOAPElement(null, "test", null);
    SOAPElement retValue = element.addAttribute(new QName("attr"), "value");
    assertSame(element, retValue);
    Attr attr = element.getAttributeNodeNS(null, "attr");
    assertNull(attr.getNamespaceURI());
    String localName = attr.getLocalName();
    // The RI creates a namespace unaware attribute node in this case; we believe
    // it's better to create a namespace aware node.
    if (localName != null) {
        assertEquals("attr", attr.getLocalName());
    }
    assertNull(attr.getPrefix());
    assertEquals("value", attr.getValue());
}

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

@Validated
@Test/*from  w w w.  j a  v  a 2s .  c  o  m*/
public void testAddAttributeFromNameWithNamespace() throws Exception {
    SOAPEnvelope envelope = saajUtil.createSOAP11Envelope();
    SOAPBody body = envelope.addBody();
    SOAPElement element = body.addChildElement(new QName("urn:test", "test"));
    SOAPElement retValue = element.addAttribute(envelope.createName("attr", "p", "urn:ns"), "value");
    assertSame(element, retValue);
    Attr attr = element.getAttributeNodeNS("urn:ns", "attr");
    assertEquals("urn:ns", attr.getNamespaceURI());
    assertEquals("attr", attr.getLocalName());
    assertEquals("p", attr.getPrefix());
    assertEquals("value", attr.getValue());
    Attr nsDecl = element.getAttributeNodeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "p");
    assertNotNull(nsDecl);
    assertEquals("urn:ns", nsDecl.getValue());
}