use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.
the class UnitTestMockRegistry method lookup.
@Override
public OMNode lookup(String key) {
if (key == null) {
handleException("Resource cannot be found.");
}
if (key.isEmpty()) {
handleException("Resource cannot be empty");
}
String resourcePath = getAbsolutePathToRegistry(key);
if (resourcePath != null && testMockRegistry.containsKey(resourcePath)) {
RegistryResource resource = testMockRegistry.get(resourcePath);
String sourceOfResource = resource.getArtifact();
OMNode omNode = null;
try {
ByteArrayInputStream inputStream = new ByteArrayInputStream(sourceOfResource.getBytes());
try {
XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
StAXOMBuilder builder = new StAXOMBuilder(parser);
omNode = builder.getDocumentElement();
} catch (OMException ignored) {
omNode = readNonXML(resource);
} catch (XMLStreamException ignored) {
omNode = readNonXML(resource);
} catch (Exception e) {
log.error("Error while reading the resource '" + key + "'", e);
} finally {
try {
inputStream.close();
} catch (IOException e) {
log.error("Error while closing the input stream", e);
}
}
} catch (Exception e) {
log.error("Creating OMNode from registry resource artifact failed", e);
}
return omNode;
} else {
return null;
}
}
use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.
the class SamplingService 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();
if (!(element instanceof SOAPHeaderBlock)) {
if (element instanceof OMElement) {
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);
}
}
}
use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.
the class MessageHelper method cloneSOAPEnvelope.
/**
* This method will clone the provided SOAPEnvelope and returns the cloned envelope
* as an exact copy of the provided envelope
*
* @param envelope - this will be cloned to get the new envelope
* @return cloned SOAPEnvelope from the provided one
*/
public static SOAPEnvelope cloneSOAPEnvelope(SOAPEnvelope envelope) {
SOAPFactory fac;
if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(envelope.getBody().getNamespace().getNamespaceURI())) {
fac = OMAbstractFactory.getSOAP11Factory();
} else {
fac = OMAbstractFactory.getSOAP12Factory();
}
SOAPEnvelope newEnvelope = fac.getDefaultEnvelope();
Iterator childIterator;
if (envelope.getHeader() != null) {
SOAPHeader body = envelope.getHeader();
childIterator = body.getChildren();
while (childIterator.hasNext()) {
Object bodyNs = childIterator.next();
if (bodyNs instanceof SOAPHeaderBlock) {
try {
newEnvelope.getHeader().addChild(ElementHelper.toSOAPHeaderBlock(((OMElement) bodyNs).cloneOMElement(), fac));
} catch (Exception e) {
handleException(e);
}
} else if (bodyNs instanceof OMElement) {
newEnvelope.getHeader().addChild(((OMElement) bodyNs).cloneOMElement());
}
}
}
if (envelope.getBody() != null) {
// fault would lead to class cast exceptions if accessed through the getFault method
if (envelope.getBody().getFirstElement() instanceof SOAPFault && envelope.getBody().hasFault()) {
SOAPFault fault = envelope.getBody().getFault();
newEnvelope.getBody().addFault(cloneSOAPFault(fault));
} else {
OMElement body = envelope.getBody().cloneOMElement();
Iterator ns = body.getAllDeclaredNamespaces();
OMNamespace bodyNs = body.getNamespace();
String nsUri = bodyNs.getNamespaceURI();
String nsPrefix = bodyNs.getPrefix();
while (ns.hasNext()) {
OMNamespace namespace = ((OMNamespace) ns.next());
if (nsUri != null && !nsUri.equals(namespace.getNamespaceURI()) && nsPrefix != null && !nsPrefix.equals(namespace.getPrefix())) {
newEnvelope.getBody().declareNamespace(namespace);
}
ns.remove();
}
Iterator attributes = body.getAllAttributes();
while (attributes.hasNext()) {
OMAttribute attrb = (OMAttribute) attributes.next();
newEnvelope.getBody().addAttribute(attrb);
attributes.remove();
}
Iterator itr = body.getChildren();
while (itr.hasNext()) {
OMNode node = (OMNode) itr.next();
itr.remove();
newEnvelope.getBody().addChild(node);
}
/**
* Copy the namespaces declared in envelope , Fix for
* https://wso2.org/jira/browse/CARBON-16086
*/
Iterator allDeclaredNamespaces = envelope.getAllDeclaredNamespaces();
while (allDeclaredNamespaces.hasNext()) {
newEnvelope.declareNamespace((OMNamespace) allDeclaredNamespaces.next());
}
}
}
return newEnvelope;
}
use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.
the class PayloadHelper method getTextPayload.
// Text payload is carried in a wrapper element with QName TEXTELT
public static String getTextPayload(SOAPEnvelope envelope) {
OMElement el = getXMLPayload(envelope);
if (el == null)
return null;
if (!el.getQName().equals(TEXTELT)) {
log.error("Wrong QName " + el.getQName());
return null;
}
OMNode textNode = el.getFirstOMChild();
if (textNode.getType() != OMNode.TEXT_NODE) {
log.error("Text Node not found");
return null;
}
OMText text = (OMText) textNode;
return text.getText();
}
use of org.apache.axiom.om.OMNode in project wso2-synapse by wso2.
the class CalloutMediator method mediate.
public boolean mediate(MessageContext synCtx) {
if (synCtx.getEnvironment().isDebuggerEnabled()) {
if (super.divertMediationRoute(synCtx)) {
return true;
}
}
SynapseLog synLog = getLog(synCtx);
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("Start : Callout mediator");
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Message : " + synCtx.getEnvelope());
}
}
try {
if (!initClientOptions) {
blockingMsgSender.setInitClientOptions(false);
}
if (endpointKey != null) {
endpoint = synCtx.getEndpoint(endpointKey);
}
if (synLog.isTraceOrDebugEnabled()) {
if (!isWrappingEndpointCreated) {
synLog.traceOrDebug("Using the defined endpoint : " + endpoint.getName());
} else {
if (serviceURL != null) {
synLog.traceOrDebug("Using the serviceURL : " + serviceURL);
} else {
synLog.traceOrDebug("Using the To header as the EPR ");
}
if (securityOn) {
synLog.traceOrDebug("Security enabled within the Callout Mediator config");
if (wsSecPolicyKey != null) {
synLog.traceOrDebug("Using security policy key : " + wsSecPolicyKey);
} else {
if (inboundWsSecPolicyKey != null) {
synLog.traceOrDebug("Using inbound security policy key : " + inboundWsSecPolicyKey);
}
if (outboundWsSecPolicyKey != null) {
synLog.traceOrDebug("Using outbound security policy key : " + outboundWsSecPolicyKey);
}
}
}
}
}
org.apache.axis2.context.MessageContext axis2MsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
if (isWrappingEndpointCreated) {
if (Constants.VALUE_TRUE.equals(axis2MsgCtx.getProperty(Constants.Configuration.ENABLE_MTOM))) {
((AbstractEndpoint) endpoint).getDefinition().setUseMTOM(true);
}
}
if (this.serviceURL != null && this.serviceURL.contains(DISTRIBUTED_TX_BEGIN_CHECK_STR)) {
try {
initContext(synCtx);
try {
TranscationManger.lookUp(txContext);
} catch (Exception e) {
handleException("Cloud not get the context name " + USER_TX_LOOKUP_STR, e, synCtx);
}
TranscationManger.beginTransaction();
axis2MsgCtx.setProperty(NhttpConstants.DISTRIBUTED_TRANSACTION, TranscationManger.getTransaction());
axis2MsgCtx.setProperty(NhttpConstants.DISTRIBUTED_TRANSACTION_MANAGER, TranscationManger.getTransactionManager());
} catch (Exception e) {
handleException("Error starting transaction", synCtx);
}
}
MessageContext synapseOutMsgCtx = MessageHelper.cloneMessageContext(synCtx);
// Send the SOAP Header Blocks to support WS-Addressing
setSoapHeaderBlock(synapseOutMsgCtx);
// Set NTLM options from parent message context to cloned message context
setNTLMOptions(synCtx, synapseOutMsgCtx);
if (!useEnvelopeAsSource && // if the payload is JSON, we do not consider the request (ie. source) path. Instead, we use the complete payload.
!JsonUtil.hasAJsonPayload(((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext())) {
SOAPBody soapBody = synapseOutMsgCtx.getEnvelope().getBody();
for (Iterator itr = soapBody.getChildElements(); itr.hasNext(); ) {
OMElement child = (OMElement) itr.next();
child.detach();
}
soapBody.addChild(getRequestPayload(synCtx));
}
if (action != null) {
synapseOutMsgCtx.setWSAAction(action);
}
if (synLog.isTraceOrDebugEnabled()) {
synLog.traceOrDebug("About to invoke the service");
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Request message payload : " + synapseOutMsgCtx.getEnvelope());
}
}
synapseOutMsgCtx.setProperty(SynapseConstants.BLOCKING_MSG_SENDER, blockingMsgSender);
// Clear the message context properties related to endpoint in last service invocation
Set keySet = synapseOutMsgCtx.getPropertyKeySet();
if (keySet != null) {
keySet.remove(SynapseConstants.RECEIVING_SEQUENCE);
keySet.remove(EndpointDefinition.DYNAMIC_URL_VALUE);
keySet.remove(SynapseConstants.LAST_ENDPOINT);
keySet.remove(SynapseConstants.BLOCKING_SENDER_ERROR);
}
Object faultHandlerBeforeInvocation = getLastSequenceFaultHandler(synapseOutMsgCtx);
synapseOutMsgCtx.setProperty(SynapseConstants.LAST_SEQ_FAULT_HANDLER, faultHandlerBeforeInvocation);
endpoint.send(synapseOutMsgCtx);
// Check for rollback status
org.apache.axis2.context.MessageContext outAxis2MsgCtx = ((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext();
if (outAxis2MsgCtx.getProperty(SynapseConstants.SET_ROLLBACK_ONLY) != null) {
axis2MsgCtx.setProperty(SynapseConstants.SET_ROLLBACK_ONLY, outAxis2MsgCtx.getProperty(SynapseConstants.SET_ROLLBACK_ONLY));
}
// check whether fault sequence is already invoked
if (faultHandlerBeforeInvocation != getLastSequenceFaultHandler(synapseOutMsgCtx)) {
return false;
}
if (!("true".equals(synapseOutMsgCtx.getProperty(SynapseConstants.BLOCKING_SENDER_ERROR)))) {
if (synapseOutMsgCtx.getProperty(SynapseConstants.OUT_ONLY) == null || "false".equals(synapseOutMsgCtx.getProperty(SynapseConstants.OUT_ONLY))) {
setResponseHttpSc(synapseOutMsgCtx, synCtx);
if (synLog.isTraceTraceEnabled()) {
synLog.traceTrace("Response payload received : " + synapseOutMsgCtx.getEnvelope());
}
if (synapseOutMsgCtx.getEnvelope() != null) {
org.apache.axis2.context.MessageContext resultAxisMsgCtx = ((Axis2MessageContext) synapseOutMsgCtx).getAxis2MessageContext();
org.apache.axis2.context.MessageContext inAxisMsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
if (JsonUtil.hasAJsonPayload(resultAxisMsgCtx)) {
JsonUtil.cloneJsonPayload(resultAxisMsgCtx, inAxisMsgCtx);
} else {
if (targetXPath != null) {
Object o = targetXPath.evaluate(synCtx);
OMElement result = synapseOutMsgCtx.getEnvelope().getBody().getFirstElement();
if (o != null && o instanceof OMElement) {
OMNode tgtNode = (OMElement) o;
tgtNode.insertSiblingAfter(result);
tgtNode.detach();
} else if (o != null && o instanceof List && !((List) o).isEmpty()) {
// Always fetches *only* the first
OMNode tgtNode = (OMElement) ((List) o).get(0);
tgtNode.insertSiblingAfter(result);
tgtNode.detach();
} else {
handleException("Evaluation of target XPath expression : " + targetXPath.toString() + " did not yeild an OMNode", synCtx);
}
} else if (targetKey != null) {
OMElement result = synapseOutMsgCtx.getEnvelope().getBody().getFirstElement();
synCtx.setProperty(targetKey, result);
} else {
synCtx.setEnvelope(synapseOutMsgCtx.getEnvelope());
}
}
// Set HTTP Status code
inAxisMsgCtx.setProperty(SynapseConstants.HTTP_SC, resultAxisMsgCtx.getProperty(SynapseConstants.HTTP_SC));
if ("false".equals(synCtx.getProperty(SynapseConstants.BLOCKING_SENDER_PRESERVE_REQ_HEADERS))) {
inAxisMsgCtx.setProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS, resultAxisMsgCtx.getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS));
}
} else {
synLog.traceOrDebug("Service returned a null response");
}
}
} else {
log.info("Error while performing the callout operation");
return false;
}
} catch (AxisFault e) {
handleException("Error invoking service : " + serviceURL + (action != null ? " with action : " + action : ""), e, synCtx);
} catch (JaxenException e) {
handleException("Error while evaluating the XPath expression: " + targetXPath, e, synCtx);
}
synLog.traceOrDebug("End : Callout mediator");
return true;
}
Aggregations