Search in sources :

Example 81 with OMAttribute

use of org.apache.axiom.om.OMAttribute in project ballerina by ballerina-lang.

the class BXMLItem method removeAttribute.

/**
 * {@inheritDoc}
 */
@Override
public void removeAttribute(String qname) {
    if (nodeType != XMLNodeType.ELEMENT || qname.isEmpty()) {
        return;
    }
    OMElement omElement = (OMElement) omNode;
    OMAttribute attribute = omElement.getAttribute(getQname(qname));
    if (attribute == null) {
        return;
    }
    omElement.removeAttribute(attribute);
}
Also used : OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 82 with OMAttribute

use of org.apache.axiom.om.OMAttribute in project ballerina by ballerina-lang.

the class BXMLItem method setAttributes.

/**
 * {@inheritDoc}
 */
@Override
public void setAttributes(BMap<String, ?> attributes) {
    if (nodeType != XMLNodeType.ELEMENT || attributes == null) {
        return;
    }
    // Remove existing attributes
    OMElement omElement = ((OMElement) omNode);
    Iterator<OMAttribute> attrIterator = omElement.getAllAttributes();
    while (attrIterator.hasNext()) {
        omElement.removeAttribute(attrIterator.next());
    }
    // Remove existing namespace declarations
    Iterator<OMNamespace> namespaceIterator = omElement.getAllDeclaredNamespaces();
    while (namespaceIterator.hasNext()) {
        namespaceIterator.next();
        namespaceIterator.remove();
    }
    String localName, uri;
    Set<String> attributeQNames = attributes.keySet();
    for (String qname : attributeQNames) {
        if (qname.startsWith("{") && qname.indexOf('}') > 0) {
            localName = qname.substring(qname.indexOf('}') + 1, qname.length());
            uri = qname.substring(1, qname.indexOf('}'));
        } else {
            localName = qname;
            uri = STRING_NULL_VALUE;
        }
        // Validate whether the attribute name is an XML supported qualified name,
        // according to the XML recommendation.
        XMLValidationUtils.validateXMLName(localName);
        setAttribute(localName, uri, STRING_NULL_VALUE, attributes.get(qname).stringValue());
    }
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 83 with OMAttribute

use of org.apache.axiom.om.OMAttribute in project ofbiz-framework by apache.

the class SOAPEventHandler method createAndSendSOAPResponse.

private void createAndSendSOAPResponse(Map<String, Object> serviceResults, String serviceName, HttpServletResponse response) throws EventHandlerException {
    try {
        // setup the response
        if (Debug.verboseOn())
            Debug.logVerbose("[EventHandler] : Setting up response message", module);
        String xmlResults = SoapSerializer.serialize(serviceResults);
        // Debug.logInfo("xmlResults ==================" + xmlResults, module);
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xmlResults));
        StAXOMBuilder resultsBuilder = (StAXOMBuilder) OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), reader);
        OMElement resultSer = resultsBuilder.getDocumentElement();
        // create the response soap
        SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
        SOAPEnvelope resEnv = factory.createSOAPEnvelope();
        SOAPBody resBody = factory.createSOAPBody();
        OMElement resService = factory.createOMElement(new QName(serviceName + "Response"));
        resService.addChild(resultSer.getFirstElement());
        resBody.addChild(resService);
        resEnv.addChild(resBody);
        // The declareDefaultNamespace method doesn't work see (https://issues.apache.org/jira/browse/AXIS2-3156)
        // so the following doesn't work:
        // resService.declareDefaultNamespace(ModelService.TNS);
        // instead, create the xmlns attribute directly:
        OMAttribute defaultNS = factory.createOMAttribute("xmlns", null, ModelService.TNS);
        resService.addAttribute(defaultNS);
        // log the response message
        if (Debug.verboseOn()) {
            try {
                Debug.logInfo("Response Message:\n" + resEnv + "\n", module);
            } catch (Throwable t) {
            }
        }
        resEnv.serialize(response.getOutputStream());
        response.getOutputStream().flush();
    } catch (Exception e) {
        Debug.logError(e, module);
        throw new EventHandlerException(e.getMessage(), e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) IOException(java.io.IOException) WSDLException(javax.wsdl.WSDLException) SOAPBody(org.apache.axiom.soap.SOAPBody) StringReader(java.io.StringReader) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 84 with OMAttribute

use of org.apache.axiom.om.OMAttribute in project ofbiz-framework by apache.

the class SOAPEventHandler method sendError.

private void sendError(HttpServletResponse res, Object object, String serviceName) throws EventHandlerException {
    try {
        // setup the response
        res.setContentType("text/xml");
        String xmlResults = SoapSerializer.serialize(object);
        XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xmlResults));
        StAXOMBuilder resultsBuilder = (StAXOMBuilder) OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), xmlReader);
        OMElement resultSer = resultsBuilder.getDocumentElement();
        // create the response soap
        SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
        SOAPEnvelope resEnv = factory.createSOAPEnvelope();
        SOAPBody resBody = factory.createSOAPBody();
        OMElement errMsg = factory.createOMElement(new QName((serviceName != null ? serviceName : "") + "Response"));
        errMsg.addChild(resultSer.getFirstElement());
        resBody.addChild(errMsg);
        resEnv.addChild(resBody);
        // The declareDefaultNamespace method doesn't work see (https://issues.apache.org/jira/browse/AXIS2-3156)
        // so the following doesn't work:
        // resService.declareDefaultNamespace(ModelService.TNS);
        // instead, create the xmlns attribute directly:
        OMAttribute defaultNS = factory.createOMAttribute("xmlns", null, ModelService.TNS);
        errMsg.addAttribute(defaultNS);
        // log the response message
        if (Debug.verboseOn()) {
            try {
                Debug.logInfo("Response Message:\n" + resEnv + "\n", module);
            } catch (Throwable t) {
            }
        }
        resEnv.serialize(res.getOutputStream());
        res.getOutputStream().flush();
    } catch (Exception e) {
        throw new EventHandlerException(e.getMessage(), e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) SOAPFactory(org.apache.axiom.soap.SOAPFactory) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) IOException(java.io.IOException) WSDLException(javax.wsdl.WSDLException) SOAPBody(org.apache.axiom.soap.SOAPBody) StringReader(java.io.StringReader) StAXOMBuilder(org.apache.axiom.om.impl.builder.StAXOMBuilder) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 85 with OMAttribute

use of org.apache.axiom.om.OMAttribute in project webservices-axiom by apache.

the class OMAttributeHelperTest method testDetachedElement.

public void testDetachedElement() {
    OMNamespace top = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory().createOMNamespace("urn:test1", "t1");
    OMElement ome = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory().createOMElement("test", top);
    OMElement child = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory().createOMElement(new QName("test"), ome);
    OMAttribute oma = child.addAttribute("attr", "value", top);
    OMElement target = OMAbstractFactory.getOMFactory().createOMElement("test2", "urn:test2", "t2");
    AttributeHelper.importOMAttribute(oma, target);
}
Also used : OMNamespace(org.apache.axiom.om.OMNamespace) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement) OMAttribute(org.apache.axiom.om.OMAttribute)

Aggregations

OMAttribute (org.apache.axiom.om.OMAttribute)187 OMElement (org.apache.axiom.om.OMElement)116 QName (javax.xml.namespace.QName)82 Iterator (java.util.Iterator)41 OMFactory (org.apache.axiom.om.OMFactory)31 OMNamespace (org.apache.axiom.om.OMNamespace)28 SynapseException (org.apache.synapse.SynapseException)28 JaxenException (org.jaxen.JaxenException)28 OMNode (org.apache.axiom.om.OMNode)13 Value (org.apache.synapse.mediators.Value)12 SynapseXPath (org.apache.synapse.util.xpath.SynapseXPath)11 ArrayList (java.util.ArrayList)8 Endpoint (org.apache.synapse.endpoints.Endpoint)7 IOException (java.io.IOException)6 List (java.util.List)6 OMText (org.apache.axiom.om.OMText)6 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)6 StringReader (java.io.StringReader)5 SOAPHeaderBlock (org.apache.axiom.soap.SOAPHeaderBlock)5 EndpointDefinition (org.apache.synapse.endpoints.EndpointDefinition)5