Example usage for javax.xml.soap SOAPFactory createName

List of usage examples for javax.xml.soap SOAPFactory createName

Introduction

In this page you can find the example usage for javax.xml.soap SOAPFactory createName.

Prototype

public abstract Name createName(String localName, String prefix, String uri) throws SOAPException;

Source Link

Document

Creates a new Name object initialized with the given local name, namespace prefix, and namespace URI.

Usage

From source file:org.apache.ws.scout.transport.SaajTransport.java

private SOAPMessage createSOAPMessage(Element elem) throws Exception {
    String prefix = "";
    MessageFactory msgFactory = MessageFactory.newInstance();
    SOAPFactory factory = SOAPFactory.newInstance();

    SOAPMessage message = msgFactory.createMessage();
    message.getSOAPHeader().detachNode();
    SOAPPart soapPart = message.getSOAPPart();
    SOAPBody soapBody = soapPart.getEnvelope().getBody();
    //Create the outer body element
    Name bodyName = factory.createName(elem.getNodeName(), prefix, UDDI_V2_NAMESPACE);
    SOAPBodyElement bodyElement = soapBody.addBodyElement(bodyName);
    bodyElement.addNamespaceDeclaration(prefix, UDDI_V2_NAMESPACE);
    appendAttributes(bodyElement, elem.getAttributes(), factory);
    appendElements(bodyElement, elem.getChildNodes(), factory);
    return message;
}

From source file:org.apache.ws.scout.transport.SaajTransport.java

private void appendAttributes(SOAPElement bodyElement, NamedNodeMap nnm, SOAPFactory factory)
        throws SOAPException {
    int len = nnm != null ? nnm.getLength() : 0;
    for (int i = 0; i < len; i++) {
        Node n = nnm.item(i);//from  w  ww . jav  a2 s  .  co m
        String nodename = n.getNodeName();
        String nodevalue = n.getNodeValue();
        if ("xmlns".equals(nodename))
            continue;
        if (nodename.startsWith("xmlns:"))
            continue;
        //Special case:  xml:lang
        if ("xml:lang".equals(nodename)) {
            Name xmlLang = factory.createName("lang", "xml", "");
            bodyElement.addAttribute(xmlLang, nodevalue);
        } else
            bodyElement.addAttribute(factory.createName(nodename), nodevalue);
    }
}

From source file:org.apache.ws.scout.transport.SaajTransport.java

private void appendElements(SOAPElement bodyElement, NodeList nlist, SOAPFactory factory) throws SOAPException {
    String prefix = "";
    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, UDDI_V2_NAMESPACE);
            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());
        }//from w w w .j  a  v a 2 s  . c  o  m
    }
}

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

private SOAPMessage createSOAPMessage(Element elem) throws Exception {
    String prefix = "uddi";
    MessageFactory msgFactory = MessageFactory.newInstance();
    SOAPFactory factory = SOAPFactory.newInstance();

    SOAPMessage message = msgFactory.createMessage();
    message.getSOAPHeader().detachNode();
    SOAPPart soapPart = message.getSOAPPart();
    SOAPBody soapBody = soapPart.getEnvelope().getBody();
    //Create the outer body element
    String uddins = IRegistry.UDDI_V2_NAMESPACE;
    Name bodyName = factory.createName(elem.getNodeName(), prefix, uddins);
    SOAPBodyElement bodyElement = soapBody.addBodyElement(bodyName);
    bodyElement.addNamespaceDeclaration(prefix, uddins);
    appendAttributes(bodyElement, elem.getAttributes(), factory);
    appendElements(bodyElement, elem.getChildNodes(), factory);
    return message;
}

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

private void appendAttributes(SOAPElement bodyElement, NamedNodeMap nnm, SOAPFactory factory)
        throws SOAPException {
    int len = nnm != null ? nnm.getLength() : 0;
    for (int i = 0; i < len; i++) {
        Node n = nnm.item(i);// w w w.ja  v a  2 s.  com
        String nodename = n.getNodeName();
        String nodevalue = n.getNodeValue();
        if ("xmlns".equals(nodename))
            continue;
        //Special case:  xml:lang
        if ("xml:lang".equals(nodename)) {
            Name xmlLang = factory.createName("lang", "xml", "http://www.w3.org/TR/REC-xml/");
            bodyElement.addAttribute(xmlLang, nodevalue);
        } else
            bodyElement.addAttribute(factory.createName(nodename), nodevalue);
    }
}

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());
        }//from w w  w. ja v  a2  s  .com
    }
}

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

public void testCopyAttributes_soapDom() throws Exception {
    String xml = "<lunch time='1200' produce:lettuce='0.1lb' fish:fillet='0.25lb' "
            + " xmlns:produce='urn:example:produce' xmlns:fish='urn:example:fish'/>";
    Element source = XmlUtil.parseText(xml);
    SOAPFactory soapFactory = SOAPFactory.newInstance();
    SOAPElement target = soapFactory.createElement("detail");
    // perform the copy
    SoapUtil.copyAttributes(target, source);
    // qualified attributes
    assertEquals("0.1lb",
            target.getAttributeValue(soapFactory.createName("lettuce", null, "urn:example:produce")));
    assertEquals("0.25lb",
            target.getAttributeValue(soapFactory.createName("fillet", null, "urn:example:fish")));
    // local attribute
    assertEquals("1200", target.getAttributeValue(soapFactory.createName("time")));
}

From source file:org.wso2.carbon.device.mgt.mobile.windows.api.services.enrollment.util.MessageHandler.java

/**
 * This method adds Timestamp for SOAP header, and adds Content-length for HTTP header for
 * avoiding HTTP chunking./*from  w  ww. j a va 2 s.c o m*/
 *
 * @param context - Context of the SOAP Message
 */
@Override
public boolean handleMessage(SOAPMessageContext context) {

    Boolean outBoundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outBoundProperty) {
        SOAPMessage message = context.getMessage();
        SOAPHeader header = null;
        SOAPEnvelope envelope = null;
        try {
            header = message.getSOAPHeader();
            envelope = message.getSOAPPart().getEnvelope();
        } catch (SOAPException e) {
            Response.serverError().entity("SOAP message content cannot be read.").build();
        }
        try {
            if ((header == null) && (envelope != null)) {
                header = envelope.addHeader();
            }
        } catch (SOAPException e) {
            Response.serverError().entity("SOAP header cannot be added.").build();
        }

        SOAPFactory soapFactory = null;
        try {
            soapFactory = SOAPFactory.newInstance();
        } catch (SOAPException e) {
            Response.serverError().entity("Cannot get an instance of SOAP factory.").build();
        }

        QName qNamesSecurity = new QName(PluginConstants.WS_SECURITY_TARGET_NAMESPACE,
                PluginConstants.CertificateEnrolment.SECURITY);
        SOAPHeaderElement Security = null;
        Name attributeName = null;
        try {
            if (header != null) {
                Security = header.addHeaderElement(qNamesSecurity);
            }
            if (soapFactory != null) {
                attributeName = soapFactory.createName(PluginConstants.CertificateEnrolment.TIMESTAMP_ID,
                        PluginConstants.CertificateEnrolment.TIMESTAMP_U,
                        PluginConstants.CertificateEnrolment.WSS_SECURITY_UTILITY);
            }
        } catch (SOAPException e) {
            Response.serverError().entity("Security header cannot be added.").build();
        }

        QName qNameTimestamp = new QName(PluginConstants.CertificateEnrolment.WSS_SECURITY_UTILITY,
                PluginConstants.CertificateEnrolment.TIMESTAMP);
        SOAPHeaderElement timestamp = null;
        try {
            if (header != null) {
                timestamp = header.addHeaderElement(qNameTimestamp);
                timestamp.addAttribute(attributeName, PluginConstants.CertificateEnrolment.TIMESTAMP_0);
            }
        } catch (SOAPException e) {
            Response.serverError().entity("Exception while adding timestamp header.").build();
        }
        DateTime dateTime = new DateTime();
        DateTime expiredDateTime = dateTime.plusMinutes(VALIDITY_TIME);
        String createdISOTime = dateTime.toString(ISODateTimeFormat.dateTime());
        String expiredISOTime = expiredDateTime.toString(ISODateTimeFormat.dateTime());
        createdISOTime = createdISOTime.substring(TIMESTAMP_BEGIN_INDEX,
                createdISOTime.length() - TIMESTAMP_END_INDEX);
        createdISOTime = createdISOTime + TIME_ZONE;
        expiredISOTime = expiredISOTime.substring(TIMESTAMP_BEGIN_INDEX,
                expiredISOTime.length() - TIMESTAMP_END_INDEX);
        expiredISOTime = expiredISOTime + TIME_ZONE;
        QName qNameCreated = new QName(PluginConstants.CertificateEnrolment.WSS_SECURITY_UTILITY,
                PluginConstants.CertificateEnrolment.CREATED);
        SOAPHeaderElement SOAPHeaderCreated = null;

        try {
            if (header != null) {
                SOAPHeaderCreated = header.addHeaderElement(qNameCreated);
                SOAPHeaderCreated.addTextNode(createdISOTime);
            }
        } catch (SOAPException e) {
            Response.serverError().entity("Exception while creating SOAP header.").build();
        }
        QName qNameExpires = new QName(PluginConstants.CertificateEnrolment.WSS_SECURITY_UTILITY,
                PluginConstants.CertificateEnrolment.EXPIRES);
        SOAPHeaderElement SOAPHeaderExpires = null;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        String messageString = null;
        try {
            if (header != null) {
                SOAPHeaderExpires = header.addHeaderElement(qNameExpires);
                SOAPHeaderExpires.addTextNode(expiredISOTime);
            }
            if ((timestamp != null) && (Security != null)) {
                timestamp.addChildElement(SOAPHeaderCreated);
                timestamp.addChildElement(SOAPHeaderExpires);
                Security.addChildElement(timestamp);
            }
            message.saveChanges();
            message.writeTo(outputStream);
            messageString = new String(outputStream.toByteArray(), PluginConstants.CertificateEnrolment.UTF_8);
        } catch (SOAPException e) {
            Response.serverError().entity("Exception while creating timestamp SOAP header.").build();
        } catch (IOException e) {
            Response.serverError().entity("Exception while writing message to output stream.").build();
        }

        Map<String, List<String>> headers = (Map<String, List<String>>) context
                .get(MessageContext.HTTP_REQUEST_HEADERS);
        headers = new HashMap<String, List<String>>();
        if (messageString != null) {
            headers.put(PluginConstants.CONTENT_LENGTH, Arrays.asList(String.valueOf(messageString.length())));
        }
        context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
    }
    return true;
}

From source file:org.wso2.carbon.mdm.mobileservices.windowspc.services.wstep.util.MessageHandler.java

/**
 * This method adds Timestamp for SOAP header, and adds Content-length for HTTP header for
 * avoiding HTTP chunking./*from   ww  w  . j a  v  a  2  s.c o  m*/
 *
 * @param context
 */
@Override
public boolean handleMessage(SOAPMessageContext context) {

    Boolean outBoundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outBoundProperty) {

        SOAPMessage message = context.getMessage();
        SOAPHeader header = null;
        SOAPEnvelope envelope = null;

        try {
            header = message.getSOAPHeader();
            envelope = message.getSOAPPart().getEnvelope();
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        if (header == null) {
            try {
                header = envelope.addHeader();
            } catch (SOAPException e) {
                Response.serverError().build();
            }
        }
        SOAPFactory soapFactory = null;

        try {
            soapFactory = SOAPFactory.newInstance();
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        QName qNamesSecurity = new QName(Constants.CertificateEnrollment.WS_SECURITY_TARGET_NAMESPACE,
                Constants.CertificateEnrollment.SECURITY);

        SOAPHeaderElement Security = null;

        try {
            Security = header.addHeaderElement(qNamesSecurity);
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        Name attributeName = null;
        try {
            attributeName = soapFactory.createName(Constants.CertificateEnrollment.TIMESTAMP_ID,
                    Constants.CertificateEnrollment.TIMESTAMP_U,
                    Constants.CertificateEnrollment.WSS_SECURITY_UTILITY);
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        QName qNameTimestamp = new QName(Constants.CertificateEnrollment.WSS_SECURITY_UTILITY,
                Constants.CertificateEnrollment.TIMESTAMP);
        SOAPHeaderElement timestamp = null;

        try {
            timestamp = header.addHeaderElement(qNameTimestamp);
            timestamp.addAttribute(attributeName, Constants.CertificateEnrollment.TIMESTAMP_0);
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        DateTime dateTime = new DateTime();
        DateTime expiredDateTime = dateTime.plusMinutes(5);
        String createdISOTime = dateTime.toString(ISODateTimeFormat.dateTime());
        String expiredISOTime = expiredDateTime.toString(ISODateTimeFormat.dateTime());
        createdISOTime = createdISOTime.substring(0, createdISOTime.length() - 6);
        createdISOTime = createdISOTime + "Z";
        expiredISOTime = expiredISOTime.substring(0, expiredISOTime.length() - 6);
        expiredISOTime = expiredISOTime + "Z";

        QName qNameCreated = new QName(Constants.CertificateEnrollment.WSS_SECURITY_UTILITY,
                Constants.CertificateEnrollment.CREATED);
        SOAPHeaderElement SOAPHeaderCreated = null;

        try {
            SOAPHeaderCreated = header.addHeaderElement(qNameCreated);
            SOAPHeaderCreated.addTextNode(createdISOTime);
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        QName qNameExpires = new QName(Constants.CertificateEnrollment.WSS_SECURITY_UTILITY,
                Constants.CertificateEnrollment.EXPIRES);
        SOAPHeaderElement SOAPHeaderExpires = null;

        try {
            SOAPHeaderExpires = header.addHeaderElement(qNameExpires);
            SOAPHeaderExpires.addTextNode(expiredISOTime);
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        try {
            timestamp.addChildElement(SOAPHeaderCreated);
            timestamp.addChildElement(SOAPHeaderExpires);
            Security.addChildElement(timestamp);
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        try {
            message.saveChanges();
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        try {
            message.writeTo(outputStream);
        } catch (IOException e) {
            Response.serverError().build();
        } catch (SOAPException e) {
            Response.serverError().build();
        }

        String messageString = null;
        try {
            messageString = new String(outputStream.toByteArray(), Constants.CertificateEnrollment.UTF_8);
        } catch (UnsupportedEncodingException e) {
            Response.serverError().build();
        }

        Map<String, List<String>> headers = (Map<String, List<String>>) context
                .get(MessageContext.HTTP_REQUEST_HEADERS);
        headers = new HashMap<String, List<String>>();
        headers.put(Constants.CertificateEnrollment.CONTENT_LENGTH,
                Arrays.asList(String.valueOf(messageString.length())));
        context.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

    }
    return true;
}