public boolean mediate()

in modules/core/src/main/java/org/apache/synapse/mediators/builtin/CalloutMediator.java [94:217]


    public boolean mediate(MessageContext synCtx) {

        SynapseLog synLog = getLog(synCtx);

        if (synLog.isTraceOrDebugEnabled()) {
            synLog.traceOrDebug("Start : Callout mediator");

            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Message : " + synCtx.getEnvelope());
            }
        }

        try {

            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 (Constants.VALUE_TRUE.equals(axis2MsgCtx.getProperty(Constants.Configuration.ENABLE_MTOM))) {
                ((AbstractEndpoint) endpoint).getDefinition().setUseMTOM(true);
            }

            MessageContext synapseOutMsgCtx = MessageHelper.cloneMessageContext(synCtx);

            if (action != null) {
                synapseOutMsgCtx.setSoapAction(action);
            }

            if (requestKey != null || requestXPath != null) {
                SOAPBody soapBody = synapseOutMsgCtx.getEnvelope().getBody();
                soapBody.removeChildren();
                soapBody.addChild(getRequestPayload(synCtx));
                if (!passHeaders) {
                    SOAPHeader soapHeader = synapseOutMsgCtx.getEnvelope().getHeader();
                    soapHeader.removeChildren();
                }
            }

            if (synLog.isTraceOrDebugEnabled()) {
                synLog.traceOrDebug("About to invoke the service");
                if (synLog.isTraceTraceEnabled()) {
                    synLog.traceTrace("Request message payload : " + synapseOutMsgCtx.getEnvelope());
                }
            }

            MessageContext resultMsgCtx = null;
            try {
                if ("true".equals(synCtx.getProperty(SynapseConstants.OUT_ONLY))) {
                    blockingMsgSender.send(endpoint, synapseOutMsgCtx);
                } else {
                    resultMsgCtx = blockingMsgSender.send(endpoint, synapseOutMsgCtx);
                    if ("true".equals(resultMsgCtx.getProperty(SynapseConstants.BLOCKING_CLIENT_ERROR))) {
                        handleFault(synCtx, (Exception) synCtx.getProperty(SynapseConstants.ERROR_EXCEPTION));
                    }
                }
            } catch (Exception ex) {
                handleFault(synCtx, ex);
            }

            if (synLog.isTraceTraceEnabled()) {
                synLog.traceTrace("Response payload received : " + resultMsgCtx.getEnvelope());
            }

            if (resultMsgCtx != null) {
                if (targetXPath != null) {
                    Object o = targetXPath.evaluate(synCtx);
                    OMElement result = resultMsgCtx.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 = resultMsgCtx.getEnvelope().getBody().getFirstElement();
                    synCtx.setProperty(targetKey, result);
                } else {
                    synCtx.setEnvelope(resultMsgCtx.getEnvelope());
                }
            } else {
                synLog.traceOrDebug("Service returned a null response");
            }

        } 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;
    }