Search in sources :

Example 86 with MessageContext

use of org.apache.axis2.context.MessageContext in project wso2-synapse by wso2.

the class WSDLEndpoint method onFault.

public void onFault(MessageContext synCtx) {
    boolean isRecursive = getParentEndpoint() instanceof FailoverEndpoint || getParentEndpoint() instanceof LoadbalanceEndpoint;
    // For setting Car name (still for Proxy)
    logSetter();
    if (synCtx.getProperty(EPConstants.TENANT_INFO_ID) != null && ((int) synCtx.getProperty(EPConstants.TENANT_INFO_ID)) != EPConstants.SUPER_TENANT_ID) {
        org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
        Options options = axis2MessageContext.getOptions();
        EndpointReference to = options.getTo();
        if (to.getAddress() != null && to.getAddress().contains(EPConstants.LOCAL_TRANSPORT_IDENTIFIER)) {
            // removing the local transport identifier from the uri scheme
            options.setTo(new EndpointReference(to.getAddress().substring(EPConstants.LOCAL_TRANSPORT_IDENTIFIER.length())));
        }
    }
    // is this an actual leaf endpoint
    if (getParentEndpoint() != null) {
        if (getContext().isMaxRetryLimitReached(isRecursive)) {
            getContext().onFailoverRetryLimit(isRecursive);
        } else {
            // is this really a fault or a timeout/connection close etc?
            if (isTimeout(synCtx)) {
                getContext().onTimeout();
            } else if (isSuspendFault(synCtx)) {
                getContext().onFault();
            }
        }
    }
    setErrorOnMessage(synCtx, null, null);
    super.onFault(synCtx);
}
Also used : Options(org.apache.axis2.client.Options) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 87 with MessageContext

use of org.apache.axis2.context.MessageContext in project wso2-synapse by wso2.

the class DynamicLoadbalanceEndpoint method sendMessage.

private void sendMessage(MessageContext synCtx) {
    logSetter();
    SessionInformation sessionInformation = null;
    Member currentMember = null;
    // TODO Temp hack: ESB removes the session id from request in a random manner.
    setCookieHeader(synCtx);
    ConfigurationContext configCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getConfigurationContext();
    if (lbMembershipHandler.getConfigurationContext() == null) {
        lbMembershipHandler.setConfigurationContext(configCtx);
    }
    if (isSessionAffinityBasedLB()) {
        // first check if this session is associated with a session. if so, get the endpoint
        // associated for that session.
        sessionInformation = (SessionInformation) synCtx.getProperty(SynapseConstants.PROP_SAL_CURRENT_SESSION_INFORMATION);
        currentMember = (Member) synCtx.getProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_MEMBER);
        if (sessionInformation == null && currentMember == null) {
            sessionInformation = dispatcher.getSession(synCtx);
            if (sessionInformation != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Current session id : " + sessionInformation.getId());
                }
                currentMember = sessionInformation.getMember();
                synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_MEMBER, currentMember);
                // This is for reliably recovery any session information if while response is getting ,
                // session information has been removed by cleaner.
                // This will not be a cost as  session information a not heavy data structure
                synCtx.setProperty(SynapseConstants.PROP_SAL_CURRENT_SESSION_INFORMATION, sessionInformation);
            }
        }
    }
    DynamicLoadbalanceFaultHandlerImpl faultHandler = new DynamicLoadbalanceFaultHandlerImpl();
    if (sessionInformation != null && currentMember != null) {
        // send message on current session
        sessionInformation.updateExpiryTime();
        sendToApplicationMember(synCtx, currentMember, faultHandler, false);
    } else {
        // prepare for a new session
        currentMember = lbMembershipHandler.getNextApplicationMember(algorithmContext);
        if (currentMember == null) {
            String msg = "No application members available";
            log.error(msg);
            throw new SynapseException(msg);
        }
        sendToApplicationMember(synCtx, currentMember, faultHandler, true);
    }
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) SessionInformation(org.apache.synapse.endpoints.dispatch.SessionInformation) SynapseException(org.apache.synapse.SynapseException) Member(org.apache.axis2.clustering.Member)

Example 88 with MessageContext

use of org.apache.axis2.context.MessageContext in project wso2-synapse by wso2.

the class DynamicLoadbalanceEndpoint method sendToApplicationMember.

protected void sendToApplicationMember(MessageContext synCtx, Member currentMember, DynamicLoadbalanceFaultHandler faultHandler, boolean newSession) {
    // Rewriting the URL
    org.apache.axis2.context.MessageContext axis2MsgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    // Removing the REST_URL_POSTFIX - this is a hack.
    // In this loadbalance endpoint we create an endpoint per request by setting the complete url as the adress.
    // If a REST message comes Axis2FlexibleMEPClient append the REST_URL_POSTFIX to the adress. Hence endpoint fails
    // do send the request. e.g.  http://localhost:8080/example/index.html/example/index.html
    axis2MsgCtx.removeProperty(NhttpConstants.REST_URL_POSTFIX);
    String transport = axis2MsgCtx.getTransportIn().getName();
    String address = synCtx.getTo().getAddress();
    int incomingPort = extractPort(synCtx, transport);
    EndpointReference to = getEndpointReferenceAfterURLRewrite(currentMember, transport, address, incomingPort);
    synCtx.setTo(to);
    faultHandler.setTo(to);
    faultHandler.setCurrentMember(currentMember);
    synCtx.pushFaultHandler(faultHandler);
    if (isFailover()) {
        synCtx.getEnvelope().build();
    }
    Endpoint endpoint = getEndpoint(to, currentMember, synCtx);
    faultHandler.setCurrentEp(endpoint);
    if (isSessionAffinityBasedLB()) {
        synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_DEFAULT_SESSION_TIMEOUT, getSessionTimeout());
        synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_DISPATCHER, dispatcher);
        prepareEndPointSequence(synCtx, endpoint);
        if (newSession) {
            synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_MEMBER, currentMember);
            // we should also indicate that this is the first message in the session. so that
            // onFault(...) method can resend only the failed attempts for the first message.
            synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_FIRST_MESSAGE_IN_SESSION, Boolean.TRUE);
        }
    }
    Map<String, String> memberHosts;
    if ((memberHosts = (Map<String, String>) currentMember.getProperties().get(HttpSessionDispatcher.HOSTS)) == null) {
        currentMember.getProperties().put(HttpSessionDispatcher.HOSTS, memberHosts = new HashMap<String, String>());
    }
    memberHosts.put(extractHost(synCtx), "true");
    setupTransportHeaders(synCtx);
    try {
        endpoint.send(synCtx);
    } catch (Exception e) {
        if (e.getMessage().toLowerCase().contains("io reactor shutdown")) {
            log.fatal("System cannot continue normal operation. Restarting", e);
            // restart
            System.exit(121);
        } else {
            throw new SynapseException(e);
        }
    }
}
Also used : SynapseException(org.apache.synapse.SynapseException) MalformedURLException(java.net.MalformedURLException) SynapseException(org.apache.synapse.SynapseException) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 89 with MessageContext

use of org.apache.axis2.context.MessageContext in project wso2-synapse by wso2.

the class FailoverEndpoint method sendMessage.

private void sendMessage(MessageContext synCtx) {
    logSetter();
    if (log.isDebugEnabled()) {
        log.debug("Failover Endpoint : " + getName());
    }
    if (getContext().isState(EndpointContext.ST_OFF)) {
        informFailure(synCtx, SynapseConstants.ENDPOINT_FO_NONE_READY, "Failover endpoint : " + getName() != null ? getName() : SynapseConstants.ANONYMOUS_ENDPOINT + " - is inactive");
        return;
    }
    boolean isARetry = false;
    Map<String, Integer> mEndpointLog = null;
    if (synCtx.getProperty(SynapseConstants.LAST_ENDPOINT) == null) {
        if (log.isDebugEnabled()) {
            log.debug(this + " Building the SoapEnvelope");
        }
        // If buildMessage attribute available in failover config it is honoured, else global property is considered
        if (isBuildMessageAttAvailable) {
            if (buildMessageAtt) {
                buildMessage(synCtx);
            }
        } else if (buildMessage) {
            buildMessage(synCtx);
        }
        synCtx.getEnvelope().buildWithAttachments();
        // If the endpoint failed during the sending, we need to keep the original envelope and reuse that for other endpoints
        if (Boolean.TRUE.equals(((Axis2MessageContext) synCtx).getAxis2MessageContext().getProperty(PassThroughConstants.MESSAGE_BUILDER_INVOKED))) {
            synCtx.setProperty(SynapseConstants.LB_FO_ENDPOINT_ORIGINAL_MESSAGE, synCtx.getEnvelope());
        }
        mEndpointLog = new HashMap<String, Integer>();
        synCtx.setProperty(SynapseConstants.ENDPOINT_LOG, mEndpointLog);
    } else {
        isARetry = true;
        mEndpointLog = (Map<String, Integer>) synCtx.getProperty(SynapseConstants.ENDPOINT_LOG);
    }
    if (getChildren().isEmpty()) {
        informFailure(synCtx, SynapseConstants.ENDPOINT_FO_NONE_READY, "FailoverLoadbalance endpoint : " + getName() + " - no child endpoints");
        return;
    }
    // evaluate the endpoint properties
    evaluateProperties(synCtx);
    if (dynamic) {
        // Dynamic fail-over mode - Switch to a backup endpoint when an error occurs
        // in the primary endpoint. But switch back to the primary as soon as it becomes
        // active again.
        boolean foundEndpoint = false;
        for (Endpoint endpoint : getChildren()) {
            if (endpoint.readyToSend()) {
                foundEndpoint = true;
                if (isARetry && metricsMBean != null) {
                    metricsMBean.reportSendingFault(SynapseConstants.ENDPOINT_FO_FAIL_OVER);
                }
                synCtx.pushFaultHandler(this);
                if (endpoint instanceof AbstractEndpoint) {
                    org.apache.axis2.context.MessageContext axisMC = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
                    Pipe pipe = (Pipe) axisMC.getProperty(PassThroughConstants.PASS_THROUGH_PIPE);
                    if (pipe != null) {
                        pipe.forceSetSerializationRest();
                    }
                    // allow the message to be content aware if the given message comes via PT
                    if (axisMC.getProperty(PassThroughConstants.PASS_THROUGH_PIPE) != null || RequestResponseUtils.isHttpCarbonMessagePresent(axisMC)) {
                        ((AbstractEndpoint) endpoint).setContentAware(true);
                        ((AbstractEndpoint) endpoint).setForceBuildMC(true);
                        if (endpoint instanceof TemplateEndpoint && ((TemplateEndpoint) endpoint).getRealEndpoint() != null) {
                            if (((TemplateEndpoint) endpoint).getRealEndpoint() instanceof AbstractEndpoint) {
                                ((AbstractEndpoint) ((TemplateEndpoint) endpoint).getRealEndpoint()).setContentAware(true);
                                ((AbstractEndpoint) ((TemplateEndpoint) endpoint).getRealEndpoint()).setForceBuildMC(true);
                            }
                        }
                    }
                }
                if (endpoint.getName() != null) {
                    mEndpointLog.put(endpoint.getName(), null);
                }
                endpoint.send(synCtx);
                break;
            }
        }
        if (!foundEndpoint) {
            String msg = "Failover endpoint : " + (getName() != null ? getName() : SynapseConstants.ANONYMOUS_ENDPOINT) + " - no ready child endpoints";
            log.warn(msg);
            informFailure(synCtx, SynapseConstants.ENDPOINT_FO_NONE_READY, msg);
        }
    } else {
        if (currentEndpoint == null) {
            currentEndpoint = getChildren().get(0);
        }
        if (currentEndpoint.readyToSend()) {
            if (isARetry && metricsMBean != null) {
                metricsMBean.reportSendingFault(SynapseConstants.ENDPOINT_FO_FAIL_OVER);
            }
            synCtx.pushFaultHandler(this);
            currentEndpoint.send(synCtx);
        } else {
            boolean foundEndpoint = false;
            for (Endpoint endpoint : getChildren()) {
                if (endpoint.readyToSend()) {
                    foundEndpoint = true;
                    currentEndpoint = endpoint;
                    if (isARetry && metricsMBean != null) {
                        metricsMBean.reportSendingFault(SynapseConstants.ENDPOINT_FO_FAIL_OVER);
                    }
                    synCtx.pushFaultHandler(this);
                    currentEndpoint.send(synCtx);
                    break;
                }
            }
            if (!foundEndpoint) {
                String msg = "Failover endpoint : " + (getName() != null ? getName() : SynapseConstants.ANONYMOUS_ENDPOINT) + " - no ready child endpoints";
                log.warn(msg);
                informFailure(synCtx, SynapseConstants.ENDPOINT_FO_NONE_READY, msg);
            }
        }
    }
}
Also used : Pipe(org.apache.synapse.transport.passthru.Pipe) Integer(org.apache.axis2.databinding.types.soapencoding.Integer) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 90 with MessageContext

use of org.apache.axis2.context.MessageContext in project wso2-synapse by wso2.

the class ServiceDynamicLoadbalanceEndpoint method sendMessage.

private void sendMessage(MessageContext synCtx) {
    logSetter();
    setCookieHeader(synCtx);
    // TODO: Refactor Session Aware LB dispatching code
    // Check whether a valid session for session aware dispatching is available
    Member currentMember = null;
    SessionInformation sessionInformation = null;
    if (isSessionAffinityBasedLB()) {
        // first check if this session is associated with a session. if so, get the endpoint
        // associated for that session.
        sessionInformation = (SessionInformation) synCtx.getProperty(SynapseConstants.PROP_SAL_CURRENT_SESSION_INFORMATION);
        currentMember = (Member) synCtx.getProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_MEMBER);
        if (sessionInformation == null && currentMember == null) {
            sessionInformation = dispatcher.getSession(synCtx);
            if (sessionInformation != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Current session id : " + sessionInformation.getId());
                }
                currentMember = sessionInformation.getMember();
                synCtx.setProperty(SynapseConstants.PROP_SAL_ENDPOINT_CURRENT_MEMBER, currentMember);
                // This is for reliably recovery any session information if while response is getting ,
                // session information has been removed by cleaner.
                // This will not be a cost as  session information a not heavy data structure
                synCtx.setProperty(SynapseConstants.PROP_SAL_CURRENT_SESSION_INFORMATION, sessionInformation);
            }
        }
    }
    // Dispatch request the relevant member
    String targetHost = getTargetHost(synCtx);
    ConfigurationContext configCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext().getConfigurationContext();
    if (slbMembershipHandler.getConfigurationContext() == null) {
        slbMembershipHandler.setConfigurationContext(configCtx);
    }
    ServiceDynamicLoadbalanceFaultHandlerImpl faultHandler = new ServiceDynamicLoadbalanceFaultHandlerImpl();
    faultHandler.setHost(targetHost);
    setupTransportHeaders(synCtx);
    if (sessionInformation != null && currentMember != null) {
        // send message on current session
        sessionInformation.updateExpiryTime();
        sendToApplicationMember(synCtx, currentMember, faultHandler, false);
    } else {
        // prepare for a new session
        currentMember = slbMembershipHandler.getNextApplicationMember(targetHost);
        if (currentMember == null) {
            String msg = "No application members available";
            log.error(msg);
            throw new SynapseException(msg);
        }
        sendToApplicationMember(synCtx, currentMember, faultHandler, true);
    }
}
Also used : ConfigurationContext(org.apache.axis2.context.ConfigurationContext) SessionInformation(org.apache.synapse.endpoints.dispatch.SessionInformation) SynapseException(org.apache.synapse.SynapseException) Member(org.apache.axis2.clustering.Member)

Aggregations

MessageContext (org.apache.axis2.context.MessageContext)218 AxisFault (org.apache.axis2.AxisFault)126 Axis2MessageContext (org.apache.synapse.core.axis2.Axis2MessageContext)84 EndpointReference (org.apache.axis2.addressing.EndpointReference)79 OMElement (org.apache.axiom.om.OMElement)75 MessageContext (org.apache.synapse.MessageContext)71 ConfigurationContext (org.apache.axis2.context.ConfigurationContext)62 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)56 IOException (java.io.IOException)55 Map (java.util.Map)52 Test (org.junit.Test)45 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)44 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)36 MessageFormatter (org.apache.axis2.transport.MessageFormatter)35 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)34 Options (org.apache.axis2.client.Options)30 HashMap (java.util.HashMap)29 AxisService (org.apache.axis2.description.AxisService)29 QName (javax.xml.namespace.QName)28 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)28