use of org.apache.axiom.om.OMAttribute in project carbon-business-process by wso2.
the class SOAPUtils method convertSOAP12toSOAP11.
/**
* Converts the version of the the message context to 1.1.
* <br />
* <b>Message Changes:</b>
* <ol>
* <li>Convert envelope, header elements</li>
* <li>For each header block convert attribute role to actor</li>
* <li>For each header block convert mustUnderstand value type</li>
* <li>For each header block remove 1.2 namespaced other attributes</li>
* </ol>
*
* <b>Fault Changes:</b>
* <ol>
* <li>Convert fault element</li>
* <li>Fault/Code to faultcode</li>
* <li>First Fault/Reason/Text to faultstring</li>
* </ol>
* @param axisOutMsgCtx message context to be converted
* @throws AxisFault in case of an error in conversion
*/
public static void convertSOAP12toSOAP11(org.apache.axis2.context.MessageContext axisOutMsgCtx) throws AxisFault {
if (log.isDebugEnabled()) {
log.debug("convert SOAP12 to SOAP11");
}
SOAPEnvelope oldEnvelope = axisOutMsgCtx.getEnvelope();
SOAPFactory soap11Factory = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope newEnvelope = soap11Factory.getDefaultEnvelope();
if (oldEnvelope.getHeader() != null) {
Iterator itr = oldEnvelope.getHeader().getChildren();
while (itr.hasNext()) {
OMNode omNode = (OMNode) itr.next();
if (omNode instanceof SOAPHeaderBlock) {
SOAPHeaderBlock soapHeaderBlock = (SOAPHeaderBlock) omNode;
SOAPHeaderBlock newSOAPHeader = soap11Factory.createSOAPHeaderBlock(soapHeaderBlock.getLocalName(), soapHeaderBlock.getNamespace());
Iterator allAttributes = soapHeaderBlock.getAllAttributes();
while (allAttributes.hasNext()) {
OMAttribute attr = (OMAttribute) allAttributes.next();
if (attr.getNamespace() != null && SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(attr.getNamespace().getNamespaceURI())) {
String attrName = attr.getLocalName();
if (SOAP_ATR_ROLE.equals(attrName)) {
OMAttribute newAtr = omNode.getOMFactory().createOMAttribute(SOAP_ATR_ACTOR, newEnvelope.getNamespace(), attr.getAttributeValue());
newSOAPHeader.addAttribute(newAtr);
} else if (SOAP_ATR_MUST_UNDERSTAND.equals(attrName)) {
boolean isMustUnderstand = soapHeaderBlock.getMustUnderstand();
newSOAPHeader.setMustUnderstand(isMustUnderstand);
} else {
log.warn("removed unsupported attribute from SOAP 1.2 " + "namespace when converting to SOAP 1.1:" + attrName);
}
} else {
newSOAPHeader.addAttribute(attr);
}
Iterator itrChildren = soapHeaderBlock.getChildren();
while (itrChildren.hasNext()) {
OMNode node = (OMNode) itrChildren.next();
itrChildren.remove();
newSOAPHeader.addChild(node);
}
newEnvelope.getHeader().addChild(newSOAPHeader);
}
} else {
itr.remove();
newEnvelope.getHeader().addChild(omNode);
}
}
}
if (oldEnvelope.getBody() != null) {
if (oldEnvelope.hasFault()) {
SOAPFault soapFault = oldEnvelope.getBody().getFault();
SOAPFault newSOAPFault = soap11Factory.createSOAPFault();
newEnvelope.getBody().addChild(newSOAPFault);
SOAPFaultCode code = soapFault.getCode();
if (code != null) {
SOAPFaultCode newSOAPFaultCode = soap11Factory.createSOAPFaultCode(newSOAPFault);
SOAPFaultValue value = code.getValue();
if (value != null) {
// get the corresponding SOAP12 fault code
// for the provided SOAP11 fault code
soap11Factory.createSOAPFaultValue(newSOAPFaultCode);
if (value.getTextAsQName() != null) {
newSOAPFaultCode.setText(getMappingSOAP11Code(value.getTextAsQName()));
}
}
}
SOAPFaultReason reason = soapFault.getReason();
if (reason != null) {
SOAPFaultReason newSOAPFaultReason = soap11Factory.createSOAPFaultReason(newSOAPFault);
List allSoapTexts = reason.getAllSoapTexts();
Iterator iterAllSoapTexts = allSoapTexts.iterator();
if (iterAllSoapTexts.hasNext()) {
SOAPFaultText soapFaultText = (SOAPFaultText) iterAllSoapTexts.next();
iterAllSoapTexts.remove();
newSOAPFaultReason.setText(soapFaultText.getText());
}
}
SOAPFaultDetail detail = soapFault.getDetail();
if (detail != null) {
SOAPFaultDetail newSOAPFaultDetail = soap11Factory.createSOAPFaultDetail(newSOAPFault);
Iterator<OMElement> iter = detail.getAllDetailEntries();
while (iter.hasNext()) {
OMElement detailEntry = iter.next();
iter.remove();
newSOAPFaultDetail.addDetailEntry(detailEntry);
}
newSOAPFault.setDetail(newSOAPFaultDetail);
}
} else {
Iterator itr = oldEnvelope.getBody().getChildren();
while (itr.hasNext()) {
OMNode omNode = (OMNode) itr.next();
if (omNode != null) {
itr.remove();
newEnvelope.getBody().addChild(omNode);
}
}
}
}
axisOutMsgCtx.setEnvelope(newEnvelope);
}
use of org.apache.axiom.om.OMAttribute in project carbon-business-process by wso2.
the class OMUtils method toDOM.
public static Element toDOM(OMElement element, Document doc, boolean deepNS) {
final Element domElement = doc.createElementNS(element.getQName().getNamespaceURI(), element.getQName().getLocalPart());
if (deepNS) {
NSContext nscontext = new NSContext();
buildNScontext(nscontext, element);
DOMUtils.injectNamespaces(domElement, nscontext);
} else {
if (element.getAllDeclaredNamespaces() != null) {
for (Iterator i = element.getAllDeclaredNamespaces(); i.hasNext(); ) {
OMNamespace omns = (OMNamespace) i.next();
if (omns.getPrefix().equals("")) {
domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns", omns.getNamespaceURI() == null ? "" : omns.getNamespaceURI());
} else {
domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + omns.getPrefix(), omns.getNamespaceURI());
}
}
}
}
for (Iterator i = element.getAllAttributes(); i.hasNext(); ) {
final OMAttribute attr = (OMAttribute) i.next();
Attr newAttr;
if (attr.getNamespace() != null) {
newAttr = doc.createAttributeNS(attr.getNamespace().getNamespaceURI(), attr.getLocalName());
} else {
newAttr = doc.createAttributeNS(null, attr.getLocalName());
}
newAttr.appendChild(doc.createTextNode(attr.getAttributeValue()));
domElement.setAttributeNodeNS(newAttr);
// Case of qualified attribute values, we're forced to add corresponding namespace declaration manually...
int colonIdx = attr.getAttributeValue().indexOf(":");
if (colonIdx > 0) {
OMNamespace attrValNs = element.findNamespaceURI(attr.getAttributeValue().substring(0, colonIdx));
if (attrValNs != null) {
domElement.setAttributeNS(DOMUtils.NS_URI_XMLNS, "xmlns:" + attrValNs.getPrefix(), attrValNs.getNamespaceURI());
}
}
}
for (Iterator i = element.getChildren(); i.hasNext(); ) {
OMNode omn = (OMNode) i.next();
switch(omn.getType()) {
case OMNode.CDATA_SECTION_NODE:
domElement.appendChild(doc.createCDATASection(((OMText) omn).getText()));
break;
case OMNode.TEXT_NODE:
domElement.appendChild(doc.createTextNode(((OMText) omn).getText()));
break;
case OMNode.ELEMENT_NODE:
domElement.appendChild(toDOM((OMElement) omn, doc, false));
break;
}
}
return domElement;
}
use of org.apache.axiom.om.OMAttribute in project carbon-business-process by wso2.
the class ServiceConfigurationUtil method loadAndEmbedPolicy.
private static void loadAndEmbedPolicy(OMElement serviceElement, EndpointConfiguration endpointConfiguration) {
Iterator serviceIterator = serviceElement.getChildrenWithLocalName("service");
while (serviceIterator.hasNext()) {
OMElement nextService = (OMElement) serviceIterator.next();
OMElement policy = nextService.getFirstChildWithName(new QName("policy"));
if (policy != null) {
OMAttribute key = policy.getAttribute(new QName("key"));
if (key != null && null != key.getAttributeValue()) {
String location = key.getAttributeValue();
OMElement policyElement = null;
if (location.startsWith(UnifiedEndpointConstants.VIRTUAL_CONF_REG) || location.startsWith(UnifiedEndpointConstants.VIRTUAL_GOV_REG) || location.startsWith(UnifiedEndpointConstants.VIRTUAL_REG)) {
policyElement = readOMElementFromRegistry(location);
} else {
if (location.startsWith(UnifiedEndpointConstants.VIRTUAL_FILE)) {
// load the policy file from file system
location = location.substring(UnifiedEndpointConstants.VIRTUAL_FILE.length());
}
if (!EndpointConfiguration.isAbsolutePath(location)) {
location = endpointConfiguration.getBasePath() + File.separator + location;
}
policyElement = readOMElementFromFile(location);
}
if (policyElement != null) {
policy.detach();
nextService.addChild(policyElement);
if (log.isDebugEnabled()) {
log.debug(" Processed Service descriptor : " + serviceElement.toString());
}
}
} else {
if (log.isDebugEnabled()) {
log.debug("Policy file not correctly specified when referring using <policy key=... syntax");
}
}
}
}
}
use of org.apache.axiom.om.OMAttribute in project ballerina by ballerina-lang.
the class XMLUtils method collectAttributesAndNamespaces.
/**
* Extract attributes and namespaces from the XML element.
*
* @param element XML element to extract attributes and namespaces
*/
@SuppressWarnings("rawtypes")
private static LinkedHashMap<String, String> collectAttributesAndNamespaces(OMElement element, boolean preserveNamespaces) {
// Extract namespaces from the element
LinkedHashMap<String, String> attributeMap = new LinkedHashMap<>();
if (preserveNamespaces) {
Iterator namespaceIterator = element.getAllDeclaredNamespaces();
while (namespaceIterator.hasNext()) {
OMNamespace namespace = (OMNamespace) namespaceIterator.next();
attributeMap.put(XML_NAMESPACE_PREFIX + namespace.getPrefix(), namespace.getNamespaceURI());
}
}
// Extract attributes from the element
Iterator attributeIterator = element.getAllAttributes();
while (attributeIterator.hasNext()) {
OMAttribute attribute = (OMAttribute) attributeIterator.next();
StringBuffer key = new StringBuffer();
if (preserveNamespaces) {
String prefix = attribute.getPrefix();
if (prefix != null) {
key.append(prefix).append(":");
}
}
key.append(attribute.getLocalName());
attributeMap.put(key.toString(), attribute.getAttributeValue());
}
return attributeMap;
}
use of org.apache.axiom.om.OMAttribute in project ballerina by ballerina-lang.
the class BXMLItem method setAttribute.
/**
* {@inheritDoc}
*/
@Override
public void setAttribute(String localName, String namespaceUri, String prefix, String value) {
if (nodeType != XMLNodeType.ELEMENT) {
return;
}
if (localName == null || localName.isEmpty()) {
throw new BLangRuntimeException("localname of the attribute cannot be empty");
}
// Validate whether the attribute name is an XML supported qualified name, according to the XML recommendation.
XMLValidationUtils.validateXMLName(localName);
XMLValidationUtils.validateXMLName(prefix);
// If the attribute already exists, update the value.
OMElement node = (OMElement) omNode;
QName qname = getQName(localName, namespaceUri, prefix);
OMAttribute attr = node.getAttribute(qname);
if (attr != null) {
attr.setAttributeValue(value);
return;
}
// If the prefix is 'xmlns' then this is a namespace addition
if (prefix != null && prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
node.declareNamespace(value, localName);
return;
}
// If the namespace is null/empty, only the local part exists. Therefore add a simple attribute.
if (namespaceUri == null || namespaceUri.isEmpty()) {
attr = new OMAttributeImpl();
attr.setAttributeValue(value);
attr.setLocalName(localName);
node.addAttribute(attr);
return;
}
// treat this attribute-add operation as a namespace addition.
if ((node.getDefaultNamespace() != null && namespaceUri.equals(node.getDefaultNamespace().getNamespaceURI())) || namespaceUri.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
node.declareNamespace(value, localName);
return;
}
OMNamespace ns = null;
if (prefix != null && !prefix.isEmpty()) {
OMNamespace existingNs = node.findNamespaceURI(prefix);
// If a namespace exists with the same prefix but a different uri, then do not add the new attribute.
if (existingNs != null && !namespaceUri.equals(existingNs.getNamespaceURI())) {
throw new BLangRuntimeException("failed to add attribute '" + prefix + ":" + localName + "'. prefix '" + prefix + "' is already bound to namespace '" + existingNs.getNamespaceURI() + "'");
}
ns = new OMNamespaceImpl(namespaceUri, prefix);
node.addAttribute(localName, value, ns);
return;
}
// We reach here if the namespace prefix is null/empty, and a namespace uri exists
if (namespaceUri != null && !namespaceUri.isEmpty()) {
prefix = null;
// Find a prefix that has the same namespaceUri, out of the defined namespaces
Iterator<String> prefixes = node.getNamespaceContext(false).getPrefixes(namespaceUri);
while (prefixes.hasNext()) {
String definedPrefix = prefixes.next();
if (definedPrefix.isEmpty()) {
continue;
}
prefix = definedPrefix;
break;
}
if (prefix != null && prefix.equals(XMLConstants.XMLNS_ATTRIBUTE)) {
// If found, and if its the default namespace, add a namespace decl
node.declareNamespace(value, localName);
return;
}
// else use the prefix. If the prefix is null, it will generate a random prefix.
ns = new OMNamespaceImpl(namespaceUri, prefix);
}
node.addAttribute(localName, value, ns);
}
Aggregations