Example usage for javax.xml.soap SOAPElement addTextNode

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

Introduction

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

Prototype

public SOAPElement addTextNode(String text) throws SOAPException;

Source Link

Document

Creates a new Text object initialized with the given String and adds it to this SOAPElement object.

Usage

From source file:org.codice.ddf.security.interceptor.AnonymousInterceptor.java

private void createIncludeTimestamp(SOAPFactory soapFactory, SOAPElement securityHeader) {
    SOAPElement timestamp = null;
    try {//w w  w .  j a va2  s.  c o m
        timestamp = soapFactory.createElement(WSConstants.TIMESTAMP_TOKEN_LN, WSConstants.WSU_PREFIX,
                WSConstants.WSU_NS);
        SOAPElement created = soapFactory.createElement(WSConstants.CREATED_LN, WSConstants.WSU_PREFIX,
                WSConstants.WSU_NS);
        DateTime dateTime = new DateTime();
        created.addTextNode(dateTime.toString());
        SOAPElement expires = soapFactory.createElement(WSConstants.EXPIRES_LN, WSConstants.WSU_PREFIX,
                WSConstants.WSU_NS);
        expires.addTextNode(dateTime.plusMinutes(5).toString());
        timestamp.addChildElement(created);
        timestamp.addChildElement(expires);
        securityHeader.addChildElement(timestamp);
    } catch (SOAPException e) {
        LOGGER.error("Unable to create security timestamp.", e);
    }
}

From source file:org.codice.ddf.security.interceptor.AnonymousInterceptor.java

private void createAddressing(SoapMessage message, SOAPMessage soapMessage, SOAPFactory soapFactory) {

    String addressingProperty = org.apache.cxf.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES_INBOUND;
    AddressingProperties addressingProperties = new AddressingProperties();
    SOAPElement action = null;

    try {/*from   ww  w.  j a  v  a 2  s .co m*/
        action = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_ACTION_NAME,
                org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX,
                org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS);
        action.addTextNode((String) message.get(org.apache.cxf.message.Message.REQUEST_URL));
        AttributedURIType attributedString = new AttributedURIType();
        String actionValue = StringUtils.defaultIfEmpty((String) message.get(SoapBindingConstants.SOAP_ACTION),
                "");
        attributedString.setValue(actionValue);
        addressingProperties.setAction(attributedString);
    } catch (SOAPException e) {
        LOGGER.error("Unable to add addressing action.", e);
    }

    SOAPElement messageId = null;
    try {
        messageId = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_MESSAGEID_NAME,
                org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX,
                org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS);
        String uuid = "urn:uuid:" + UUID.randomUUID().toString();
        messageId.addTextNode(uuid);
        AttributedURIType attributedString = new AttributedURIType();
        attributedString.setValue(uuid);
        addressingProperties.setMessageID(attributedString);
    } catch (SOAPException e) {
        LOGGER.error("Unable to add addressing action.", e);
    }

    SOAPElement to = null;
    try {
        to = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_TO_NAME,
                org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX,
                org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS);
        to.addTextNode((String) message.get(org.apache.cxf.message.Message.REQUEST_URL));
        EndpointReferenceType endpointReferenceType = new EndpointReferenceType();
        AttributedURIType attributedString = new AttributedURIType();
        attributedString.setValue((String) message.get(org.apache.cxf.message.Message.REQUEST_URL));
        endpointReferenceType.setAddress(attributedString);
        addressingProperties.setTo(endpointReferenceType);
    } catch (SOAPException e) {
        LOGGER.error("Unable to add addressing action.", e);
    }

    SOAPElement replyTo = null;
    try {
        replyTo = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_REPLYTO_NAME,
                org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX,
                org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS);
        SOAPElement address = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_ADDRESS_NAME,
                org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX,
                org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS);
        address.addTextNode(org.apache.cxf.ws.addressing.Names.WSA_ANONYMOUS_ADDRESS);
        replyTo.addChildElement(address);

        soapMessage.getSOAPHeader().addChildElement(messageId);
        soapMessage.getSOAPHeader().addChildElement(action);
        soapMessage.getSOAPHeader().addChildElement(to);
        soapMessage.getSOAPHeader().addChildElement(replyTo);
        message.put(addressingProperty, addressingProperties);
    } catch (SOAPException e) {
        LOGGER.error("Unable to add addressing action.", e);
    }
}

From source file:org.codice.ddf.security.interceptor.GuestInterceptor.java

private void createAddressing(SoapMessage message, SOAPMessage soapMessage) {
    SOAPFactory soapFactory;/*from w  w  w.j a va 2s  .  c o  m*/
    try {
        soapFactory = SOAPFactory.newInstance();
    } catch (SOAPException e) {
        LOGGER.error("Could not create a SOAPFactory.", e);
        return; // can't add anything if we can't create it
    }

    String addressingProperty = org.apache.cxf.ws.addressing.JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES_INBOUND;
    AddressingProperties addressingProperties = new AddressingProperties();

    try {
        SOAPElement action = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_ACTION_NAME,
                org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX,
                org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS);
        action.addTextNode((String) message.get(org.apache.cxf.message.Message.REQUEST_URL));
        AttributedURIType attributedString = new AttributedURIType();
        String actionValue = StringUtils.defaultIfEmpty((String) message.get(SoapBindingConstants.SOAP_ACTION),
                "");
        attributedString.setValue(actionValue);
        addressingProperties.setAction(attributedString);
        soapMessage.getSOAPHeader().addChildElement(action);
    } catch (SOAPException e) {
        LOGGER.error("Unable to add addressing action.", e);
    }

    try {
        SOAPElement messageId = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_MESSAGEID_NAME,
                org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX,
                org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS);
        String uuid = "urn:uuid:" + UUID.randomUUID().toString();
        messageId.addTextNode(uuid);
        AttributedURIType attributedString = new AttributedURIType();
        attributedString.setValue(uuid);
        addressingProperties.setMessageID(attributedString);
        soapMessage.getSOAPHeader().addChildElement(messageId);
    } catch (SOAPException e) {
        LOGGER.error("Unable to add addressing messageId.", e);
    }

    try {
        SOAPElement to = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_TO_NAME,
                org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX,
                org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS);
        to.addTextNode((String) message.get(org.apache.cxf.message.Message.REQUEST_URL));
        EndpointReferenceType endpointReferenceType = new EndpointReferenceType();
        AttributedURIType attributedString = new AttributedURIType();
        attributedString.setValue((String) message.get(org.apache.cxf.message.Message.REQUEST_URL));
        endpointReferenceType.setAddress(attributedString);
        addressingProperties.setTo(endpointReferenceType);
        soapMessage.getSOAPHeader().addChildElement(to);
    } catch (SOAPException e) {
        LOGGER.error("Unable to add addressing to.", e);
    }

    try {
        SOAPElement replyTo = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_REPLYTO_NAME,
                org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX,
                org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS);
        SOAPElement address = soapFactory.createElement(org.apache.cxf.ws.addressing.Names.WSA_ADDRESS_NAME,
                org.apache.cxf.ws.addressing.JAXWSAConstants.WSA_PREFIX,
                org.apache.cxf.ws.security.wss4j.DefaultCryptoCoverageChecker.WSA_NS);
        address.addTextNode(org.apache.cxf.ws.addressing.Names.WSA_ANONYMOUS_ADDRESS);
        replyTo.addChildElement(address);
        soapMessage.getSOAPHeader().addChildElement(replyTo);

    } catch (SOAPException e) {
        LOGGER.error("Unable to add addressing replyTo.", e);
    }
    message.put(addressingProperty, addressingProperties);
}

From source file:org.energy_home.jemma.ah.internal.greenathome.GreenathomeAppliance.java

private static SOAPMessage createSOAPRequest(String date) throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String serverURI = "http://ws.i-em.eu/v4/";

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("example", serverURI);

    // SOAP Body//from www. j ava2 s  . co  m
    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement("Get72hPlantForecast", "example");
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("plantID", "example");
    soapBodyElem1.addTextNode("telecom_02");
    SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("quantityID", "example");
    soapBodyElem2.addTextNode("frc_pac");
    SOAPElement soapBodyElem3 = soapBodyElem.addChildElement("timestamp", "example");
    soapBodyElem3.addTextNode(date);
    SOAPElement soapBodyElem4 = soapBodyElem.addChildElement("langID", "example");
    soapBodyElem4.addTextNode("en");

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", serverURI + "Get72hPlantForecast");

    soapMessage.saveChanges();

    return soapMessage;
}

From source file:org.jboss.jaxr.juddi.transport.SaajTransport.java

private void appendElements(SOAPElement bodyElement, NodeList nlist, SOAPFactory factory) throws SOAPException {
    String prefix = "uddi";
    String uddins = IRegistry.UDDI_V2_NAMESPACE;
    int len = nlist != null ? nlist.getLength() : 0;

    for (int i = 0; i < len; i++) {
        Node node = nlist.item(i);
        short nodeType = node != null ? node.getNodeType() : -100;
        if (Node.ELEMENT_NODE == nodeType) {
            Element el = (Element) node;
            Name name = factory.createName(el.getNodeName(), prefix, uddins);
            SOAPElement attachedEl = bodyElement.addChildElement(name);
            appendAttributes(attachedEl, el.getAttributes(), factory);
            appendElements(attachedEl, el.getChildNodes(), factory);
        } else if (nodeType == Node.TEXT_NODE) {
            bodyElement.addTextNode(node.getNodeValue());
        }// w  ww  .  j a v  a 2s.  com
    }
}

From source file:org.jboss.jaxr.juddi.transport.WS4EESaajTransport.java

private SOAPElement getSOAPElement(SOAPBody soapBody, Element elem) {
    String xmlns = IRegistry.UDDI_V2_NAMESPACE;
    SOAPElement soapElement = null;
    SOAPFactory factory = null;/*from  www.  jav  a  2 s.c  o m*/
    try {
        factory = SOAPFactory.newInstance();
        //Go through the element
        String name = elem.getNodeName();
        String nsuri = elem.getNamespaceURI();
        if (nsuri == null)
            nsuri = xmlns;
        soapElement = factory.createElement(name, "ns1", nsuri);
        //Get Attributes
        if (elem.hasAttributes()) {
            NamedNodeMap nnm = elem.getAttributes();
            int len = nnm != null ? nnm.getLength() : 0;
            for (int i = 0; i < len; i++) {
                Node n = nnm.item(i);
                String nodename = n.getNodeName();
                String nodevalue = n.getNodeValue();
                soapElement.addAttribute(factory.createName(nodename), nodevalue);
            }
        } else {
            soapElement.addAttribute(factory.createName("xmlns:ns1"), nsuri);
        }

        NodeList nlist = elem.getChildNodes();
        int len = nlist != null ? nlist.getLength() : 0;

        for (int i = 0; i < len; i++) {
            Node node = nlist.item(i);
            short nodeType = node != null ? node.getNodeType() : -100;
            if (Node.ELEMENT_NODE == nodeType) {
                soapElement.addChildElement(getSOAPElement(soapBody, (Element) node));
            } else if (nodeType == Node.TEXT_NODE) {
                soapElement.addTextNode(node.getNodeValue());
            }

        }
    } catch (SOAPException e) {
        e.printStackTrace();
    }
    return soapElement;
}

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

public static void copyChildNodes(SOAPElement target, Element source) throws SOAPException {
    // easy way out: no child nodes
    if (!source.hasChildNodes())
        return;//  w ww . j  a  v a  2s. c om
    // traverse child nodes
    for (Node child = source.getFirstChild(); child != null; child = child.getNextSibling()) {
        switch (child.getNodeType()) {
        case Node.ELEMENT_NODE: {
            copyChildElement(target, (Element) child);
            break;
        }
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE: {
            String text = child.getNodeValue();
            // drop whitespace-only text nodes
            if (!StringUtils.isWhitespace(text)) {
                target.addTextNode(text);
                if (traceEnabled)
                    log.trace("appended text: " + text);
            }
            break;
        }
        default:
            log.debug("discarding child: " + child);
        }
    }
}

From source file:org.mule.transport.soap.axis.style.DefaultMessageService.java

public void soapRequestResponse(SOAPEnvelope req, SOAPEnvelope resp) throws SOAPException {
    // Echo back/*from   w w  w  . j  a v  a2 s . c om*/
    logger.debug("envelopeTest Called");
    SOAPBody body = resp.getBody();
    Name ns0 = resp.createName("TestNS0", "ns0", "http://example.com");
    Name ns1 = resp.createName("TestNS1", "ns1", "http://example.com");
    SOAPElement bodyElmnt = body.addBodyElement(ns0);
    SOAPElement el = bodyElmnt.addChildElement(ns1);
    el.addTextNode("TEST RESPONSE");
}

From source file:org.openhab.binding.fritzboxtr064.internal.Tr064Comm.java

/**
 * Fetches the values for the given item configurations from the FritzBox. Calls
 * the FritzBox SOAP services delivering the values for the item configurations.
 * The resulting map contains the values of all item configurations returned by
 * the invoked services. This can be more items than were given as parameter.
 *
 * @param request//  w ww  . j ava 2 s.  c o m
 *            string from config including the command and optional parameters
 * @return Parsed values for all item configurations returned by the invoked
 *         services.
 */
public Map<ItemConfiguration, String> getTr064Values(Collection<ItemConfiguration> itemConfigurations) {
    Map<ItemConfiguration, String> values = new HashMap<>();

    for (ItemConfiguration itemConfiguration : itemConfigurations) {

        String itemCommand = itemConfiguration.getItemCommand();

        if (values.containsKey(itemCommand)) {
            // item value already read by earlier MultiItemMap
            continue;
        }

        // search for proper item Mapping
        ItemMap itemMap = determineItemMappingByItemCommand(itemCommand);

        if (itemMap == null) {
            logger.warn("No item mapping found for {}. Function not implemented by your FritzBox (?)",
                    itemConfiguration);
            continue;
        }

        // determine which url etc. to connect to for accessing required value
        Tr064Service tr064service = determineServiceByItemMapping(itemMap);

        // construct soap Body which is added to soap msg later
        SOAPBodyElement bodyData = null; // holds data to be sent to fbox
        try {
            MessageFactory mf = MessageFactory.newInstance();
            SOAPMessage msg = mf.createMessage(); // empty message
            SOAPBody body = msg.getSOAPBody(); // std. SAOP body
            QName bodyName = new QName(tr064service.getServiceType(), itemMap.getReadServiceCommand(), "u"); // header
                                                                                                             // for
                                                                                                             // body
                                                                                                             // element
            bodyData = body.addBodyElement(bodyName);
            // only if input parameter is present
            if (itemMap instanceof ParametrizedItemMap) {
                for (InputArgument inputArgument : ((ParametrizedItemMap) itemMap)
                        .getConfigInputArguments(itemConfiguration)) {
                    String dataInName = inputArgument.getName();
                    String dataInValue = inputArgument.getValue();
                    QName dataNode = new QName(dataInName);
                    SOAPElement beDataNode = bodyData.addChildElement(dataNode);
                    // if input is mac address, replace "-" with ":" as fbox wants
                    if (itemConfiguration.getItemCommand().equals("maconline")) {
                        dataInValue = dataInValue.replaceAll("-", ":");
                    }
                    beDataNode.addTextNode(dataInValue); // add data which should be requested from fbox for this
                                                         // service
                }

            }
            logger.trace("Raw SOAP Request to be sent to FritzBox: {}", soapToString(msg));

        } catch (Exception e) {
            logger.warn("Error constructing request SOAP msg for getting parameter. {}", e.getMessage());
            logger.debug("Request was: {}", itemConfiguration);
        }

        if (bodyData == null) {
            logger.warn("Could not determine data to be sent to FritzBox!");
            return null;
        }

        SOAPMessage smTr064Request = constructTr064Msg(bodyData); // construct entire msg with body element
        String soapActionHeader = tr064service.getServiceType() + "#" + itemMap.getReadServiceCommand(); // needed
                                                                                                         // to be
                                                                                                         // sent
                                                                                                         // with
                                                                                                         // request
                                                                                                         // (not
                                                                                                         // in body
                                                                                                         // ->
                                                                                                         // header)
        SOAPMessage response = readSoapResponse(soapActionHeader, smTr064Request,
                _url + tr064service.getControlUrl());
        logger.trace("Raw SOAP Response from FritzBox: {}", soapToString(response));
        if (response == null) {
            logger.warn("Error retrieving SOAP response from FritzBox");
            continue;
        }

        values.putAll(
                itemMap.getSoapValueParser().parseValuesFromSoapMessage(response, itemMap, itemConfiguration));
    }

    return values;
}

From source file:org.openhab.binding.fritzboxtr064.internal.Tr064Comm.java

/**
 * Sets a parameter in fbox. Called from event bus.
 *
 * @param request/*from w  w w.j  a va 2  s. co m*/
 *            config string from itemconfig
 * @param cmd
 *            command to set
 */
public void setTr064Value(ItemConfiguration request, Command cmd) {
    String itemCommand = request.getItemCommand();

    // search for proper item Mapping
    ItemMap itemMapForCommand = determineItemMappingByItemCommand(itemCommand);

    if (!(itemMapForCommand instanceof WritableItemMap)) {
        logger.warn("Item command {} does not support setting values", itemCommand);
        return;
    }
    WritableItemMap itemMap = (WritableItemMap) itemMapForCommand;

    Tr064Service tr064service = determineServiceByItemMapping(itemMap);

    // determine which url etc. to connect to for accessing required value
    // construct soap Body which is added to soap msg later
    SOAPBodyElement bodyData = null; // holds data to be sent to fbox
    try {
        MessageFactory mf = MessageFactory.newInstance();
        SOAPMessage msg = mf.createMessage(); // empty message
        SOAPBody body = msg.getSOAPBody(); // std. SAOP body
        QName bodyName = new QName(tr064service.getServiceType(), itemMap.getWriteServiceCommand(), "u"); // header
                                                                                                          // for
                                                                                                          // body
                                                                                                          // element
        bodyData = body.addBodyElement(bodyName);

        List<InputArgument> writeInputArguments = new ArrayList<>();
        writeInputArguments.add(itemMap.getWriteInputArgument(cmd));
        if (itemMap instanceof ParametrizedItemMap) {
            writeInputArguments.addAll(((ParametrizedItemMap) itemMap).getConfigInputArguments(request));
        }

        for (InputArgument inputArgument : writeInputArguments) {
            QName dataNode = new QName(inputArgument.getName());
            SOAPElement beDataNode = bodyData.addChildElement(dataNode);
            beDataNode.addTextNode(inputArgument.getValue());
        }

        logger.debug("SOAP Msg to send to FritzBox for setting data: {}", soapToString(msg));

    } catch (Exception e) {
        logger.error("Error constructing request SOAP msg for setting parameter. {}", e.getMessage());
        logger.debug("Request was: {}. Command was: {}.", request, cmd.toString());
    }

    if (bodyData == null) {
        logger.error("Could not determine data to be sent to FritzBox!");
        return;
    }

    SOAPMessage smTr064Request = constructTr064Msg(bodyData); // construct entire msg with body element
    String soapActionHeader = tr064service.getServiceType() + "#" + itemMap.getWriteServiceCommand(); // needed to
                                                                                                      // be sent
                                                                                                      // with
                                                                                                      // request
                                                                                                      // (not in
                                                                                                      // body ->
                                                                                                      // header)
    SOAPMessage response = readSoapResponse(soapActionHeader, smTr064Request,
            _url + tr064service.getControlUrl());
    if (response == null) {
        logger.error("Error retrieving SOAP response from FritzBox");
        return;
    }
    logger.debug("SOAP response from FritzBox: {}", soapToString(response));

    // Check if error received
    try {
        if (response.getSOAPBody().getFault() != null) {
            logger.error("Error received from FritzBox while trying to set parameter");
            logger.debug("Soap Response was: {}", soapToString(response));
        }
    } catch (SOAPException e) {
        logger.error("Could not parse soap response from FritzBox while setting parameter. {}", e.getMessage());
        logger.debug("Soap Response was: {}", soapToString(response));
    }

}