use of org.apache.axiom.soap.SOAPFactory in project wso2-synapse by wso2.
the class HeaderMediator method addCustomHeader.
private void addCustomHeader(MessageContext synCtx, String value) {
SOAPEnvelope env = synCtx.getEnvelope();
if (env == null) {
return;
}
SOAPFactory fac = (SOAPFactory) env.getOMFactory();
SOAPHeader header = env.getHeader();
if (header == null) {
header = fac.createSOAPHeader(env);
}
if (!isImplicit()) {
SOAPHeaderBlock hb = header.addHeaderBlock(qName.getLocalPart(), fac.createOMNamespace(qName.getNamespaceURI(), qName.getPrefix()));
hb.setText(value);
} else if (hasEmbeddedXml()) {
addHeaderChildrenToMessageContext(synCtx, embeddedXmlContent);
} else {
// header mediator has an implicit xml element but its content cannot be found.
handleException("Header mediator has an implicit xml element but its content cannot be found.", synCtx);
}
}
use of org.apache.axiom.soap.SOAPFactory in project wso2-synapse by wso2.
the class Source method evaluate.
public ArrayList<OMNode> evaluate(MessageContext synCtx, SynapseLog synLog) throws JaxenException {
ArrayList<OMNode> sourceNodeList = new ArrayList<OMNode>();
if (sourceType == EnrichMediator.CUSTOM) {
assert xpath != null : "XPath should be non null in case of CUSTOM";
List selectedNodeList = xpath.selectNodes(synCtx);
if (selectedNodeList != null && selectedNodeList.size() != 0) {
for (Object o : selectedNodeList) {
if (o instanceof OMElement) {
if (clone) {
OMElement ins = ((OMElement) o).cloneOMElement();
if (o instanceof SOAPHeaderBlock) {
SOAPFactory fac = (SOAPFactory) ((OMElement) o).getOMFactory();
try {
sourceNodeList.add(ElementHelper.toSOAPHeaderBlock(ins, fac));
} catch (Exception e) {
synLog.error(e);
throw new JaxenException(e);
}
} else {
sourceNodeList.add(ins);
}
} else {
sourceNodeList.add((OMElement) o);
}
} else if (o instanceof OMText) {
sourceNodeList.add((OMText) o);
} else if (o instanceof String) {
OMFactory fac = OMAbstractFactory.getOMFactory();
sourceNodeList.add(fac.createOMText(o.toString()));
}
}
} else {
synLog.error("Specified node by xpath cannot be found.");
}
} else if (sourceType == EnrichMediator.BODY) {
if (clone) {
if (synCtx.getEnvelope().getBody().getFirstElement() != null) {
sourceNodeList.add(synCtx.getEnvelope().getBody().getFirstElement().cloneOMElement());
}
} else {
if (synCtx.getEnvelope().getBody().getFirstElement() != null) {
sourceNodeList.add(synCtx.getEnvelope().getBody().getFirstElement());
}
}
} else if (sourceType == EnrichMediator.ENVELOPE) {
if (clone) {
sourceNodeList.add(MessageHelper.cloneSOAPEnvelope(synCtx.getEnvelope()));
} else {
sourceNodeList.add(synCtx.getEnvelope());
}
} else if (sourceType == EnrichMediator.PROPERTY) {
assert property != null : "property shouldn't be null when type is PROPERTY";
Object o = synCtx.getProperty(property);
if (o instanceof OMElement) {
if (clone) {
sourceNodeList.add(((OMElement) o).cloneOMElement());
} else {
sourceNodeList.add((OMElement) o);
}
} else if (o instanceof String) {
String sourceStr = (String) o;
OMFactory fac = OMAbstractFactory.getOMFactory();
sourceNodeList.add(fac.createOMText(sourceStr));
} else if (o instanceof ArrayList) {
ArrayList nodesList;
if (clone) {
nodesList = MessageHelper.cloneArrayList((ArrayList) o);
} else {
nodesList = (ArrayList) o;
}
for (Object node : nodesList) {
if (node instanceof OMElement) {
if (node instanceof SOAPEnvelope) {
SOAPEnvelope soapEnvelope = (SOAPEnvelope) node;
String soapNamespace = null;
if (soapEnvelope.getNamespace() != null) {
soapNamespace = soapEnvelope.getNamespace().getNamespaceURI();
}
if (soapEnvelope.getHeader() == null && soapNamespace != null) {
SOAPFactory soapFactory;
if (soapNamespace.equals(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
soapFactory = OMAbstractFactory.getSOAP12Factory();
} else {
soapFactory = OMAbstractFactory.getSOAP11Factory();
}
soapFactory.createSOAPHeader(soapEnvelope);
}
sourceNodeList.add(soapEnvelope);
} else {
OMElement ele = (OMElement) node;
sourceNodeList.add(ele);
}
} else if (node instanceof OMText) {
sourceNodeList.add((OMText) node);
}
}
} else {
synLog.error("Invalid source property type.");
}
} else if (sourceType == EnrichMediator.INLINE) {
if (inlineOMNode instanceof OMElement) {
OMElement inlineOMElement = (OMElement) inlineOMNode;
if (inlineOMElement.getQName().getLocalPart().equals("Envelope")) {
SOAPEnvelope soapEnvelope = getSOAPEnvFromOM(inlineOMElement);
if (soapEnvelope != null) {
sourceNodeList.add(soapEnvelope);
} else {
synLog.error("Inline Source is not a valid SOAPEnvelope.");
}
} else {
sourceNodeList.add(inlineOMElement.cloneOMElement());
}
} else if (inlineOMNode instanceof OMText) {
sourceNodeList.add(inlineOMNode);
} else if (inlineKey != null) {
Object inlineObj = synCtx.getEntry(inlineKey);
if (inlineObj instanceof OMElement) {
if (((OMElement) inlineObj).getQName().getLocalPart().equals("Envelope")) {
SOAPEnvelope soapEnvelope = getSOAPEnvFromOM((OMElement) inlineObj);
if (soapEnvelope != null) {
sourceNodeList.add(soapEnvelope);
} else {
synLog.error("Specified Resource as Source is not a valid SOAPEnvelope.");
}
} else {
sourceNodeList.add((OMElement) inlineObj);
}
} else if (inlineObj instanceof OMText) {
sourceNodeList.add((OMText) inlineObj);
} else if (inlineObj instanceof String) {
sourceNodeList.add(OMAbstractFactory.getOMFactory().createOMText(inlineObj.toString()));
} else {
synLog.error("Specified Resource as Source is not valid.");
}
} else {
synLog.error("Inline Source Content is not valid.");
}
}
return sourceNodeList;
}
use of org.apache.axiom.soap.SOAPFactory in project wso2-synapse by wso2.
the class Source method getSOAPEnvFromOM.
private SOAPEnvelope getSOAPEnvFromOM(OMElement inlineElement) {
SOAPFactory soapFactory;
if (inlineElement.getQName().getNamespaceURI().equals(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI)) {
soapFactory = OMAbstractFactory.getSOAP12Factory();
} else {
soapFactory = OMAbstractFactory.getSOAP11Factory();
}
StAXSOAPModelBuilder builder = new StAXSOAPModelBuilder(inlineElement.getXMLStreamReader(), soapFactory, inlineElement.getQName().getNamespaceURI());
return builder.getSOAPEnvelope();
}
use of org.apache.axiom.soap.SOAPFactory in project wso2-synapse by wso2.
the class Target method insert.
public void insert(MessageContext synContext, ArrayList<OMNode> sourceNodeList, SynapseLog synLog) throws JaxenException {
if (targetType == EnrichMediator.CUSTOM) {
assert xpath != null : "Xpath cannot be null for CUSTOM";
if (sourceNodeList.isEmpty()) {
synLog.error("Cannot Enrich message from an empty source.");
return;
}
Object targetObj = xpath.selectSingleNode(synContext);
// if the type custom is used to enrich a property, It'll be handled in a different method
if (xpath.getExpression().startsWith(SynapseXPathConstants.GET_PROPERTY_FUNCTION)) {
this.handleProperty(xpath, synContext, sourceNodeList, synLog);
} else {
if (targetObj instanceof SOAPHeaderImpl) {
OMElement targetElem = (OMElement) targetObj;
ArrayList<OMNode> headerSourceNodeList = new ArrayList<>();
for (OMNode o : sourceNodeList) {
OMElement ins = ((OMElement) o).cloneOMElement();
SOAPFactory fac = (SOAPFactory) synContext.getEnvelope().getOMFactory();
try {
headerSourceNodeList.add(ElementHelper.toSOAPHeaderBlock(ins, fac));
} catch (Exception e) {
log.error("Error occurred while transforming the OMElement to SOAPHeaderBlock ", e);
throw new JaxenException(e);
}
}
insertElement(headerSourceNodeList, targetElem, synLog);
} else if (targetObj instanceof OMElement) {
OMElement targetElem = (OMElement) targetObj;
insertElement(sourceNodeList, targetElem, synLog);
} else if (targetObj instanceof OMText) {
OMText targetText = (OMText) targetObj;
if (sourceNodeList.get(0) instanceof OMText) {
if (targetText.getParent() != null) {
Object parent = targetText.getParent();
if (parent instanceof OMElement) {
((OMElement) parent).setText(((OMText) sourceNodeList.get(0)).getText());
}
}
} else if (sourceNodeList.get(0) instanceof OMElement) {
Object targetParent = targetText.getParent();
if (targetParent instanceof OMElement) {
targetText.detach();
synchronized (sourceNodeList.get(0)) {
((OMElement) targetParent).addChild(sourceNodeList.get(0));
}
}
}
} else if (targetObj instanceof OMAttribute) {
OMAttribute attribute = (OMAttribute) targetObj;
attribute.setAttributeValue(((OMText) sourceNodeList.get(0)).getText());
} else {
synLog.error("Invalid Target object to be enrich.");
throw new SynapseException("Invalid Target object to be enrich.");
}
}
} else if (targetType == EnrichMediator.BODY) {
SOAPEnvelope env = synContext.getEnvelope();
SOAPBody body = env.getBody();
OMElement e = body.getFirstElement();
if (e != null) {
insertElement(sourceNodeList, e, synLog);
} else {
// if the body is empty just add as a child
for (OMNode elem : sourceNodeList) {
if (elem instanceof OMElement) {
synchronized (elem) {
body.addChild(elem);
}
} else {
synLog.error("Invalid Object type to be inserted into message body");
}
}
}
} else if (targetType == EnrichMediator.ENVELOPE) {
OMNode node = sourceNodeList.get(0);
if (node instanceof SOAPEnvelope) {
try {
synContext.setEnvelope((SOAPEnvelope) node);
} catch (AxisFault axisFault) {
synLog.error("Failed to set the SOAP Envelope");
throw new SynapseException("Failed to set the SOAP Envelope");
}
} else {
synLog.error("SOAPEnvelope is expected");
throw new SynapseException("A SOAPEnvelope is expected");
}
} else if (targetType == EnrichMediator.PROPERTY) {
assert property != null : "Property cannot be null for PROPERTY type";
if (action != null && property != null) {
Object propertyObj = synContext.getProperty(property);
OMElement documentElement = null;
try {
if (isOMElement(propertyObj)) {
documentElement = (OMElement) propertyObj;
} else {
documentElement = AXIOMUtil.stringToOM((String) propertyObj);
}
} catch (Exception e1) {
// just ignoring the phaser error
}
if (documentElement != null && action.equals(ACTION_ADD_CHILD)) {
// logic should valid only when adding child elements, and other cases
// such as sibling and replacement using the else condition
insertElement(sourceNodeList, documentElement, synLog);
if (isOMElement(propertyObj)) {
synContext.setProperty(property, documentElement);
} else {
synContext.setProperty(property, documentElement.getText());
}
} else {
synContext.setProperty(property, sourceNodeList);
}
} else {
synContext.setProperty(property, sourceNodeList);
}
}
}
use of org.apache.axiom.soap.SOAPFactory in project wso2-synapse by wso2.
the class ForwardingService method setSoapHeaderBlock.
private void setSoapHeaderBlock(MessageContext synCtx) {
// Send the SOAP Header Blocks to support WS-Addressing
if (synCtx.getEnvelope().getHeader() != null) {
Iterator iHeader = synCtx.getEnvelope().getHeader().getChildren();
SOAPFactory fac;
if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(synCtx.getEnvelope().getBody().getNamespace().getNamespaceURI())) {
fac = OMAbstractFactory.getSOAP11Factory();
} else {
fac = OMAbstractFactory.getSOAP12Factory();
}
List<OMNode> newHeaderNodes = new ArrayList<OMNode>();
while (iHeader.hasNext()) {
try {
Object element = iHeader.next();
// OMSourcedElementImpl.datasource to null, causing an NPE when serializing in future.
if (element instanceof OMElement && !(element instanceof SOAPHeaderBlock)) {
newHeaderNodes.add(ElementHelper.toSOAPHeaderBlock((OMElement) element, fac));
iHeader.remove();
}
} catch (OMException e) {
log.error("Unable to convert to SoapHeader Block", e);
} catch (Exception e) {
log.error("Unable to convert to SoapHeader Block", e);
}
}
for (OMNode newHeaderNode : newHeaderNodes) {
synCtx.getEnvelope().getHeader().addChild(newHeaderNode);
}
}
}
Aggregations